|
42 | 42 |
|
43 | 43 | const maxResponseErrors = 50 // number of invalid responses tolerated (makes the protocol less brittle but still avoids spam)
|
44 | 44 |
|
| 45 | +// if the total encoded size of a sent transaction batch is over txSizeCostLimit |
| 46 | +// per transaction then the request cost is calculated as proportional to the |
| 47 | +// encoded size instead of the transaction count |
| 48 | +const txSizeCostLimit = 0x4000 |
| 49 | + |
45 | 50 | const (
|
46 | 51 | announceTypeNone = iota
|
47 | 52 | announceTypeSimple
|
@@ -170,6 +175,32 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
|
170 | 175 | return cost
|
171 | 176 | }
|
172 | 177 |
|
| 178 | +func (p *peer) GetTxRelayCost(amount, size int) uint64 { |
| 179 | + p.lock.RLock() |
| 180 | + defer p.lock.RUnlock() |
| 181 | + |
| 182 | + var msgcode uint64 |
| 183 | + switch p.version { |
| 184 | + case lpv1: |
| 185 | + msgcode = SendTxMsg |
| 186 | + case lpv2: |
| 187 | + msgcode = SendTxV2Msg |
| 188 | + default: |
| 189 | + panic(nil) |
| 190 | + } |
| 191 | + |
| 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 |
| 194 | + if sizeCost > cost { |
| 195 | + cost = sizeCost |
| 196 | + } |
| 197 | + |
| 198 | + if cost > p.fcServerParams.BufLimit { |
| 199 | + cost = p.fcServerParams.BufLimit |
| 200 | + } |
| 201 | + return cost |
| 202 | +} |
| 203 | + |
173 | 204 | // HasBlock checks if the peer has a given block
|
174 | 205 | func (p *peer) HasBlock(hash common.Hash, number uint64, hasState bool) bool {
|
175 | 206 | p.lock.RLock()
|
@@ -307,9 +338,9 @@ func (p *peer) RequestTxStatus(reqID, cost uint64, txHashes []common.Hash) error
|
307 | 338 | return sendRequest(p.rw, GetTxStatusMsg, reqID, cost, txHashes)
|
308 | 339 | }
|
309 | 340 |
|
310 |
| -// SendTxStatus sends a batch of transactions to be added to the remote transaction pool. |
311 |
| -func (p *peer) SendTxs(reqID, cost uint64, txs types.Transactions) error { |
312 |
| - p.Log().Debug("Fetching batch of transactions", "count", len(txs)) |
| 341 | +// SendTxs sends a batch of transactions to be added to the remote transaction pool. |
| 342 | +func (p *peer) SendTxs(reqID, cost uint64, txs rlp.RawValue) error { |
| 343 | + p.Log().Debug("Fetching batch of transactions", "size", len(txs)) |
313 | 344 | switch p.version {
|
314 | 345 | case lpv1:
|
315 | 346 | return p2p.Send(p.rw, SendTxMsg, txs) // old message format does not include reqID
|
|
0 commit comments