Skip to content

Commit c0a034e

Browse files
rjl493456442karalabe
authored andcommitted
eth, les: reject stale request (#19689)
* eth, les: reject stale request * les: reuse local head number
1 parent 3d3e83e commit c0a034e

5 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎eth/backend.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func (s *Ethereum) EthVersion() int { return int(s.protocolMa
481481
func (s *Ethereum) NetVersion() uint64 { return s.networkID }
482482
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
483483
func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.protocolManager.acceptTxs) == 1 }
484+
func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruning }
484485

485486
// Protocols implements node.Service, returning all the currently configured
486487
// network protocols to start.

‎les/handler.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
698698
p.Log().Warn("Failed to retrieve header for code", "block", *number, "hash", request.BHash)
699699
continue
700700
}
701+
// Refuse to search stale state data in the database since looking for
702+
// a non-exist key is kind of expensive.
703+
local := pm.blockchain.CurrentHeader().Number.Uint64()
704+
if !pm.server.archiveMode && header.Number.Uint64()+core.TriesInMemory <= local {
705+
p.Log().Debug("Reject stale code request", "number", header.Number.Uint64(), "head", local)
706+
continue
707+
}
701708
triedb := pm.blockchain.StateCache().TrieDB()
702709

703710
account, err := pm.getAccount(triedb, header.Root, common.BytesToHash(request.AccKey))
@@ -852,6 +859,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
852859
p.Log().Warn("Failed to retrieve header for proof", "block", *number, "hash", request.BHash)
853860
continue
854861
}
862+
// Refuse to search stale state data in the database since looking for
863+
// a non-exist key is kind of expensive.
864+
local := pm.blockchain.CurrentHeader().Number.Uint64()
865+
if !pm.server.archiveMode && header.Number.Uint64()+core.TriesInMemory <= local {
866+
p.Log().Debug("Reject stale trie request", "number", header.Number.Uint64(), "head", local)
867+
continue
868+
}
855869
root = header.Root
856870
}
857871
// Open the account or storage trie for the request

‎les/handler_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,31 @@ func testGetCode(t *testing.T, protocol int) {
278278
}
279279
}
280280

281+
// Tests that the stale contract codes can't be retrieved based on account addresses.
282+
func TestGetStaleCodeLes2(t *testing.T) { testGetStaleCode(t, 2) }
283+
func TestGetStaleCodeLes3(t *testing.T) { testGetStaleCode(t, 3) }
284+
285+
func testGetStaleCode(t *testing.T, protocol int) {
286+
server, tearDown := newServerEnv(t, core.TriesInMemory+4, protocol, nil)
287+
defer tearDown()
288+
bc := server.pm.blockchain.(*core.BlockChain)
289+
290+
check := func(number uint64, expected [][]byte) {
291+
req := &CodeReq{
292+
BHash: bc.GetHeaderByNumber(number).Hash(),
293+
AccKey: crypto.Keccak256(testContractAddr[:]),
294+
}
295+
cost := server.tPeer.GetRequestCost(GetCodeMsg, 1)
296+
sendRequest(server.tPeer.app, GetCodeMsg, 42, cost, []*CodeReq{req})
297+
if err := expectResponse(server.tPeer.app, CodeMsg, 42, testBufLimit, expected); err != nil {
298+
t.Errorf("codes mismatch: %v", err)
299+
}
300+
}
301+
check(0, [][]byte{}) // Non-exist contract
302+
check(testContractDeployed, [][]byte{}) // Stale contract
303+
check(bc.CurrentHeader().Number.Uint64(), [][]byte{testContractCodeDeployed}) // Fresh contract
304+
}
305+
281306
// Tests that the transaction receipts can be retrieved based on hashes.
282307
func TestGetReceiptLes2(t *testing.T) { testGetReceipt(t, 2) }
283308

@@ -338,6 +363,43 @@ func testGetProofs(t *testing.T, protocol int) {
338363
}
339364
}
340365

