Skip to content

Commit

Permalink
all: move light.NodeSet to trienode.ProofSet (ethereum#28287)
Browse files Browse the repository at this point in the history
This is a minor refactor in preparation of changes to range verifier. This PR contains no intentional functional changes but moves (and renames) the light.NodeSet
  • Loading branch information
holiman authored and devopsbo3 committed Nov 10, 2023
1 parent 7aa36bb commit 6ffa809
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 67 deletions.
6 changes: 3 additions & 3 deletions cmd/devp2p/internal/ethtest/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(light.NodeList, len(proof))
nodes := make(trienode.ProofList, len(proof))
for i, node := range proof {
nodes[i] = node
}
proofdb := nodes.NodeSet()
proofdb := nodes.Set()

var end []byte
if len(keys) > 0 {
Expand Down
10 changes: 5 additions & 5 deletions eth/protocols/snap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -321,7 +321,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
it.Release()

// Generate the Merkle proofs for the first and last account
proof := light.NewNodeSet()
proof := trienode.NewProofSet()
if err := tr.Prove(req.Origin[:], proof); err != nil {
log.Warn("Failed to prove account range", "origin", req.Origin, "err", err)
return nil, nil
Expand All @@ -333,7 +333,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
}
}
var proofs [][]byte
for _, blob := range proof.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
return accounts, proofs
Expand Down Expand Up @@ -427,7 +427,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
if err != nil {
return nil, nil
}
proof := light.NewNodeSet()
proof := trienode.NewProofSet()
if err := stTrie.Prove(origin[:], proof); err != nil {
log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err)
return nil, nil
Expand All @@ -438,7 +438,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
return nil, nil
}
}
for _, blob := range proof.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
// Proof terminates the reply as proofs are only added if a node
Expand Down
10 changes: 5 additions & 5 deletions eth/protocols/snap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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(light.NodeList, len(proof))
nodes := make(trienode.ProofList, len(proof))
for i, node := range proof {
nodes[i] = node
}
proofdb := nodes.NodeSet()
proofdb := nodes.Set()

