Skip to content

Commit 46df10f

Browse files
committed
chore(lib/trie): LoadFromMap as function
- Trie mutation should be transactional/atomic as a method - Changed from method to function to avoid leaving a trie in a bad state - Function returns a new empty trie with all the key and value pairs given
1 parent 14dda74 commit 46df10f

File tree

6 files changed

+28
-70
lines changed

6 files changed

+28
-70
lines changed

dot/import.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ func newTrieFromPairs(filename string) (*trie.Trie, error) {
6262
entries[pairArr[0].(string)] = pairArr[1].(string)
6363
}
6464

65-
tr := trie.NewEmptyTrie()
66-
err = tr.LoadFromMap(entries)
65+
tr, err := trie.LoadFromMap(entries)
6766
if err != nil {
6867
return nil, err
6968
}
7069

71-
return tr, nil
70+
return &tr, nil
7271
}
7372

7473
func newHeaderFromFile(filename string) (*types.Header, error) {

dot/node_integration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ func TestInitNode_LoadStorageRoot(t *testing.T) {
186186
node, err := NewNode(cfg, ks)
187187
require.NoError(t, err)
188188

189-
expected := &trie.Trie{}
190-
err = expected.LoadFromMap(gen.GenesisFields().Raw["top"])
189+
expected, err := trie.LoadFromMap(gen.GenesisFields().Raw["top"])
191190
require.NoError(t, err)
192191

193192
expectedRoot, err := expected.Hash()

lib/runtime/wasmer/exports_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,7 @@ func newTrieFromPairs(t *testing.T, filename string) *trie.Trie {
10831083
entries[pairArr[0].(string)] = pairArr[1].(string)
10841084
}
10851085

1086-
tr := trie.NewEmptyTrie()
1087-
err = tr.LoadFromMap(entries)
1086+
tr, err := trie.LoadFromMap(entries)
10881087
require.NoError(t, err)
1089-
return tr
1088+
return &tr
10901089
}

lib/runtime/wasmer/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewTrieFromGenesis(gen genesis.Genesis) (tr trie.Trie, err error) {
2626
ErrGenesisTopNotFound, gen.Name)
2727
}
2828

29-
err = tr.LoadFromMap(keyValues)
29+
tr, err = trie.LoadFromMap(keyValues)
3030
if err != nil {
3131
return tr, fmt.Errorf("loading genesis top key values into trie: %w", err)
3232
}

lib/trie/trie.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,25 +511,26 @@ func (t *Trie) insertInBranch(parentBranch *Node, key, value []byte) (
511511
return newParentBranch, mutated, nodesCreated
512512
}
513513

514-
// LoadFromMap loads the given data mapping of key to value into the trie.
514+
// LoadFromMap loads the given data mapping of key to value into a new empty trie.
515515
// The keys are in hexadecimal little Endian encoding and the values
516516
// are hexadecimal encoded.
517-
func (t *Trie) LoadFromMap(data map[string]string) (err error) {
517+
func LoadFromMap(data map[string]string) (trie Trie, err error) {
518+
trie = *NewEmptyTrie()
518519
for key, value := range data {
519520
keyLEBytes, err := common.HexToBytes(key)
520521
if err != nil {
521-
return fmt.Errorf("cannot convert key hex to bytes: %w", err)
522+
return Trie{}, fmt.Errorf("cannot convert key hex to bytes: %w", err)
522523
}
523524

524525
valueBytes, err := common.HexToBytes(value)
525526
if err != nil {
526-
return fmt.Errorf("cannot convert value hex to bytes: %w", err)
527+
return Trie{}, fmt.Errorf("cannot convert value hex to bytes: %w", err)
527528
}
528529

529-
t.Put(keyLEBytes, valueBytes)
530+
trie.Put(keyLEBytes, valueBytes)
530531
}
531532

532-
return nil
533+
return trie, nil
533534
}
534535

535536
// GetKeysWithPrefix returns all keys in little Endian

lib/trie/trie_test.go

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,19 +1568,27 @@ func Test_Trie_insertInBranch(t *testing.T) {
15681568
}
15691569
}
15701570

1571-
func Test_Trie_LoadFromMap(t *testing.T) {
1571+
func Test_LoadFromMap(t *testing.T) {
15721572
t.Parallel()
15731573

15741574
testCases := map[string]struct {
1575-
trie Trie
15761575
data map[string]string
15771576
expectedTrie Trie
15781577
errWrapped error
15791578
errMessage string
15801579
}{
1581-
"nil data": {},
1580+
"nil data": {
1581+
expectedTrie: Trie{
1582+
childTries: map[common.Hash]*Trie{},
1583+
deletedMerkleValues: map[string]struct{}{},
1584+
},
1585+
},
15821586
"empty data": {
15831587
data: map[string]string{},
1588+
expectedTrie: Trie{
1589+
childTries: map[common.Hash]*Trie{},
1590+
deletedMerkleValues: map[string]struct{}{},
1591+
},
15841592
},
15851593
"bad key": {
15861594
data: map[string]string{
@@ -1622,56 +1630,8 @@ func Test_Trie_LoadFromMap(t *testing.T) {
16221630
},
16231631
}),
16241632
},
1625-
},
1626-
},
1627-
"override trie": {
1628-
trie: Trie{
1629-
root: &Node{
1630-
Key: []byte{00, 01},
1631-
SubValue: []byte{106},
1632-
Dirty: true,
1633-
Descendants: 2,
1634-
Children: padRightChildren([]*Node{
1635-
{
1636-
SubValue: []byte{9},
1637-
},
1638-
nil,
1639-
{
1640-
Key: []byte{0},
1641-
SubValue: []byte{107},
1642-
Dirty: true,
1643-
},
1644-
}),
1645-
},
1646-
},
1647-
data: map[string]string{
1648-
"0x01": "0x06",
1649-
"0x0120": "0x07",
1650-
"0x0130": "0x08",
1651-
},
1652-
expectedTrie: Trie{
1653-
root: &Node{
1654-
Key: []byte{00, 01},
1655-
SubValue: []byte{6},
1656-
Dirty: true,
1657-
Descendants: 3,
1658-
Children: padRightChildren([]*Node{
1659-
{
1660-
SubValue: []byte{9},
1661-
},
1662-
nil,
1663-
{
1664-
Key: []byte{0},
1665-
SubValue: []byte{7},
1666-
Dirty: true,
1667-
},
1668-
{
1669-
Key: []byte{0},
1670-
SubValue: []byte{8},
1671-
Dirty: true,
1672-
},
1673-
}),
1674-
},
1633+
childTries: map[common.Hash]*Trie{},
1634+
deletedMerkleValues: map[string]struct{}{},
16751635
},
16761636
},
16771637
}
@@ -1681,14 +1641,14 @@ func Test_Trie_LoadFromMap(t *testing.T) {
16811641
t.Run(name, func(t *testing.T) {
16821642
t.Parallel()
16831643

1684-
err := testCase.trie.LoadFromMap(testCase.data)
1644+
trie, err := LoadFromMap(testCase.data)
16851645

16861646
assert.ErrorIs(t, err, testCase.errWrapped)
16871647
if testCase.errWrapped != nil {
16881648
assert.EqualError(t, err, testCase.errMessage)
16891649
}
16901650

1691-
assert.Equal(t, testCase.expectedTrie, testCase.trie)
1651+
assert.Equal(t, testCase.expectedTrie, trie)
16921652
})
16931653
}
16941654
}

0 commit comments

Comments
 (0)