mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
les: implement size-based SendTx cost calculation
This commit is contained in:
parent
557f603005
commit
baf0689dd3
2 changed files with 38 additions and 5 deletions
35
les/peer.go
35
les/peer.go
|
|
@ -49,6 +49,11 @@ const (
|
||||||
allowedUpdateRate = time.Millisecond * 10 // time constant for recharging one byte of allowance
|
allowedUpdateRate = time.Millisecond * 10 // time constant for recharging one byte of allowance
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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 = 0x10000
|
||||||
|
|
||||||
const (
|
const (
|
||||||
announceTypeNone = iota
|
announceTypeNone = iota
|
||||||
announceTypeSimple
|
announceTypeSimple
|
||||||
|
|
@ -200,6 +205,32 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
|
||||||
return cost
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
cost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(amount)
|
||||||
|
sizeCost := p.fcCosts[msgcode].baseCost + p.fcCosts[msgcode].reqCost*uint64(size)/txSizeCostLimit
|
||||||
|
if sizeCost > cost {
|
||||||
|
cost = sizeCost
|
||||||
|
}
|
||||||
|
|
||||||
|
if cost > p.fcServerParams.BufLimit {
|
||||||
|
cost = p.fcServerParams.BufLimit
|
||||||
|
}
|
||||||
|
return cost
|
||||||
|
}
|
||||||
|
|
||||||
// HasBlock checks if the peer has a given block
|
// HasBlock checks if the peer has a given block
|
||||||
func (p *peer) HasBlock(hash common.Hash, number uint64, hasState bool) bool {
|
func (p *peer) HasBlock(hash common.Hash, number uint64, hasState bool) bool {
|
||||||
p.lock.RLock()
|
p.lock.RLock()
|
||||||
|
|
@ -338,8 +369,8 @@ func (p *peer) RequestTxStatus(reqID, cost uint64, txHashes []common.Hash) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendTxStatus sends a batch of transactions to be added to the remote transaction pool.
|
// 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 {
|
func (p *peer) SendTxs(reqID, cost uint64, txs rlp.RawValue) error {
|
||||||
p.Log().Debug("Fetching batch of transactions", "count", len(txs))
|
p.Log().Debug("Sending batch of transactions", "size", len(txs))
|
||||||
switch p.version {
|
switch p.version {
|
||||||
case lpv1:
|
case lpv1:
|
||||||
return p2p.Send(p.rw, SendTxMsg, txs) // old message format does not include reqID
|
return p2p.Send(p.rw, SendTxMsg, txs) // old message format does not include reqID
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ltrInfo struct {
|
type ltrInfo struct {
|
||||||
|
|
@ -113,21 +114,22 @@ func (self *LesTxRelay) send(txs types.Transactions, count int) {
|
||||||
for p, list := range sendTo {
|
for p, list := range sendTo {
|
||||||
pp := p
|
pp := p
|
||||||
ll := list
|
ll := list
|
||||||
|
enc, _ := rlp.EncodeToBytes(ll)
|
||||||
|
|
||||||
reqID := genReqID()
|
reqID := genReqID()
|
||||||
rq := &distReq{
|
rq := &distReq{
|
||||||
getCost: func(dp distPeer) uint64 {
|
getCost: func(dp distPeer) uint64 {
|
||||||
peer := dp.(*peer)
|
peer := dp.(*peer)
|
||||||
return peer.GetRequestCost(SendTxMsg, len(ll))
|
return peer.GetTxRelayCost(len(ll), len(enc))
|
||||||
},
|
},
|
||||||
canSend: func(dp distPeer) bool {
|
canSend: func(dp distPeer) bool {
|
||||||
return dp.(*peer) == pp
|
return dp.(*peer) == pp
|
||||||
},
|
},
|
||||||
request: func(dp distPeer) func() {
|
request: func(dp distPeer) func() {
|
||||||
peer := dp.(*peer)
|
peer := dp.(*peer)
|
||||||
cost := peer.GetRequestCost(SendTxMsg, len(ll))
|
cost := peer.GetTxRelayCost(len(ll), len(enc))
|
||||||
peer.fcServer.QueueRequest(reqID, cost)
|
peer.fcServer.QueueRequest(reqID, cost)
|
||||||
return func() { peer.SendTxs(reqID, cost, ll) }
|
return func() { peer.SendTxs(reqID, cost, enc) }
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
self.reqDist.queue(rq)
|
self.reqDist.queue(rq)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue