Skip to content

Commit 6b1e4f4

Browse files
authored
all: move light.NodeSet to trienode.ProofSet (#28287)
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
1 parent db9afae commit 6b1e4f4

File tree

13 files changed

+74
-67
lines changed

13 files changed

+74
-67
lines changed

cmd/devp2p/internal/ethtest/snap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
"github.com/ethereum/go-ethereum/crypto"
2828
"github.com/ethereum/go-ethereum/eth/protocols/snap"
2929
"github.com/ethereum/go-ethereum/internal/utesting"
30-
"github.com/ethereum/go-ethereum/light"
3130
"github.com/ethereum/go-ethereum/trie"
31+
"github.com/ethereum/go-ethereum/trie/trienode"
3232
"golang.org/x/crypto/sha3"
3333
)
3434

@@ -530,11 +530,11 @@ func (s *Suite) snapGetAccountRange(t *utesting.T, tc *accRangeTest) error {
530530
for i, key := range hashes {
531531
keys[i] = common.CopyBytes(key[:])
532532
}
533-
nodes := make(light.NodeList, len(proof))
533+
nodes := make(trienode.ProofList, len(proof))
534534
for i, node := range proof {
535535
nodes[i] = node
536536
}
537-
proofdb := nodes.NodeSet()
537+
proofdb := nodes.Set()
538538

539539
var end []byte
540540
if len(keys) > 0 {

eth/protocols/snap/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import (
2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core"
2626
"github.com/ethereum/go-ethereum/core/types"
27-
"github.com/ethereum/go-ethereum/light"
2827
"github.com/ethereum/go-ethereum/log"
2928
"github.com/ethereum/go-ethereum/metrics"
3029
"github.com/ethereum/go-ethereum/p2p"
3130
"github.com/ethereum/go-ethereum/p2p/enode"
3231
"github.com/ethereum/go-ethereum/p2p/enr"
3332
"github.com/ethereum/go-ethereum/trie"
33+
"github.com/ethereum/go-ethereum/trie/trienode"
3434
)
3535

3636
const (
@@ -321,7 +321,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
321321
it.Release()
322322

323323
// Generate the Merkle proofs for the first and last account
324-
proof := light.NewNodeSet()
324+
proof := trienode.NewProofSet()
325325
if err := tr.Prove(req.Origin[:], proof); err != nil {
326326
log.Warn("Failed to prove account range", "origin", req.Origin, "err", err)
327327
return nil, nil
@@ -333,7 +333,7 @@ func ServiceGetAccountRangeQuery(chain *core.BlockChain, req *GetAccountRangePac
333333
}
334334
}
335335
var proofs [][]byte
336-
for _, blob := range proof.NodeList() {
336+
for _, blob := range proof.List() {
337337
proofs = append(proofs, blob)
338338
}
339339
return accounts, proofs
@@ -427,7 +427,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
427427
if err != nil {
428428
return nil, nil
429429
}
430-
proof := light.NewNodeSet()
430+
proof := trienode.NewProofSet()
431431
if err := stTrie.Prove(origin[:], proof); err != nil {
432432
log.Warn("Failed to prove storage range", "origin", req.Origin, "err", err)
433433
return nil, nil
@@ -438,7 +438,7 @@ func ServiceGetStorageRangesQuery(chain *core.BlockChain, req *GetStorageRangesP
438438
return nil, nil
439439
}
440440
}
441-
for _, blob := range proof.NodeList() {
441+
for _, blob := range proof.List() {
442442
proofs = append(proofs, blob)
443443
}
444444
// Proof terminates the reply as proofs are only added if a node

eth/protocols/snap/sync.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ import (
3737
"github.com/ethereum/go-ethereum/crypto"
3838
"github.com/ethereum/go-ethereum/ethdb"
3939
"github.com/ethereum/go-ethereum/event"
40-
"github.com/ethereum/go-ethereum/light"
4140
"github.com/ethereum/go-ethereum/log"
4241
"github.com/ethereum/go-ethereum/p2p/msgrate"
4342
"github.com/ethereum/go-ethereum/rlp"
4443
"github.com/ethereum/go-ethereum/trie"
44+
"github.com/ethereum/go-ethereum/trie/trienode"
4545
"golang.org/x/crypto/sha3"
4646
)
4747

@@ -2394,11 +2394,11 @@ func (s *Syncer) OnAccounts(peer SyncPeer, id uint64, hashes []common.Hash, acco
23942394
for i, key := range hashes {
23952395
keys[i] = common.CopyBytes(key[:])
23962396
}
2397-
nodes := make(light.NodeList, len(proof))
2397+
nodes := make(trienode.ProofList, len(proof))
23982398
for i, node := range proof {
23992399
nodes[i] = node
24002400
}
2401-
proofdb := nodes.NodeSet()
2401+
proofdb := nodes.Set()
24022402

24032403
var end []byte
24042404
if len(keys) > 0 {
@@ -2639,7 +2639,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
26392639
for j, key := range hashes[i] {
26402640
keys[j] = common.CopyBytes(key[:])
26412641
}
2642-
nodes := make(light.NodeList, 0, len(proof))
2642+
nodes := make(trienode.ProofList, 0, len(proof))
26432643
if i == len(hashes)-1 {
26442644
for _, node := range proof {
26452645
nodes = append(nodes, node)
@@ -2658,7 +2658,7 @@ func (s *Syncer) OnStorage(peer SyncPeer, id uint64, hashes [][]common.Hash, slo
26582658
} else {
26592659
// A proof was attached, the response is only partial, check that the
26602660
// returned data is indeed part of the storage trie
2661-
proofdb := nodes.NodeSet()
2661+
proofdb := nodes.Set()
26622662

26632663
var end []byte
26642664
if len(keys) > 0 {

eth/protocols/snap/sync_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/ethereum/go-ethereum/core/types"
3232
"github.com/ethereum/go-ethereum/crypto"
3333
"github.com/ethereum/go-ethereum/ethdb"
34-
"github.com/ethereum/go-ethereum/light"
3534
"github.com/ethereum/go-ethereum/log"
3635
"github.com/ethereum/go-ethereum/rlp"
3736
"github.com/ethereum/go-ethereum/trie"
@@ -273,7 +272,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H
273272
// Unless we send the entire trie, we need to supply proofs
274273
// Actually, we need to supply proofs either way! This seems to be an implementation
275274
// quirk in go-ethereum
276-
proof := light.NewNodeSet()
275+
proof := trienode.NewProofSet()
277276
if err := t.accountTrie.Prove(origin[:], proof); err != nil {
278277
t.logger.Error("Could not prove inexistence of origin", "origin", origin, "error", err)
279278
}
@@ -283,7 +282,7 @@ func createAccountRequestResponse(t *testPeer, root common.Hash, origin common.H
283282
t.logger.Error("Could not prove last item", "error", err)
284283
}
285284
}
286-
for _, blob := range proof.NodeList() {
285+
for _, blob := range proof.List() {
287286
proofs = append(proofs, blob)
288287
}
289288
return keys, vals, proofs
@@ -353,7 +352,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
353352
if originHash != (common.Hash{}) || (abort && len(keys) > 0) {
354353
// If we're aborting, we need to prove the first and last item
355354
// This terminates the response (and thus the loop)
356-
proof := light.NewNodeSet()
355+
proof := trienode.NewProofSet()
357356
stTrie := t.storageTries[account]
358357

359358
// Here's a potential gotcha: when constructing the proof, we cannot
@@ -368,7 +367,7 @@ func createStorageRequestResponse(t *testPeer, root common.Hash, accounts []comm
368367
t.logger.Error("Could not prove last item", "error", err)
369368
}
370369
}
371-
for _, blob := range proof.NodeList() {
370+
for _, blob := range proof.List() {
372371
proofs = append(proofs, blob)
373372
}
374373
break
@@ -411,7 +410,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
411410
if exit {
412411
// If we're aborting, we need to prove the first and last item
413412
// This terminates the response (and thus the loop)
414-
proof := light.NewNodeSet()
413+
proof := trienode.NewProofSet()
415414
stTrie := t.storageTries[account]
416415

417416
// Here's a potential gotcha: when constructing the proof, we cannot
@@ -427,7 +426,7 @@ func createStorageRequestResponseAlwaysProve(t *testPeer, root common.Hash, acco
427426
t.logger.Error("Could not prove last item", "error", err)
428427
}
429428
}
430-
for _, blob := range proof.NodeList() {
429+
for _, blob := range proof.List() {
431430
proofs = append(proofs, blob)
432431
}
433432
break
@@ -599,9 +598,10 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
599598
vals = append(vals, entry.v)
600599
}
601600
// The proofs
602-
proof := light.NewNodeSet()
601+
proof := trienode.NewProofSet()
603602
if err := t.accountTrie.Prove(origin[:], proof); err != nil {
604603
t.logger.Error("Could not prove origin", "origin", origin, "error", err)
604+
t.logger.Error("Could not prove origin", "origin", origin, "error", err)
605605
}
606606
// The bloat: add proof of every single element
607607
for _, entry := range t.accountValues {
@@ -614,7 +614,7 @@ func testSyncBloatedProof(t *testing.T, scheme string) {
614614
keys = append(keys[:1], keys[2:]...)
615615
vals = append(vals[:1], vals[2:]...)
616616
}
617-
for _, blob := range proof.NodeList() {
617+
for _, blob := range proof.List() {
618618
proofs = append(proofs, blob)
619619
}
620620
if err := t.remote.OnAccounts(t, requestId, keys, vals, proofs); err != nil {

les/client_handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/light"
2828
"github.com/ethereum/go-ethereum/p2p"
29+
"github.com/ethereum/go-ethereum/trie/trienode"
2930
)
3031

3132
// clientHandler is responsible for receiving and processing all incoming server
@@ -236,7 +237,7 @@ func (h *clientHandler) handleMsg(p *serverPeer) error {
236237
p.Log().Trace("Received les/2 proofs response")
237238
var resp struct {
238239
ReqID, BV uint64
239-
Data light.NodeList
240+
Data trienode.ProofList
240241
}
241242
if err := msg.Decode(&resp); err != nil {
242243
return errResp(ErrDecode, "msg %v: %v", msg, err)

les/handler_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/ethereum/go-ethereum/params"
3838
"github.com/ethereum/go-ethereum/rlp"
3939
"github.com/ethereum/go-ethereum/trie"
40+
"github.com/ethereum/go-ethereum/trie/trienode"
4041
)
4142

4243
func expectResponse(r p2p.MsgReader, msgcode, reqID, bv uint64, data interface{}) error {
@@ -401,7 +402,7 @@ func testGetProofs(t *testing.T, protocol int) {
401402
bc := server.handler.blockchain
402403

403404
var proofreqs []ProofReq
404-
proofsV2 := light.NewNodeSet()
405+
proofsV2 := trienode.NewProofSet()
405406

406407
accounts := []common.Address{bankAddr, userAddr1, userAddr2, signerAddr, {}}
407408
for i := uint64(0); i <= bc.CurrentBlock().Number.Uint64(); i++ {
@@ -419,7 +420,7 @@ func testGetProofs(t *testing.T, protocol int) {
419420
}
420421
// Send the proof request and verify the response
421422
sendRequest(rawPeer.app, GetProofsV2Msg, 42, proofreqs)
422-
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.NodeList()); err != nil {
423+
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, proofsV2.List()); err != nil {
423424
t.Errorf("proofs mismatch: %v", err)
424425
}
425426
}
@@ -456,10 +457,10 @@ func testGetStaleProof(t *testing.T, protocol int) {
456457

457458
var expected []rlp.RawValue
458459
if wantOK {
459-
proofsV2 := light.NewNodeSet()
460+
proofsV2 := trienode.NewProofSet()
460461
t, _ := trie.New(trie.StateTrieID(header.Root), server.backend.Blockchain().TrieDB())
461462
t.Prove(account, proofsV2)
462-
expected = proofsV2.NodeList()
463+
expected = proofsV2.List()
463464
}
464465
if err := expectResponse(rawPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil {
465466
t.Errorf("codes mismatch: %v", err)

les/odr_requests.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/log"
3131
"github.com/ethereum/go-ethereum/rlp"
3232
"github.com/ethereum/go-ethereum/trie"
33+
"github.com/ethereum/go-ethereum/trie/trienode"
3334
)
3435

3536
var (
@@ -222,9 +223,9 @@ func (r *TrieRequest) Validate(db ethdb.Database, msg *Msg) error {
222223
if msg.MsgType != MsgProofsV2 {
223224
return errInvalidMessageType
224225
}
225-
proofs := msg.Obj.(light.NodeList)
226+
proofs := msg.Obj.(trienode.ProofList)
226227
// Verify the proof and store if checks out
227-
nodeSet := proofs.NodeSet()
228+
nodeSet := proofs.Set()
228229
reads := &readTraceDB{db: nodeSet}
229230
if _, err := trie.VerifyProof(r.Id.Root, r.Key, reads); err != nil {
230231
return fmt.Errorf("merkle proof verification failed: %v", err)
@@ -308,7 +309,7 @@ type HelperTrieReq struct {
308309
}
309310

310311
type HelperTrieResps struct { // describes all responses, not just a single one
311-
Proofs light.NodeList
312+
Proofs trienode.ProofList
312313
AuxData [][]byte
313314
}
314315

@@ -356,7 +357,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
356357
if len(resp.AuxData) != 1 {
357358
return errInvalidEntryCount
358359
}
359-
nodeSet := resp.Proofs.NodeSet()
360+
nodeSet := resp.Proofs.Set()
360361
headerEnc := resp.AuxData[0]
361362
if len(headerEnc) == 0 {
362363
return errHeaderUnavailable
@@ -451,7 +452,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
451452
}
452453
resps := msg.Obj.(HelperTrieResps)
453454
proofs := resps.Proofs
454-
nodeSet := proofs.NodeSet()
455+
nodeSet := proofs.Set()
455456
reads := &readTraceDB{db: nodeSet}
456457

457458
r.BloomBits = make([][]byte, len(r.SectionIndexList))

les/peer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/ethereum/go-ethereum/p2p"
4141
"github.com/ethereum/go-ethereum/p2p/enode"
4242
"github.com/ethereum/go-ethereum/rlp"
43+
"github.com/ethereum/go-ethereum/trie/trienode"
4344
)
4445

4546
var (
@@ -899,7 +900,7 @@ func (p *clientPeer) replyReceiptsRLP(reqID uint64, receipts []rlp.RawValue) *re
899900
}
900901

901902
// replyProofsV2 creates a reply with a batch of merkle proofs, corresponding to the ones requested.
902-
func (p *clientPeer) replyProofsV2(reqID uint64, proofs light.NodeList) *reply {
903+
func (p *clientPeer) replyProofsV2(reqID uint64, proofs trienode.ProofList) *reply {
903904
data, _ := rlp.EncodeToBytes(proofs)
904905
return &reply{p.rw, ProofsV2Msg, reqID, data}
905906
}

les/server_requests.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/metrics"
3131
"github.com/ethereum/go-ethereum/rlp"
3232
"github.com/ethereum/go-ethereum/trie"
33+
"github.com/ethereum/go-ethereum/trie/trienode"
3334
)
3435

3536
// serverBackend defines the backend functions needed for serving LES requests
@@ -378,7 +379,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
378379
err error
379380
)
380381
bc := backend.BlockChain()
381-
nodes := light.NewNodeSet()
382+
nodes := trienode.NewProofSet()
382383

383384
for i, request := range r.Reqs {
384385
if i != 0 && !waitOrStop() {
@@ -444,7 +445,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
444445
break
445446
}
446447
}
447-
return p.replyProofsV2(r.ReqID, nodes.NodeList())
448+
return p.replyProofsV2(r.ReqID, nodes.List())
448449
}, r.ReqID, uint64(len(r.Reqs)), nil
449450
}
450451

@@ -463,7 +464,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
463464
auxData [][]byte
464465
)
465466
bc := backend.BlockChain()
466-
nodes := light.NewNodeSet()
467+
nodes := trienode.NewProofSet()
467468
for i, request := range r.Reqs {
468469
if i != 0 && !waitOrStop() {
469470
return nil
@@ -498,7 +499,7 @@ func handleGetHelperTrieProofs(msg Decoder) (serveRequestFn, uint64, uint64, err
498499
break
499500
}
500501
}
501-
return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData})
502+
return p.replyHelperTrieProofs(r.ReqID, HelperTrieResps{Proofs: nodes.List(), AuxData: auxData})
502503
}, r.ReqID, uint64(len(r.Reqs)), nil
503504
}
504505

0 commit comments

Comments
 (0)