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
|
|
|
*/
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
package mempool
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
|
|
|
"github.com/tendermint/tendermint/block"
|
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2014-09-11 01:11:41 -07:00
|
|
|
)
|
2014-09-10 02:43:16 -07:00
|
|
|
|
|
|
|
type Mempool struct {
|
2014-10-20 19:02:10 -07:00
|
|
|
mtx sync.Mutex
|
2015-01-14 20:34:53 -08:00
|
|
|
state *sm.State
|
|
|
|
txs []block.Tx
|
2014-09-10 02:43:16 -07:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func NewMempool(state *sm.State) *Mempool {
|
2014-09-10 02:43:16 -07:00
|
|
|
return &Mempool{
|
2014-10-20 19:02:10 -07:00
|
|
|
state: state,
|
2014-09-10 02:43:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 01:11:41 -07:00
|
|
|
// Apply tx to the state and remember it.
|
2015-01-14 20:34:53 -08:00
|
|
|
func (mem *Mempool) AddTx(tx block.Tx) (err error) {
|
2014-09-10 02:43:16 -07:00
|
|
|
mem.mtx.Lock()
|
|
|
|
defer mem.mtx.Unlock()
|
2014-10-07 00:43:34 -07:00
|
|
|
err = mem.state.ExecTx(tx)
|
2014-09-10 16:56:02 -07:00
|
|
|
if err != nil {
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("AddTx() error", "tx", tx, "error", err)
|
2014-09-10 16:56:02 -07:00
|
|
|
return err
|
2014-09-10 02:43:16 -07:00
|
|
|
} else {
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("AddTx() success", "tx", tx)
|
2014-09-10 02:43:16 -07:00
|
|
|
mem.txs = append(mem.txs, tx)
|
2014-09-10 16:56:02 -07:00
|
|
|
return nil
|
2014-09-10 02:43:16 -07:00
|
|
|
}
|
|
|
|
}
|
2014-09-11 01:11:41 -07:00
|
|
|
|
2015-01-14 20:34:53 -08:00
|
|
|
func (mem *Mempool) GetProposalTxs() []block.Tx {
|
2014-09-11 01:11:41 -07:00
|
|
|
mem.mtx.Lock()
|
|
|
|
defer mem.mtx.Unlock()
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("GetProposalTxs:", "txs", mem.txs)
|
2014-11-05 03:08:29 -08:00
|
|
|
return mem.txs
|
2014-09-11 01:11:41 -07:00
|
|
|
}
|
|
|
|
|
2014-10-13 20:07:26 -07:00
|
|
|
// "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-14 20:34:53 -08:00
|
|
|
func (mem *Mempool) ResetForBlockAndState(block_ *block.Block, state *sm.State) {
|
2014-09-11 01:11:41 -07:00
|
|
|
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-14 20:34:53 -08:00
|
|
|
for _, tx := range block_.Data.Txs {
|
|
|
|
txHash := binary.BinarySha256(tx)
|
2014-09-11 01:11:41 -07:00
|
|
|
blockTxsMap[string(txHash)] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, filter all txs from mem.txs that are in blockTxsMap
|
2015-01-14 20:34:53 -08:00
|
|
|
txs := []block.Tx{}
|
2014-09-11 01:11:41 -07:00
|
|
|
for _, tx := range mem.txs {
|
2015-01-14 20:34:53 -08:00
|
|
|
txHash := binary.BinarySha256(tx)
|
2014-09-11 01:11:41 -07:00
|
|
|
if _, ok := blockTxsMap[string(txHash)]; ok {
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash)
|
2014-09-11 01:11:41 -07:00
|
|
|
continue
|
|
|
|
} else {
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("Filter in, still new", "tx", tx, "txHash", txHash)
|
2014-09-11 01:11:41 -07:00
|
|
|
txs = append(txs, tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, filter all txs that aren't valid given new state.
|
2015-01-14 20:34:53 -08:00
|
|
|
validTxs := []block.Tx{}
|
2014-09-11 01:11:41 -07:00
|
|
|
for _, tx := range txs {
|
2014-10-07 00:43:34 -07:00
|
|
|
err := mem.state.ExecTx(tx)
|
2015-01-10 05:41:50 -08:00
|
|
|
if err == nil {
|
|
|
|
log.Debug("Filter in, valid", "tx", tx)
|
2014-09-11 01:11:41 -07:00
|
|
|
validTxs = append(validTxs, tx)
|
|
|
|
} else {
|
|
|
|
// tx is no longer valid.
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("Filter out, no longer valid", "tx", tx, "error", err)
|
2014-09-11 01:11:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done!
|
2015-01-10 05:41:50 -08:00
|
|
|
log.Debug("New txs", "txs", validTxs, "oldTxs", mem.txs)
|
2014-09-11 01:11:41 -07:00
|
|
|
mem.txs = validTxs
|
|
|
|
}
|