Skip to content

Commit

Permalink
Finish findScheduledChange method
Browse files Browse the repository at this point in the history
  • Loading branch information
dimartiro committed Oct 14, 2024
1 parent 4b48037 commit 0bef22c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
18 changes: 16 additions & 2 deletions internal/client/consensus/grandpa/warp_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,22 @@ func findScheduledChange(
}

switch val := digestValue.(type) {
case types.GrandpaScheduledChange:
return &val, nil
case types.ConsensusDigest:
consensusDigest := types.GrandpaConsensusDigest{}
if val.ConsensusEngineID == types.GrandpaEngineID {
err := scale.Unmarshal(val.Data, &consensusDigest)
if err != nil {
return nil, err
}

scheduledChange, err := consensusDigest.Value()
if err != nil {
return nil, err
}

parsedScheduledChange, _ := scheduledChange.(types.GrandpaScheduledChange)
return &parsedScheduledChange, nil
}
}
}
return nil, nil
Expand Down
72 changes: 64 additions & 8 deletions internal/client/consensus/grandpa/warp_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
Expand Down Expand Up @@ -84,28 +85,43 @@ func TestGenerateWarpSyncProofOk(t *testing.T) {
parentHash = blockHeaders[blockNumber-2].Hash()
}

digest := &types.Digest{}

if blockNumber == 4 {
// Last block in a set must contain a grandpa scheduled change
// So we create an empty one just to pass the nil check
scheduledChange := createGRANDPAConsensusDigest(t, types.GrandpaScheduledChange{
Auths: []types.GrandpaAuthoritiesRaw{},
Delay: 2,
})
consensuDigest := []types.ConsensusDigest{
scheduledChange,
}
digest.Add(consensuDigest)
}

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

blockHeaders = append(blockHeaders, header)

// Mock block state responses
blockStateMock.EXPECT().GetHeader(header.Hash()).Return(header, nil).AnyTimes()
blockStateMock.EXPECT().GetHeaderByNumber(blockNumber).Return(header, nil).AnyTimes()

// authorities set changes happens only in block 5
if blockNumber < 5 {
grandpaStateMock.EXPECT().GetAuthoritiesChangesFromBlock(blockNumber).Return([]uint{5}, nil).AnyTimes()
grandpaStateMock.EXPECT().GetAuthoritiesChangesFromBlock(blockNumber).Return([]uint{4}, nil).AnyTimes()
} else if blockNumber == 5 {
blockStateMock.EXPECT().GetJustification(header.Hash()).Return(encodedJustification1, nil).AnyTimes()
} else {
grandpaStateMock.EXPECT().GetAuthoritiesChangesFromBlock(blockNumber).Return([]uint{}, nil).AnyTimes()
}

blockHeaders = append(blockHeaders, header)

// Mock block state responses
blockStateMock.EXPECT().GetHeader(header.Hash()).Return(header, nil).AnyTimes()
blockStateMock.EXPECT().GetHeaderByNumber(blockNumber).Return(header, nil).AnyTimes()
}

blockStateMock.EXPECT().GetHighestFinalisedHeader().Return(blockHeaders[len(blockHeaders)-1], nil).AnyTimes()
Expand All @@ -124,3 +140,43 @@ func TestGenerateWarpSyncProofOk(t *testing.T) {
}
assert.Equal(t, expectedProof, proof)
}

func TestFindScheduledChange(t *testing.T) {
t.Parallel()

scheduledChange := createGRANDPAConsensusDigest(t, types.GrandpaScheduledChange{
Auths: []types.GrandpaAuthoritiesRaw{},
Delay: 2,
})

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

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

View workflow job for this annotation

GitHub Actions / linting

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

blockHeader := &types.Header{
ParentHash: common.Hash{0x00},
Number: 1,
Digest: digest,
}

// Find scheduled change in block header
scheduledChangeDigest, err := findScheduledChange(*blockHeader)
assert.NoError(t, err)
assert.NotNil(t, scheduledChangeDigest)
}

func createGRANDPAConsensusDigest(t *testing.T, digestData any) types.ConsensusDigest {
t.Helper()

grandpaConsensusDigest := types.NewGrandpaConsensusDigest()
assert.NoError(t, grandpaConsensusDigest.SetValue(digestData))

marshaledData, err := scale.Marshal(grandpaConsensusDigest)
assert.NoError(t, err)

return types.ConsensusDigest{
ConsensusEngineID: types.GrandpaEngineID,
Data: marshaledData,
}
}

0 comments on commit 0bef22c

Please sign in to comment.