From 51c973c17fd2c6f03247037cb3cd59dc79de1607 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 24 Feb 2022 13:49:10 -0500 Subject: [PATCH 01/15] use promauto for whatever reason --- dot/state/tries.go | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/dot/state/tries.go b/dot/state/tries.go index 68fff7aeda..2ccefb1a56 100644 --- a/dot/state/tries.go +++ b/dot/state/tries.go @@ -4,40 +4,52 @@ package state import ( - "errors" - "fmt" "sync" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/trie" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + triesGauge = promauto.NewGauge(prometheus.GaugeOpts{ + Namespace: "gossamer_storage_tries", + Name: "cached_total", + Help: "total number of tries cached in memory", + }) + setCounter = promauto.NewCounter(prometheus.CounterOpts{ + Namespace: "gossamer_storage_tries", + Name: "set_total", + Help: "total number of tries cached set in memory", + }) + deleteCounter = promauto.NewCounter(prometheus.CounterOpts{ + Namespace: "gossamer_storage_tries", + Name: "delete_total", + Help: "total number of tries deleted from memory", + }) ) // Tries is a thread safe map of root hash // to trie. type Tries struct { - rootToTrie map[common.Hash]*trie.Trie - mapMutex sync.RWMutex - triesGauge prometheus.Gauge + rootToTrie map[common.Hash]*trie.Trie + mapMutex sync.RWMutex + triesGauge prometheus.Gauge + setCounter prometheus.Counter + deleteCounter prometheus.Counter } // NewTries creates a new thread safe map of root hash // to trie using the trie given as a first trie. func NewTries(t *trie.Trie) (trs *Tries, err error) { - triesGauge := prometheus.NewGauge(prometheus.GaugeOpts{ - Namespace: "gossamer_storage", - Name: "tries_cached_total", - Help: "total number of tries cached in memory", - }) - err = prometheus.Register(triesGauge) - if err != nil && !errors.As(err, &prometheus.AlreadyRegisteredError{}) { - return nil, fmt.Errorf("cannot register tries gauge: %w", err) - } return &Tries{ rootToTrie: map[common.Hash]*trie.Trie{ t.MustHash(): t, }, - triesGauge: triesGauge, + triesGauge: triesGauge, + setCounter: setCounter, + deleteCounter: deleteCounter, }, nil } @@ -53,6 +65,7 @@ func (t *Tries) softSet(root common.Hash, trie *trie.Trie) { } t.triesGauge.Inc() + t.setCounter.Inc() t.rootToTrie[root] = trie } @@ -63,6 +76,7 @@ func (t *Tries) delete(root common.Hash) { // Note we use .Set instead of .Dec in case nothing // was deleted since nothing existed at the hash given. t.triesGauge.Set(float64(len(t.rootToTrie))) + t.deleteCounter.Inc() } // get retrieves the trie corresponding to the root hash given From 46c0578af9c443f345e527ac7103181fdedd5f66 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 25 Feb 2022 12:40:40 -0500 Subject: [PATCH 02/15] delete prev state root --- dot/state/block_finalisation.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index f7fe569191..d34cc70a07 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -176,6 +176,17 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er ), ) + lastFinalisedHeader, err := bs.GetHeader(bs.lastFinalised) + if err != nil { + return fmt.Errorf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) + } + stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) + if stateRootTrie != nil { + bs.tries.delete(lastFinalisedHeader.StateRoot) + } else { + return fmt.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + } + bs.lastFinalised = hash return nil } @@ -243,7 +254,6 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { logger.Tracef("cleaned out finalised block from memory; block number %s with hash %s", block.Header.Number, hash) } - return batch.Flush() } From c162ca2b653dfa4bca9843019f0a7bf34e9119b0 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 09:13:09 -0500 Subject: [PATCH 03/15] delete finalised tries, update tests --- dot/state/block_finalisation.go | 26 ++++++----- dot/state/block_finalisation_test.go | 29 ++++-------- dot/state/block_notify_test.go | 25 +--------- dot/state/block_race_test.go | 2 +- dot/state/block_test.go | 28 ++++------- dot/state/helpers_test.go | 5 +- dot/state/storage_notify_test.go | 37 ++------------- dot/state/storage_test.go | 70 ++++++---------------------- dot/state/test_helpers.go | 19 +++++++- dot/state/tries_test.go | 34 +++----------- 10 files changed, 78 insertions(+), 197 deletions(-) diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index d34cc70a07..de7eea8da6 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -136,9 +136,9 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er return fmt.Errorf("failed to set highest round and set ID: %w", err) } - if err := bs.handleFinalisedBlock(hash); err != nil { - return fmt.Errorf("failed to set number->hash mapping on finalisation: %w", err) - } + // if err := bs.handleFinalisedBlock(hash); err != nil { + // return fmt.Errorf("failed to set number->hash mapping on finalisation: %w", err) + // } if round > 0 { bs.notifyFinalized(hash, round, setID) @@ -176,15 +176,17 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er ), ) - lastFinalisedHeader, err := bs.GetHeader(bs.lastFinalised) - if err != nil { - return fmt.Errorf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) - } - stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) - if stateRootTrie != nil { - bs.tries.delete(lastFinalisedHeader.StateRoot) - } else { - return fmt.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + if !bs.lastFinalised.Equal(hash) { + lastFinalisedHeader, err := bs.GetHeader(bs.lastFinalised) + if err != nil { + return fmt.Errorf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) + } + stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) + if stateRootTrie != nil { + bs.tries.delete(lastFinalisedHeader.StateRoot) + } else { + return fmt.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + } } bs.lastFinalised = hash diff --git a/dot/state/block_finalisation_test.go b/dot/state/block_finalisation_test.go index 5020f46e28..bd05c6c796 100644 --- a/dot/state/block_finalisation_test.go +++ b/dot/state/block_finalisation_test.go @@ -10,7 +10,6 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/trie" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" ) @@ -64,16 +63,7 @@ func TestHighestRoundAndSetID(t *testing.T) { } func TestBlockState_SetFinalisedHash(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - bs := newTestBlockState(t, testGenesisHeader, tries) + bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) h, err := bs.GetFinalisedHash(0, 0) require.NoError(t, err) require.Equal(t, testGenesisHeader.Hash(), h) @@ -84,10 +74,13 @@ func TestBlockState_SetFinalisedHash(t *testing.T) { require.NotNil(t, di) err = digest.Add(*di) require.NoError(t, err) + + someStateRoot := common.Hash{1, 1} header := &types.Header{ ParentHash: testGenesisHeader.Hash(), Number: big.NewInt(1), Digest: digest, + StateRoot: someStateRoot, } testhash := header.Hash() @@ -100,6 +93,9 @@ func TestBlockState_SetFinalisedHash(t *testing.T) { }) require.NoError(t, err) + // set tries with some state root + bs.tries.softSet(someStateRoot, trie.NewEmptyTrie()) + err = bs.SetFinalisedHash(testhash, 1, 1) require.NoError(t, err) @@ -109,16 +105,7 @@ func TestBlockState_SetFinalisedHash(t *testing.T) { } func TestSetFinalisedHash_setFirstSlotOnFinalisation(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00).Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - bs := newTestBlockState(t, testGenesisHeader, tries) + bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) firstSlot := uint64(42069) digest := types.NewDigest() diff --git a/dot/state/block_notify_test.go b/dot/state/block_notify_test.go index ab12805a63..59a355d113 100644 --- a/dot/state/block_notify_test.go +++ b/dot/state/block_notify_test.go @@ -10,11 +10,8 @@ import ( "time" "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/runtime" runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks" - "github.com/ChainSafe/gossamer/lib/trie" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" ) @@ -47,16 +44,7 @@ func TestFreeImportedBlockNotifierChannel(t *testing.T) { } func TestFinalizedChannel(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00).Times(3) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - bs := newTestBlockState(t, testGenesisHeader, tries) + bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) ch := bs.GetFinalisedNotifierChannel() @@ -111,16 +99,7 @@ func TestImportChannel_Multi(t *testing.T) { } func TestFinalizedChannel_Multi(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - bs := newTestBlockState(t, testGenesisHeader, tries) + bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) num := 5 chs := make([]chan *types.FinalisationInfo, num) diff --git a/dot/state/block_race_test.go b/dot/state/block_race_test.go index 80d8aa3657..c3bf689a82 100644 --- a/dot/state/block_race_test.go +++ b/dot/state/block_race_test.go @@ -28,7 +28,7 @@ func TestConcurrencySetHeader(t *testing.T) { dbs[i] = NewInMemoryDB(t) } - tries := (*Tries)(nil) // not used in this test + tries := newTriesEmpty() // not used in this test pend := new(sync.WaitGroup) pend.Add(threads) diff --git a/dot/state/block_test.go b/dot/state/block_test.go index ad96b93399..dea186c7c9 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -37,6 +37,12 @@ func newTestBlockState(t *testing.T, header *types.Header, tries *Tries) *BlockS bs, err := NewBlockStateFromGenesis(db, tries, header, telemetryMock) require.NoError(t, err) + + tr := trie.NewEmptyTrie() + err = tr.Load(bs.db, header.StateRoot) + require.NoError(t, err) + bs.tries.softSet(header.StateRoot, tr) + return bs } @@ -262,17 +268,8 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) { } func TestFinalization_DeleteBlock(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00).Times(5) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - bs := newTestBlockState(t, testGenesisHeader, tries) - AddBlocksToState(t, bs, 5, false) + bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) + AddBlocksToState(t, bs, 5, false, true) btBefore := bs.bt.DeepCopy() before := bs.bt.GetAllBlocks() @@ -482,14 +479,7 @@ func TestAddBlockToBlockTree(t *testing.T) { } func TestNumberIsFinalised(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(0.00).Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } + tries := newTriesEmpty() bs := newTestBlockState(t, testGenesisHeader, tries) fin, err := bs.NumberIsFinalised(big.NewInt(0)) diff --git a/dot/state/helpers_test.go b/dot/state/helpers_test.go index 68d6246905..934b865f94 100644 --- a/dot/state/helpers_test.go +++ b/dot/state/helpers_test.go @@ -15,7 +15,10 @@ import ( func newTriesEmpty() *Tries { return &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), + rootToTrie: make(map[common.Hash]*trie.Trie), + triesGauge: triesGauge, + setCounter: setCounter, + deleteCounter: deleteCounter, } } diff --git a/dot/state/storage_notify_test.go b/dot/state/storage_notify_test.go index 509681c2d3..b35cf59926 100644 --- a/dot/state/storage_notify_test.go +++ b/dot/state/storage_notify_test.go @@ -13,23 +13,12 @@ import ( "github.com/ChainSafe/chaindb" "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/trie" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) func TestStorageState_RegisterStorageObserver(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - ss := newTestStorageState(t, tries) + ss := newTestStorageState(t, newTriesEmpty()) ts, err := ss.TrieState(nil) require.NoError(t, err) @@ -64,16 +53,7 @@ func TestStorageState_RegisterStorageObserver(t *testing.T) { } func TestStorageState_RegisterStorageObserver_Multi(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - ss := newTestStorageState(t, tries) + ss := newTestStorageState(t, newTriesEmpty()) ts, err := ss.TrieState(nil) require.NoError(t, err) @@ -125,18 +105,7 @@ func TestStorageState_RegisterStorageObserver_Multi(t *testing.T) { func TestStorageState_RegisterStorageObserver_Multi_Filter(t *testing.T) { t.Skip() // this seems to fail often on CI - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - // TODO restrict those mock calls once test is fixed. - triesGauge.EXPECT().Inc().AnyTimes() - triesGauge.EXPECT().Set(gomock.Any()).AnyTimes() - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - ss := newTestStorageState(t, tries) + ss := newTestStorageState(t, newTriesEmpty()) ts, err := ss.TrieState(nil) require.NoError(t, err) diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index 676c8e2703..743d0bcf81 100644 --- a/dot/state/storage_test.go +++ b/dot/state/storage_test.go @@ -32,16 +32,7 @@ func newTestStorageState(t *testing.T, tries *Tries) *StorageState { } func TestStorage_StoreAndLoadTrie(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc() - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - storage := newTestStorageState(t, tries) + storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) @@ -61,16 +52,16 @@ func TestStorage_StoreAndLoadTrie(t *testing.T) { } func TestStorage_GetStorageByBlockHash(t *testing.T) { - ctrl := gomock.NewController(t) + // ctrl := gomock.NewController(t) - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } + // triesGauge := NewMockGauge(ctrl) + // triesGauge.EXPECT().Inc().Times(2) + // tries := &Tries{ + // rootToTrie: make(map[common.Hash]*trie.Trie), + // triesGauge: triesGauge, + // } - storage := newTestStorageState(t, tries) + storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) @@ -105,17 +96,7 @@ func TestStorage_GetStorageByBlockHash(t *testing.T) { } func TestStorage_TrieState(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(3) - triesGauge.EXPECT().Set(1.00).Times(1) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - storage := newTestStorageState(t, tries) + storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) ts.Set([]byte("noot"), []byte("washere")) @@ -135,17 +116,7 @@ func TestStorage_TrieState(t *testing.T) { } func TestStorage_LoadFromDB(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(4) - triesGauge.EXPECT().Set(1.00).Times(3) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - storage := newTestStorageState(t, tries) + storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) @@ -190,16 +161,7 @@ func TestStorage_LoadFromDB(t *testing.T) { } func TestStorage_StoreTrie_NotSyncing(t *testing.T) { - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(2) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } - - storage := newTestStorageState(t, tries) + storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) @@ -233,13 +195,7 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) { err = genTrie.PutChild([]byte("keyToChild"), testChildTrie) require.NoError(t, err) - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Inc().Times(2) - triesGauge.EXPECT().Set(0.00) - tries := &Tries{ - rootToTrie: make(map[common.Hash]*trie.Trie), - triesGauge: triesGauge, - } + tries := newTriesEmpty() blockState, err := NewBlockStateFromGenesis(db, tries, genHeader, telemetryMock) require.NoError(t, err) diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index 9c4b241f43..e8a36bf07f 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -5,6 +5,7 @@ package state import ( "crypto/rand" + "encoding/binary" "fmt" "math/big" "testing" @@ -61,7 +62,7 @@ type testBranch struct { // AddBlocksToState adds `depth` number of blocks to the BlockState, optionally with random branches func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, - withBranches bool) ([]*types.Header, []*types.Header) { + withBranches bool, withFakeStateRoots ...interface{}) ([]*types.Header, []*types.Header) { var ( currentChain, branchChains []*types.Header branches []testBranch @@ -82,16 +83,30 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, err = digest.Add(*prd) require.NoError(t, err) + var stateRoot common.Hash + + if len(withFakeStateRoots) > 0 { + // generate some dummy state root based on block number + bs := make([]byte, 4) + binary.LittleEndian.PutUint32(bs, uint32(i)) + stateRoot = common.Hash{bs[0], bs[1], bs[2], bs[3]} + } else { + stateRoot = trie.EmptyHash + } + block := &types.Block{ Header: types.Header{ ParentHash: previousHash, Number: big.NewInt(int64(i)), - StateRoot: trie.EmptyHash, + StateRoot: stateRoot, Digest: digest, }, Body: types.Body{}, } + // write to tries, since this will need to be deleted + blockState.tries.softSet(stateRoot, trie.NewEmptyTrie()) + currentChain = append(currentChain, &block.Header) hash := block.Header.Hash() diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index 58e0492479..cff6bd72a1 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -9,8 +9,6 @@ import ( "github.com/ChainSafe/gossamer/internal/trie/node" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/trie" - "github.com/golang/mock/gomock" - "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -27,11 +25,9 @@ func Test_NewTries(t *testing.T) { rootToTrie: map[common.Hash]*trie.Trie{ tr.MustHash(): tr, }, - triesGauge: prometheus.NewGauge(prometheus.GaugeOpts{ - Namespace: "gossamer_storage", - Name: "tries_cached_total", - Help: "total number of tries cached in memory", - }), + triesGauge: triesGauge, + setCounter: setCounter, + deleteCounter: deleteCounter, } assert.Equal(t, expectedTries, rootToTrie) @@ -74,17 +70,8 @@ func Test_Tries_softSet(t *testing.T) { testCase := testCase t.Run(name, func(t *testing.T) { t.Parallel() - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - if testCase.triesGaugeInc { - triesGauge.EXPECT().Inc() - } - - tries := &Tries{ - rootToTrie: testCase.rootToTrie, - triesGauge: triesGauge, - } + tries := newTriesEmpty() + tries.rootToTrie = testCase.rootToTrie tries.softSet(testCase.root, testCase.trie) @@ -129,15 +116,8 @@ func Test_Tries_delete(t *testing.T) { testCase := testCase t.Run(name, func(t *testing.T) { t.Parallel() - ctrl := gomock.NewController(t) - - triesGauge := NewMockGauge(ctrl) - triesGauge.EXPECT().Set(testCase.triesGaugeSet) - - tries := &Tries{ - rootToTrie: testCase.rootToTrie, - triesGauge: triesGauge, - } + tries := newTriesEmpty() + tries.rootToTrie = testCase.rootToTrie tries.delete(testCase.root) From 8c811a9847529049edce19aa9ef24d168c63cd3f Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 09:25:13 -0500 Subject: [PATCH 04/15] cleanup --- dot/state/block_finalisation.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index de7eea8da6..c8f03fc122 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -136,10 +136,6 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er return fmt.Errorf("failed to set highest round and set ID: %w", err) } - // if err := bs.handleFinalisedBlock(hash); err != nil { - // return fmt.Errorf("failed to set number->hash mapping on finalisation: %w", err) - // } - if round > 0 { bs.notifyFinalized(hash, round, setID) } From 2bbbcbb334cd94c334a5aaeccdf61b6331d6cf48 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 09:26:07 -0500 Subject: [PATCH 05/15] cleanup --- dot/state/storage_test.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/dot/state/storage_test.go b/dot/state/storage_test.go index 743d0bcf81..5d1c7616be 100644 --- a/dot/state/storage_test.go +++ b/dot/state/storage_test.go @@ -52,15 +52,6 @@ func TestStorage_StoreAndLoadTrie(t *testing.T) { } func TestStorage_GetStorageByBlockHash(t *testing.T) { - // ctrl := gomock.NewController(t) - - // triesGauge := NewMockGauge(ctrl) - // triesGauge.EXPECT().Inc().Times(2) - // tries := &Tries{ - // rootToTrie: make(map[common.Hash]*trie.Trie), - // triesGauge: triesGauge, - // } - storage := newTestStorageState(t, newTriesEmpty()) ts, err := storage.TrieState(&trie.EmptyHash) require.NoError(t, err) From 04ac9879a929f4cb32341d024b725702dabf647c Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 15:23:57 -0500 Subject: [PATCH 06/15] put in defer func, and log out errors --- dot/state/block_finalisation.go | 23 ++++++++++++----------- dot/state/block_test.go | 2 +- dot/state/test_helpers.go | 21 ++------------------- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index c8f03fc122..7b27dfc849 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -118,7 +118,6 @@ func (bs *BlockState) GetHighestFinalisedHeader() (*types.Header, error) { func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) error { bs.Lock() defer bs.Unlock() - has, _ := bs.HasHeader(hash) if !has { return fmt.Errorf("cannot finalise unknown block %s", hash) @@ -173,16 +172,18 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er ) if !bs.lastFinalised.Equal(hash) { - lastFinalisedHeader, err := bs.GetHeader(bs.lastFinalised) - if err != nil { - return fmt.Errorf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) - } - stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) - if stateRootTrie != nil { - bs.tries.delete(lastFinalisedHeader.StateRoot) - } else { - return fmt.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) - } + defer func(lastFinalised common.Hash) { + lastFinalisedHeader, err := bs.GetHeader(lastFinalised) + if err != nil { + logger.Debugf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) + } + stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) + if stateRootTrie != nil { + bs.tries.delete(lastFinalisedHeader.StateRoot) + } else { + logger.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + } + }(bs.lastFinalised) } bs.lastFinalised = hash diff --git a/dot/state/block_test.go b/dot/state/block_test.go index dea186c7c9..ac1be2a8a6 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -269,7 +269,7 @@ func TestAddBlock_BlockNumberToHash(t *testing.T) { func TestFinalization_DeleteBlock(t *testing.T) { bs := newTestBlockState(t, testGenesisHeader, newTriesEmpty()) - AddBlocksToState(t, bs, 5, false, true) + AddBlocksToState(t, bs, 5, false) btBefore := bs.bt.DeepCopy() before := bs.bt.GetAllBlocks() diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index e8a36bf07f..ff8627a935 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -5,7 +5,6 @@ package state import ( "crypto/rand" - "encoding/binary" "fmt" "math/big" "testing" @@ -61,8 +60,7 @@ type testBranch struct { } // AddBlocksToState adds `depth` number of blocks to the BlockState, optionally with random branches -func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, - withBranches bool, withFakeStateRoots ...interface{}) ([]*types.Header, []*types.Header) { +func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, withBranches bool) ([]*types.Header, []*types.Header) { var ( currentChain, branchChains []*types.Header branches []testBranch @@ -83,30 +81,15 @@ func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, err = digest.Add(*prd) require.NoError(t, err) - var stateRoot common.Hash - - if len(withFakeStateRoots) > 0 { - // generate some dummy state root based on block number - bs := make([]byte, 4) - binary.LittleEndian.PutUint32(bs, uint32(i)) - stateRoot = common.Hash{bs[0], bs[1], bs[2], bs[3]} - } else { - stateRoot = trie.EmptyHash - } - block := &types.Block{ Header: types.Header{ ParentHash: previousHash, Number: big.NewInt(int64(i)), - StateRoot: stateRoot, + StateRoot: trie.EmptyHash, Digest: digest, }, Body: types.Body{}, } - - // write to tries, since this will need to be deleted - blockState.tries.softSet(stateRoot, trie.NewEmptyTrie()) - currentChain = append(currentChain, &block.Header) hash := block.Header.Hash() From 6be7f6e711d31d1ee242788bfa28dcb6959c1920 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 15:57:05 -0500 Subject: [PATCH 07/15] fix lint --- dot/state/test_helpers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dot/state/test_helpers.go b/dot/state/test_helpers.go index ff8627a935..37e188c9be 100644 --- a/dot/state/test_helpers.go +++ b/dot/state/test_helpers.go @@ -60,7 +60,8 @@ type testBranch struct { } // AddBlocksToState adds `depth` number of blocks to the BlockState, optionally with random branches -func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, withBranches bool) ([]*types.Header, []*types.Header) { +func AddBlocksToState(t *testing.T, blockState *BlockState, depth int, + withBranches bool) ([]*types.Header, []*types.Header) { var ( currentChain, branchChains []*types.Header branches []testBranch From 09580805f9733e9fdfd492afbad9364a0a46e36f Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Wed, 2 Mar 2022 17:02:48 -0500 Subject: [PATCH 08/15] tweak TestSync_SingleBlockProducer --- tests/stress/helpers.go | 2 +- tests/stress/stress_test.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/stress/helpers.go b/tests/stress/helpers.go index acc843e0b1..8bcf797621 100644 --- a/tests/stress/helpers.go +++ b/tests/stress/helpers.go @@ -113,7 +113,7 @@ func compareBlocksByNumberWithRetry(t *testing.T, nodes []*utils.Node, num strin var hashes map[common.Hash][]string var err error - timeout := time.After(60 * time.Second) + timeout := time.After(120 * time.Second) doneBlockProduction: for { time.Sleep(time.Second) diff --git a/tests/stress/stress_test.go b/tests/stress/stress_test.go index 4f112c2eb8..93b2c23542 100644 --- a/tests/stress/stress_test.go +++ b/tests/stress/stress_test.go @@ -102,7 +102,7 @@ func TestSync_SingleBlockProducer(t *testing.T) { require.NoError(t, err) nodes = append(nodes, node) - time.Sleep(time.Second * 60) + time.Sleep(time.Second * 30) defer func() { errList := utils.StopNodes(t, nodes) @@ -118,6 +118,7 @@ func TestSync_SingleBlockProducer(t *testing.T) { require.NoError(t, err, i) continue } + t.Log("hashes", hashes) // there will only be one key in the mapping for _, nodesWithHash := range hashes { From 36996504462bad4cb7cac46d038b4e9d9845b000 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Mar 2022 21:35:53 -0500 Subject: [PATCH 09/15] remove comment --- dot/state/block_race_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/state/block_race_test.go b/dot/state/block_race_test.go index c3bf689a82..f2ba01e116 100644 --- a/dot/state/block_race_test.go +++ b/dot/state/block_race_test.go @@ -28,7 +28,7 @@ func TestConcurrencySetHeader(t *testing.T) { dbs[i] = NewInMemoryDB(t) } - tries := newTriesEmpty() // not used in this test + tries := newTriesEmpty() pend := new(sync.WaitGroup) pend.Add(threads) From 69c7e9e621a2c62414d1b2a2a0a3aac48daaf541 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Thu, 3 Mar 2022 21:52:23 -0500 Subject: [PATCH 10/15] add mock counters for tries test --- dot/state/mock_counter_test.go | 112 +++++++++++++++++++++++++++++++++ dot/state/tries_test.go | 46 +++++++++++--- 2 files changed, 149 insertions(+), 9 deletions(-) create mode 100644 dot/state/mock_counter_test.go diff --git a/dot/state/mock_counter_test.go b/dot/state/mock_counter_test.go new file mode 100644 index 0000000000..78621cf4ef --- /dev/null +++ b/dot/state/mock_counter_test.go @@ -0,0 +1,112 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/prometheus/client_golang/prometheus (interfaces: Counter) + +// Package state is a generated GoMock package. +package state + +import ( + reflect "reflect" + + gomock "github.com/golang/mock/gomock" + prometheus "github.com/prometheus/client_golang/prometheus" + io_prometheus_client "github.com/prometheus/client_model/go" +) + +// MockCounter is a mock of Counter interface. +type MockCounter struct { + ctrl *gomock.Controller + recorder *MockCounterMockRecorder +} + +// MockCounterMockRecorder is the mock recorder for MockCounter. +type MockCounterMockRecorder struct { + mock *MockCounter +} + +// NewMockCounter creates a new mock instance. +func NewMockCounter(ctrl *gomock.Controller) *MockCounter { + mock := &MockCounter{ctrl: ctrl} + mock.recorder = &MockCounterMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockCounter) EXPECT() *MockCounterMockRecorder { + return m.recorder +} + +// Add mocks base method. +func (m *MockCounter) Add(arg0 float64) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Add", arg0) +} + +// Add indicates an expected call of Add. +func (mr *MockCounterMockRecorder) Add(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Add", reflect.TypeOf((*MockCounter)(nil).Add), arg0) +} + +// Collect mocks base method. +func (m *MockCounter) Collect(arg0 chan<- prometheus.Metric) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Collect", arg0) +} + +// Collect indicates an expected call of Collect. +func (mr *MockCounterMockRecorder) Collect(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Collect", reflect.TypeOf((*MockCounter)(nil).Collect), arg0) +} + +// Desc mocks base method. +func (m *MockCounter) Desc() *prometheus.Desc { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Desc") + ret0, _ := ret[0].(*prometheus.Desc) + return ret0 +} + +// Desc indicates an expected call of Desc. +func (mr *MockCounterMockRecorder) Desc() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Desc", reflect.TypeOf((*MockCounter)(nil).Desc)) +} + +// Describe mocks base method. +func (m *MockCounter) Describe(arg0 chan<- *prometheus.Desc) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Describe", arg0) +} + +// Describe indicates an expected call of Describe. +func (mr *MockCounterMockRecorder) Describe(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Describe", reflect.TypeOf((*MockCounter)(nil).Describe), arg0) +} + +// Inc mocks base method. +func (m *MockCounter) Inc() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Inc") +} + +// Inc indicates an expected call of Inc. +func (mr *MockCounterMockRecorder) Inc() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Inc", reflect.TypeOf((*MockCounter)(nil).Inc)) +} + +// Write mocks base method. +func (m *MockCounter) Write(arg0 *io_prometheus_client.Metric) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Write", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Write indicates an expected call of Write. +func (mr *MockCounterMockRecorder) Write(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockCounter)(nil).Write), arg0) +} diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index cff6bd72a1..a43ebd038a 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -9,6 +9,7 @@ import ( "github.com/ChainSafe/gossamer/internal/trie/node" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/trie" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -34,7 +35,7 @@ func Test_NewTries(t *testing.T) { } //go:generate mockgen -destination=mock_gauge_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Gauge - +//go:generate mockgen -destination=mock_counter_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Counter func Test_Tries_softSet(t *testing.T) { t.Parallel() @@ -70,8 +71,23 @@ func Test_Tries_softSet(t *testing.T) { testCase := testCase t.Run(name, func(t *testing.T) { t.Parallel() - tries := newTriesEmpty() - tries.rootToTrie = testCase.rootToTrie + ctrl := gomock.NewController(t) + + triesGauge := NewMockGauge(ctrl) + if testCase.triesGaugeInc { + triesGauge.EXPECT().Inc() + } + + setCounter := NewMockCounter(ctrl) + if testCase.triesGaugeInc { + setCounter.EXPECT().Inc() + } + + tries := &Tries{ + rootToTrie: testCase.rootToTrie, + triesGauge: triesGauge, + setCounter: setCounter, + } tries.softSet(testCase.root, testCase.trie) @@ -86,29 +102,29 @@ func Test_Tries_delete(t *testing.T) { testCases := map[string]struct { rootToTrie map[common.Hash]*trie.Trie root common.Hash - triesGaugeSet float64 + deleteCounterInc bool expectedRootToTrie map[common.Hash]*trie.Trie }{ "not found": { rootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, - root: common.Hash{1, 2, 3}, - triesGaugeSet: 1, + root: common.Hash{1, 2, 3}, expectedRootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, + deleteCounterInc: true, }, "deleted": { rootToTrie: map[common.Hash]*trie.Trie{ {1, 2, 3}: {}, {3, 4, 5}: {}, }, - root: common.Hash{1, 2, 3}, - triesGaugeSet: 1, + root: common.Hash{1, 2, 3}, expectedRootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, + deleteCounterInc: true, }, } @@ -116,7 +132,19 @@ func Test_Tries_delete(t *testing.T) { testCase := testCase t.Run(name, func(t *testing.T) { t.Parallel() - tries := newTriesEmpty() + ctrl := gomock.NewController(t) + deleteCounter := NewMockCounter(ctrl) + if testCase.deleteCounterInc { + deleteCounter.EXPECT().Inc() + } + + tries := &Tries{ + rootToTrie: testCase.rootToTrie, + triesGauge: triesGauge, + setCounter: setCounter, + deleteCounter: deleteCounter, + } + tries.rootToTrie = testCase.rootToTrie tries.delete(testCase.root) From 1865b17f1cf4873a350eacb283517507749a21f3 Mon Sep 17 00:00:00 2001 From: Timothy Wu Date: Thu, 3 Mar 2022 21:54:02 -0500 Subject: [PATCH 11/15] Update tests/stress/stress_test.go Co-authored-by: Quentin McGaw --- tests/stress/stress_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/stress/stress_test.go b/tests/stress/stress_test.go index 93b2c23542..58afc5823d 100644 --- a/tests/stress/stress_test.go +++ b/tests/stress/stress_test.go @@ -118,7 +118,6 @@ func TestSync_SingleBlockProducer(t *testing.T) { require.NoError(t, err, i) continue } - t.Log("hashes", hashes) // there will only be one key in the mapping for _, nodesWithHash := range hashes { From 50544c7f9808e85553beaacd84ec54da75a25430 Mon Sep 17 00:00:00 2001 From: Timothy Wu Date: Fri, 4 Mar 2022 14:11:20 -0500 Subject: [PATCH 12/15] Update dot/state/tries_test.go Co-authored-by: Quentin McGaw --- dot/state/tries_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index a43ebd038a..49a51a8e67 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -145,8 +145,6 @@ func Test_Tries_delete(t *testing.T) { deleteCounter: deleteCounter, } - tries.rootToTrie = testCase.rootToTrie - tries.delete(testCase.root) assert.Equal(t, testCase.expectedRootToTrie, tries.rootToTrie) From 71bbd7742f2a3dffb8aa731c6a00c518b617991f Mon Sep 17 00:00:00 2001 From: Timothy Wu Date: Fri, 4 Mar 2022 14:11:29 -0500 Subject: [PATCH 13/15] Update dot/state/tries_test.go Co-authored-by: Quentin McGaw --- dot/state/tries_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index 49a51a8e67..10660a24a2 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -36,6 +36,7 @@ func Test_NewTries(t *testing.T) { //go:generate mockgen -destination=mock_gauge_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Gauge //go:generate mockgen -destination=mock_counter_test.go -package $GOPACKAGE github.com/prometheus/client_golang/prometheus Counter + func Test_Tries_softSet(t *testing.T) { t.Parallel() From 7b68e097c2edc7b1cea7d6ba0048d4d58bb3b655 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Mar 2022 14:20:17 -0500 Subject: [PATCH 14/15] cr feedback --- dot/state/block_test.go | 2 ++ dot/state/tries_test.go | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/dot/state/block_test.go b/dot/state/block_test.go index ac1be2a8a6..bf111c3a69 100644 --- a/dot/state/block_test.go +++ b/dot/state/block_test.go @@ -38,6 +38,8 @@ func newTestBlockState(t *testing.T, header *types.Header, tries *Tries) *BlockS bs, err := NewBlockStateFromGenesis(db, tries, header, telemetryMock) require.NoError(t, err) + // loads in-memory tries with genesis state root, should be deleted + // after another block is finalised tr := trie.NewEmptyTrie() err = tr.Load(bs.db, header.StateRoot) require.NoError(t, err) diff --git a/dot/state/tries_test.go b/dot/state/tries_test.go index a43ebd038a..8405edf911 100644 --- a/dot/state/tries_test.go +++ b/dot/state/tries_test.go @@ -104,12 +104,14 @@ func Test_Tries_delete(t *testing.T) { root common.Hash deleteCounterInc bool expectedRootToTrie map[common.Hash]*trie.Trie + triesGaugeSet float64 }{ "not found": { rootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, - root: common.Hash{1, 2, 3}, + root: common.Hash{1, 2, 3}, + triesGaugeSet: 1, expectedRootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, @@ -120,7 +122,8 @@ func Test_Tries_delete(t *testing.T) { {1, 2, 3}: {}, {3, 4, 5}: {}, }, - root: common.Hash{1, 2, 3}, + root: common.Hash{1, 2, 3}, + triesGaugeSet: 1, expectedRootToTrie: map[common.Hash]*trie.Trie{ {3, 4, 5}: {}, }, @@ -133,6 +136,9 @@ func Test_Tries_delete(t *testing.T) { t.Run(name, func(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) + triesGauge := NewMockGauge(ctrl) + triesGauge.EXPECT().Set(testCase.triesGaugeSet) + deleteCounter := NewMockCounter(ctrl) if testCase.deleteCounterInc { deleteCounter.EXPECT().Inc() @@ -141,7 +147,6 @@ func Test_Tries_delete(t *testing.T) { tries := &Tries{ rootToTrie: testCase.rootToTrie, triesGauge: triesGauge, - setCounter: setCounter, deleteCounter: deleteCounter, } From cca78fdd5ea2d87ecb6ab72c82681e53544c4412 Mon Sep 17 00:00:00 2001 From: Tim Wu Date: Fri, 4 Mar 2022 21:16:51 -0500 Subject: [PATCH 15/15] move to BlockState.deleteFromTries --- dot/state/block_finalisation.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index 7b27dfc849..fb71ac0f19 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -173,15 +173,9 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er if !bs.lastFinalised.Equal(hash) { defer func(lastFinalised common.Hash) { - lastFinalisedHeader, err := bs.GetHeader(lastFinalised) + err := bs.deleteFromTries(lastFinalised) if err != nil { - logger.Debugf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) - } - stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) - if stateRootTrie != nil { - bs.tries.delete(lastFinalisedHeader.StateRoot) - } else { - logger.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + logger.Debugf("%v", err) } }(bs.lastFinalised) } @@ -190,6 +184,20 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er return nil } +func (bs *BlockState) deleteFromTries(lastFinalised common.Hash) error { + lastFinalisedHeader, err := bs.GetHeader(lastFinalised) + if err != nil { + return fmt.Errorf("unable to retrieve header for last finalised block, hash: %s, err: %s", bs.lastFinalised, err) + } + stateRootTrie := bs.tries.get(lastFinalisedHeader.StateRoot) + if stateRootTrie != nil { + bs.tries.delete(lastFinalisedHeader.StateRoot) + } else { + return fmt.Errorf("unable to find trie with stateroot hash: %s", lastFinalisedHeader.StateRoot) + } + return nil +} + func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { if curr.Equal(bs.lastFinalised) { return nil