Skip to content

Commit c7e1ac0

Browse files
gballetrjl493456442
authored andcommitted
core/state, trie: remove Try prefix in Trie accessors (ethereum#26975)
This change renames StateTrie methods to remove the Try* prefix. We added the Trie methods with prefix 'Try' a long time ago, working around the problem that most existing methods of Trie did not return the database error. This weird naming convention has persisted until now. Co-authored-by: Gary Rong <garyrong0905@gmail.com>
1 parent 75d7493 commit c7e1ac0

File tree

8 files changed

+54
-55
lines changed

8 files changed

+54
-55
lines changed

core/state/database.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,36 @@ type Trie interface {
6868
// TODO(fjl): remove this when StateTrie is removed
6969
GetKey([]byte) []byte
7070

71-
// TryGetStorage returns the value for key stored in the trie. The value bytes
71+
// GetStorage returns the value for key stored in the trie. The value bytes
7272
// must not be modified by the caller. If a node was not found in the database,
7373
// a trie.MissingNodeError is returned.
74-
TryGetStorage(addr common.Address, key []byte) ([]byte, error)
74+
GetStorage(addr common.Address, key []byte) ([]byte, error)
7575

76-
// TryGetAccount abstracts an account read from the trie. It retrieves the
76+
// GetAccount abstracts an account read from the trie. It retrieves the
7777
// account blob from the trie with provided account address and decodes it
7878
// with associated decoding algorithm. If the specified account is not in
7979
// the trie, nil will be returned. If the trie is corrupted(e.g. some nodes
8080
// are missing or the account blob is incorrect for decoding), an error will
8181
// be returned.
82-
TryGetAccount(address common.Address) (*types.StateAccount, error)
82+
GetAccount(address common.Address) (*types.StateAccount, error)
8383

84-
// TryUpdateStorage associates key with value in the trie. If value has length zero,
84+
// UpdateStorage associates key with value in the trie. If value has length zero,
8585
// any existing value is deleted from the trie. The value bytes must not be modified
8686
// by the caller while they are stored in the trie. If a node was not found in the
8787
// database, a trie.MissingNodeError is returned.
88-
TryUpdateStorage(addr common.Address, key, value []byte) error
88+
UpdateStorage(addr common.Address, key, value []byte) error
8989

90-
// TryUpdateAccount abstracts an account write to the trie. It encodes the
90+
// UpdateAccount abstracts an account write to the trie. It encodes the
9191
// provided account object with associated algorithm and then updates it
9292
// in the trie with provided address.
93-
TryUpdateAccount(address common.Address, account *types.StateAccount) error
93+
UpdateAccount(address common.Address, account *types.StateAccount) error
9494

95-
// TryDeleteStorage removes any existing value for key from the trie. If a node
95+
// DeleteStorage removes any existing value for key from the trie. If a node
9696
// was not found in the database, a trie.MissingNodeError is returned.
97-
TryDeleteStorage(addr common.Address, key []byte) error
97+
DeleteStorage(addr common.Address, key []byte) error
9898

99-
// TryDeleteAccount abstracts an account deletion from the trie.
100-
TryDeleteAccount(address common.Address) error
99+
// DeleteAccount abstracts an account deletion from the trie.
100+
DeleteAccount(address common.Address) error
101101

102102
// Hash returns the root hash of the trie. It does not write to the database and
103103
// can be used even if the trie doesn't have one.

core/state/state_object.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
201201
s.db.setError(err)
202202
return common.Hash{}
203203
}
204-
enc, err = tr.TryGetStorage(s.address, key.Bytes())
204+
enc, err = tr.GetStorage(s.address, key.Bytes())
205205
if metrics.EnabledExpensive {
206206
s.db.StorageReads += time.Since(start)
207207
}
@@ -294,15 +294,15 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
294294

295295
var v []byte
296296
if (value == common.Hash{}) {
297-
if err := tr.TryDeleteStorage(s.address, key[:]); err != nil {
297+
if err := tr.DeleteStorage(s.address, key[:]); err != nil {
298298
s.db.setError(err)
299299
return nil, err
300300
}
301301
s.db.StorageDeleted += 1
302302
} else {
303303
// Encoding []byte cannot fail, ok to ignore the error.
304304
v, _ = rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
305-
if err := tr.TryUpdateStorage(s.address, key[:], v); err != nil {
305+
if err := tr.UpdateStorage(s.address, key[:], v); err != nil {
306306
s.db.setError(err)
307307
return nil, err
308308
}

core/state/statedb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
512512
}
513513
// Encode the account and update the account trie
514514
addr := obj.Address()
515-
if err := s.trie.TryUpdateAccount(addr, &obj.data); err != nil {
515+
if err := s.trie.UpdateAccount(addr, &obj.data); err != nil {
516516
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
517517
}
518518

@@ -533,7 +533,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
533533
}
534534
// Delete the account from the trie
535535
addr := obj.Address()
536-
if err := s.trie.TryDeleteAccount(addr); err != nil {
536+
if err := s.trie.DeleteAccount(addr); err != nil {
537537
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
538538
}
539539
}
@@ -587,7 +587,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
587587
if data == nil {
588588
start := time.Now()
589589
var err error
590-
data, err = s.trie.TryGetAccount(addr)
590+
data, err = s.trie.GetAccount(addr)
591591
if metrics.EnabledExpensive {
592592
s.AccountReads += time.Since(start)
593593
}

core/state/trie_prefetcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ func (sf *subfetcher) loop() {
339339
sf.dups++
340340
} else {
341341
if len(task) == common.AddressLength {
342-
sf.trie.TryGetAccount(common.BytesToAddress(task))
342+
sf.trie.GetAccount(common.BytesToAddress(task))
343343
} else {
344-
sf.trie.TryGetStorage(sf.addr, task)
344+
sf.trie.GetStorage(sf.addr, task)
345345
}
346346
sf.seen[string(task)] = struct{}{}
347347
}

eth/protocols/snap/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
418418
if err != nil {
419419
return nil, nil
420420
}
421-
acc, err := accTrie.TryGetAccountByHash(account)
421+
acc, err := accTrie.GetAccountByHash(account)
422422
if err != nil || acc == nil {
423423
return nil, nil
424424
}
@@ -510,7 +510,7 @@ func ServiceGetTrieNodesQuery(chain *core.BlockChain, req *GetTrieNodesPacket, s
510510

511511
case 1:
512512
// If we're only retrieving an account trie node, fetch it directly
513-
blob, resolved, err := accTrie.TryGetNode(pathset[0])
513+
blob, resolved, err := accTrie.GetNode(pathset[0])
514514
loads += resolved // always account database reads, even for failures
515515
if err != nil {
516516
break
@@ -524,7 +524,7 @@ func ServiceGetTrieNodesQuery(chain *core.BlockChain, req *GetTrieNodesPacket, s
524524
if snap == nil {
525525
// We don't have the requested state snapshotted yet (or it is stale),
526526
// but can look up the account via the trie instead.
527-
account, err := accTrie.TryGetAccountByHash(common.BytesToHash(pathset[0]))
527+
account, err := accTrie.GetAccountByHash(common.BytesToHash(pathset[0]))
528528
loads += 8 // We don't know the exact cost of lookup, this is an estimate
529529
if err != nil || account == nil {
530530
break
@@ -545,7 +545,7 @@ func ServiceGetTrieNodesQuery(chain *core.BlockChain, req *GetTrieNodesPacket, s
545545
break
546546
}
547547
for _, path := range pathset[1:] {
548-
blob, resolved, err := stTrie.TryGetNode(path)
548+
blob, resolved, err := stTrie.GetNode(path)
549549
loads += resolved // always account database reads, even for failures
550550
if err != nil {
551551
break

light/trie.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ type odrTrie struct {
105105
trie *trie.Trie
106106
}
107107

108-
func (t *odrTrie) TryGetStorage(_ common.Address, key []byte) ([]byte, error) {
108+
func (t *odrTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
109109
key = crypto.Keccak256(key)
110110
var res []byte
111111
err := t.do(key, func() (err error) {
@@ -115,7 +115,7 @@ func (t *odrTrie) TryGetStorage(_ common.Address, key []byte) ([]byte, error) {
115115
return res, err
116116
}
117117

118-
func (t *odrTrie) TryGetAccount(address common.Address) (*types.StateAccount, error) {
118+
func (t *odrTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
119119
var res types.StateAccount
120120
key := crypto.Keccak256(address.Bytes())
121121
err := t.do(key, func() (err error) {
@@ -131,7 +131,7 @@ func (t *odrTrie) TryGetAccount(address common.Address) (*types.StateAccount, er
131131
return &res, err
132132
}
133133

134-
func (t *odrTrie) TryUpdateAccount(address common.Address, acc *types.StateAccount) error {
134+
func (t *odrTrie) UpdateAccount(address common.Address, acc *types.StateAccount) error {
135135
key := crypto.Keccak256(address.Bytes())
136136
value, err := rlp.EncodeToBytes(acc)
137137
if err != nil {
@@ -142,22 +142,22 @@ func (t *odrTrie) TryUpdateAccount(address common.Address, acc *types.StateAccou
142142
})
143143
}
144144

145-
func (t *odrTrie) TryUpdateStorage(_ common.Address, key, value []byte) error {
145+
func (t *odrTrie) UpdateStorage(_ common.Address, key, value []byte) error {
146146
key = crypto.Keccak256(key)
147147
return t.do(key, func() error {
148148
return t.trie.TryUpdate(key, value)
149149
})
150150
}
151151

152-
func (t *odrTrie) TryDeleteStorage(_ common.Address, key []byte) error {
152+
func (t *odrTrie) DeleteStorage(_ common.Address, key []byte) error {
153153
key = crypto.Keccak256(key)
154154
return t.do(key, func() error {
155155
return t.trie.TryDelete(key)
156156
})
157157
}
158158

159159
// TryDeleteAccount abstracts an account deletion from the trie.
160-
func (t *odrTrie) TryDeleteAccount(address common.Address) error {
160+
func (t *odrTrie) DeleteAccount(address common.Address) error {
161161
key := crypto.Keccak256(address.Bytes())
162162
return t.do(key, func() error {
163163
return t.trie.TryDelete(key)

trie/secure_trie.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,25 @@ func NewStateTrie(id *ID, db *Database) (*StateTrie, error) {
7575
// Get returns the value for key stored in the trie.
7676
// The value bytes must not be modified by the caller.
7777
func (t *StateTrie) Get(key []byte) []byte {
78-
res, err := t.TryGetStorage(common.Address{}, key)
78+
res, err := t.GetStorage(common.Address{}, key)
7979
if err != nil {
8080
log.Error("Unhandled trie error in StateTrie.Get", "err", err)
8181
}
8282
return res
8383
}
8484

85-
// TryGet returns the value for key stored in the trie.
86-
// The value bytes must not be modified by the caller.
87-
// If the specified node is not in the trie, nil will be returned.
85+
// GetStorage attempts to retrieve a storage slot with provided account address
86+
// and slot key. The value bytes must not be modified by the caller.
87+
// If the specified storage slot is not in the trie, nil will be returned.
8888
// If a trie node is not found in the database, a MissingNodeError is returned.
89-
func (t *StateTrie) TryGetStorage(_ common.Address, key []byte) ([]byte, error) {
89+
func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
9090
return t.trie.TryGet(t.hashKey(key))
9191
}
9292

93-
// TryGetAccount attempts to retrieve an account with provided account address.
93+
// GetAccount attempts to retrieve an account with provided account address.
9494
// If the specified account is not in the trie, nil will be returned.
9595
// If a trie node is not found in the database, a MissingNodeError is returned.
96-
func (t *StateTrie) TryGetAccount(address common.Address) (*types.StateAccount, error) {
96+
func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
9797
res, err := t.trie.TryGet(t.hashKey(address.Bytes()))
9898
if res == nil || err != nil {
9999
return nil, err
@@ -103,10 +103,10 @@ func (t *StateTrie) TryGetAccount(address common.Address) (*types.StateAccount,
103103
return ret, err
104104
}
105105

106-
// TryGetAccountByHash does the same thing as TryGetAccount, however
107-
// it expects an account hash that is the hash of address. This constitutes an
108-
// abstraction leak, since the client code needs to know the key format.
109-
func (t *StateTrie) TryGetAccountByHash(addrHash common.Hash) (*types.StateAccount, error) {
106+
// GetAccountByHash does the same thing as GetAccount, however it expects an
107+
// account hash that is the hash of address. This constitutes an abstraction
108+
// leak, since the client code needs to know the key format.
109+
func (t *StateTrie) GetAccountByHash(addrHash common.Hash) (*types.StateAccount, error) {
110110
res, err := t.trie.TryGet(addrHash.Bytes())
111111
if res == nil || err != nil {
112112
return nil, err
@@ -116,11 +116,11 @@ func (t *StateTrie) TryGetAccountByHash(addrHash common.Hash) (*types.StateAccou
116116
return ret, err
117117
}
118118

119-
// TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
119+
// GetNode attempts to retrieve a trie node by compact-encoded path. It is not
120120
// possible to use keybyte-encoding as the path might contain odd nibbles.
121121
// If the specified trie node is not in the trie, nil will be returned.
122122
// If a trie node is not found in the database, a MissingNodeError is returned.
123-
func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error) {
123+
func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
124124
return t.trie.TryGetNode(path)
125125
}
126126

@@ -131,20 +131,20 @@ func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error) {
131131
// The value bytes must not be modified by the caller while they are
132132
// stored in the trie.
133133
func (t *StateTrie) Update(key, value []byte) {
134-
if err := t.TryUpdateStorage(common.Address{}, key, value); err != nil {
134+
if err := t.UpdateStorage(common.Address{}, key, value); err != nil {
135135
log.Error("Unhandled trie error in StateTrie.Update", "err", err)
136136
}
137137
}
138138

139-
// TryUpdate associates key with value in the trie. Subsequent calls to
139+
// UpdateStorage associates key with value in the trie. Subsequent calls to
140140
// Get will return value. If value has length zero, any existing value
141141
// is deleted from the trie and calls to Get will return nil.
142142
//
143143
// The value bytes must not be modified by the caller while they are
144144
// stored in the trie.
145145
//
146146
// If a node is not found in the database, a MissingNodeError is returned.
147-
func (t *StateTrie) TryUpdateStorage(_ common.Address, key, value []byte) error {
147+
func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
148148
hk := t.hashKey(key)
149149
err := t.trie.TryUpdate(hk, value)
150150
if err != nil {
@@ -154,9 +154,8 @@ func (t *StateTrie) TryUpdateStorage(_ common.Address, key, value []byte) error
154154
return nil
155155
}
156156

157-
// TryUpdateAccount account will abstract the write of an account to the
158-
// secure trie.
159-
func (t *StateTrie) TryUpdateAccount(address common.Address, acc *types.StateAccount) error {
157+
// UpdateAccount will abstract the write of an account to the secure trie.
158+
func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccount) error {
160159
hk := t.hashKey(address.Bytes())
161160
data, err := rlp.EncodeToBytes(acc)
162161
if err != nil {
@@ -171,22 +170,22 @@ func (t *StateTrie) TryUpdateAccount(address common.Address, acc *types.StateAcc
171170

172171
// Delete removes any existing value for key from the trie.
173172
func (t *StateTrie) Delete(key []byte) {
174-
if err := t.TryDeleteStorage(common.Address{}, key); err != nil {
173+
if err := t.DeleteStorage(common.Address{}, key); err != nil {
175174
log.Error("Unhandled trie error in StateTrie.Delete", "err", err)
176175
}
177176
}
178177

179-
// TryDelete removes any existing value for key from the trie.
178+
// DeleteStorage removes any existing storage slot from the trie.
180179
// If the specified trie node is not in the trie, nothing will be changed.
181180
// If a node is not found in the database, a MissingNodeError is returned.
182-
func (t *StateTrie) TryDeleteStorage(_ common.Address, key []byte) error {
181+
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
183182
hk := t.hashKey(key)
184183
delete(t.getSecKeyCache(), string(hk))
185184
return t.trie.TryDelete(hk)
186185
}
187186

188-
// TryDeleteAccount abstracts an account deletion from the trie.
189-
func (t *StateTrie) TryDeleteAccount(address common.Address) error {
187+
// DeleteAccount abstracts an account deletion from the trie.
188+
func (t *StateTrie) DeleteAccount(address common.Address) error {
190189
hk := t.hashKey(address.Bytes())
191190
delete(t.getSecKeyCache(), string(hk))
192191
return t.trie.TryDelete(hk)

trie/sync_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func testIterativeSync(t *testing.T, count int, bypath bool) {
154154
}
155155
} else {
156156
for i, element := range elements {
157-
data, _, err := srcTrie.TryGetNode(element.syncPath[len(element.syncPath)-1])
157+
data, _, err := srcTrie.GetNode(element.syncPath[len(element.syncPath)-1])
158158
if err != nil {
159159
t.Fatalf("failed to retrieve node data for path %x: %v", element.path, err)
160160
}

0 commit comments

Comments
 (0)