Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dot/sync): fix block request and response logic #1907

Merged
merged 19 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ func (bs *BlockState) AddBlockToBlockTree(header *types.Header) error {
return bs.bt.AddBlock(header, arrivalTime)
}

// GetAllBlocksAtNumber returns all unfinalised blocks with the given number
func (bs *BlockState) GetAllBlocksAtNumber(num *big.Int) ([]common.Hash, error) {
header, err := bs.GetHeaderByNumber(num)
if err != nil {
return nil, err
}

return bs.GetAllBlocksAtDepth(header.ParentHash), nil
}

// GetAllBlocksAtDepth returns all hashes with the depth of the given hash plus one
func (bs *BlockState) GetAllBlocksAtDepth(hash common.Hash) []common.Hash {
return bs.bt.GetAllBlocksAtNumber(hash)
Expand Down
30 changes: 25 additions & 5 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,7 @@ func (cs *chainSync) doSync(req *network.BlockRequestMessage) *workerError {

if req.Direction == network.Descending {
// reverse blocks before pre-validating and placing in ready queue
for i, j := 0, len(resp.BlockData)-1; i < j; i, j = i+1, j-1 {
resp.BlockData[i], resp.BlockData[j] = resp.BlockData[j], resp.BlockData[i]
}
reverseBlockData(resp.BlockData)
}

// perform some pre-validation of response, error if failure
Expand Down Expand Up @@ -897,10 +895,18 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) {
} else {
// in tip-syncing mode, we know the hash of the block on the fork we wish to sync
start, _ = variadic.NewUint64OrHash(w.startHash)

// if we're doing descending requests and not at the last (highest starting) request,
// then use number as start block
if w.direction == network.Descending && i != numRequests-1 {
start = variadic.MustNewUint64OrHash(startNumber)
}
}

var end *common.Hash
if !w.targetHash.IsEmpty() {
if !w.targetHash.IsEmpty() && i == numRequests-1 {
// if we're on our last request (which should contain the target hash),
// then add it
end = &w.targetHash
}

Expand All @@ -911,7 +917,21 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) {
Direction: w.direction,
Max: &max,
}
startNumber += maxResponseSize

switch w.direction {
case network.Ascending:
startNumber += maxResponseSize
case network.Descending:
startNumber -= maxResponseSize
}
Comment on lines +921 to +926
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not if/else? Are we expecting more directions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but I think in general we kinda like switch statements here :p what do you think?

Copy link
Contributor

@qdm12 qdm12 Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think

  • if / else if two cases only
  • switch for more than two cases. Don't use else if 😉

But I'm happy with switch as well. Although the only thing scaring me is if one additional case is added in the future and this block isn't updated, nothing will be executed whereas an if / else, the else gets executed. But that's a bit silly too I have to admit!

}

// if our direction is descending, we want to send out the request with the lowest
// startNumber first
if w.direction == network.Descending {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we directly do this inside loop instead of outside?

		idx := i
		if w.direction == network.Descending {
			idx = numRequests - idx - 1
		}

		reqs[idx] = &network.BlockRequestMessage{
			RequestedData: w.requestData,
			StartingBlock: *start,
			EndBlockHash:  end,
			Direction:     w.direction,
			Max:           &max,
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean exactly?

for i, j := 0, len(reqs)-1; i < j; i, j = i+1, j-1 {
reqs[i], reqs[j] = reqs[j], reqs[i]
}
}

return reqs, nil
Expand Down
24 changes: 24 additions & 0 deletions dot/sync/chain_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ func TestWorkerToRequests(t *testing.T) {
},
},
},
{
w: &worker{
startNumber: big.NewInt(1 + maxResponseSize + (maxResponseSize / 2)),
targetNumber: big.NewInt(1),
direction: network.Descending,
requestData: bootstrapRequestData,
},
expected: []*network.BlockRequestMessage{
{
RequestedData: network.RequestedDataHeader + network.RequestedDataBody + network.RequestedDataJustification,
StartingBlock: *variadic.MustNewUint64OrHash(1 + (maxResponseSize / 2)),
EndBlockHash: nil,
Direction: network.Descending,
Max: &max64,
},
{
RequestedData: bootstrapRequestData,
StartingBlock: *variadic.MustNewUint64OrHash(1 + maxResponseSize + (maxResponseSize / 2)),
EndBlockHash: nil,
Direction: network.Descending,
Max: &max128,
},
},
},
}

for i, tc := range testCases {
Expand Down
8 changes: 7 additions & 1 deletion dot/sync/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ var (
ErrInvalidBlock = errors.New("could not verify block")

// ErrInvalidBlockRequest is returned when an invalid block request is received
ErrInvalidBlockRequest = errors.New("invalid block request")
ErrInvalidBlockRequest = errors.New("invalid block request")
errInvalidRequestDirection = errors.New("invalid request direction")
errRequestStartTooHigh = errors.New("request start number is higher than our best block")
errFailedToGetEndHashAncestor = errors.New("failed to get ancestor of end block")
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do wrap only the bottom errors, we need to export all of them so a caller can check errors against them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only this package deals with block requests/responses, so I don't think they need to be exported atm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but ultimately aren't these errors returned outside of the sync package somewhere? Maybe they're logged in this package, then yes let's keep them unexported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think they are, they're only returned to other places in this package I believe (and just logged)


// chainSync errors
errEmptyBlockData = errors.New("empty block data")
Expand All @@ -57,6 +60,9 @@ var (
errUnknownParent = errors.New("parent of first block in block response is unknown")
errUnknownBlockForJustification = errors.New("received justification for unknown block")
errFailedToGetParent = errors.New("failed to get parent header")
errNilDescendantNumber = errors.New("descendant number is nil")
errStartAndEndMismatch = errors.New("request start and end hash are not on the same chain")
errFailedToGetDescendant = errors.New("failed to find descendant block")
)

// ErrNilChannel is returned if a channel is nil
Expand Down
3 changes: 3 additions & 0 deletions dot/sync/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type BlockState interface {
StoreRuntime(common.Hash, runtime.Instance)
GetHighestFinalisedHeader() (*types.Header, error)
GetFinalisedNotifierChannel() chan *types.FinalisationInfo
GetHeaderByNumber(num *big.Int) (*types.Header, error)
GetAllBlocksAtNumber(num *big.Int) ([]common.Hash, error)
IsDescendantOf(parent, child common.Hash) (bool, error)
}

// StorageState is the interface for the storage state
Expand Down
Loading