Skip to content

Commit

Permalink
chore(pkg/scale): refactor dot/types to utilize new scale pcakage (Ch…
Browse files Browse the repository at this point in the history
  • Loading branch information
jimjbrettj authored Sep 21, 2021
1 parent bd4e06d commit edd9b19
Show file tree
Hide file tree
Showing 105 changed files with 2,013 additions and 2,439 deletions.
16 changes: 10 additions & 6 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,25 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
require.Nil(t, err)

// simulate block sent from BABE session
newBlock := &types.Block{
Header: &types.Header{
digest := types.NewDigest()
err = digest.Add(*types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest())
require.NoError(t, err)

newBlock := types.Block{
Header: types.Header{
Number: big.NewInt(1),
ParentHash: s.blockState.BestBlockHash(),
Digest: types.Digest{types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()},
Digest: digest,
},
Body: types.NewBody([]byte{}),
Body: *types.NewBody([]byte{}),
}

expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
Digest: digest,
BestBlock: true,
}

Expand All @@ -116,7 +120,7 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
state, err := s.storageState.TrieState(nil)
require.NoError(t, err)

err = s.HandleBlockProduced(newBlock, state)
err = s.HandleBlockProduced(&newBlock, state)
require.NoError(t, err)

time.Sleep(time.Second)
Expand Down
21 changes: 15 additions & 6 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package core

import (
"context"
"fmt"
"math/big"
"os"
"sync"
Expand Down Expand Up @@ -176,12 +177,20 @@ func (s *Service) HandleBlockImport(block *types.Block, state *rtstorage.TrieSta
// It is handled the same as an imported block in terms of state updates; the only difference
// is we send a BlockAnnounceMessage to our peers.
func (s *Service) HandleBlockProduced(block *types.Block, state *rtstorage.TrieState) error {
digest := types.NewDigest()
for i := range block.Header.Digest.Types {
err := digest.Add(block.Header.Digest.Types[i].Value())
if err != nil {
return err
}
}

msg := &network.BlockAnnounceMessage{
ParentHash: block.Header.ParentHash,
Number: block.Header.Number,
StateRoot: block.Header.StateRoot,
ExtrinsicsRoot: block.Header.ExtrinsicsRoot,
Digest: block.Header.Digest,
Digest: digest,
BestBlock: true,
}

Expand All @@ -190,12 +199,12 @@ func (s *Service) HandleBlockProduced(block *types.Block, state *rtstorage.TrieS
}

func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) error {
if block == nil || block.Header == nil || state == nil {
return nil
if block == nil || state == nil {
return fmt.Errorf("unable to handle block due to nil parameter")
}

// store updates state trie nodes in database
err := s.storageState.StoreTrie(state, block.Header)
err := s.storageState.StoreTrie(state, &block.Header)
if err != nil {
logger.Warn("failed to store state trie for imported block", "block", block.Header.Hash(), "error", err)
return err
Expand All @@ -215,7 +224,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
logger.Debug("imported block and stored state trie", "block", block.Header.Hash(), "state root", state.MustRoot())

// handle consensus digests
s.digestHandler.HandleDigests(block.Header)
s.digestHandler.HandleDigests(&block.Header)

rt, err := s.blockState.GetRuntime(&block.Header.ParentHash)
if err != nil {
Expand All @@ -235,7 +244,7 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er
}

// check if block production epoch transitioned
if err := s.handleCurrentSlot(block.Header); err != nil {
if err := s.handleCurrentSlot(&block.Header); err != nil {
logger.Warn("failed to handle epoch for block", "block", block.Header.Hash(), "error", err)
return err
}
Expand Down
104 changes: 57 additions & 47 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -61,20 +60,20 @@ func addTestBlocksToStateWithParent(t *testing.T, previousHash common.Hash, dept

for i := 1; i <= depth; i++ {
block := &types.Block{
Header: &types.Header{
Header: types.Header{
ParentHash: previousHash,
Number: big.NewInt(int64(i)).Add(previousNum, big.NewInt(int64(i))),
Digest: types.Digest{},
Digest: types.NewDigest(),
},
Body: &types.Body{},
Body: types.Body{},
}

previousHash = block.Header.Hash()

blockState.StoreRuntime(block.Header.Hash(), rt)
err := blockState.AddBlock(block)
require.NoError(t, err)
headers = append(headers, block.Header)
headers = append(headers, &block.Header)
}

return headers
Expand Down Expand Up @@ -119,21 +118,25 @@ func TestAnnounceBlock(t *testing.T) {
defer s.Stop()

// simulate block sent from BABE session
newBlock := &types.Block{
Header: &types.Header{
ParentHash: s.blockState.BestBlockHash(),
digest := types.NewDigest()
err = digest.Add(*types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest())
require.NoError(t, err)

newBlock := types.Block{
Header: types.Header{
Number: big.NewInt(1),
Digest: types.Digest{types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest()},
ParentHash: s.blockState.BestBlockHash(),
Digest: digest,
},
Body: &types.Body{},
Body: *types.NewBody([]byte{}),
}

expected := &network.BlockAnnounceMessage{
ParentHash: newBlock.Header.ParentHash,
Number: newBlock.Header.Number,
StateRoot: newBlock.Header.StateRoot,
ExtrinsicsRoot: newBlock.Header.ExtrinsicsRoot,
Digest: newBlock.Header.Digest,
Digest: digest,
BestBlock: true,
}

Expand All @@ -142,7 +145,7 @@ func TestAnnounceBlock(t *testing.T) {
state, err := s.storageState.TrieState(nil)
require.NoError(t, err)

err = s.HandleBlockProduced(newBlock, state)
err = s.HandleBlockProduced(&newBlock, state)
require.NoError(t, err)

time.Sleep(time.Second)
Expand Down Expand Up @@ -208,27 +211,27 @@ func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
err = bs.AddBlock(block1)
require.NoError(t, err)

block2 := sync.BuildBlock(t, rt, block1.Header, nil)
block2 := sync.BuildBlock(t, rt, &block1.Header, nil)
bs.StoreRuntime(block2.Header.Hash(), rt)
err = bs.AddBlock(block2)
require.NoError(t, err)

block3 := sync.BuildBlock(t, rt, block2.Header, nil)
block3 := sync.BuildBlock(t, rt, &block2.Header, nil)
bs.StoreRuntime(block3.Header.Hash(), rt)
err = bs.AddBlock(block3)
require.NoError(t, err)

block4 := sync.BuildBlock(t, rt, block3.Header, nil)
block4 := sync.BuildBlock(t, rt, &block3.Header, nil)
bs.StoreRuntime(block4.Header.Hash(), rt)
err = bs.AddBlock(block4)
require.NoError(t, err)

block5 := sync.BuildBlock(t, rt, block4.Header, nil)
block5 := sync.BuildBlock(t, rt, &block4.Header, nil)
bs.StoreRuntime(block5.Header.Hash(), rt)
err = bs.AddBlock(block5)
require.NoError(t, err)

block31 := sync.BuildBlock(t, rt, block2.Header, nil)
block31 := sync.BuildBlock(t, rt, &block2.Header, nil)
bs.StoreRuntime(block31.Header.Hash(), rt)
err = bs.AddBlock(block31)
require.NoError(t, err)
Expand All @@ -238,7 +241,7 @@ func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
// Add extrinsic to block `block31`
ext := createExtrinsic(t, rt, bs.GenesisHash(), nonce)

block41 := sync.BuildBlock(t, rt, block31.Header, ext)
block41 := sync.BuildBlock(t, rt, &block31.Header, ext)
bs.StoreRuntime(block41.Header.Hash(), rt)
err = bs.AddBlock(block41)
require.NoError(t, err)
Expand Down Expand Up @@ -304,15 +307,14 @@ func TestHandleChainReorg_WithReorg_Transactions(t *testing.T) {
body, err := types.NewBodyFromExtrinsics([]types.Extrinsic{tx})
require.NoError(t, err)

digest := types.NewDigest()
block := &types.Block{
Header: &types.Header{
Header: types.Header{
ParentHash: ancestor.Header.Hash(),
Number: big.NewInt(0).Add(ancestor.Header.Number, big.NewInt(1)),
Digest: types.Digest{
utils.NewMockDigestItem(1),
},
Digest: digest,
},
Body: body,
Body: *body,
}

s.blockState.StoreRuntime(block.Header.Hash(), rt)
Expand Down Expand Up @@ -376,7 +378,7 @@ func TestMaintainTransactionPool_EmptyBlock(t *testing.T) {
}

err := s.maintainTransactionPool(&types.Block{
Body: types.NewBody([]byte{}),
Body: *types.NewBody([]byte{}),
})
require.NoError(t, err)

Expand Down Expand Up @@ -425,7 +427,7 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) {
require.NoError(t, err)

err = s.maintainTransactionPool(&types.Block{
Body: body,
Body: *body,
})
require.NoError(t, err)

Expand Down Expand Up @@ -501,20 +503,28 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
currSpecVersion := v.SpecVersion() // genesis runtime version.
hash := s.blockState.BestBlockHash() // genesisHash

digest := types.NewDigest()
err = digest.Add(types.PreRuntimeDigest{
ConsensusEngineID: types.BabeEngineID,
Data: common.MustHexToBytes("0x0201000000ef55a50f00000000"),
})
require.NoError(t, err)

newBlock1 := &types.Block{
Header: &types.Header{
Header: types.Header{
ParentHash: hash,
Number: big.NewInt(1),
Digest: types.Digest{utils.NewMockDigestItem(1)}},
Body: types.NewBody([]byte("Old Runtime")),
Digest: types.NewDigest()},
Body: *types.NewBody([]byte("Old Runtime")),
}

newBlockRTUpdate := &types.Block{
Header: &types.Header{
Header: types.Header{
ParentHash: hash,
Number: big.NewInt(1),
Digest: types.Digest{utils.NewMockDigestItem(2)}},
Body: types.NewBody([]byte("Updated Runtime")),
Digest: digest,
},
Body: *types.NewBody([]byte("Updated Runtime")),
}

ts, err := s.storageState.TrieState(nil) // Pass genesis root
Expand Down Expand Up @@ -590,11 +600,12 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
blockHash := common.MustHexToHash("0x86aa36a140dfc449c30dbce16ce0fea33d5c3786766baa764e33f336841b9e29") // hash for known test code substitution

newBlock := &types.Block{
Header: &types.Header{
Header: types.Header{
ParentHash: blockHash,
Number: big.NewInt(1),
Digest: types.Digest{utils.NewMockDigestItem(1)}},
Body: types.NewBody([]byte("Updated Runtime")),
Digest: types.NewDigest(),
},
Body: *types.NewBody([]byte("Updated Runtime")),
}

err = s.handleCodeSubstitution(blockHash)
Expand Down Expand Up @@ -629,15 +640,15 @@ func TestTryQueryStore_WhenThereIsDataToRetrieve(t *testing.T) {
require.NoError(t, err)

header, err := types.NewHeader(s.blockState.GenesisHash(), storageStateTrie.MustRoot(),
common.Hash{}, big.NewInt(1), nil)
common.Hash{}, big.NewInt(1), types.NewDigest())
require.NoError(t, err)

err = s.storageState.StoreTrie(storageStateTrie, header)
require.NoError(t, err)

testBlock := &types.Block{
Header: header,
Body: types.NewBody([]byte{}),
Header: *header,
Body: *types.NewBody([]byte{}),
}

err = s.blockState.AddBlock(testBlock)
Expand All @@ -659,15 +670,15 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) {
require.NoError(t, err)

header, err := types.NewHeader(s.blockState.GenesisHash(), storageStateTrie.MustRoot(),
common.Hash{}, big.NewInt(1), nil)
common.Hash{}, big.NewInt(1), types.NewDigest())
require.NoError(t, err)

err = s.storageState.StoreTrie(storageStateTrie, header)
require.NoError(t, err)

testBlock := &types.Block{
Header: header,
Body: types.NewBody([]byte{}),
Header: *header,
Body: *types.NewBody([]byte{}),
}

err = s.blockState.AddBlock(testBlock)
Expand All @@ -687,12 +698,12 @@ func TestTryQueryStore_WhenDoesNotHaveDataToRetrieve(t *testing.T) {
func TestTryQueryState_WhenDoesNotHaveStateRoot(t *testing.T) {
s := NewTestService(t, nil)

header, err := types.NewHeader(s.blockState.GenesisHash(), common.Hash{}, common.Hash{}, big.NewInt(1), nil)
header, err := types.NewHeader(s.blockState.GenesisHash(), common.Hash{}, common.Hash{}, big.NewInt(1), types.NewDigest())
require.NoError(t, err)

testBlock := &types.Block{
Header: header,
Body: types.NewBody([]byte{}),
Header: *header,
Body: *types.NewBody([]byte{}),
}

err = s.blockState.AddBlock(testBlock)
Expand Down Expand Up @@ -769,16 +780,15 @@ func createNewBlockAndStoreDataAtBlock(t *testing.T, s *Service, key, value []by
storageStateTrie.Set(key, value)
require.NoError(t, err)

header, err := types.NewHeader(parentHash, storageStateTrie.MustRoot(),
common.Hash{}, big.NewInt(number), nil)
header, err := types.NewHeader(parentHash, storageStateTrie.MustRoot(), common.Hash{}, big.NewInt(number), types.NewDigest())
require.NoError(t, err)

err = s.storageState.StoreTrie(storageStateTrie, header)
require.NoError(t, err)

testBlock := &types.Block{
Header: header,
Body: types.NewBody([]byte{}),
Header: *header,
Body: *types.NewBody([]byte{}),
}

err = s.blockState.AddBlock(testBlock)
Expand Down
Loading

0 comments on commit edd9b19

Please sign in to comment.