Skip to content

Commit 6d4670a

Browse files
core, trie: refactor trie API ethereum#26995 (#1147)
In this PR, all TryXXX(e.g. TryGet) APIs of trie are renamed to XXX(e.g. Get) with an error returned. The original XXX(e.g. Get) APIs are renamed to MustXXX(e.g. MustGet) and does not return any error -- they print a log output. A future PR will change the behaviour to panic on errorrs. Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
1 parent b743f7c commit 6d4670a

File tree

20 files changed

+218
-194
lines changed

20 files changed

+218
-194
lines changed

XDCx/tradingstate/XDCx_trie.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (t *XDCXTrie) Get(key []byte) []byte {
7777
// The value bytes must not be modified by the caller.
7878
// If a node was not found in the database, a MissingNodeError is returned.
7979
func (t *XDCXTrie) TryGet(key []byte) ([]byte, error) {
80-
return t.trie.TryGet(key)
80+
return t.trie.Get(key)
8181
}
8282

8383
// TryGetBestLeftKey returns the value of max left leaf
@@ -117,7 +117,7 @@ func (t *XDCXTrie) Update(key, value []byte) {
117117
//
118118
// If a node was not found in the database, a MissingNodeError is returned.
119119
func (t *XDCXTrie) TryUpdate(key, value []byte) error {
120-
err := t.trie.TryUpdate(key, value)
120+
err := t.trie.Update(key, value)
121121
if err != nil {
122122
return err
123123
}
@@ -136,7 +136,7 @@ func (t *XDCXTrie) Delete(key []byte) {
136136
// If a node was not found in the database, a MissingNodeError is returned.
137137
func (t *XDCXTrie) TryDelete(key []byte) error {
138138
delete(t.getSecKeyCache(), string(key))
139-
return t.trie.TryDelete(key)
139+
return t.trie.Delete(key)
140140
}
141141

142142
// GetKey returns the sha3 preimage of a hashed key that was

XDCxlending/lendingstate/XDCx_trie.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (t *XDCXTrie) Get(key []byte) []byte {
7777
// The value bytes must not be modified by the caller.
7878
// If a node was not found in the database, a MissingNodeError is returned.
7979
func (t *XDCXTrie) TryGet(key []byte) ([]byte, error) {
80-
return t.trie.TryGet(key)
80+
return t.trie.Get(key)
8181
}
8282

8383
// TryGetBestLeftKey returns the value of max left leaf
@@ -113,7 +113,7 @@ func (t *XDCXTrie) Update(key, value []byte) {
113113
//
114114
// If a node was not found in the database, a MissingNodeError is returned.
115115
func (t *XDCXTrie) TryUpdate(key, value []byte) error {
116-
err := t.trie.TryUpdate(key, value)
116+
err := t.trie.Update(key, value)
117117
if err != nil {
118118
return err
119119
}
@@ -132,7 +132,7 @@ func (t *XDCXTrie) Delete(key []byte) {
132132
// If a node was not found in the database, a MissingNodeError is returned.
133133
func (t *XDCXTrie) TryDelete(key []byte) error {
134134
delete(t.getSecKeyCache(), string(key))
135-
return t.trie.TryDelete(key)
135+
return t.trie.Delete(key)
136136
}
137137

138138
// GetKey returns the sha3 preimage of a hashed key that was

core/rawdb/accessors_indexes_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ func (h *testHasher) Reset() {
4343
h.hasher.Reset()
4444
}
4545

46-
func (h *testHasher) Update(key, val []byte) {
46+
func (h *testHasher) Update(key, val []byte) error {
4747
h.hasher.Write(key)
4848
h.hasher.Write(val)
49+
return nil
4950
}
5051

5152
func (h *testHasher) Hash() common.Hash {

core/state/sync_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,22 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool) {
214214
for i, node := range nodeElements {
215215
if bypath {
216216
if len(node.syncPath) == 1 {
217-
data, _, err := srcTrie.TryGetNode(node.syncPath[0])
217+
data, _, err := srcTrie.GetNode(node.syncPath[0])
218218
if err != nil {
219219
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[0], err)
220220
}
221221
nodeResults[i] = trie.NodeSyncResult{Path: node.path, Data: data}
222222
} else {
223223
var acc types.StateAccount
224-
if err := rlp.DecodeBytes(srcTrie.Get(node.syncPath[0]), &acc); err != nil {
224+
if err := rlp.DecodeBytes(srcTrie.MustGet(node.syncPath[0]), &acc); err != nil {
225225
t.Fatalf("failed to decode account on path %x: %v", node.syncPath[0], err)
226226
}
227227
id := trie.StorageTrieID(srcRoot, common.BytesToHash(node.syncPath[0]), acc.Root)
228228
stTrie, err := trie.New(id, srcDb.TrieDB())
229229
if err != nil {
230230
t.Fatalf("failed to retriev storage trie for path %x: %v", node.syncPath[1], err)
231231
}
232-
data, _, err := stTrie.TryGetNode(node.syncPath[1])
232+
data, _, err := stTrie.GetNode(node.syncPath[1])
233233
if err != nil {
234234
t.Fatalf("failed to retrieve node data for path %x: %v", node.syncPath[1], err)
235235
}

core/types/block_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,10 @@ func (h *testHasher) Reset() {
163163
h.hasher.Reset()
164164
}
165165

166-
func (h *testHasher) Update(key, val []byte) {
166+
func (h *testHasher) Update(key, val []byte) error {
167167
h.hasher.Write(key)
168168
h.hasher.Write(val)
169+
return nil
169170
}
170171

171172
func (h *testHasher) Hash() common.Hash {

core/types/hashing.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) {
6161
// This is internal, do not use.
6262
type TrieHasher interface {
6363
Reset()
64-
Update([]byte, []byte)
64+
Update([]byte, []byte) error
6565
Hash() common.Hash
6666
}
6767

@@ -92,6 +92,9 @@ func DeriveSha(list DerivableList, hasher TrieHasher) common.Hash {
9292
// StackTrie requires values to be inserted in increasing hash order, which is not the
9393
// order that `list` provides hashes in. This insertion sequence ensures that the
9494
// order is correct.
95+
//
96+
// The error returned by hasher is omitted because hasher will produce an incorrect
97+
// hash in case any error occurs.
9598
indexBuf := make([]byte, 0, list.Len())
9699
for i := 1; i < list.Len() && i <= 0x7f; i++ {
97100
indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i))

core/types/hashing_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,10 @@ func (d *hashToHumanReadable) Reset() {
202202
d.data = make([]byte, 0)
203203
}
204204

205-
func (d *hashToHumanReadable) Update(i []byte, i2 []byte) {
205+
func (d *hashToHumanReadable) Update(i []byte, i2 []byte) error {
206206
l := fmt.Sprintf("%x %x\n", i, i2)
207207
d.data = append(d.data, []byte(l)...)
208+
return nil
208209
}
209210

210211
func (d *hashToHumanReadable) Hash() common.Hash {

internal/ethapi/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ func (h *testHasher) Reset() {
4444
h.hasher.Reset()
4545
}
4646

47-
func (h *testHasher) Update(key, val []byte) {
47+
func (h *testHasher) Update(key, val []byte) error {
4848
h.hasher.Write(key)
4949
h.hasher.Write(val)
50+
return nil
5051
}
5152

5253
func (h *testHasher) Hash() common.Hash {

trie/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/XinFinOrg/XDPoSChain/common"
2323
)
2424

25-
// MissingNodeError is returned by the trie functions (TryGet, TryUpdate, TryDelete)
25+
// MissingNodeError is returned by the trie functions (Get, Update, Delete)
2626
// in the case where a trie node is not present in the local database. It contains
2727
// information necessary for retrieving the missing node.
2828
type MissingNodeError struct {

trie/iterator_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestIterator(t *testing.T) {
5858
all := make(map[string]string)
5959
for _, val := range vals {
6060
all[val.k] = val.v
61-
trie.Update([]byte(val.k), []byte(val.v))
61+
trie.MustUpdate([]byte(val.k), []byte(val.v))
6262
}
6363
root, nodes := trie.Commit(false)
6464
db.Update(NewWithNodeSet(nodes))
@@ -93,8 +93,8 @@ func TestIteratorLargeData(t *testing.T) {
9393
for i := byte(0); i < 255; i++ {
9494
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
9595
value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
96-
trie.Update(value.k, value.v)
97-
trie.Update(value2.k, value2.v)
96+
trie.MustUpdate(value.k, value.v)
97+
trie.MustUpdate(value2.k, value2.v)
9898
vals[string(value.k)] = value
9999
vals[string(value2.k)] = value2
100100
}
@@ -182,7 +182,7 @@ var testdata2 = []kvs{
182182
func TestIteratorSeek(t *testing.T) {
183183
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
184184
for _, val := range testdata1 {
185-
trie.Update([]byte(val.k), []byte(val.v))
185+
trie.MustUpdate([]byte(val.k), []byte(val.v))
186186
}
187187

188188
// Seek to the middle.
@@ -224,7 +224,7 @@ func TestDifferenceIterator(t *testing.T) {
224224
dba := NewDatabase(rawdb.NewMemoryDatabase())
225225
triea := NewEmpty(dba)
226226
for _, val := range testdata1 {
227-
triea.Update([]byte(val.k), []byte(val.v))
227+
triea.MustUpdate([]byte(val.k), []byte(val.v))
228228
}
229229
rootA, nodesA := triea.Commit(false)
230230
dba.Update(NewWithNodeSet(nodesA))
@@ -233,7 +233,7 @@ func TestDifferenceIterator(t *testing.T) {
233233
dbb := NewDatabase(rawdb.NewMemoryDatabase())
234234
trieb := NewEmpty(dbb)
235235
for _, val := range testdata2 {
236-
trieb.Update([]byte(val.k), []byte(val.v))
236+
trieb.MustUpdate([]byte(val.k), []byte(val.v))
237237
}
238238
rootB, nodesB := trieb.Commit(false)
239239
dbb.Update(NewWithNodeSet(nodesB))
@@ -266,7 +266,7 @@ func TestUnionIterator(t *testing.T) {
266266
dba := NewDatabase(rawdb.NewMemoryDatabase())
267267
triea := NewEmpty(dba)
268268
for _, val := range testdata1 {
269-
triea.Update([]byte(val.k), []byte(val.v))
269+
triea.MustUpdate([]byte(val.k), []byte(val.v))
270270
}
271271
rootA, nodesA := triea.Commit(false)
272272
dba.Update(NewWithNodeSet(nodesA))
@@ -275,7 +275,7 @@ func TestUnionIterator(t *testing.T) {
275275
dbb := NewDatabase(rawdb.NewMemoryDatabase())
276276
trieb := NewEmpty(dbb)
277277
for _, val := range testdata2 {
278-
trieb.Update([]byte(val.k), []byte(val.v))
278+
trieb.MustUpdate([]byte(val.k), []byte(val.v))
279279
}
280280
rootB, nodesB := trieb.Commit(false)
281281
dbb.Update(NewWithNodeSet(nodesB))
@@ -318,7 +318,7 @@ func TestUnionIterator(t *testing.T) {
318318
func TestIteratorNoDups(t *testing.T) {
319319
tr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
320320
for _, val := range testdata1 {
321-
tr.Update([]byte(val.k), []byte(val.v))
321+
tr.MustUpdate([]byte(val.k), []byte(val.v))
322322
}
323323
checkIteratorNoDups(t, tr.NodeIterator(nil), nil)
324324
}
@@ -333,7 +333,7 @@ func testIteratorContinueAfterError(t *testing.T, memonly bool) {
333333

334334
tr := NewEmpty(triedb)
335335
for _, val := range testdata1 {
336-
tr.Update([]byte(val.k), []byte(val.v))
336+
tr.MustUpdate([]byte(val.k), []byte(val.v))
337337
}
338338
_, nodes := tr.Commit(false)
339339
triedb.Update(NewWithNodeSet(nodes))
@@ -425,7 +425,7 @@ func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
425425

426426
ctr := NewEmpty(triedb)
427427
for _, val := range testdata1 {
428-
ctr.Update([]byte(val.k), []byte(val.v))
428+
ctr.MustUpdate([]byte(val.k), []byte(val.v))
429429
}
430430
root, nodes := ctr.Commit(false)
431431
triedb.Update(NewWithNodeSet(nodes))
@@ -540,7 +540,7 @@ func makeLargeTestTrie() (*Database, *StateTrie, *loggingDb) {
540540
binary.BigEndian.PutUint64(val, uint64(i))
541541
key = crypto.Keccak256(key)
542542
val = crypto.Keccak256(val)
543-
trie.Update(key, val)
543+
trie.MustUpdate(key, val)
544544
}
545545
_, nodes := trie.Commit(false)
546546
triedb.Update(NewWithNodeSet(nodes))
@@ -580,7 +580,7 @@ func TestIteratorNodeBlob(t *testing.T) {
580580
all := make(map[string]string)
581581
for _, val := range vals {
582582
all[val.k] = val.v
583-
trie.Update([]byte(val.k), []byte(val.v))
583+
trie.MustUpdate([]byte(val.k), []byte(val.v))
584584
}
585585
_, nodes := trie.Commit(false)
586586
triedb.Update(NewWithNodeSet(nodes))

0 commit comments

Comments
 (0)