diff options
author | Felix Lange <fjl@twurst.com> | 2017-01-05 22:58:00 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-01-06 21:15:22 +0800 |
commit | d3b751e4d94f95f6cc89544852f2d5811e075665 (patch) | |
tree | 14e3417894593f9922551dcb24c3a9d3383f69a2 /trie | |
parent | 7731061903bb992f7630ab389863951efb360258 (diff) | |
download | go-tangerine-d3b751e4d94f95f6cc89544852f2d5811e075665.tar.gz go-tangerine-d3b751e4d94f95f6cc89544852f2d5811e075665.tar.zst go-tangerine-d3b751e4d94f95f6cc89544852f2d5811e075665.zip |
trie: remove dependency on ethdb
This removes the core/types -> leveldb dependency.
Diffstat (limited to 'trie')
-rw-r--r-- | trie/sync.go | 24 | ||||
-rw-r--r-- | trie/sync_test.go | 12 | ||||
-rw-r--r-- | trie/trie.go | 6 |
3 files changed, 19 insertions, 23 deletions
diff --git a/trie/sync.go b/trie/sync.go index 2158ab750..168501392 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -21,7 +21,6 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/ethdb" "gopkg.in/karalabe/cookiejar.v2/collections/prque" ) @@ -58,13 +57,13 @@ type TrieSyncLeafCallback func(leaf []byte, parent common.Hash) error // unknown trie hashes to retrieve, accepts node data associated with said hashes // and reconstructs the trie step by step until all is done. type TrieSync struct { - database ethdb.Database // State database for storing all the assembled node data + database DatabaseReader requests map[common.Hash]*request // Pending requests pertaining to a key hash queue *prque.Prque // Priority queue with the pending requests } // NewTrieSync creates a new trie data download scheduler. -func NewTrieSync(root common.Hash, database ethdb.Database, callback TrieSyncLeafCallback) *TrieSync { +func NewTrieSync(root common.Hash, database DatabaseReader, callback TrieSyncLeafCallback) *TrieSync { ts := &TrieSync{ database: database, requests: make(map[common.Hash]*request), @@ -145,7 +144,7 @@ func (s *TrieSync) Missing(max int) []common.Hash { // Process injects a batch of retrieved trie nodes data, returning if something // was committed to the database and also the index of an entry if processing of // it failed. -func (s *TrieSync) Process(results []SyncResult) (bool, int, error) { +func (s *TrieSync) Process(results []SyncResult, dbw DatabaseWriter) (bool, int, error) { committed := false for i, item := range results { @@ -157,7 +156,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) { // If the item is a raw entry request, commit directly if request.raw { request.data = item.Data - s.commit(request, nil) + s.commit(request, dbw) committed = true continue } @@ -174,7 +173,7 @@ func (s *TrieSync) Process(results []SyncResult) (bool, int, error) { return committed, i, err } if len(requests) == 0 && request.deps == 0 { - s.commit(request, nil) + s.commit(request, dbw) committed = true continue } @@ -266,16 +265,9 @@ func (s *TrieSync) children(req *request, object node) ([]*request, error) { // commit finalizes a retrieval request and stores it into the database. If any // of the referencing parent requests complete due to this commit, they are also // committed themselves. -func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) { - // Create a new batch if none was specified - if batch == nil { - batch = s.database.NewBatch() - defer func() { - err = batch.Write() - }() - } +func (s *TrieSync) commit(req *request, dbw DatabaseWriter) (err error) { // Write the node content to disk - if err := batch.Put(req.hash[:], req.data); err != nil { + if err := dbw.Put(req.hash[:], req.data); err != nil { return err } delete(s.requests, req.hash) @@ -284,7 +276,7 @@ func (s *TrieSync) commit(req *request, batch ethdb.Batch) (err error) { for _, parent := range req.parents { parent.deps-- if parent.deps == 0 { - if err := s.commit(parent, batch); err != nil { + if err := s.commit(parent, dbw); err != nil { return err } } diff --git a/trie/sync_test.go b/trie/sync_test.go index 5292fe5cb..5edbd0746 100644 --- a/trie/sync_test.go +++ b/trie/sync_test.go @@ -122,7 +122,7 @@ func testIterativeTrieSync(t *testing.T, batch int) { } results[i] = SyncResult{hash, data} } - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } queue = append(queue[:0], sched.Missing(batch)...) @@ -152,7 +152,7 @@ func TestIterativeDelayedTrieSync(t *testing.T) { } results[i] = SyncResult{hash, data} } - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } queue = append(queue[len(results):], sched.Missing(10000)...) @@ -190,7 +190,7 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) { results = append(results, SyncResult{hash, data}) } // Feed the retrieved results back and queue new tasks - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } queue = make(map[common.Hash]struct{}) @@ -231,7 +231,7 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) { } } // Feed the retrieved results back and queue new tasks - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } for _, result := range results { @@ -272,7 +272,7 @@ func TestDuplicateAvoidanceTrieSync(t *testing.T) { results[i] = SyncResult{hash, data} } - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } queue = append(queue[:0], sched.Missing(0)...) @@ -304,7 +304,7 @@ func TestIncompleteTrieSync(t *testing.T) { results[i] = SyncResult{hash, data} } // Process each of the trie nodes - if _, index, err := sched.Process(results); err != nil { + if _, index, err := sched.Process(results, dstDb); err != nil { t.Fatalf("failed to process result #%d: %v", index, err) } for _, result := range results { diff --git a/trie/trie.go b/trie/trie.go index 035a80e74..cd9e20cac 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -60,8 +60,12 @@ func init() { // Database must be implemented by backing stores for the trie. type Database interface { + DatabaseReader DatabaseWriter - // Get returns the value for key from the database. +} + +// DatabaseReader wraps the Get method of a backing store for the trie. +type DatabaseReader interface { Get(key []byte) (value []byte, err error) } |