366+
// Tests that the stale contract codes can't be retrieved based on account addresses.
367+
func TestGetStaleProofLes2(t *testing.T) { testGetStaleProof(t, 2) }
368+
func TestGetStaleProofLes3(t *testing.T) { testGetStaleProof(t, 3) }
369+
370+
func testGetStaleProof(t *testing.T, protocol int) {
371+
server, tearDown := newServerEnv(t, core.TriesInMemory+4, protocol, nil)
372+
defer tearDown()
373+
bc := server.pm.blockchain.(*core.BlockChain)
374+
375+
check := func(number uint64, wantOK bool) {
376+
var (
377+
header = bc.GetHeaderByNumber(number)
378+
account = crypto.Keccak256(testBankAddress.Bytes())
379+
)
380+
req := &ProofReq{
381+
BHash: header.Hash(),
382+
Key: account,
383+
}
384+
cost := server.tPeer.GetRequestCost(GetProofsV2Msg, 1)
385+
sendRequest(server.tPeer.app, GetProofsV2Msg, 42, cost, []*ProofReq{req})
386+
387+
var expected []rlp.RawValue
388+
if wantOK {
389+
proofsV2 := light.NewNodeSet()
390+
t, _ := trie.New(header.Root, trie.NewDatabase(server.db))
391+
t.Prove(crypto.Keccak256(account), 0, proofsV2)
392+
expected = proofsV2.NodeList()
393+
}
394+
if err := expectResponse(server.tPeer.app, ProofsV2Msg, 42, testBufLimit, expected); err != nil {
395+
t.Errorf("codes mismatch: %v", err)
396+
}
397+
}
398+
check(0, false) // Non-exist proof
399+
check(2, false) // Stale proof
400+
check(bc.CurrentHeader().Number.Uint64(), true) // Fresh proof
401+
}
402+
341403
// Tests that CHT proofs can be correctly retrieved.
342404
func TestGetCHTProofsLes2(t *testing.T) { testGetCHTProofs(t, 2) }
343405

‎les/peer.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,14 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
551551
send = send.add("serveHeaders", nil)
552552
send = send.add("serveChainSince", uint64(0))
553553
send = send.add("serveStateSince", uint64(0))
554-
send = send.add("serveRecentState", uint64(core.TriesInMemory-4))
554+
555+
// If local ethereum node is running in archive mode, advertise ourselves we have
556+
// all version state data. Otherwise only recent state is available.
557+
stateRecent := uint64(core.TriesInMemory - 4)
558+
if server.archiveMode {
559+
stateRecent = 0
560+
}
561+
send = send.add("serveRecentState", stateRecent)
555562
send = send.add("txRelay", nil)
556563
}
557564
send = send.add("flowControl/BL", server.defParams.BufLimit)

‎les/server.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151
type LesServer struct {
5252
lesCommons
5353

54+
archiveMode bool // Flag whether the ethereum node runs in archive mode.
55+
5456
fcManager *flowcontrol.ClientManager // nil if our node is client only
5557
costTracker *costTracker
5658
testCost uint64
@@ -93,7 +95,8 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
9395
nil,
9496
quitSync,
9597
new(sync.WaitGroup),
96-
config.ULC, eth.Synced)
98+
config.ULC,
99+
eth.Synced)
97100
if err != nil {
98101
return nil, err
99102
}
@@ -120,6 +123,7 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
120123
bloomTrieIndexer: light.NewBloomTrieIndexer(eth.ChainDb(), nil, params.BloomBitsBlocks, params.BloomTrieFrequency),
121124
protocolManager: pm,
122125
},
126+
archiveMode: eth.ArchiveMode(),
123127
quitSync: quitSync,
124128
lesTopics: lesTopics,
125129
onlyAnnounce: config.OnlyAnnounce,

0 commit comments

Comments
 (0)