Skip to content

Commit

Permalink
Reuse uint64 for block numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Oct 14, 2024
1 parent 0bef22c commit 5caab20
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
18 changes: 9 additions & 9 deletions internal/client/consensus/grandpa/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type WarpSyncFragment struct {
Header types.Header
// A justification for the header above which proves its finality. In order to validate it the
// verifier must be aware of the authorities and set id for which the justification refers to.
Justification GrandpaJustification[hash.H256, uint]
Justification GrandpaJustification[hash.H256, uint64]
}

type WarpSyncProof struct {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (w *WarpSyncProof) addFragment(fragment WarpSyncFragment) (limitReached boo
return false, nil
}

func (w *WarpSyncProof) lastProofBlockNumber() uint {
func (w *WarpSyncProof) lastProofBlockNumber() uint64 {
if len(w.Proofs) == 0 {
return 0
}
Expand Down Expand Up @@ -139,11 +139,6 @@ func (w *WarpSyncProof) verify(
return &SetIdAuthorityList{currentSetId, currentAuthorities}, nil
}

type SetIdAuthorityList struct {
grandpa.SetID
types.AuthorityList
}

type WarpSyncProofProvider struct {
blockState BlockState
grandpaState GrandpaState
Expand All @@ -157,6 +152,11 @@ func NewWarpSyncProofProvider(blockState BlockState, grandpaState GrandpaState)
}
}

type SetIdAuthorityList struct {
grandpa.SetID
types.AuthorityList
}

// Generate build a warp sync encoded proof starting from the given block hash
func (p *WarpSyncProofProvider) Generate(start common.Hash) ([]byte, error) {
// Get and traverse all GRANDPA authorities changes from the given block hash
Expand Down Expand Up @@ -206,7 +206,7 @@ func (p *WarpSyncProofProvider) Generate(start common.Hash) ([]byte, error) {
return nil, err
}

justification, err := decodeJustification[hash.H256, uint, runtime.BlakeTwo256](encJustification)
justification, err := decodeJustification[hash.H256, uint64, runtime.BlakeTwo256](encJustification)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func (p *WarpSyncProofProvider) Generate(start common.Hash) ([]byte, error) {
return nil, err
}

justification, err := decodeJustification[hash.H256, uint, runtime.BlakeTwo256](latestJustification)
justification, err := decodeJustification[hash.H256, uint64, runtime.BlakeTwo256](latestJustification)
if err != nil {
return nil, err
}
Expand Down
23 changes: 13 additions & 10 deletions internal/client/consensus/grandpa/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestGenerateWarpSyncProofOk(t *testing.T) {
parentHash = blockHeaders[blockNumber-2].Hash()
}

digest := &types.Digest{}
digest := types.NewDigest()

if blockNumber == 4 {
// Last block in a set must contain a grandpa scheduled change
Expand All @@ -94,25 +94,28 @@ func TestGenerateWarpSyncProofOk(t *testing.T) {
Auths: []types.GrandpaAuthoritiesRaw{},
Delay: 2,
})
consensuDigest := []types.ConsensusDigest{
scheduledChange,
}
digest.Add(consensuDigest)

digestAddArgs := make([]any, 1)
digestAddArgs[0] = types.ConsensusDigest(scheduledChange)

Check failure on line 99 in internal/client/consensus/grandpa/warp_sync_test.go

View workflow job for this annotation

GitHub Actions / linting

unnecessary conversion (unconvert)
digest.Add(digestAddArgs...)
}

header = types.NewHeader(
parentHash,
common.Hash{byte(blockNumber)},
common.Hash{byte(blockNumber)},
blockNumber,
*digest,
digest,
)

// authorities set changes happens only in block 5
t.Logf("Header %d: %s", blockNumber, header.Hash().String())

// authorities set changes happens only in block 4
if blockNumber < 5 {
grandpaStateMock.EXPECT().GetAuthoritiesChangesFromBlock(blockNumber).Return([]uint{4}, nil).AnyTimes()
} else if blockNumber == 5 {
blockStateMock.EXPECT().GetJustification(header.Hash()).Return(encodedJustification1, nil).AnyTimes()
if blockNumber == 4 {
blockStateMock.EXPECT().GetJustification(header.Hash()).Return(encodedJustification1, nil).AnyTimes()
}
} else {
grandpaStateMock.EXPECT().GetAuthoritiesChangesFromBlock(blockNumber).Return([]uint{}, nil).AnyTimes()
}
Expand All @@ -136,7 +139,7 @@ func TestGenerateWarpSyncProofOk(t *testing.T) {
assert.NoError(t, err)

expectedProof := []byte{
0x4, 0x30, 0x78, 0x31, 0x63, 0x61, 0x34, 0x30, 0x32, 0x32, 0x35, 0x37, 0x31, 0x38, 0x36, 0x65, 0x65, 0x34, 0x33, 0x34, 0x36, 0x66, 0x64, 0x32, 0x63, 0x30, 0x39, 0x66, 0x65, 0x65, 0x62, 0x39, 0x31, 0x14, 0x30, 0x78, 0x30, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x30, 0x35, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0, 0x2a, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xec, 0x94, 0x1a, 0xe4, 0xe1, 0x86, 0x62, 0x8c, 0x96, 0x9a, 0x17, 0xb9, 0x2b, 0xe7, 0xac, 0xc2, 0x45, 0x96, 0x1b, 0xbf, 0xca, 0x32, 0x6c, 0x5b, 0xdc, 0x39, 0xd6, 0x2f, 0xca, 0x3e, 0x46, 0xee, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0xec, 0x94, 0x1a, 0xe4, 0xe1, 0x86, 0x62, 0x8c, 0x96, 0x9a, 0x17, 0xb9, 0x2b, 0xe7, 0xac, 0xc2, 0x45, 0x96, 0x1b, 0xbf, 0xca, 0x32, 0x6c, 0x5b, 0xdc, 0x39, 0xd6, 0x2f, 0xca, 0x3e, 0x46, 0xee, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc9, 0xe8, 0x1a, 0x88, 0x1f, 0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42, 0x3d, 0x43, 0x34, 0x1, 0x4f, 0xa6, 0xb0, 0xee, 0x0, 0x1,
}
assert.Equal(t, expectedProof, proof)
}
Expand Down

0 comments on commit 5caab20

Please sign in to comment.