aboutsummaryrefslogtreecommitdiffstats
path: root/trie
diff options
context:
space:
mode:
Diffstat (limited to 'trie')
-rw-r--r--trie/encoding.go2
-rw-r--r--trie/hasher.go2
-rw-r--r--trie/iterator_test.go2
-rw-r--r--trie/sync.go24
-rw-r--r--trie/sync_test.go18
-rw-r--r--trie/trie.go6
6 files changed, 25 insertions, 29 deletions
diff --git a/trie/encoding.go b/trie/encoding.go
index 761bad188..2037118dd 100644
--- a/trie/encoding.go
+++ b/trie/encoding.go
@@ -80,7 +80,7 @@ func compactHexEncode(nibbles []byte) []byte {
}
l := (nl + 1) / 2
var str = make([]byte, l)
- for i, _ := range str {
+ for i := range str {
b := nibbles[i*2] * 16
if nl > i*2 {
b += nibbles[i*2+1]
diff --git a/trie/hasher.go b/trie/hasher.go
index e6261819c..98c309531 100644
--- a/trie/hasher.go
+++ b/trie/hasher.go
@@ -52,7 +52,7 @@ func returnHasherToPool(h *hasher) {
// hash collapses a node down into a hash node, also returning a copy of the
// original node initialzied with the computed hash to replace the original one.
func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error) {
- // If we're not storing the node, just hashing, use avaialble cached data
+ // If we're not storing the node, just hashing, use available cached data
if hash, dirty := n.cache(); hash != nil {
if db == nil {
return hash, n, nil
diff --git a/trie/iterator_test.go b/trie/iterator_test.go
index 2bcc3700e..c56ac85be 100644
--- a/trie/iterator_test.go
+++ b/trie/iterator_test.go
@@ -105,7 +105,7 @@ func TestNodeIteratorCoverage(t *testing.T) {
}
}
// Cross check the hashes and the database itself
- for hash, _ := range hashes {
+ for hash := range hashes {
if _, err := db.Get(hash.Bytes()); err != nil {
t.Errorf("failed to retrieve reported node %x: %v", hash, err)
}
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..4168c4d65 100644
--- a/trie/sync_test.go
+++ b/trie/sync_test.go
@@ -67,7 +67,7 @@ func checkTrieContents(t *testing.T, db Database, root []byte, content map[strin
t.Fatalf("inconsistent trie at %x: %v", root, err)
}
for key, val := range content {
- if have := trie.Get([]byte(key)); bytes.Compare(have, val) != 0 {
+ if have := trie.Get([]byte(key)); !bytes.Equal(have, val) {
t.Errorf("entry %x: content mismatch: have %x, want %x", key, have, val)
}
}
@@ -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)...)
@@ -182,7 +182,7 @@ func testIterativeRandomTrieSync(t *testing.T, batch int) {
for len(queue) > 0 {
// Fetch all the queued nodes in a random order
results := make([]SyncResult, 0, len(queue))
- for hash, _ := range queue {
+ for hash := range queue {
data, err := srcDb.Get(hash.Bytes())
if err != nil {
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
@@ -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{})
@@ -219,7 +219,7 @@ func TestIterativeRandomDelayedTrieSync(t *testing.T) {
for len(queue) > 0 {
// Sync only half of the scheduled nodes, even those in random order
results := make([]SyncResult, 0, len(queue)/2+1)
- for hash, _ := range queue {
+ for hash := range queue {
data, err := srcDb.Get(hash.Bytes())
if err != nil {
t.Fatalf("failed to retrieve node data for %x: %v", hash, err)
@@ -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)
}