Skip to content

Commit 7684d88

Browse files
gzliudangballetrjl493456442
authored
core/state, trie: remove Try prefix in Trie accessors ethereum#26975 (#1146)
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: Guillaume Ballet <[email protected]> Co-authored-by: Gary Rong <[email protected]>
1 parent b3d354a commit 7684d88

File tree

5 files changed

+42
-43
lines changed

5 files changed

+42
-43
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
@@ -199,7 +199,7 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
199199
s.setError(err)
200200
return common.Hash{}
201201
}
202-
enc, err := tr.TryGetStorage(s.address, key.Bytes())
202+
enc, err := tr.GetStorage(s.address, key.Bytes())
203203
s.db.StorageReads += time.Since(start)
204204
if err != nil {
205205
s.setError(err)
@@ -275,15 +275,15 @@ func (s *stateObject) updateTrie(db Database) (Trie, error) {
275275
s.originStorage[key] = value
276276

277277
if (value == common.Hash{}) {
278-
if err := tr.TryDeleteStorage(s.address, key[:]); err != nil {
278+
if err := tr.DeleteStorage(s.address, key[:]); err != nil {
279279
s.setError(err)
280280
return nil, err
281281
}
282282
s.db.StorageDeleted += 1
283283
} else {
284284
// Encoding []byte cannot fail, ok to ignore the error.
285285
v, _ := rlp.EncodeToBytes(common.TrimLeftZeroes(value[:]))
286-
if err := tr.TryUpdateStorage(s.address, key[:], v); err != nil {
286+
if err := tr.UpdateStorage(s.address, key[:], v); err != nil {
287287
s.setError(err)
288288
return nil, err
289289
}

core/state/statedb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
539539

540540
// Encode the account and update the account trie
541541
addr := obj.Address()
542-
if err := s.trie.TryUpdateAccount(addr, &obj.data); err != nil {
542+
if err := s.trie.UpdateAccount(addr, &obj.data); err != nil {
543543
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
544544
}
545545
}
@@ -551,7 +551,7 @@ func (s *StateDB) deleteStateObject(obj *stateObject) {
551551

552552
// Delete the account from the trie
553553
addr := obj.Address()
554-
if err := s.trie.TryDeleteAccount(addr); err != nil {
554+
if err := s.trie.DeleteAccount(addr); err != nil {
555555
s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
556556
}
557557
}
@@ -586,7 +586,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
586586
}
587587
// Load the object from the database
588588
start := time.Now()
589-
data, err := s.trie.TryGetAccount(addr)
589+
data, err := s.trie.GetAccount(addr)
590590
s.AccountReads += time.Since(start)
591591
if err != nil {
592592
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %w", addr.Bytes(), err))

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)