From e873fed81540b916c803120fa3b0639bffdec383 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Fri, 31 Aug 2018 15:55:07 +0400 Subject: [PATCH] calculate amino overhead on the fly --- mempool/mempool.go | 9 +++++++-- types/tx.go | 6 ------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mempool/mempool.go b/mempool/mempool.go index ec93202e3..6ee6c42bc 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 896d7be07..489f0b232 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 ?