Skip to content

Commit

Permalink
Add exported Tries methods to set tries
Browse files Browse the repository at this point in the history
- `SetEmptyTrie` to set an empty trie without taking a state version argument
- `SetTrie` to change `NewTries` constructor to NOT take a trie as input
  • Loading branch information
qdm12 committed Aug 24, 2022
1 parent 624afee commit ec41fd8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dot/state/tries.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func NewTries(t *trie.Trie) (tries *Tries) {
}
}

func (t *Tries) SetEmptyTrie() {
t.softSet(trie.EmptyHash, trie.NewEmptyTrie())
}

func (t *Tries) SetTrie(trie *trie.Trie) {
t.softSet(trie.MustHash(), trie)
}

// softSet sets the given trie at the given root hash
// in the memory map only if it is not already set.
func (t *Tries) softSet(root common.Hash, trie *trie.Trie) {
Expand Down
43 changes: 43 additions & 0 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,49 @@ func Test_NewTries(t *testing.T) {
assert.Equal(t, expectedTries, rootToTrie)
}

func Test_Tries_SetEmptyTrie(t *testing.T) {
t.Parallel()

tr := trie.NewTrie(&node.Node{Key: []byte{1}})

tries := NewTries(tr)
tries.SetEmptyTrie()

expectedTries := &Tries{
rootToTrie: map[common.Hash]*trie.Trie{
tr.MustHash(): tr,
trie.EmptyHash: trie.NewEmptyTrie(),
},
triesGauge: triesGauge,
setCounter: setCounter,
deleteCounter: deleteCounter,
}

assert.Equal(t, expectedTries, tries)
}

func Test_Tries_SetTrie(t *testing.T) {
t.Parallel()

trieA := trie.NewTrie(&node.Node{Key: []byte{1}})
trieB := trie.NewTrie(&node.Node{Key: []byte{2}})

tries := NewTries(trieA)
tries.SetTrie(trieB)

expectedTries := &Tries{
rootToTrie: map[common.Hash]*trie.Trie{
trieA.MustHash(): trieA,
trieB.MustHash(): trieB,
},
triesGauge: triesGauge,
setCounter: setCounter,
deleteCounter: deleteCounter,
}

assert.Equal(t, expectedTries, tries)
}

//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

Expand Down

0 comments on commit ec41fd8

Please sign in to comment.