Skip to content

Commit 5e42215

Browse files
authored
refactor(dot/state): only store finalised blocks in database (#1833)
1 parent 67bb5ef commit 5e42215

37 files changed

+612
-1053
lines changed

dot/core/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
201201

202202
nonce := uint64(0)
203203

204-
// Add extrinsic to block `block31`
204+
// Add extrinsic to block `block41`
205205
ext := createExtrinsic(t, rt, bs.GenesisHash(), nonce)
206206

207207
block41 := sync.BuildBlock(t, rt, &block31.Header, ext)

dot/digest/digest_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ func TestHandler_GrandpaScheduledChange(t *testing.T) {
9494

9595
headers, _ := state.AddBlocksToState(t, handler.blockState.(*state.BlockState), 2, false)
9696
for i, h := range headers {
97-
handler.blockState.(*state.BlockState).SetFinalisedHash(h.Hash(), uint64(i), 0)
97+
err = handler.blockState.(*state.BlockState).SetFinalisedHash(h.Hash(), uint64(i), 0)
98+
require.NoError(t, err)
9899
}
99100

100101
// authorities should change on start of block 3 from start
101102
headers, _ = state.AddBlocksToState(t, handler.blockState.(*state.BlockState), 1, false)
102103
for _, h := range headers {
103-
handler.blockState.(*state.BlockState).SetFinalisedHash(h.Hash(), 3, 0)
104+
err = handler.blockState.(*state.BlockState).SetFinalisedHash(h.Hash(), 3, 0)
105+
require.NoError(t, err)
104106
}
105107

106108
time.Sleep(time.Millisecond * 500)

dot/rpc/modules/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (cm *ChainModule) GetBlock(r *http.Request, req *ChainHashRequest, res *Cha
103103
return err
104104
}
105105
for _, e := range ext {
106-
res.Block.Body = append(res.Block.Body, fmt.Sprintf("0x%x", e))
106+
res.Block.Body = append(res.Block.Body, e.String())
107107
}
108108
}
109109
return nil

dot/rpc/modules/chain_test.go

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,14 @@ func TestChainGetFinalizedHeadByRound(t *testing.T) {
314314

315315
digest := types.NewDigest()
316316
digest.Add(*types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest())
317-
header := &types.Header{
317+
318+
header := types.Header{
318319
ParentHash: genesisHeader.Hash(),
319320
Number: big.NewInt(1),
320321
Digest: digest,
321322
}
322323
err = state.Block.AddBlock(&types.Block{
323-
Header: *header,
324+
Header: header,
324325
Body: types.Body{},
325326
})
326327
require.NoError(t, err)
@@ -369,60 +370,48 @@ func newTestStateService(t *testing.T) *state.Service {
369370
rt, err := wasmer.NewRuntimeFromGenesis(rtCfg)
370371
require.NoError(t, err)
371372

372-
err = loadTestBlocks(t, genesisHeader.Hash(), stateSrvc.Block, rt)
373-
require.NoError(t, err)
373+
loadTestBlocks(t, genesisHeader.Hash(), stateSrvc.Block, rt)
374374

375375
t.Cleanup(func() {
376376
stateSrvc.Stop()
377377
})
378378
return stateSrvc
379379
}
380380

381-
func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runtime.Instance) error {
382-
// Create header
383-
header0 := &types.Header{
384-
Number: big.NewInt(0),
381+
func loadTestBlocks(t *testing.T, gh common.Hash, bs *state.BlockState, rt runtime.Instance) {
382+
header1 := &types.Header{
383+
Number: big.NewInt(1),
385384
Digest: types.NewDigest(),
386385
ParentHash: gh,
387386
StateRoot: trie.EmptyHash,
388387
}
389-
// Create blockHash
390-
blockHash0 := header0.Hash()
391-
block0 := &types.Block{
392-
Header: *header0,
393-
Body: sampleBodyBytes,
394-
}
395388

396-
err := bs.AddBlock(block0)
397-
if err != nil {
398-
return err
389+
block1 := &types.Block{
390+
Header: *header1,
391+
Body: sampleBodyBytes,
399392
}
400393

401-
bs.StoreRuntime(block0.Header.Hash(), rt)
394+
err := bs.AddBlock(block1)
395+
require.NoError(t, err)
396+
bs.StoreRuntime(header1.Hash(), rt)
402397

403-
// Create header & blockData for block 1
404398
digest := types.NewDigest()
405399
err = digest.Add(*types.NewBabeSecondaryPlainPreDigest(0, 1).ToPreRuntimeDigest())
406400
require.NoError(t, err)
407-
header1 := &types.Header{
408-
Number: big.NewInt(1),
401+
402+
header2 := &types.Header{
403+
Number: big.NewInt(2),
409404
Digest: digest,
410-
ParentHash: blockHash0,
405+
ParentHash: header1.Hash(),
411406
StateRoot: trie.EmptyHash,
412407
}
413408

414-
block1 := &types.Block{
415-
Header: *header1,
409+
block2 := &types.Block{
410+
Header: *header2,
416411
Body: sampleBodyBytes,
417412
}
418413

419-
// Add the block1 to the DB
420-
err = bs.AddBlock(block1)
421-
if err != nil {
422-
return err
423-
}
424-
425-
bs.StoreRuntime(block1.Header.Hash(), rt)
426-
427-
return nil
414+
err = bs.AddBlock(block2)
415+
require.NoError(t, err)
416+
bs.StoreRuntime(header2.Hash(), rt)
428417
}

dot/rpc/modules/dev_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newState(t *testing.T) (*state.BlockState, *state.EpochState) {
3535
_, _, genesisHeader := genesis.NewTestGenesisWithTrieAndHeader(t)
3636
bs, err := state.NewBlockStateFromGenesis(db, genesisHeader)
3737
require.NoError(t, err)
38-
es, err := state.NewEpochStateFromGenesis(db, genesisBABEConfig)
38+
es, err := state.NewEpochStateFromGenesis(db, bs, genesisBABEConfig)
3939
require.NoError(t, err)
4040
return bs, es
4141
}

dot/rpc/modules/state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) {
543543
b := &types.Block{
544544
Header: types.Header{
545545
ParentHash: chain.Block.BestBlockHash(),
546-
Number: big.NewInt(2),
546+
Number: big.NewInt(3),
547547
StateRoot: sr1,
548548
},
549549
Body: *types.NewBody([]types.Extrinsic{[]byte{}}),
@@ -557,7 +557,7 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) {
557557

558558
chain.Block.StoreRuntime(b.Header.Hash(), rt)
559559

560-
hash, _ := chain.Block.GetBlockHash(big.NewInt(2))
560+
hash, _ := chain.Block.GetBlockHash(big.NewInt(3))
561561
core := newCoreService(t, chain)
562562
return NewStateModule(net, chain.Storage, core), &hash, &sr1
563563
}

dot/rpc/modules/system_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func setupSystemModule(t *testing.T) *SystemModule {
308308
require.NoError(t, err)
309309
err = chain.Block.AddBlock(&types.Block{
310310
Header: types.Header{
311-
Number: big.NewInt(1),
311+
Number: big.NewInt(3),
312312
ParentHash: chain.Block.BestBlockHash(),
313313
StateRoot: ts.MustRoot(),
314314
},

dot/services.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,6 @@ func createStateService(cfg *Config) (*state.Service, error) {
7474
}
7575
}
7676

77-
// load most recent state from database
78-
latestState, err := stateSrvc.Base.LoadLatestStorageHash()
79-
if err != nil {
80-
return nil, fmt.Errorf("failed to load latest state root hash: %s", err)
81-
}
82-
83-
// load most recent state from database
84-
_, err = stateSrvc.Storage.LoadFromDB(latestState)
85-
if err != nil {
86-
return nil, fmt.Errorf("failed to load latest state from database: %s", err)
87-
}
88-
8977
return stateSrvc, nil
9078
}
9179

dot/state/base.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,6 @@ func (s *BaseState) LoadNodeGlobalName() (string, error) {
5454
return string(nodeName), nil
5555
}
5656

57-
// StoreBestBlockHash stores the hash at the BestBlockHashKey
58-
func (s *BaseState) StoreBestBlockHash(hash common.Hash) error {
59-
return s.db.Put(common.BestBlockHashKey, hash[:])
60-
}
61-
62-
// LoadBestBlockHash loads the hash stored at BestBlockHashKey
63-
func (s *BaseState) LoadBestBlockHash() (common.Hash, error) {
64-
hash, err := s.db.Get(common.BestBlockHashKey)
65-
if err != nil {
66-
return common.Hash{}, err
67-
}
68-
69-
return common.NewHash(hash), nil
70-
}
71-
7257
// StoreGenesisData stores the given genesis data at the known GenesisDataKey.
7358
func (s *BaseState) StoreGenesisData(gen *genesis.Data) error {
7459
enc, err := json.Marshal(gen)
@@ -95,21 +80,6 @@ func (s *BaseState) LoadGenesisData() (*genesis.Data, error) {
9580
return data, nil
9681
}
9782

98-
// StoreLatestStorageHash stores the current root hash in the database at LatestStorageHashKey
99-
func (s *BaseState) StoreLatestStorageHash(root common.Hash) error {
100-
return s.db.Put(common.LatestStorageHashKey, root[:])
101-
}
102-
103-
// LoadLatestStorageHash retrieves the hash stored at LatestStorageHashKey from the DB
104-
func (s *BaseState) LoadLatestStorageHash() (common.Hash, error) {
105-
hashbytes, err := s.db.Get(common.LatestStorageHashKey)
106-
if err != nil {
107-
return common.Hash{}, err
108-
}
109-
110-
return common.NewHash(hashbytes), nil
111-
}
112-
11383
// StoreCodeSubstitutedBlockHash stores the hash at the CodeSubstitutedBlock key
11484
func (s *BaseState) StoreCodeSubstitutedBlockHash(hash common.Hash) error {
11585
return s.db.Put(common.CodeSubstitutedBlock, hash[:])

dot/state/base_test.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,6 @@ func TestTrie_StoreAndLoadFromDB(t *testing.T) {
3939
require.Equal(t, expected, tt.MustHash())
4040
}
4141

42-
type test struct {
43-
key []byte
44-
value []byte
45-
}
46-
47-
func TestStoreAndLoadLatestStorageHash(t *testing.T) {
48-
db := NewInMemoryDB(t)
49-
base := NewBaseState(db)
50-
tt := trie.NewEmptyTrie()
51-
52-
tests := []test{
53-
{key: []byte{0x01, 0x35}, value: []byte("pen")},
54-
{key: []byte{0x01, 0x35, 0x79}, value: []byte("penguin")},
55-
{key: []byte{0x01, 0x35, 0x7}, value: []byte("g")},
56-
{key: []byte{0xf2}, value: []byte("feather")},
57-
{key: []byte{0xf2, 0x3}, value: []byte("f")},
58-
{key: []byte{0x09, 0xd3}, value: []byte("noot")},
59-
{key: []byte{0x07}, value: []byte("ramen")},
60-
{key: []byte{0}, value: nil},
61-
}
62-
63-
for _, test := range tests {
64-
tt.Put(test.key, test.value)
65-
}
66-
67-
expected, err := tt.Hash()
68-
require.NoError(t, err)
69-
70-
err = base.StoreLatestStorageHash(expected)
71-
require.NoError(t, err)
72-
73-
hash, err := base.LoadLatestStorageHash()
74-
require.NoError(t, err)
75-
require.Equal(t, expected, hash)
76-
}
77-
7842
func TestStoreAndLoadGenesisData(t *testing.T) {
7943
db := NewInMemoryDB(t)
8044
base := NewBaseState(db)
@@ -99,20 +63,6 @@ func TestStoreAndLoadGenesisData(t *testing.T) {
9963
require.Equal(t, expected, gen)
10064
}
10165

102-
func TestStoreAndLoadBestBlockHash(t *testing.T) {
103-
db := NewInMemoryDB(t)
104-
base := NewBaseState(db)
105-
106-
hash, _ := common.HexToHash("0x3f5a19b9e9507e05276216f3877bb289e47885f8184010c65d0e41580d3663cc")
107-
108-
err := base.StoreBestBlockHash(hash)
109-
require.NoError(t, err)
110-
111-
res, err := base.LoadBestBlockHash()
112-
require.NoError(t, err)
113-
require.Equal(t, hash, res)
114-
}
115-
11666
func TestLoadStoreEpochLength(t *testing.T) {
11767
db := NewInMemoryDB(t)
11868
base := NewBaseState(db)

0 commit comments

Comments
 (0)