tendermint/mempool/mempool.go

99 lines
2.5 KiB
Go
Raw Normal View History

2014-09-10 02:43:16 -07:00
/*
Mempool receives new transactions and applies them to the latest committed state.
2014-09-11 22:44:59 -07:00
If the transaction is acceptable, then it broadcasts the tx to peers.
2014-09-10 02:43:16 -07:00
2014-12-23 23:20:49 -08:00
When this node happens to be the next proposer, it simply uses the recently
modified state (and the associated transactions) to construct a proposal.
2014-09-10 02:43:16 -07:00
*/
package mempool
import (
"sync"
"github.com/tendermint/tendermint/binary"
2015-01-15 22:43:15 -08:00
blk "github.com/tendermint/tendermint/block"
sm "github.com/tendermint/tendermint/state"
)
2014-09-10 02:43:16 -07:00
type Mempool struct {
mtx sync.Mutex
state *sm.State
2015-01-15 22:43:15 -08:00
txs []blk.Tx
2014-09-10 02:43:16 -07:00
}
func NewMempool(state *sm.State) *Mempool {
2014-09-10 02:43:16 -07:00
return &Mempool{
state: state,
2014-09-10 02:43:16 -07:00
}
}
// Apply tx to the state and remember it.
2015-01-15 22:43:15 -08:00
func (mem *Mempool) AddTx(tx blk.Tx) (err error) {
2014-09-10 02:43:16 -07:00
mem.mtx.Lock()
defer mem.mtx.Unlock()
err = mem.state.ExecTx(tx, false)
if err != nil {
log.Debug("AddTx() error", "tx", tx, "error", err)
return err
2014-09-10 02:43:16 -07:00
} else {
log.Debug("AddTx() success", "tx", tx)
2014-09-10 02:43:16 -07:00
mem.txs = append(mem.txs, tx)
return nil
2014-09-10 02:43:16 -07:00
}
}
2015-01-15 22:43:15 -08:00
func (mem *Mempool) GetProposalTxs() []blk.Tx {
mem.mtx.Lock()
defer mem.mtx.Unlock()
log.Debug("GetProposalTxs:", "txs", mem.txs)
return mem.txs
}
// "block" is the new block being committed.
// "state" is the result of state.AppendBlock("block").
// Txs that are present in "block" are discarded from mempool.
// Txs that have become invalid in the new "state" are also discarded.
2015-01-15 22:43:15 -08:00
func (mem *Mempool) ResetForBlockAndState(block *blk.Block, state *sm.State) {
mem.mtx.Lock()
defer mem.mtx.Unlock()
mem.state = state.Copy()
// First, create a lookup map of txns in new block.
blockTxsMap := make(map[string]struct{})
2015-01-15 22:43:15 -08:00
for _, tx := range block.Data.Txs {
txHash := binary.BinarySha256(tx)
blockTxsMap[string(txHash)] = struct{}{}
}
// Next, filter all txs from mem.txs that are in blockTxsMap
2015-01-15 22:43:15 -08:00
txs := []blk.Tx{}
for _, tx := range mem.txs {
txHash := binary.BinarySha256(tx)
if _, ok := blockTxsMap[string(txHash)]; ok {
log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash)
continue
} else {
log.Debug("Filter in, still new", "tx", tx, "txHash", txHash)
txs = append(txs, tx)
}
}
// Next, filter all txs that aren't valid given new state.
2015-01-15 22:43:15 -08:00
validTxs := []blk.Tx{}
for _, tx := range txs {
err := mem.state.ExecTx(tx, false)
if err == nil {
log.Debug("Filter in, valid", "tx", tx)
validTxs = append(validTxs, tx)
} else {
// tx is no longer valid.
log.Debug("Filter out, no longer valid", "tx", tx, "error", err)
}
}
// We're done!
log.Debug("New txs", "txs", validTxs, "oldTxs", mem.txs)
mem.txs = validTxs
}