var end []byte
if len(keys) > 0 {
Expand Down Expand Up @@ -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(light.NodeList, 0, len(proof))
nodes := make(trienode.ProofList, 0, len(proof))
if i == len(hashes)-1 {
for _, node := range proof {
nodes = append(nodes, node)
Expand All @@ -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.NodeSet()
proofdb := nodes.Set()

var end []byte
if len(keys) > 0 {
Expand Down
18 changes: 9 additions & 9 deletions eth/protocols/snap/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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"
Expand Down Expand Up @@ -273,7 +272,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 := light.NewNodeSet()
proof := trienode.NewProofSet()
if err := t.accountTrie.Prove(origin[:], proof); err != nil {
t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err)
}
Expand All @@ -283,7 +282,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.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
return keys, vals, proofs
Expand Down Expand Up @@ -353,7 +352,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 := light.NewNodeSet()
proof := trienode.NewProofSet()
stTrie := t.storageTries[account]

// Here's a potential gotcha: when constructing the proof, we cannot
Expand All @@ -368,7 +367,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
t.logger.Error("Could not prove last item", "error", err)
}
}
for _, blob := range proof.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
break
Expand Down Expand Up @@ -411,7 +410,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 := light.NewNodeSet()
proof := trienode.NewProofSet()
stTrie := t.storageTries[account]

// Here's a potential gotcha: when constructing the proof, we cannot
Expand All @@ -427,7 +426,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
t.logger.Error("Could not prove last item", "error", err)
}
}
for _, blob := range proof.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
break
Expand Down Expand Up @@ -599,9 +598,10 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
vals = append(vals, entry.v)
}
// The proofs
proof := light.NewNodeSet()
proof := trienode.NewProofSet()
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 {
Expand All @@ -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.NodeList() {
for _, blob := range proof.List() {
proofs = append(proofs, blob)
}
if err := t.remote.OnAccounts(t, requestId, keys, vals, proofs); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion les/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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
Expand Down Expand Up @@ -236,7 +237,7 @@ func (h *clientHandler) handleMsg(p *serverPeer) error {
p.Log().Trace("Received les/2 proofs response")
var resp struct {
ReqID, BV uint64
Data light.NodeList
Data trienode.ProofList
}
if err := msg.Decode(&resp); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)
Expand Down
9 changes: 5 additions & 4 deletions les/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ 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 {
Expand Down Expand Up @@ -401,7 +402,7 @@ func testGetProofs(t *testing.T, protocol int) {
bc := server.handler.blockchain

var proofreqs []ProofReq
proofsV2 := light.NewNodeSet()
proofsV2 := trienode.NewProofSet()

accounts := []common.Address{bankAddr, userAddr1, userAddr2, signerAddr, {}}
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
Expand All @@ -419,7 +420,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.NodeList()); err != nil {
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.List()); err != nil {
t.Errorf("proofs mismatch: %v", err)
}
}
Expand Down Expand Up @@ -456,10 +457,10 @@ func testGetStaleProof(t *testing.T, protocol int) {

var expected []rlp.RawValue
if wantOK {
proofsV2 := light.NewNodeSet()
proofsV2 := trienode.NewProofSet()
t, _ := trie.New(trie.StateTrieID(header.Root), server.backend.Blockchain().TrieDB())
t.Prove(account, proofsV2)
expected = proofsV2.NodeList()
expected = proofsV2.List()
}
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil {
t.Errorf("codes mismatch: %v", err)
Expand Down
11 changes: 6 additions & 5 deletions les/odr_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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 (
Expand Down Expand Up @@ -222,9 +223,9 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
if msg.MsgType != MsgProofsV2 {
return errInvalidMessageType
}
proofs := msg.Obj.(light.NodeList)
proofs := msg.Obj.(trienode.ProofList)
// Verify the proof and store if checks out
nodeSet := proofs.NodeSet()
nodeSet := proofs.Set()
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)
Expand Down Expand Up @@ -308,7 +309,7 @@ type HelperTrieReq struct {
}

type HelperTrieResps struct { // describes all responses, not just a single one
Proofs light.NodeList
Proofs trienode.ProofList
AuxData [][]byte
}

Expand Down Expand Up @@ -356,7 +357,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
if len(resp.AuxData) != 1 {
return errInvalidEntryCount
}
nodeSet := resp.Proofs.NodeSet()
nodeSet := resp.Proofs.Set()
headerEnc := resp.AuxData[0]
if len(headerEnc) == 0 {
return errHeaderUnavailable
Expand Down Expand Up @@ -451,7 +452,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
}
resps := msg.Obj.(HelperTrieResps)
proofs := resps.Proofs
nodeSet := proofs.NodeSet()
nodeSet := proofs.Set()
reads := &readTraceDB{db: nodeSet}

r.BloomBits = make([][]byte, len(r.SectionIndexList))
Expand Down
3 changes: 2 additions & 1 deletion les/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 (
Expand Down Expand Up @@ -899,7 +900,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 light.NodeList) *reply {
func (p *clientPeer) replyProofsV2(reqID uint64, proofs trienode.ProofList) *reply {
data, _ := rlp.EncodeToBytes(proofs)
return &reply{p.rw, ProofsV2Msg, reqID, data}
}
Expand Down
9 changes: 5 additions & 4 deletions les/server_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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
Expand Down Expand Up @@ -378,7 +379,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
err error
)
bc := backend.BlockChain()
nodes := light.NewNodeSet()
nodes := trienode.NewProofSet()

for i, request := range r.Reqs {
if i != 0 && !waitOrStop() {
Expand Down Expand Up @@ -444,7 +445,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
break
}
}
return p.replyProofsV2(r.ReqID, nodes.NodeList())
return p.replyProofsV2(r.ReqID, nodes.List())
}, r.ReqID, uint64(len(r.Reqs)), nil
}

Expand All @@ -463,7 +464,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
auxData [][]byte
)
bc := backend.BlockChain()
nodes := light.NewNodeSet()
nodes := trienode.NewProofSet()
for i, request := range r.Reqs {
if i != 0 && !waitOrStop() {
return nil
Expand Down Expand Up @@ -498,7 +499,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
break
}
}
return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData})
return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.List(), AuxData: auxData})
}, r.ReqID, uint64(len(r.Reqs)), nil
}

Expand Down
Loading

0 comments on commit 6ffa809

Please sign in to comment.