diff --git a/mempool/mempool.go b/mempool/mempool.go index ec93202e..6ee6c42b 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -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 diff --git a/types/tx.go b/types/tx.go index 896d7be0..489f0b23 100644 --- a/types/tx.go +++ b/types/tx.go @@ -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 ?