bitxhub/pkg/order/mempool/mempool.go

84 lines
2.3 KiB
Go
Raw Normal View History

2020-09-24 15:11:33 +08:00
package mempool
import (
"time"
2020-09-24 15:11:33 +08:00
"github.com/meshplus/bitxhub-model/pb"
raftproto "github.com/meshplus/bitxhub/pkg/order/etcdraft/proto"
)
var _ MemPool = (*mempoolImpl)(nil)
type MemPool interface {
// ProcessTransactions process transaction from api and other vp nodes.
ProcessTransactions(txs []*pb.Transaction, isLeader, isLocal bool) *raftproto.RequestBatch
2020-09-24 15:11:33 +08:00
// GenerateBlock generate a block
GenerateBlock() *raftproto.RequestBatch
2020-09-24 15:11:33 +08:00
2020-09-27 17:25:33 +08:00
// Remove removes the committed transactions from mempool
CommitTransactions(state *ChainState)
2020-09-24 15:11:33 +08:00
// HasPendingRequest checks if there is non-batched tx(s) in mempool pool or not
HasPendingRequest() bool
2020-09-24 15:11:33 +08:00
SetBatchSeqNo(batchSeq uint64)
2020-09-24 15:11:33 +08:00
GetTimeoutTransactions(rebroadcastDuration time.Duration) [][]*pb.Transaction
External
2020-09-24 15:11:33 +08:00
}
// External is a concurrent and safe interface, which can be called by api module directly.
type External interface {
2020-09-24 15:11:33 +08:00
// GetPendingNonceByAccount will return the latest pending nonce of a given account
GetPendingNonceByAccount(account string) uint64
2020-09-24 15:11:33 +08:00
// IsPoolFull check if memPool has exceeded the limited txSize.
IsPoolFull() bool
2020-09-24 15:11:33 +08:00
}
// NewMempool return the mempool instance.
func NewMempool(config *Config) MemPool {
return newMempoolImpl(config)
2020-09-24 15:11:33 +08:00
}
// GenerateRequestBatch generates a transaction batch and post it
// to outside if there are transactions in txPool.
func (mpi *mempoolImpl) GenerateBlock() *raftproto.RequestBatch {
if mpi.txStore.priorityNonBatchSize == 0 {
mpi.logger.Debug("Mempool is empty")
return nil
2020-09-24 15:11:33 +08:00
}
batch, err := mpi.generateBlock()
if err != nil {
mpi.logger.Error("Generator batch failed")
return nil
2020-09-24 15:11:33 +08:00
}
return batch
2020-09-24 15:11:33 +08:00
}
func (mpi *mempoolImpl) HasPendingRequest() bool {
return mpi.txStore.priorityNonBatchSize > 0
2020-09-24 15:11:33 +08:00
}
func (mpi *mempoolImpl) CommitTransactions(state *ChainState) {
gcStartTime := time.Now()
mpi.processCommitTransactions(state)
duration := time.Now().Sub(gcStartTime).Nanoseconds()
mpi.logger.Debugf("GC duration %v", duration)
2020-09-24 15:11:33 +08:00
}
func (mpi *mempoolImpl) GetPendingNonceByAccount(account string) uint64 {
return mpi.txStore.nonceCache.getPendingNonce(account)
2020-09-24 15:11:33 +08:00
}
func (mpi *mempoolImpl) IsPoolFull() bool {
return uint64(len(mpi.txStore.txHashMap)) >= mpi.poolSize
2020-09-24 15:11:33 +08:00
}
func (mpi *mempoolImpl) SetBatchSeqNo(batchSeq uint64) {
mpi.batchSeqNo = batchSeq
}