calculate amino overhead on the fly

This commit is contained in:
Anton Kaliaev 2018-08-31 15:55:07 +04:00
parent e957f322c7
commit e873fed815
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 7 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"container/list"
"crypto/sha256"
"encoding/binary"
"fmt"
"sync"
"sync/atomic"
@ -385,6 +386,8 @@ func (mem *Mempool) notifyTxsAvailable() {
// If max is negative, there is no cap on the size of all returned
// transactions (~ all available transactions).
func (mem *Mempool) ReapMaxBytes(max int) types.Txs {
var buf [binary.MaxVarintLen64]byte
mem.proxyMtx.Lock()
defer mem.proxyMtx.Unlock()
@ -400,10 +403,12 @@ func (mem *Mempool) ReapMaxBytes(max int) types.Txs {
txs := make([]types.Tx, 0, mem.txs.Len())
for e := mem.txs.Front(); e != nil; e = e.Next() {
memTx := e.Value.(*mempoolTx)
if max > 0 && cur+len(memTx.tx)+types.MaxAminoOverheadForTx > max {
// amino.UvarintSize is not used here because it won't be possible to reuse buf
aminoOverhead := binary.PutUvarint(buf[:], uint64(len(memTx.tx)))
if max > 0 && cur+len(memTx.tx)+aminoOverhead > max {
return txs
}
cur += len(memTx.tx) + types.MaxAminoOverheadForTx
cur += len(memTx.tx) + aminoOverhead
txs = append(txs, memTx.tx)
}
return txs

View File

@ -11,12 +11,6 @@ import (
cmn "github.com/tendermint/tendermint/libs/common"
)
const (
// MaxAminoOverheadForTx - maximum amino overhead to encode a transaction
// (ranges from 1 to 4 bytes).
MaxAminoOverheadForTx = 4
)
// Tx is an arbitrary byte array.
// NOTE: Tx has no types at this level, so when wire encoded it's just length-prefixed.
// Might we want types here ?