transaction_pool.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package core
  2. import (
  3. "fmt"
  4. "github.com/ethereum/go-ethereum/core/types"
  5. "github.com/ethereum/go-ethereum/ethutil"
  6. "github.com/ethereum/go-ethereum/event"
  7. "github.com/ethereum/go-ethereum/logger"
  8. )
  9. var txplogger = logger.NewLogger("TXP")
  10. const txPoolQueueSize = 50
  11. type TxPoolHook chan *types.Transaction
  12. type TxMsg struct {
  13. Tx *types.Transaction
  14. }
  15. const (
  16. minGasPrice = 1000000
  17. )
  18. type TxProcessor interface {
  19. ProcessTransaction(tx *types.Transaction)
  20. }
  21. // The tx pool a thread safe transaction pool handler. In order to
  22. // guarantee a non blocking pool we use a queue channel which can be
  23. // independently read without needing access to the actual pool.
  24. type TxPool struct {
  25. // Queueing channel for reading and writing incoming
  26. // transactions to
  27. queueChan chan *types.Transaction
  28. // Quiting channel
  29. quit chan bool
  30. // The actual pool
  31. //pool *list.List
  32. txs map[string]*types.Transaction
  33. SecondaryProcessor TxProcessor
  34. subscribers []chan TxMsg
  35. eventMux *event.TypeMux
  36. }
  37. func NewTxPool(eventMux *event.TypeMux) *TxPool {
  38. return &TxPool{
  39. txs: make(map[string]*types.Transaction),
  40. queueChan: make(chan *types.Transaction, txPoolQueueSize),
  41. quit: make(chan bool),
  42. eventMux: eventMux,
  43. }
  44. }
  45. func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
  46. hash := tx.Hash()
  47. if pool.txs[string(hash)] != nil {
  48. return fmt.Errorf("Known transaction (%x)", hash[0:4])
  49. }
  50. if len(tx.To()) != 0 && len(tx.To()) != 20 {
  51. return fmt.Errorf("Invalid recipient. len = %d", len(tx.To()))
  52. }
  53. v, _, _ := tx.Curve()
  54. if v > 28 || v < 27 {
  55. return fmt.Errorf("tx.v != (28 || 27) => %v", v)
  56. }
  57. /* XXX this kind of validation needs to happen elsewhere in the gui when sending txs.
  58. Other clients should do their own validation. Value transfer could throw error
  59. but doesn't necessarily invalidate the tx. Gas can still be payed for and miner
  60. can still be rewarded for their inclusion and processing.
  61. // Get the sender
  62. senderAddr := tx.From()
  63. if senderAddr == nil {
  64. return fmt.Errorf("invalid sender")
  65. }
  66. sender := pool.stateQuery.GetAccount(senderAddr)
  67. totAmount := new(big.Int).Set(tx.Value())
  68. // Make sure there's enough in the sender's account. Having insufficient
  69. // funds won't invalidate this transaction but simple ignores it.
  70. if sender.Balance().Cmp(totAmount) < 0 {
  71. return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From())
  72. }
  73. */
  74. return nil
  75. }
  76. func (self *TxPool) addTx(tx *types.Transaction) {
  77. self.txs[string(tx.Hash())] = tx
  78. }
  79. func (self *TxPool) Add(tx *types.Transaction) error {
  80. err := self.ValidateTransaction(tx)
  81. if err != nil {
  82. return err
  83. }
  84. self.addTx(tx)
  85. var to string
  86. if len(tx.To()) > 0 {
  87. to = ethutil.Bytes2Hex(tx.To()[:4])
  88. } else {
  89. to = "[NEW_CONTRACT]"
  90. }
  91. txplogger.Debugf("(t) %x => %s (%v) %x\n", tx.From()[:4], to, tx.Value, tx.Hash())
  92. // Notify the subscribers
  93. go self.eventMux.Post(TxPreEvent{tx})
  94. return nil
  95. }
  96. func (self *TxPool) Size() int {
  97. return len(self.txs)
  98. }
  99. func (self *TxPool) AddTransactions(txs []*types.Transaction) {
  100. for _, tx := range txs {
  101. if err := self.Add(tx); err != nil {
  102. txplogger.Infoln(err)
  103. } else {
  104. txplogger.Infof("tx %x\n", tx.Hash()[0:4])
  105. }
  106. }
  107. }
  108. func (self *TxPool) GetTransactions() (txs types.Transactions) {
  109. txs = make(types.Transactions, self.Size())
  110. i := 0
  111. for _, tx := range self.txs {
  112. txs[i] = tx
  113. i++
  114. }
  115. return
  116. }
  117. func (pool *TxPool) RemoveInvalid(query StateQuery) {
  118. var removedTxs types.Transactions
  119. for _, tx := range pool.txs {
  120. sender := query.GetAccount(tx.From())
  121. err := pool.ValidateTransaction(tx)
  122. if err != nil || sender.Nonce >= tx.Nonce() {
  123. removedTxs = append(removedTxs, tx)
  124. }
  125. }
  126. pool.RemoveSet(removedTxs)
  127. }
  128. func (self *TxPool) RemoveSet(txs types.Transactions) {
  129. for _, tx := range txs {
  130. delete(self.txs, string(tx.Hash()))
  131. }
  132. }
  133. func (pool *TxPool) Flush() []*types.Transaction {
  134. txList := pool.GetTransactions()
  135. pool.txs = make(map[string]*types.Transaction)
  136. return txList
  137. }
  138. func (pool *TxPool) Start() {
  139. }
  140. func (pool *TxPool) Stop() {
  141. pool.Flush()
  142. txplogger.Infoln("Stopped")
  143. }