You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

  1. /*
  2. Mempool receives new transactions and applies them to the latest committed state.
  3. If the transaction is acceptable, then it broadcasts the tx to peers.
  4. When this node happens to be the next proposer, it simply uses the recently
  5. modified state (and the associated transactions) to construct a proposal.
  6. */
  7. package mempool
  8. import (
  9. "sync"
  10. "github.com/tendermint/tendermint/binary"
  11. sm "github.com/tendermint/tendermint/state"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. type Mempool struct {
  15. mtx sync.Mutex
  16. state *sm.State
  17. txs []types.Tx
  18. }
  19. func NewMempool(state *sm.State) *Mempool {
  20. return &Mempool{
  21. state: state,
  22. }
  23. }
  24. func (mem *Mempool) GetState() *sm.State {
  25. return mem.state
  26. }
  27. // Apply tx to the state and remember it.
  28. func (mem *Mempool) AddTx(tx types.Tx) (err error) {
  29. mem.mtx.Lock()
  30. defer mem.mtx.Unlock()
  31. err = mem.state.ExecTx(tx, false)
  32. if err != nil {
  33. log.Debug("AddTx() error", "tx", tx, "error", err)
  34. return err
  35. } else {
  36. log.Debug("AddTx() success", "tx", tx)
  37. mem.txs = append(mem.txs, tx)
  38. return nil
  39. }
  40. }
  41. func (mem *Mempool) GetProposalTxs() []types.Tx {
  42. mem.mtx.Lock()
  43. defer mem.mtx.Unlock()
  44. log.Debug("GetProposalTxs:", "txs", mem.txs)
  45. return mem.txs
  46. }
  47. // "block" is the new block being committed.
  48. // "state" is the result of state.AppendBlock("block").
  49. // Txs that are present in "block" are discarded from mempool.
  50. // Txs that have become invalid in the new "state" are also discarded.
  51. func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) {
  52. mem.mtx.Lock()
  53. defer mem.mtx.Unlock()
  54. mem.state = state.Copy()
  55. // First, create a lookup map of txns in new block.
  56. blockTxsMap := make(map[string]struct{})
  57. for _, tx := range block.Data.Txs {
  58. txHash := binary.BinarySha256(tx)
  59. blockTxsMap[string(txHash)] = struct{}{}
  60. }
  61. // Next, filter all txs from mem.txs that are in blockTxsMap
  62. txs := []types.Tx{}
  63. for _, tx := range mem.txs {
  64. txHash := binary.BinarySha256(tx)
  65. if _, ok := blockTxsMap[string(txHash)]; ok {
  66. log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash)
  67. continue
  68. } else {
  69. log.Debug("Filter in, still new", "tx", tx, "txHash", txHash)
  70. txs = append(txs, tx)
  71. }
  72. }
  73. // Next, filter all txs that aren't valid given new state.
  74. validTxs := []types.Tx{}
  75. for _, tx := range txs {
  76. err := mem.state.ExecTx(tx, false)
  77. if err == nil {
  78. log.Debug("Filter in, valid", "tx", tx)
  79. validTxs = append(validTxs, tx)
  80. } else {
  81. // tx is no longer valid.
  82. log.Debug("Filter out, no longer valid", "tx", tx, "error", err)
  83. }
  84. }
  85. // We're done!
  86. log.Debug("New txs", "txs", validTxs, "oldTxs", mem.txs)
  87. mem.txs = validTxs
  88. }