diff options
author | Péter Szilágyi <peterke@gmail.com> | 2019-04-10 20:45:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-10 20:45:13 +0800 |
commit | cdae1c59abc32f85debfa29577fbf1ed036ebf73 (patch) | |
tree | 1cead73084658ee9b15288d6ecab07bbf37cf90c /les/peer.go | |
parent | 14ae1246b789dd2a0a2bd22f0c7d3256daa26759 (diff) | |
parent | 0b00e19ed93512865dccdef1a340c27e6985aa82 (diff) | |
download | go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar.gz go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar.bz2 go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar.lz go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar.xz go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.tar.zst go-tangerine-cdae1c59abc32f85debfa29577fbf1ed036ebf73.zip |
Merge pull request #19437 from zsfelfoldi/fix-sendtxv1.8.26
les: fix SendTx cost calculation and verify cost table
Diffstat (limited to 'les/peer.go')
-rw-r--r-- | les/peer.go | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/les/peer.go b/les/peer.go index 678384f0e..5072010c5 100644 --- a/les/peer.go +++ b/les/peer.go @@ -42,6 +42,11 @@ var ( const maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam) +// if the total encoded size of a sent transaction batch is over txSizeCostLimit +// per transaction then the request cost is calculated as proportional to the +// encoded size instead of the transaction count +const txSizeCostLimit = 0x4000 + const ( announceTypeNone = iota announceTypeSimple @@ -163,7 +168,41 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 { p.lock.RLock() defer p.lock.RUnlock() - cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount) + costs := p.fcCosts[msgcode] + if costs == nil { + return 0 + } + cost := costs.baseCost + costs.reqCost*uint64(amount) + if cost > p.fcServerParams.BufLimit { + cost = p.fcServerParams.BufLimit + } + return cost +} + +func (p *peer) GetTxRelayCost(amount, size int) uint64 { + p.lock.RLock() + defer p.lock.RUnlock() + + var msgcode uint64 + switch p.version { + case lpv1: + msgcode = SendTxMsg + case lpv2: + msgcode = SendTxV2Msg + default: + panic(nil) + } + + costs := p.fcCosts[msgcode] + if costs == nil { + return 0 + } + cost := costs.baseCost + costs.reqCost*uint64(amount) + sizeCost := costs.baseCost + costs.reqCost*uint64(size)/txSizeCostLimit + if sizeCost > cost { + cost = sizeCost + } + if cost > p.fcServerParams.BufLimit { cost = p.fcServerParams.BufLimit } @@ -307,9 +346,9 @@ func (p *peer) RequestTxStatus(reqID, cost uint64, txHashes []common.Hash) error return sendRequest(p.rw, GetTxStatusMsg, reqID, cost, txHashes) } -// SendTxStatus sends a batch of transactions to be added to the remote transaction pool. -func (p *peer) SendTxs(reqID, cost uint64, txs types.Transactions) error { - p.Log().Debug("Fetching batch of transactions", "count", len(txs)) +// SendTxs sends a batch of transactions to be added to the remote transaction pool. +func (p *peer) SendTxs(reqID, cost uint64, txs rlp.RawValue) error { + p.Log().Debug("Fetching batch of transactions", "size", len(txs)) switch p.version { case lpv1: return p2p.Send(p.rw, SendTxMsg, txs) // old message format does not include reqID @@ -485,6 +524,20 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis p.fcServerParams = params p.fcServer = flowcontrol.NewServerNode(params) p.fcCosts = MRC.decode() + var checkList []uint64 + switch p.version { + case lpv1: + checkList = reqListV1 + case lpv2: + checkList = reqListV2 + default: + panic(nil) + } + for _, msgCode := range checkList { + if p.fcCosts[msgCode] == nil { + return errResp(ErrUselessPeer, "peer does not support message %d", msgCode) + } + } } p.headInfo = &announceData{Td: rTd, Hash: rHash, Number: rNum} |