Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 43ee906

Browse files
jsvisaacud
authored andcommitted
trie: rename TrieSync to Sync and improve hexToKeybytes (#16804)
This removes a golint warning: type name will be used as trie.TrieSync by other packages, and that stutters; consider calling this Sync. In hexToKeybytes len(hex) is even and (even+1)/2 == even/2, remove the +1.
1 parent c6a64ec commit 43ee906

File tree

5 files changed

+37
-37
lines changed

5 files changed

+37
-37
lines changed

core/state/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
)
2626

2727
// NewStateSync create a new state trie download scheduler.
28-
func NewStateSync(root common.Hash, database trie.DatabaseReader) *trie.TrieSync {
29-
var syncer *trie.TrieSync
28+
func NewStateSync(root common.Hash, database trie.DatabaseReader) *trie.Sync {
29+
var syncer *trie.Sync
3030
callback := func(leaf []byte, parent common.Hash) error {
3131
var obj Account
3232
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
@@ -36,6 +36,6 @@ func NewStateSync(root common.Hash, database trie.DatabaseReader) *trie.TrieSync
3636
syncer.AddRawEntry(common.BytesToHash(obj.CodeHash), 64, parent)
3737
return nil
3838
}
39-
syncer = trie.NewTrieSync(root, database, callback)
39+
syncer = trie.NewSync(root, database, callback)
4040
return syncer
4141
}

eth/downloader/statesync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (d *Downloader) runStateSync(s *stateSync) *stateSync {
214214
type stateSync struct {
215215
d *Downloader // Downloader instance to access and manage current peerset
216216

217-
sched *trie.TrieSync // State trie sync scheduler defining the tasks
217+
sched *trie.Sync // State trie sync scheduler defining the tasks
218218
keccak hash.Hash // Keccak256 hasher to verify deliveries with
219219
tasks map[common.Hash]*stateTask // Set of tasks currently queued for retrieval
220220

trie/encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func hexToKeybytes(hex []byte) []byte {
8383
if len(hex)&1 != 0 {
8484
panic("can't convert hex key of odd length")
8585
}
86-
key := make([]byte, (len(hex)+1)/2)
86+
key := make([]byte, len(hex)/2)
8787
decodeNibbles(hex, key)
8888
return key
8989
}

trie/sync.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ func newSyncMemBatch() *syncMemBatch {
6868
}
6969
}
7070

71-
// TrieSync is the main state trie synchronisation scheduler, which provides yet
71+
// Sync is the main state trie synchronisation scheduler, which provides yet
7272
// unknown trie hashes to retrieve, accepts node data associated with said hashes
7373
// and reconstructs the trie step by step until all is done.
74-
type TrieSync struct {
74+
type Sync struct {
7575
database DatabaseReader // Persistent database to check for existing entries
7676
membatch *syncMemBatch // Memory buffer to avoid frequest database writes
7777
requests map[common.Hash]*request // Pending requests pertaining to a key hash
7878
queue *prque.Prque // Priority queue with the pending requests
7979
}
8080

81-
// NewTrieSync creates a new trie data download scheduler.
82-
func NewTrieSync(root common.Hash, database DatabaseReader, callback LeafCallback) *TrieSync {
83-
ts := &TrieSync{
81+
// NewSync creates a new trie data download scheduler.
82+
func NewSync(root common.Hash, database DatabaseReader, callback LeafCallback) *Sync {
83+
ts := &Sync{
8484
database: database,
8585
membatch: newSyncMemBatch(),
8686
requests: make(map[common.Hash]*request),
@@ -91,7 +91,7 @@ func NewTrieSync(root common.Hash, database DatabaseReader, callback LeafCallbac
9191
}
9292

9393
// AddSubTrie registers a new trie to the sync code, rooted at the designated parent.
94-
func (s *TrieSync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callback LeafCallback) {
94+
func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callback LeafCallback) {
9595
// Short circuit if the trie is empty or already known
9696
if root == emptyRoot {
9797
return
@@ -126,7 +126,7 @@ func (s *TrieSync) AddSubTrie(root common.Hash, depth int, parent common.Hash, c
126126
// interpreted as a trie node, but rather accepted and stored into the database
127127
// as is. This method's goal is to support misc state metadata retrievals (e.g.
128128
// contract code).
129-
func (s *TrieSync) AddRawEntry(hash common.Hash, depth int, parent common.Hash) {
129+
func (s *Sync) AddRawEntry(hash common.Hash, depth int, parent common.Hash) {
130130
// Short circuit if the entry is empty or already known
131131
if hash == emptyState {
132132
return
@@ -156,7 +156,7 @@ func (s *TrieSync) AddRawEntry(hash common.Hash, depth int, parent common.Hash)
156156
}
157157

158158
// Missing retrieves the known missing nodes from the trie for retrieval.
159-
func (s *TrieSync) Missing(max int) []common.Hash {
159+
func (s *Sync) Missing(max int) []common.Hash {
160160
requests := []common.Hash{}
161161
for !s.queue.Empty() && (max == 0 || len(requests) < max) {
162162
requests = append(requests, s.queue.PopItem().(common.Hash))
@@ -167,7 +167,7 @@ func (s *TrieSync) Missing(max int) []common.Hash {
167167
// Process injects a batch of retrieved trie nodes data, returning if something
168168
// was committed to the database and also the index of an entry if processing of
169169
// it failed.
170-
func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
170+
func (s *Sync) Process(results []SyncResult) (bool, int, error) {
171171
committed := false
172172

173173
for i, item := range results {
@@ -213,7 +213,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
213213

214214
// Commit flushes the data stored in the internal membatch out to persistent
215215
// storage, returning the number of items written and any occurred error.
216-
func (s *TrieSync) Commit(dbw ethdb.Putter) (int, error) {
216+
func (s *Sync) Commit(dbw ethdb.Putter) (int, error) {
217217
// Dump the membatch into a database dbw
218218
for i, key := range s.membatch.order {
219219
if err := dbw.Put(key[:], s.membatch.batch[key]); err != nil {
@@ -228,14 +228,14 @@ func (s *TrieSync) Commit(dbw ethdb.Putter) (int, error) {
228228
}
229229

230230
// Pending returns the number of state entries currently pending for download.
231-
func (s *TrieSync) Pending() int {
231+
func (s *Sync) Pending() int {
232232
return len(s.requests)
233233
}
234234

235235
// schedule inserts a new state retrieval request into the fetch queue. If there
236236
// is already a pending request for this node, the new request will be discarded
237237
// and only a parent reference added to the old one.
238-
func (s *TrieSync) schedule(req *request) {
238+
func (s *Sync) schedule(req *request) {
239239
// If we're already requesting this node, add a new reference and stop
240240
if old, ok := s.requests[req.hash]; ok {
241241
old.parents = append(old.parents, req.parents...)
@@ -248,7 +248,7 @@ func (s *TrieSync) schedule(req *request) {
248248

249249
// children retrieves all the missing children of a state trie entry for future
250250
// retrieval scheduling.
251-
func (s *TrieSync) children(req *request, object node) ([]*request, error) {
251+
func (s *Sync) children(req *request, object node) ([]*request, error) {
252252
// Gather all the children of the node, irrelevant whether known or not
253253
type child struct {
254254
node node
@@ -310,7 +310,7 @@ func (s *TrieSync) children(req *request, object node) ([]*request, error) {
310310
// commit finalizes a retrieval request and stores it into the membatch. If any
311311
// of the referencing parent requests complete due to this commit, they are also
312312
// committed themselves.
313-
func (s *TrieSync) commit(req *request) (err error) {
313+
func (s *Sync) commit(req *request) (err error) {
314314
// Write the node content to the membatch
315315
s.membatch.batch[req.hash] = req.data
316316
s.membatch.order = append(s.membatch.order, req.hash)

trie/sync_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,32 @@ func checkTrieConsistency(db *Database, root common.Hash) error {
8787
}
8888

8989
// Tests that an empty trie is not scheduled for syncing.
90-
func TestEmptyTrieSync(t *testing.T) {
90+
func TestEmptySync(t *testing.T) {
9191
dbA := NewDatabase(ethdb.NewMemDatabase())
9292
dbB := NewDatabase(ethdb.NewMemDatabase())
9393
emptyA, _ := New(common.Hash{}, dbA)
9494
emptyB, _ := New(emptyRoot, dbB)
9595

9696
for i, trie := range []*Trie{emptyA, emptyB} {
97-
if req := NewTrieSync(trie.Hash(), ethdb.NewMemDatabase(), nil).Missing(1); len(req) != 0 {
97+
if req := NewSync(trie.Hash(), ethdb.NewMemDatabase(), nil).Missing(1); len(req) != 0 {
9898
t.Errorf("test %d: content requested for empty trie: %v", i, req)
9999
}
100100
}
101101
}
102102

103103
// Tests that given a root hash, a trie can sync iteratively on a single thread,
104104
// requesting retrieval tasks and returning all of them in one go.
105-
func TestIterativeTrieSyncIndividual(t *testing.T) { testIterativeTrieSync(t, 1) }
106-
func TestIterativeTrieSyncBatched(t *testing.T) { testIterativeTrieSync(t, 100) }
105+
func TestIterativeSyncIndividual(t *testing.T) { testIterativeSync(t, 1) }
106+
func TestIterativeSyncBatched(t *testing.T) { testIterativeSync(t, 100) }
107107

108-
func testIterativeTrieSync(t *testing.T, batch int) {
108+
func testIterativeSync(t *testing.T, batch int) {
109109
// Create a random trie to copy
110110
srcDb, srcTrie, srcData := makeTestTrie()
111111

112112
// Create a destination trie and sync with the scheduler
113113
diskdb := ethdb.NewMemDatabase()
114114
triedb := NewDatabase(diskdb)
115-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
115+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
116116

117117
queue := append([]common.Hash{}, sched.Missing(batch)...)
118118
for len(queue) > 0 {
@@ -138,14 +138,14 @@ func testIterativeTrieSync(t *testing.T, batch int) {
138138

139139
// Tests that the trie scheduler can correctly reconstruct the state even if only
140140
// partial results are returned, and the others sent only later.
141-
func TestIterativeDelayedTrieSync(t *testing.T) {
141+
func TestIterativeDelayedSync(t *testing.T) {
142142
// Create a random trie to copy
143143
srcDb, srcTrie, srcData := makeTestTrie()
144144

145145
// Create a destination trie and sync with the scheduler
146146
diskdb := ethdb.NewMemDatabase()
147147
triedb := NewDatabase(diskdb)
148-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
148+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
149149

150150
queue := append([]common.Hash{}, sched.Missing(10000)...)
151151
for len(queue) > 0 {
@@ -173,17 +173,17 @@ func TestIterativeDelayedTrieSync(t *testing.T) {
173173
// Tests that given a root hash, a trie can sync iteratively on a single thread,
174174
// requesting retrieval tasks and returning all of them in one go, however in a
175175
// random order.
176-
func TestIterativeRandomTrieSyncIndividual(t *testing.T) { testIterativeRandomTrieSync(t, 1) }
177-
func TestIterativeRandomTrieSyncBatched(t *testing.T) { testIterativeRandomTrieSync(t, 100) }
176+
func TestIterativeRandomSyncIndividual(t *testing.T) { testIterativeRandomSync(t, 1) }
177+
func TestIterativeRandomSyncBatched(t *testing.T) { testIterativeRandomSync(t, 100) }
178178

179-
func testIterativeRandomTrieSync(t *testing.T, batch int) {
179+
func testIterativeRandomSync(t *testing.T, batch int) {
180180
// Create a random trie to copy
181181
srcDb, srcTrie, srcData := makeTestTrie()
182182

183183
// Create a destination trie and sync with the scheduler
184184
diskdb := ethdb.NewMemDatabase()
185185
triedb := NewDatabase(diskdb)
186-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
186+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
187187

188188
queue := make(map[common.Hash]struct{})
189189
for _, hash := range sched.Missing(batch) {
@@ -217,14 +217,14 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) {
217217

218218
// Tests that the trie scheduler can correctly reconstruct the state even if only
219219
// partial results are returned (Even those randomly), others sent only later.
220-
func TestIterativeRandomDelayedTrieSync(t *testing.T) {
220+
func TestIterativeRandomDelayedSync(t *testing.T) {
221221
// Create a random trie to copy
222222
srcDb, srcTrie, srcData := makeTestTrie()
223223

224224
// Create a destination trie and sync with the scheduler
225225
diskdb := ethdb.NewMemDatabase()
226226
triedb := NewDatabase(diskdb)
227-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
227+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
228228

229229
queue := make(map[common.Hash]struct{})
230230
for _, hash := range sched.Missing(10000) {
@@ -264,14 +264,14 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) {
264264

265265
// Tests that a trie sync will not request nodes multiple times, even if they
266266
// have such references.
267-
func TestDuplicateAvoidanceTrieSync(t *testing.T) {
267+
func TestDuplicateAvoidanceSync(t *testing.T) {
268268
// Create a random trie to copy
269269
srcDb, srcTrie, srcData := makeTestTrie()
270270

271271
// Create a destination trie and sync with the scheduler
272272
diskdb := ethdb.NewMemDatabase()
273273
triedb := NewDatabase(diskdb)
274-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
274+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
275275

276276
queue := append([]common.Hash{}, sched.Missing(0)...)
277277
requested := make(map[common.Hash]struct{})
@@ -304,14 +304,14 @@ func TestDuplicateAvoidanceTrieSync(t *testing.T) {
304304

305305
// Tests that at any point in time during a sync, only complete sub-tries are in
306306
// the database.
307-
func TestIncompleteTrieSync(t *testing.T) {
307+
func TestIncompleteSync(t *testing.T) {
308308
// Create a random trie to copy
309309
srcDb, srcTrie, _ := makeTestTrie()
310310

311311
// Create a destination trie and sync with the scheduler
312312
diskdb := ethdb.NewMemDatabase()
313313
triedb := NewDatabase(diskdb)
314-
sched := NewTrieSync(srcTrie.Hash(), diskdb, nil)
314+
sched := NewSync(srcTrie.Hash(), diskdb, nil)
315315

316316
added := []common.Hash{}
317317
queue := append([]common.Hash{}, sched.Missing(1)...)

0 commit comments

Comments
 (0)