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.

97 lines
2.4 KiB

10 years ago
  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 takes the recently
  5. modified state (and the associated transactions) and use that as the proposal.
  6. */
  7. package mempool
  8. import (
  9. "sync"
  10. . "github.com/tendermint/tendermint/binary"
  11. . "github.com/tendermint/tendermint/blocks"
  12. "github.com/tendermint/tendermint/state"
  13. )
  14. type Mempool struct {
  15. mtx sync.Mutex
  16. lastBlock *Block
  17. state *state.State
  18. txs []Tx
  19. }
  20. func NewMempool(lastBlock *Block, state *state.State) *Mempool {
  21. return &Mempool{
  22. lastBlock: lastBlock,
  23. state: state,
  24. }
  25. }
  26. // Apply tx to the state and remember it.
  27. func (mem *Mempool) AddTx(tx Tx) (err error) {
  28. mem.mtx.Lock()
  29. defer mem.mtx.Unlock()
  30. err = mem.state.ExecTx(tx)
  31. if err != nil {
  32. return err
  33. } else {
  34. mem.txs = append(mem.txs, tx)
  35. return nil
  36. }
  37. }
  38. // Returns a new block from the current state and associated transactions.
  39. // The block's Validation is empty, and some parts of the header too.
  40. func (mem *Mempool) MakeProposalBlock() (*Block, *state.State) {
  41. mem.mtx.Lock()
  42. defer mem.mtx.Unlock()
  43. nextBlock := mem.lastBlock.MakeNextBlock()
  44. nextBlock.Data.Txs = mem.txs
  45. return nextBlock, mem.state
  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 *Block, state *state.State) {
  52. mem.mtx.Lock()
  53. defer mem.mtx.Unlock()
  54. mem.lastBlock = block
  55. mem.state = state.Copy()
  56. // First, create a lookup map of txns in new block.
  57. blockTxsMap := make(map[string]struct{})
  58. for _, tx := range block.Data.Txs {
  59. txHash := BinaryHash(tx)
  60. blockTxsMap[string(txHash)] = struct{}{}
  61. }
  62. // Next, filter all txs from mem.txs that are in blockTxsMap
  63. txs := []Tx{}
  64. for _, tx := range mem.txs {
  65. txHash := BinaryHash(tx)
  66. if _, ok := blockTxsMap[string(txHash)]; ok {
  67. continue
  68. } else {
  69. txs = append(txs, tx)
  70. }
  71. }
  72. // Next, filter all txs that aren't valid given new state.
  73. validTxs := []Tx{}
  74. for _, tx := range txs {
  75. err := mem.state.ExecTx(tx)
  76. if err != nil {
  77. validTxs = append(validTxs, tx)
  78. } else {
  79. // tx is no longer valid.
  80. }
  81. }
  82. // We're done!
  83. mem.txs = validTxs
  84. }