From e868e6ceb9ab3fa83d76b9f9256f693050cbe2a7 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 13 Dec 2021 18:50:35 +0000 Subject: [PATCH] Change deletedKeys to be a set (map) - Uniformize together with inserted keys - Refactor calling code --- dot/state/pruner/pruner.go | 22 +++++++++++----------- dot/state/storage.go | 4 ++-- lib/runtime/storage/trie.go | 5 +++-- lib/trie/database.go | 11 ++++++++--- lib/trie/trie.go | 10 +++++----- lib/trie/trie_test.go | 2 +- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/dot/state/pruner/pruner.go b/dot/state/pruner/pruner.go index 9d06f216275..2c08710915c 100644 --- a/dot/state/pruner/pruner.go +++ b/dot/state/pruner/pruner.go @@ -50,7 +50,7 @@ type Config struct { // Pruner is implemented by FullNode and ArchiveNode. type Pruner interface { - StoreJournalRecord(deleted []common.Hash, insertedHashesSet map[common.Hash]struct{}, + StoreJournalRecord(deletedHashesSet, insertedHashesSet map[common.Hash]struct{}, blockHash common.Hash, blockNum int64) error } @@ -58,7 +58,7 @@ type Pruner interface { type ArchiveNode struct{} // StoreJournalRecord for archive node doesn't do anything. -func (a *ArchiveNode) StoreJournalRecord(_ []common.Hash, _ map[common.Hash]struct{}, +func (a *ArchiveNode) StoreJournalRecord(_, _ map[common.Hash]struct{}, _ common.Hash, _ int64) error { return nil } @@ -90,7 +90,7 @@ type journalRecord struct { // Hash of keys that are inserted into state trie of the block insertedHashesSet map[common.Hash]struct{} // Hash of keys that are deleted from state trie of the block - deletedKeys []common.Hash + deletedHashesSet map[common.Hash]struct{} } type journalKey struct { @@ -98,12 +98,12 @@ type journalKey struct { blockHash common.Hash } -func newJournalRecord(hash common.Hash, insertedHashesSet map[common.Hash]struct{}, - deletedKeys []common.Hash) *journalRecord { +func newJournalRecord(hash common.Hash, insertedHashesSet, + deletedHashesSet map[common.Hash]struct{}) *journalRecord { return &journalRecord{ blockHash: hash, insertedHashesSet: insertedHashesSet, - deletedKeys: deletedKeys, + deletedHashesSet: deletedHashesSet, } } @@ -139,9 +139,9 @@ func NewFullNode(db, storageDB chaindb.Database, retainBlocks int64, l log.Level } // StoreJournalRecord stores journal record into DB and add deathRow into deathList -func (p *FullNode) StoreJournalRecord(deleted []common.Hash, insertedHashesSet map[common.Hash]struct{}, +func (p *FullNode) StoreJournalRecord(deletedHashesSet, insertedHashesSet map[common.Hash]struct{}, blockHash common.Hash, blockNum int64) error { - jr := newJournalRecord(blockHash, insertedHashesSet, deleted) + jr := newJournalRecord(blockHash, insertedHashesSet, deletedHashesSet) key := &journalKey{blockNum, blockHash} err := p.storeJournal(key, jr) @@ -170,13 +170,13 @@ func (p *FullNode) addDeathRow(jr *journalRecord, blockNum int64) { p.processInsertedKeys(jr.insertedHashesSet, jr.blockHash) // add deleted keys from journal to death index - for _, k := range jr.deletedKeys { + for k := range jr.deletedHashesSet { p.deathIndex[k] = blockNum } deletedKeys := make(map[common.Hash]int64) - for _, data := range jr.deletedKeys { - deletedKeys[data] = blockNum + for k := range jr.deletedHashesSet { + deletedKeys[k] = blockNum } blockIndex := blockNum - p.pendingNumber diff --git a/dot/state/storage.go b/dot/state/storage.go index 50706a53689..5b71fa4592a 100644 --- a/dot/state/storage.go +++ b/dot/state/storage.go @@ -112,8 +112,8 @@ func (s *StorageState) StoreTrie(ts *rtstorage.TrieState, header *types.Header) return fmt.Errorf("failed to get state trie inserted keys: block %s %w", header.Hash(), err) } - delKeys := ts.GetDeletedNodeHashes() - err = s.pruner.StoreJournalRecord(delKeys, insertedNodeHashes, header.Hash(), header.Number.Int64()) + deletedNodeHashes := ts.GetDeletedNodeHashes() + err = s.pruner.StoreJournalRecord(deletedNodeHashes, insertedNodeHashes, header.Hash(), header.Number.Int64()) if err != nil { return err } diff --git a/lib/runtime/storage/trie.go b/lib/runtime/storage/trie.go index 25f3a2f6fca..acb9e68f594 100644 --- a/lib/runtime/storage/trie.go +++ b/lib/runtime/storage/trie.go @@ -282,8 +282,9 @@ func (s *TrieState) GetInsertedNodeHashes() (hashesSet map[common.Hash]struct{}, return s.t.GetInsertedNodeHashes() } -// GetDeletedNodeHashes returns the hash of nodes that are deleted from state trie since last block produced -func (s *TrieState) GetDeletedNodeHashes() []common.Hash { +// GetDeletedNodeHashes returns the hash of nodes that were deleted +// from the state trie since the last block produced. +func (s *TrieState) GetDeletedNodeHashes() (hashesSet map[common.Hash]struct{}) { s.lock.RLock() defer s.lock.RUnlock() return s.t.GetDeletedNodeHash() diff --git a/lib/trie/database.go b/lib/trie/database.go index 1fe6e96d1f2..1ceac869edc 100644 --- a/lib/trie/database.go +++ b/lib/trie/database.go @@ -452,8 +452,13 @@ func (t *Trie) getInsertedNodeHashes(n Node, hashes map[common.Hash]struct{}) (e return nil } -// GetDeletedNodeHash returns the hash of nodes that were +// GetDeletedNodeHash returns a set of all the hashes of nodes that were // deleted from the trie since the last snapshot was made. -func (t *Trie) GetDeletedNodeHash() []common.Hash { - return t.deletedKeys +// The returned set is a copy of the internal set to prevent data races. +func (t *Trie) GetDeletedNodeHash() (hashesSet map[common.Hash]struct{}) { + hashesSet = make(map[common.Hash]struct{}, len(t.deletedKeys)) + for k := range t.deletedKeys { + hashesSet[k] = struct{}{} + } + return hashesSet } diff --git a/lib/trie/trie.go b/lib/trie/trie.go index 73f3f3ec4db..e91d52cf40d 100644 --- a/lib/trie/trie.go +++ b/lib/trie/trie.go @@ -23,7 +23,7 @@ type Trie struct { generation uint64 root Node childTries map[common.Hash]*Trie // Used to store the child tries. - deletedKeys []common.Hash + deletedKeys map[common.Hash]struct{} } // NewEmptyTrie creates a trie with a nil root @@ -37,7 +37,7 @@ func NewTrie(root Node) *Trie { root: root, childTries: make(map[common.Hash]*Trie), generation: 0, // Initially zero but increases after every snapshot. - deletedKeys: make([]common.Hash, 0), + deletedKeys: make(map[common.Hash]struct{}), } } @@ -48,7 +48,7 @@ func (t *Trie) Snapshot() *Trie { children[h] = &Trie{ generation: c.generation + 1, root: c.root, - deletedKeys: make([]common.Hash, 0), + deletedKeys: make(map[common.Hash]struct{}), } } @@ -56,7 +56,7 @@ func (t *Trie) Snapshot() *Trie { generation: t.generation + 1, root: t.root, childTries: children, - deletedKeys: make([]common.Hash, 0), + deletedKeys: make(map[common.Hash]struct{}), } return newTrie @@ -77,7 +77,7 @@ func (t *Trie) maybeUpdateGeneration(n Node) Node { oldNodeHash := n.GetHash() if len(oldNodeHash) > 0 { hash := common.BytesToHash(oldNodeHash) - t.deletedKeys = append(t.deletedKeys, hash) + t.deletedKeys[hash] = struct{}{} } return newNode } diff --git a/lib/trie/trie_test.go b/lib/trie/trie_test.go index e564c52df29..cc0b0d28f27 100644 --- a/lib/trie/trie_test.go +++ b/lib/trie/trie_test.go @@ -511,7 +511,7 @@ func TestTrieDiff(t *testing.T) { err = newTrie.WriteDirty(storageDB) require.NoError(t, err) - for _, key := range deletedKeys { + for key := range deletedKeys { err = storageDB.Del(key.ToBytes()) require.NoError(t, err) }