Skip to content

Commit d3b751e

Browse files
committed
trie: remove dependency on ethdb
This removes the core/types -> leveldb dependency.
1 parent 7731061 commit d3b751e

6 files changed

Lines changed: 37 additions & 37 deletions

File tree

‎core/state/sync.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"math/big"
2222

2323
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/ethdb"
2524
"github.com/ethereum/go-ethereum/rlp"
2625
"github.com/ethereum/go-ethereum/trie"
2726
)
@@ -32,7 +31,7 @@ import (
3231
type StateSync trie.TrieSync
3332

3433
// NewStateSync create a new state trie download scheduler.
35-
func NewStateSync(root common.Hash, database ethdb.Database) *StateSync {
34+
func NewStateSync(root common.Hash, database trie.DatabaseReader) *StateSync {
3635
var syncer *trie.TrieSync
3736

3837
callback := func(leaf []byte, parent common.Hash) error {
@@ -62,8 +61,8 @@ func (s *StateSync) Missing(max int) []common.Hash {
6261
// Process injects a batch of retrieved trie nodes data, returning if something
6362
// was committed to the database and also the index of an entry if processing of
6463
// it failed.
65-
func (s *StateSync) Process(list []trie.SyncResult) (bool, int, error) {
66-
return (*trie.TrieSync)(s).Process(list)
64+
func (s *StateSync) Process(list []trie.SyncResult, dbw trie.DatabaseWriter) (bool, int, error) {
65+
return (*trie.TrieSync)(s).Process(list, dbw)
6766
}
6867

6968
// Pending returns the number of state entries currently pending for download.

‎core/state/sync_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func testIterativeStateSync(t *testing.T, batch int) {
138138
}
139139
results[i] = trie.SyncResult{Hash: hash, Data: data}
140140
}
141-
if _, index, err := sched.Process(results); err != nil {
141+
if _, index, err := sched.Process(results, dstDb); err != nil {
142142
t.Fatalf("failed to process result #%d: %v", index, err)
143143
}
144144
queue = append(queue[:0], sched.Missing(batch)...)
@@ -168,7 +168,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
168168
}
169169
results[i] = trie.SyncResult{Hash: hash, Data: data}
170170
}
171-
if _, index, err := sched.Process(results); err != nil {
171+
if _, index, err := sched.Process(results, dstDb); err != nil {
172172
t.Fatalf("failed to process result #%d: %v", index, err)
173173
}
174174
queue = append(queue[len(results):], sched.Missing(0)...)
@@ -206,7 +206,7 @@ func testIterativeRandomStateSync(t *testing.T, batch int) {
206206
results = append(results, trie.SyncResult{Hash: hash, Data: data})
207207
}
208208
// Feed the retrieved results back and queue new tasks
209-
if _, index, err := sched.Process(results); err != nil {
209+
if _, index, err := sched.Process(results, dstDb); err != nil {
210210
t.Fatalf("failed to process result #%d: %v", index, err)
211211
}
212212
queue = make(map[common.Hash]struct{})
@@ -249,7 +249,7 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
249249
}
250250
}
251251
// Feed the retrieved results back and queue new tasks
252-
if _, index, err := sched.Process(results); err != nil {
252+
if _, index, err := sched.Process(results, dstDb); err != nil {
253253
t.Fatalf("failed to process result #%d: %v", index, err)
254254
}
255255
for _, hash := range sched.Missing(0) {
@@ -283,7 +283,7 @@ func TestIncompleteStateSync(t *testing.T) {
283283
results[i] = trie.SyncResult{Hash: hash, Data: data}
284284
}
285285
// Process each of the state nodes
286-
if _, index, err := sched.Process(results); err != nil {
286+
if _, index, err := sched.Process(results, dstDb); err != nil {
287287
t.Fatalf("failed to process result #%d: %v", index, err)
288288
}
289289
for _, result := range results {

‎eth/downloader/queue.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,15 +1123,20 @@ func (q *queue) deliverNodeData(results []trie.SyncResult, callback func(int, bo
11231123
callback(i, progressed, errNoFetchesPending)
11241124
return
11251125
}
1126-
if prog, _, err := q.stateScheduler.Process([]trie.SyncResult{result}); err != nil {
1127-
// Processing a state result failed, bail out
1126+
1127+
batch := q.stateDatabase.NewBatch()
1128+
prog, _, err := q.stateScheduler.Process([]trie.SyncResult{result}, batch)
1129+
if err != nil {
1130+
q.stateSchedLock.Unlock()
1131+
callback(i, progressed, err)
1132+
}
1133+
if err = batch.Write(); err != nil {
11281134
q.stateSchedLock.Unlock()
11291135
callback(i, progressed, err)
1130-
return
1131-
} else if prog {
1132-
progressed = true
11331136
}
1137+
11341138
// Item processing succeeded, release the lock (temporarily)
1139+
progressed = progressed || prog
11351140
q.stateSchedLock.Unlock()
11361141
}
11371142
callback(len(results), progressed, nil)

‎trie/sync.go‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222

2323
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/ethdb"
2524
"gopkg.in/karalabe/cookiejar.v2/collections/prque"
2625
)
2726

@@ -58,13 +57,13 @@ type TrieSyncLeafCallback func(leaf []byte, parent common.Hash) error
5857
// unknown trie hashes to retrieve, accepts node data associated with said hashes
5958
// and reconstructs the trie step by step until all is done.
6059
type TrieSync struct {
61-
database ethdb.Database // State database for storing all the assembled node data
60+
database DatabaseReader
6261
requests map[common.Hash]*request // Pending requests pertaining to a key hash
6362
queue *prque.Prque // Priority queue with the pending requests
6463
}
6564

6665
// NewTrieSync creates a new trie data download scheduler.
67-
func NewTrieSync(root common.Hash, database ethdb.Database, callback TrieSyncLeafCallback) *TrieSync {
66+
func NewTrieSync(root common.Hash, database DatabaseReader, callback TrieSyncLeafCallback) *TrieSync {
6867
ts := &TrieSync{
6968
database: database,
7069
requests: make(map[common.Hash]*request),
@@ -145,7 +144,7 @@ func (s *TrieSync) Missing(max int) []common.Hash {
145144
// Process injects a batch of retrieved trie nodes data, returning if something
146145
// was committed to the database and also the index of an entry if processing of
147146
// it failed.
148-
func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
147+
func (s *TrieSync) Process(results []SyncResult, dbw DatabaseWriter) (bool, int, error) {
149148
committed := false
150149

151150
for i, item := range results {
@@ -157,7 +156,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
157156
// If the item is a raw entry request, commit directly
158157
if request.raw {
159158
request.data = item.Data
160-
s.commit(request, nil)
159+
s.commit(request, dbw)
161160
committed = true
162161
continue
163162
}
@@ -174,7 +173,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) {
174173
return committed, i, err
175174
}
176175
if len(requests) == 0 && request.deps == 0 {
177-
s.commit(request, nil)
176+
s.commit(request, dbw)
178177
committed = true
179178
continue
180179
}
@@ -266,16 +265,9 @@ func (s *TrieSync) children(req *request, object node) ([]*request, error) {
266265
// commit finalizes a retrieval request and stores it into the database. If any
267266
// of the referencing parent requests complete due to this commit, they are also
268267
// committed themselves.
269-
func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) {
270-
// Create a new batch if none was specified
271-
if batch == nil {
272-
batch = s.database.NewBatch()
273-
defer func() {
274-
err = batch.Write()
275-
}()
276-
}
268+
func (s *TrieSync) commit(req *request, dbw DatabaseWriter) (err error) {
277269
// Write the node content to disk
278-
if err := batch.Put(req.hash[:], req.data); err != nil {
270+
if err := dbw.Put(req.hash[:], req.data); err != nil {
279271
return err
280272
}
281273
delete(s.requests, req.hash)
@@ -284,7 +276,7 @@ func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) {
284276
for _, parent := range req.parents {
285277
parent.deps--
286278
if parent.deps == 0 {
287-
if err := s.commit(parent, batch); err != nil {
279+
if err := s.commit(parent, dbw); err != nil {
288280
return err
289281
}
290282
}

‎trie/sync_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func testIterativeTrieSync(t *testing.T, batch int) {
122122
}
123123
results[i] = SyncResult{hash, data}
124124
}
125-
if _, index, err := sched.Process(results); err != nil {
125+
if _, index, err := sched.Process(results, dstDb); err != nil {
126126
t.Fatalf("failed to process result #%d: %v", index, err)
127127
}
128128
queue = append(queue[:0], sched.Missing(batch)...)
@@ -152,7 +152,7 @@ func TestIterativeDelayedTrieSync(t *testing.T) {
152152
}
153153
results[i] = SyncResult{hash, data}
154154
}
155-
if _, index, err := sched.Process(results); err != nil {
155+
if _, index, err := sched.Process(results, dstDb); err != nil {
156156
t.Fatalf("failed to process result #%d: %v", index, err)
157157
}
158158
queue = append(queue[len(results):], sched.Missing(10000)...)
@@ -190,7 +190,7 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) {
190190
results = append(results, SyncResult{hash, data})
191191
}
192192
// Feed the retrieved results back and queue new tasks
193-
if _, index, err := sched.Process(results); err != nil {
193+
if _, index, err := sched.Process(results, dstDb); err != nil {
194194
t.Fatalf("failed to process result #%d: %v", index, err)
195195
}
196196
queue = make(map[common.Hash]struct{})
@@ -231,7 +231,7 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) {
231231
}
232232
}
233233
// Feed the retrieved results back and queue new tasks
234-
if _, index, err := sched.Process(results); err != nil {
234+
if _, index, err := sched.Process(results, dstDb); err != nil {
235235
t.Fatalf("failed to process result #%d: %v", index, err)
236236
}
237237
for _, result := range results {
@@ -272,7 +272,7 @@ func TestDuplicateAvoidanceTrieSync(t *testing.T) {
272272

273273
results[i] = SyncResult{hash, data}
274274
}
275-
if _, index, err := sched.Process(results); err != nil {
275+
if _, index, err := sched.Process(results, dstDb); err != nil {
276276
t.Fatalf("failed to process result #%d: %v", index, err)
277277
}
278278
queue = append(queue[:0], sched.Missing(0)...)
@@ -304,7 +304,7 @@ func TestIncompleteTrieSync(t *testing.T) {
304304
results[i] = SyncResult{hash, data}
305305
}
306306
// Process each of the trie nodes
307-
if _, index, err := sched.Process(results); err != nil {
307+
if _, index, err := sched.Process(results, dstDb); err != nil {
308308
t.Fatalf("failed to process result #%d: %v", index, err)
309309
}
310310
for _, result := range results {

‎trie/trie.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ func init() {
6060

6161
// Database must be implemented by backing stores for the trie.
6262
type Database interface {
63+
DatabaseReader
6364
DatabaseWriter
64-
// Get returns the value for key from the database.
65+
}
66+
67+
// DatabaseReader wraps the Get method of a backing store for the trie.
68+
type DatabaseReader interface {
6569
Get(key []byte) (value []byte, err error)
6670
}
6771

0 commit comments

Comments
 (0)