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 6 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 @@ -494,6 +494,16 @@ func (bs *BlockState) AddBlockToBlockTree(header *types.Header) error {
return bs.bt.AddBlock(header, uint64(arrivalTime.UnixNano()))
}

// 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.GetAllBlocksAtDepth(hash)
Expand Down
22 changes: 21 additions & 1 deletion dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,12 @@ 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.NewUint64OrHash(startNumber)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe use the Must equivalent? Just in case we modify NewUint64OrHash eventually, to avoid bad surprises.

}
}

var end *common.Hash
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]
}
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
}

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
3 changes: 3 additions & 0 deletions dot/sync/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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)
qdm12 marked this conversation as resolved.
Show resolved Hide resolved
IsDescendantOf(parent, child common.Hash) (bool, error)
}

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