Skip to content

Commit

Permalink
Change deletedKeys to be a set (map)
Browse files Browse the repository at this point in the history
- Uniformize together with inserted keys
- Refactor calling code
  • Loading branch information
qdm12 committed Jan 6, 2022
1 parent 9c6aae1 commit dd46af2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
22 changes: 11 additions & 11 deletions dot/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ 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
}

// ArchiveNode is a no-op since we don't prune nodes in archive mode.
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
}
Expand Down Expand Up @@ -90,20 +90,20 @@ 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 {
blockNum int64
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,
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions dot/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions lib/runtime/storage/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 8 additions & 3 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,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
}
10 changes: 5 additions & 5 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}),
}
}

Expand All @@ -48,15 +48,15 @@ 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{}),
}
}

newTrie := &Trie{
generation: t.generation + 1,
root: t.root,
childTries: children,
deletedKeys: make([]common.Hash, 0),
deletedKeys: make(map[common.Hash]struct{}),
}

return newTrie
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit dd46af2

Please sign in to comment.