Skip to content

Commit c8d8126

Browse files
committed
les: check required message types in cost table
1 parent 0de9f32 commit c8d8126

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

les/handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ func (pm *ProtocolManager) handle(p *peer) error {
324324
}
325325
}
326326

327-
var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
327+
var (
328+
reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
329+
reqListV1 = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, GetHeaderProofsMsg}
330+
reqListV2 = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, SendTxV2Msg, GetTxStatusMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
331+
)
328332

329333
// handleMsg is invoked whenever an inbound message is received from a remote
330334
// peer. The remote connection is torn down upon returning any error.

les/peer.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
168168
p.lock.RLock()
169169
defer p.lock.RUnlock()
170170

171-
cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount)
171+
costs := p.fcCosts[msgcode]
172+
if costs == nil {
173+
return 0
174+
}
175+
cost := costs.baseCost + costs.reqCost*uint64(amount)
172176
if cost > p.fcServerParams.BufLimit {
173177
cost = p.fcServerParams.BufLimit
174178
}
@@ -189,8 +193,12 @@ func (p *peer) GetTxRelayCost(amount, size int) uint64 {
189193
panic(nil)
190194
}
191195

192-
cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount)
193-
sizeCost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(size)/txSizeCostLimit
196+
costs := p.fcCosts[msgcode]
197+
if costs == nil {
198+
return 0
199+
}
200+
cost := costs.baseCost + costs.reqCost*uint64(amount)
201+
sizeCost := costs.baseCost + costs.reqCost*uint64(size)/txSizeCostLimit
194202
if sizeCost > cost {
195203
cost = sizeCost
196204
}
@@ -516,6 +524,20 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
516524
p.fcServerParams = params
517525
p.fcServer = flowcontrol.NewServerNode(params)
518526
p.fcCosts = MRC.decode()
527+
var checkList []uint64
528+
switch p.version {
529+
case lpv1:
530+
checkList = reqListV1
531+
case lpv2:
532+
checkList = reqListV2
533+
default:
534+
panic(nil)
535+
}
536+
for _, msgCode := range checkList {
537+
if p.fcCosts[msgCode] == nil {
538+
return errResp(ErrUselessPeer, "peer does not support message %d", msgCode)
539+
}
540+
}
519541
}
520542

521543
p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum}

0 commit comments

Comments
 (0)