Skip to content

Commit

Permalink
refactor(pkg/scale): revise VaryingDataType to an interface (#3669)
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 authored Jan 30, 2024
1 parent 5ccea40 commit 644b212
Show file tree
Hide file tree
Showing 72 changed files with 1,986 additions and 2,028 deletions.
2 changes: 1 addition & 1 deletion dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestServiceHandleTransactionMessage(t *testing.T) {
runtimeMock3 := NewMockInstance(ctrl)

invalidTransaction := runtime.NewInvalidTransaction()
err := invalidTransaction.Set(runtime.Future{})
err := invalidTransaction.SetValue(runtime.Future{})
require.NoError(t, err)

type args struct {
Expand Down
4 changes: 2 additions & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (s *Service) HandleBlockProduced(block *types.Block, state *rtstorage.TrieS
func createBlockAnnounce(block *types.Block, isBestBlock bool) (
blockAnnounce *network.BlockAnnounceMessage, err error) {
digest := types.NewDigest()
for i := range block.Header.Digest.Types {
digestValue, err := block.Header.Digest.Types[i].Value()
for i := range block.Header.Digest {
digestValue, err := block.Header.Digest[i].Value()
if err != nil {
return nil, fmt.Errorf("getting value of digest type at index %d: %w", i, err)
}
Expand Down
4 changes: 2 additions & 2 deletions dot/digest/block_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewBlockImportHandler(epochState EpochState, grandpaState GrandpaState) *Bl

// HandleDigests handles consensus digests for an imported block
func (h *BlockImportHandler) HandleDigests(header *types.Header) error {
consensusDigests := toConsensusDigests(header.Digest.Types)
consensusDigests := toConsensusDigests(header.Digest)
consensusDigests, err := checkForGRANDPAForcedChanges(consensusDigests)
if err != nil {
return fmt.Errorf("failed while checking GRANDPA digests: %w", err)
Expand Down Expand Up @@ -76,7 +76,7 @@ func (h *BlockImportHandler) handleConsensusDigest(d *types.ConsensusDigest, hea
}

// toConsensusDigests converts a slice of scale.VaryingDataType to a slice of types.ConsensusDigest.
func toConsensusDigests(scaleVaryingTypes []scale.VaryingDataType) []types.ConsensusDigest {
func toConsensusDigests(scaleVaryingTypes types.Digest) []types.ConsensusDigest {
consensusDigests := make([]types.ConsensusDigest, 0, len(scaleVaryingTypes))

for _, d := range scaleVaryingTypes {
Expand Down
14 changes: 7 additions & 7 deletions dot/digest/block_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBlockImportHandle(t *testing.T) {
})

versionedNextConfigData := types.NewVersionedNextConfigData()
versionedNextConfigData.Set(types.NextConfigDataV1{
versionedNextConfigData.SetValue(types.NextConfigDataV1{
C1: 9,
C2: 10,
SecondarySlots: 1,
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestBlockImportHandle(t *testing.T) {
}

versionedNextConfigData := types.NewVersionedNextConfigData()
versionedNextConfigData.Set(types.NextConfigDataV1{
versionedNextConfigData.SetValue(types.NextConfigDataV1{
C1: 9,
C2: 10,
SecondarySlots: 1,
Expand Down Expand Up @@ -288,11 +288,11 @@ func TestBlockImportHandle(t *testing.T) {
}
}

func createBABEConsensusDigest(t *testing.T, digestData scale.VaryingDataTypeValue) types.ConsensusDigest {
func createBABEConsensusDigest(t *testing.T, digestData any) types.ConsensusDigest {
t.Helper()

babeConsensusDigest := types.NewBabeConsensusDigest()
require.NoError(t, babeConsensusDigest.Set(digestData))
require.NoError(t, babeConsensusDigest.SetValue(digestData))

marshaledData, err := scale.Marshal(babeConsensusDigest)
require.NoError(t, err)
Expand All @@ -303,11 +303,11 @@ func createBABEConsensusDigest(t *testing.T, digestData scale.VaryingDataTypeVal
}
}

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

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

marshaledData, err := scale.Marshal(grandpaConsensusDigest)
require.NoError(t, err)
Expand All @@ -323,7 +323,7 @@ func createBlockWithDigests(t *testing.T, genesisHeader *types.Header, digestsTo
t.Helper()

digest := types.NewDigest()
digestAddArgs := make([]scale.VaryingDataTypeValue, len(digestsToApply))
digestAddArgs := make([]any, len(digestsToApply))

for idx, consensusDigest := range digestsToApply {
digestAddArgs[idx] = consensusDigest
Expand Down
36 changes: 18 additions & 18 deletions dot/digest/digest_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestHandler_GrandpaScheduledChange(t *testing.T) {
}

var digest = types.NewGrandpaConsensusDigest()
err = digest.Set(sc)
err = digest.SetValue(sc)
require.NoError(t, err)

data, err := scale.Marshal(digest)
Expand Down Expand Up @@ -110,48 +110,48 @@ func TestHandler_GrandpaScheduledChange(t *testing.T) {

func TestMultipleGRANDPADigests_ShouldIncludeJustForcedChanges(t *testing.T) {
tests := map[string]struct {
digestsTypes []scale.VaryingDataTypeValue
expectedHandled []scale.VaryingDataTypeValue
digestsTypes []any
expectedHandled []any
}{
"forced_and_scheduled_changes_same_block": {
digestsTypes: []scale.VaryingDataTypeValue{
digestsTypes: []any{
types.GrandpaForcedChange{},
types.GrandpaScheduledChange{},
},
expectedHandled: []scale.VaryingDataTypeValue{
expectedHandled: []any{
types.GrandpaForcedChange{},
},
},
"only_scheduled_change_in_block": {
digestsTypes: []scale.VaryingDataTypeValue{
digestsTypes: []any{
types.GrandpaScheduledChange{},
},
expectedHandled: []scale.VaryingDataTypeValue{
expectedHandled: []any{
types.GrandpaScheduledChange{},
},
},
"more_than_one_forced_changes_in_block": {
digestsTypes: []scale.VaryingDataTypeValue{
digestsTypes: []any{
types.GrandpaForcedChange{},
types.GrandpaForcedChange{},
types.GrandpaForcedChange{},
types.GrandpaScheduledChange{},
},
expectedHandled: []scale.VaryingDataTypeValue{
expectedHandled: []any{
types.GrandpaForcedChange{},
types.GrandpaForcedChange{},
types.GrandpaForcedChange{},
},
},
"multiple_consensus_digests_in_block": {
digestsTypes: []scale.VaryingDataTypeValue{
digestsTypes: []any{
types.GrandpaOnDisabled{},
types.GrandpaPause{},
types.GrandpaResume{},
types.GrandpaForcedChange{},
types.GrandpaScheduledChange{},
},
expectedHandled: []scale.VaryingDataTypeValue{
expectedHandled: []any{
types.GrandpaOnDisabled{},
types.GrandpaPause{},
types.GrandpaResume{},
Expand All @@ -167,7 +167,7 @@ func TestMultipleGRANDPADigests_ShouldIncludeJustForcedChanges(t *testing.T) {

for _, item := range tt.digestsTypes {
var digest = types.NewGrandpaConsensusDigest()
require.NoError(t, digest.Set(item))
require.NoError(t, digest.SetValue(item))

data, err := scale.Marshal(digest)
require.NoError(t, err)
Expand All @@ -190,7 +190,7 @@ func TestMultipleGRANDPADigests_ShouldIncludeJustForcedChanges(t *testing.T) {

for _, item := range tt.expectedHandled {
var digest = types.NewGrandpaConsensusDigest()
require.NoError(t, digest.Set(item))
require.NoError(t, digest.SetValue(item))

data, err := scale.Marshal(digest)
require.NoError(t, err)
Expand All @@ -215,7 +215,7 @@ func TestHandler_HandleBABEOnDisabled(t *testing.T) {
}

var digest = types.NewBabeConsensusDigest()
err := digest.Set(types.BABEOnDisabled{
err := digest.SetValue(types.BABEOnDisabled{
ID: 7,
})
require.NoError(t, err)
Expand All @@ -236,7 +236,7 @@ func createHeaderWithPreDigest(t *testing.T, slotNumber uint64) *types.Header {
t.Helper()

babeHeader := types.NewBabeDigest()
err := babeHeader.Set(*types.NewBabePrimaryPreDigest(0, slotNumber, [32]byte{}, [64]byte{}))
err := babeHeader.SetValue(*types.NewBabePrimaryPreDigest(0, slotNumber, [32]byte{}, [64]byte{}))
require.NoError(t, err)

enc, err := scale.Marshal(babeHeader)
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestHandler_HandleNextEpochData(t *testing.T) {
}

digest := types.NewBabeConsensusDigest()
err = digest.Set(nextEpochData)
err = digest.SetValue(nextEpochData)
require.NoError(t, err)

data, err := scale.Marshal(digest)
Expand Down Expand Up @@ -339,9 +339,9 @@ func TestHandler_HandleNextConfigData(t *testing.T) {
}

versionedNextConfigData := types.NewVersionedNextConfigData()
versionedNextConfigData.Set(nextConfigData)
versionedNextConfigData.SetValue(nextConfigData)

err := digest.Set(versionedNextConfigData)
err := digest.SetValue(versionedNextConfigData)
require.NoError(t, err)

data, err := scale.Marshal(digest)
Expand Down
5 changes: 2 additions & 3 deletions dot/digest/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/pkg/scale"
)

// BlockState interface for block state methods
Expand All @@ -21,14 +20,14 @@ type BlockState interface {
// EpochState is the interface for state.EpochState
type EpochState interface {
GetEpochForBlock(header *types.Header) (uint64, error)
HandleBABEDigest(header *types.Header, digest scale.VaryingDataType) error
HandleBABEDigest(header *types.Header, digest types.BabeConsensusDigest) error
FinalizeBABENextEpochData(finalizedHeader *types.Header) error
FinalizeBABENextConfigData(finalizedHeader *types.Header) error
}

// GrandpaState is the interface for the state.GrandpaState
type GrandpaState interface {
HandleGRANDPADigest(header *types.Header, digest scale.VaryingDataType) error
HandleGRANDPADigest(header *types.Header, digest types.GrandpaConsensusDigest) error
ApplyScheduledChanges(finalizedHeader *types.Header) error
}

Expand Down
3 changes: 1 addition & 2 deletions dot/digest/mock_epoch_state_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions dot/digest/mock_grandpa_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions dot/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ func newWestendDevGenesisWithTrieAndHeader(t *testing.T) (

extrinsicRoot := trie.EmptyHash
const number = 0
digest := types.NewDigest()
genesisHeader = *types.NewHeader(parentHash,
stateRoot, extrinsicRoot, number, digest)
stateRoot, extrinsicRoot, number, nil)

return gen, genesisTrie, genesisHeader
}
2 changes: 1 addition & 1 deletion dot/import_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestNewHeaderFromFile(t *testing.T) {
digest := types.NewDigest()
err = scale.Unmarshal(digestBytes, &digest)
require.NoError(t, err)
require.Len(t, digest.Types, 2)
require.Len(t, digest, 2)

expected := &types.Header{
ParentHash: common.MustHexToHash("0x3b45c9c22dcece75a30acc9c2968cb311e6b0557350f83b430f47559db786975"),
Expand Down
4 changes: 2 additions & 2 deletions dot/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Test_newHeaderFromFile(t *testing.T) {
}

preRuntimeDigestItem := types.NewDigestItem()
err := preRuntimeDigestItem.Set(preRuntimeDigest)
err := preRuntimeDigestItem.SetValue(preRuntimeDigest)
require.NoError(t, err)
preRuntimeDigestItemValue, err := preRuntimeDigestItem.Value()
require.NoError(t, err)
Expand All @@ -94,7 +94,7 @@ func Test_newHeaderFromFile(t *testing.T) {
}

sealDigestItem := types.NewDigestItem()
err = sealDigestItem.Set(sealDigest)
err = sealDigestItem.SetValue(sealDigest)
require.NoError(t, err)
sealDigestItemValue, err := sealDigestItem.Value()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type BlockAnnounceMessage struct {
Number uint
StateRoot common.Hash
ExtrinsicsRoot common.Hash
Digest scale.VaryingDataTypeSlice
Digest types.Digest
BestBlock bool
}

Expand Down
8 changes: 4 additions & 4 deletions dot/network/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestEncodeBlockResponseMessage_WithBody(t *testing.T) {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf})

header := types.NewHeader(testHash, testHash, testHash, 1, types.NewDigest())
header := types.NewHeader(testHash, testHash, testHash, 1, nil)

exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}}
body := types.NewBody(types.BytesArrayToExtrinsics(exts))
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestEncodeBlockResponseMessage_WithAll(t *testing.T) {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0xa, 0xb, 0xc, 0xd, 0xe, 0xf})

header := types.NewHeader(testHash, testHash, testHash, 1, types.NewDigest())
header := types.NewHeader(testHash, testHash, testHash, 1, nil)

exts := [][]byte{{1, 3, 5, 7}, {9, 1, 2}, {3, 4, 5}}
body := types.NewBody(types.BytesArrayToExtrinsics(exts))
Expand Down Expand Up @@ -297,7 +297,7 @@ func TestDecode_BlockAnnounceMessage(t *testing.T) {

bhm := BlockAnnounceMessage{
Number: 0,
Digest: types.NewDigest(),
Digest: nil,
}

err := bhm.Decode(announceMessage)
Expand All @@ -314,7 +314,7 @@ func TestDecode_BlockAnnounceMessage(t *testing.T) {
Number: 1,
StateRoot: stateRoot,
ExtrinsicsRoot: extrinsicsRoot,
Digest: types.NewDigest(),
Digest: nil,
}

require.Equal(t, expected, bhm)
Expand Down
2 changes: 1 addition & 1 deletion dot/network/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestCreateDecoder_BlockAnnounce(t *testing.T) {
Number: 77,
StateRoot: common.Hash{2},
ExtrinsicsRoot: common.Hash{3},
Digest: types.NewDigest(),
Digest: nil,
}

enc, err = testBlockAnnounce.Encode()
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func HeaderToJSON(header types.Header) (ChainBlockHeaderResponse, error) {
res.Number = common.UintToHex(header.Number)
}

for _, item := range header.Digest.Types {
for _, item := range header.Digest {
enc, err := scale.Marshal(item)
if err != nil {
return ChainBlockHeaderResponse{}, err
Expand Down
Loading

0 comments on commit 644b212

Please sign in to comment.