diff --git a/cmd/devp2p/internal/ethtest/snap.go b/cmd/devp2p/internal/ethtest/snap.go index ea528e5e2f2e..f947e4bc9bae 100644 --- a/cmd/devp2p/internal/ethtest/snap.go +++ b/cmd/devp2p/internal/ethtest/snap.go @@ -27,8 +27,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth/protocols/snap" "github.com/ethereum/go-ethereum/internal/utesting" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" "golang.org/x/crypto/sha3" ) @@ -530,11 +530,11 @@ func (s *Suite) snapGetAccountRange(t *utesting.T, tc *accRangeTest) error { for i, key := range hashes { keys[i] = common.CopyBytes(key[:]) } - nodes := make(trienode.ProofList, len(proof)) + nodes := make(light.NodeList, len(proof)) for i, node := range proof { nodes[i] = node } - proofdb := nodes.Set() + proofdb := nodes.NodeSet() var end []byte if len(keys) > 0 { diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index ce23ca514cb5..b2fd03766eca 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -24,13 +24,13 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" ) const ( @@ -321,7 +321,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac it.Release() // Generate the Merkle proofs for the first and last account - proof := trienode.NewProofSet() + proof := light.NewNodeSet() if err := tr.Prove(req.Origin[:], proof); err != nil { log.Warn("Failed to prove account range", "origin", req.Origin, "err", err) return nil, nil @@ -333,7 +333,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac } } var proofs [][]byte - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } return accounts, proofs @@ -427,7 +427,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP if err != nil { return nil, nil } - proof := trienode.NewProofSet() + proof := light.NewNodeSet() if err := stTrie.Prove(origin[:], proof); err != nil { log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err) return nil, nil @@ -438,7 +438,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP return nil, nil } } - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } // Proof terminates the reply as proofs are only added if a node diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 6a2d92c00991..0f5f2ccdfeb9 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -37,11 +37,11 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/msgrate" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" "golang.org/x/crypto/sha3" ) @@ -2394,11 +2394,11 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco for i, key := range hashes { keys[i] = common.CopyBytes(key[:]) } - nodes := make(trienode.ProofList, len(proof)) + nodes := make(light.NodeList, len(proof)) for i, node := range proof { nodes[i] = node } - proofdb := nodes.Set() + proofdb := nodes.NodeSet() var end []byte if len(keys) > 0 { @@ -2639,7 +2639,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo for j, key := range hashes[i] { keys[j] = common.CopyBytes(key[:]) } - nodes := make(trienode.ProofList, 0, len(proof)) + nodes := make(light.NodeList, 0, len(proof)) if i == len(hashes)-1 { for _, node := range proof { nodes = append(nodes, node) @@ -2658,7 +2658,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo } else { // A proof was attached, the response is only partial, check that the // returned data is indeed part of the storage trie - proofdb := nodes.Set() + proofdb := nodes.NodeSet() var end []byte if len(keys) > 0 { diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index 1ee381a6619e..1514ad4e1344 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" @@ -272,7 +273,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H // Unless we send the entire trie, we need to supply proofs // Actually, we need to supply proofs either way! This seems to be an implementation // quirk in go-ethereum - proof := trienode.NewProofSet() + proof := light.NewNodeSet() if err := t.accountTrie.Prove(origin[:], proof); err != nil { t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err) } @@ -282,7 +283,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H t.logger.Error("Could not prove last item", "error", err) } } - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } return keys, vals, proofs @@ -352,7 +353,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm if originHash != (common.Hash{}) || (abort && len(keys) > 0) { // If we're aborting, we need to prove the first and last item // This terminates the response (and thus the loop) - proof := trienode.NewProofSet() + proof := light.NewNodeSet() stTrie := t.storageTries[account] // Here's a potential gotcha: when constructing the proof, we cannot @@ -367,7 +368,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm t.logger.Error("Could not prove last item", "error", err) } } - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } break @@ -410,7 +411,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco if exit { // If we're aborting, we need to prove the first and last item // This terminates the response (and thus the loop) - proof := trienode.NewProofSet() + proof := light.NewNodeSet() stTrie := t.storageTries[account] // Here's a potential gotcha: when constructing the proof, we cannot @@ -426,7 +427,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco t.logger.Error("Could not prove last item", "error", err) } } - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } break @@ -598,10 +599,9 @@ func testSyncBloatedProof(t *testing.T, scheme string) { vals = append(vals, entry.v) } // The proofs - proof := trienode.NewProofSet() + proof := light.NewNodeSet() if err := t.accountTrie.Prove(origin[:], proof); err != nil { t.logger.Error("Could not prove origin", "origin", origin, "error", err) - t.logger.Error("Could not prove origin", "origin", origin, "error", err) } // The bloat: add proof of every single element for _, entry := range t.accountValues { @@ -614,7 +614,7 @@ func testSyncBloatedProof(t *testing.T, scheme string) { keys = append(keys[:1], keys[2:]...) vals = append(vals[:1], vals[2:]...) } - for _, blob := range proof.List() { + for _, blob := range proof.NodeList() { proofs = append(proofs, blob) } if err := t.remote.OnAccounts(t, requestId, keys, vals, proofs); err != nil { diff --git a/les/client_handler.go b/les/client_handler.go index 50f6dce879ff..4cfeba08fe14 100644 --- a/les/client_handler.go +++ b/les/client_handler.go @@ -26,7 +26,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/light" "github.com/ethereum/go-ethereum/p2p" - "github.com/ethereum/go-ethereum/trie/trienode" ) // clientHandler is responsible for receiving and processing all incoming server @@ -237,7 +236,7 @@ func (h *clientHandler) handleMsg(p *serverPeer) error { p.Log().Trace("Received les/2 proofs response") var resp struct { ReqID, BV uint64 - Data trienode.ProofList + Data light.NodeList } if err := msg.Decode(&resp); err != nil { return errResp(ErrDecode, "msg %v: %v", msg, err) diff --git a/les/handler_test.go b/les/handler_test.go index c803a5ddb3f2..26a083f475da 100644 --- a/les/handler_test.go +++ b/les/handler_test.go @@ -37,7 +37,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" ) func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error { @@ -402,7 +401,7 @@ func testGetProofs(t *testing.T, protocol int) { bc := server.handler.blockchain var proofreqs []ProofReq - proofsV2 := trienode.NewProofSet() + proofsV2 := light.NewNodeSet() accounts := []common.Address{bankAddr, userAddr1, userAddr2, signerAddr, {}} for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ { @@ -420,7 +419,7 @@ func testGetProofs(t *testing.T, protocol int) { } // Send the proof request and verify the response sendRequest(rawPeer.app, GetProofsV2Msg, 42, proofreqs) - if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.List()); err != nil { + if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.NodeList()); err != nil { t.Errorf("proofs mismatch: %v", err) } } @@ -457,10 +456,10 @@ func testGetStaleProof(t *testing.T, protocol int) { var expected []rlp.RawValue if wantOK { - proofsV2 := trienode.NewProofSet() + proofsV2 := light.NewNodeSet() t, _ := trie.New(trie.StateTrieID(header.Root), server.backend.Blockchain().TrieDB()) t.Prove(account, proofsV2) - expected = proofsV2.List() + expected = proofsV2.NodeList() } if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil { t.Errorf("codes mismatch: %v", err) diff --git a/les/odr_requests.go b/les/odr_requests.go index c907018590fa..2b23e0540cc0 100644 --- a/les/odr_requests.go +++ b/les/odr_requests.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" ) var ( @@ -223,9 +222,9 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error { if msg.MsgType != MsgProofsV2 { return errInvalidMessageType } - proofs := msg.Obj.(trienode.ProofList) + proofs := msg.Obj.(light.NodeList) // Verify the proof and store if checks out - nodeSet := proofs.Set() + nodeSet := proofs.NodeSet() reads := &readTraceDB{db: nodeSet} if _, err := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil { return fmt.Errorf("merkle proof verification failed: %v", err) @@ -309,7 +308,7 @@ type HelperTrieReq struct { } type HelperTrieResps struct { // describes all responses, not just a single one - Proofs trienode.ProofList + Proofs light.NodeList AuxData [][]byte } @@ -357,7 +356,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error { if len(resp.AuxData) != 1 { return errInvalidEntryCount } - nodeSet := resp.Proofs.Set() + nodeSet := resp.Proofs.NodeSet() headerEnc := resp.AuxData[0] if len(headerEnc) == 0 { return errHeaderUnavailable @@ -452,7 +451,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error { } resps := msg.Obj.(HelperTrieResps) proofs := resps.Proofs - nodeSet := proofs.Set() + nodeSet := proofs.NodeSet() reads := &readTraceDB{db: nodeSet} r.BloomBits = make([][]byte, len(r.SectionIndexList)) diff --git a/les/peer.go b/les/peer.go index 58cb92870031..48381689ef7a 100644 --- a/les/peer.go +++ b/les/peer.go @@ -40,7 +40,6 @@ import ( "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/trie/trienode" ) var ( @@ -900,7 +899,7 @@ func (p *clientPeer) replyReceiptsRLP(reqID uint64, receipts []rlp.RawValue) *re } // replyProofsV2 creates a reply with a batch of merkle proofs, corresponding to the ones requested. -func (p *clientPeer) replyProofsV2(reqID uint64, proofs trienode.ProofList) *reply { +func (p *clientPeer) replyProofsV2(reqID uint64, proofs light.NodeList) *reply { data, _ := rlp.EncodeToBytes(proofs) return &reply{p.rw, ProofsV2Msg, reqID, data} } diff --git a/les/server_requests.go b/les/server_requests.go index 9a249f04c924..485be6d9e9e6 100644 --- a/les/server_requests.go +++ b/les/server_requests.go @@ -30,7 +30,6 @@ import ( "github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" ) // serverBackend defines the backend functions needed for serving LES requests @@ -379,7 +378,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) { err error ) bc := backend.BlockChain() - nodes := trienode.NewProofSet() + nodes := light.NewNodeSet() for i, request := range r.Reqs { if i != 0 && !waitOrStop() { @@ -445,7 +444,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) { break } } - return p.replyProofsV2(r.ReqID, nodes.List()) + return p.replyProofsV2(r.ReqID, nodes.NodeList()) }, r.ReqID, uint64(len(r.Reqs)), nil } @@ -464,7 +463,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err auxData [][]byte ) bc := backend.BlockChain() - nodes := trienode.NewProofSet() + nodes := light.NewNodeSet() for i, request := range r.Reqs { if i != 0 && !waitOrStop() { return nil @@ -499,7 +498,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err break } } - return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.List(), AuxData: auxData}) + return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData}) }, r.ReqID, uint64(len(r.Reqs)), nil } diff --git a/trie/trienode/proof.go b/light/nodeset.go similarity index 73% rename from trie/trienode/proof.go rename to light/nodeset.go index 012f0087dded..3662596785c7 100644 --- a/trie/trienode/proof.go +++ b/light/nodeset.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package trienode +package light import ( "errors" @@ -26,9 +26,9 @@ import ( "github.com/ethereum/go-ethereum/rlp" ) -// ProofSet stores a set of trie nodes. It implements trie.Database and can also +// NodeSet stores a set of trie nodes. It implements trie.Database and can also // act as a cache for another trie.Database. -type ProofSet struct { +type NodeSet struct { nodes map[string][]byte order []string @@ -36,15 +36,15 @@ type ProofSet struct { lock sync.RWMutex } -// NewProofSet creates an empty node set -func NewProofSet() *ProofSet { - return &ProofSet{ +// NewNodeSet creates an empty node set +func NewNodeSet() *NodeSet { + return &NodeSet{ nodes: make(map[string][]byte), } } // Put stores a new node in the set -func (db *ProofSet) Put(key []byte, value []byte) error { +func (db *NodeSet) Put(key []byte, value []byte) error { db.lock.Lock() defer db.lock.Unlock() @@ -61,7 +61,7 @@ func (db *ProofSet) Put(key []byte, value []byte) error { } // Delete removes a node from the set -func (db *ProofSet) Delete(key []byte) error { +func (db *NodeSet) Delete(key []byte) error { db.lock.Lock() defer db.lock.Unlock() @@ -70,7 +70,7 @@ func (db *ProofSet) Delete(key []byte) error { } // Get returns a stored node -func (db *ProofSet) Get(key []byte) ([]byte, error) { +func (db *NodeSet) Get(key []byte) ([]byte, error) { db.lock.RLock() defer db.lock.RUnlock() @@ -81,13 +81,13 @@ func (db *ProofSet) Get(key []byte) ([]byte, error) { } // Has returns true if the node set contains the given key -func (db *ProofSet) Has(key []byte) (bool, error) { +func (db *NodeSet) Has(key []byte) (bool, error) { _, err := db.Get(key) return err == nil, nil } // KeyCount returns the number of nodes in the set -func (db *ProofSet) KeyCount() int { +func (db *NodeSet) KeyCount() int { db.lock.RLock() defer db.lock.RUnlock() @@ -95,19 +95,19 @@ func (db *ProofSet) KeyCount() int { } // DataSize returns the aggregated data size of nodes in the set -func (db *ProofSet) DataSize() int { +func (db *NodeSet) DataSize() int { db.lock.RLock() defer db.lock.RUnlock() return db.dataSize } -// List converts the node set to a ProofList -func (db *ProofSet) List() ProofList { +// NodeList converts the node set to a NodeList +func (db *NodeSet) NodeList() NodeList { db.lock.RLock() defer db.lock.RUnlock() - var values ProofList + var values NodeList for _, key := range db.order { values = append(values, db.nodes[key]) } @@ -115,7 +115,7 @@ func (db *ProofSet) List() ProofList { } // Store writes the contents of the set to the given database -func (db *ProofSet) Store(target ethdb.KeyValueWriter) { +func (db *NodeSet) Store(target ethdb.KeyValueWriter) { db.lock.RLock() defer db.lock.RUnlock() @@ -124,36 +124,36 @@ func (db *ProofSet) Store(target ethdb.KeyValueWriter) { } } -// ProofList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter. -type ProofList []rlp.RawValue +// NodeList stores an ordered list of trie nodes. It implements ethdb.KeyValueWriter. +type NodeList []rlp.RawValue // Store writes the contents of the list to the given database -func (n ProofList) Store(db ethdb.KeyValueWriter) { +func (n NodeList) Store(db ethdb.KeyValueWriter) { for _, node := range n { db.Put(crypto.Keccak256(node), node) } } -// Set converts the node list to a ProofSet -func (n ProofList) Set() *ProofSet { - db := NewProofSet() +// NodeSet converts the node list to a NodeSet +func (n NodeList) NodeSet() *NodeSet { + db := NewNodeSet() n.Store(db) return db } // Put stores a new node at the end of the list -func (n *ProofList) Put(key []byte, value []byte) error { +func (n *NodeList) Put(key []byte, value []byte) error { *n = append(*n, value) return nil } // Delete panics as there's no reason to remove a node from the list. -func (n *ProofList) Delete(key []byte) error { +func (n *NodeList) Delete(key []byte) error { panic("not supported") } // DataSize returns the aggregated data size of nodes in the list -func (n ProofList) DataSize() int { +func (n NodeList) DataSize() int { var size int for _, node := range n { size += len(node) diff --git a/light/odr.go b/light/odr.go index 39f626ee2c53..2597027435ba 100644 --- a/light/odr.go +++ b/light/odr.go @@ -27,7 +27,6 @@ import ( "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/trie/trienode" ) // NoOdr is the default context passed to an ODR capable function when the ODR @@ -91,7 +90,7 @@ func StorageTrieID(state *TrieID, address common.Address, root common.Hash) *Tri type TrieRequest struct { Id *TrieID Key []byte - Proof *trienode.ProofSet + Proof *NodeSet } // StoreResult stores the retrieved data in local database @@ -144,7 +143,7 @@ type ChtRequest struct { ChtRoot common.Hash Header *types.Header Td *big.Int - Proof *trienode.ProofSet + Proof *NodeSet } // StoreResult stores the retrieved data in local database @@ -164,7 +163,7 @@ type BloomRequest struct { SectionIndexList []uint64 BloomTrieRoot common.Hash BloomBits [][]byte - Proofs *trienode.ProofSet + Proofs *NodeSet } // StoreResult stores the retrieved data in local database diff --git a/light/odr_test.go b/light/odr_test.go index c415d73e7ef2..d8a7f1067556 100644 --- a/light/odr_test.go +++ b/light/odr_test.go @@ -37,7 +37,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/trienode" ) var ( @@ -96,7 +95,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error { if err != nil { panic(err) } - nodes := trienode.NewProofSet() + nodes := NewNodeSet() t.Prove(req.Key, nodes) req.Proof = nodes case *CodeRequest: diff --git a/light/postprocess.go b/light/postprocess.go index a317e30b90a4..13d75f8617ae 100644 --- a/light/postprocess.go +++ b/light/postprocess.go @@ -363,7 +363,7 @@ func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uin func (b *BloomTrieIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error { indexCh := make(chan uint, types.BloomBitLength) type res struct { - nodes *trienode.ProofSet + nodes *NodeSet err error } resCh := make(chan res, types.BloomBitLength)