transaction_pool.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. if len(tx.To()) != 0 && len(tx.To()) != 20 {
  47. return fmt.Errorf("Invalid recipient. len = %d", len(tx.To()))
  48. }
  49. v, _, _ := tx.Curve()
  50. if v > 28 || v < 27 {
  51. return fmt.Errorf("tx.v != (28 || 27) => %v", v)
  52. }
  53. /* XXX this kind of validation needs to happen elsewhere in the gui when sending txs.
  54. Other clients should do their own validation. Value transfer could throw error
  55. but doesn't necessarily invalidate the tx. Gas can still be payed for and miner
  56. can still be rewarded for their inclusion and processing.
  57. // Get the sender
  58. senderAddr := tx.From()
  59. if senderAddr == nil {
  60. return fmt.Errorf("invalid sender")
  61. }
  62. sender := pool.stateQuery.GetAccount(senderAddr)
  63. totAmount := new(big.Int).Set(tx.Value())
  64. // Make sure there's enough in the sender's account. Having insufficient
  65. // funds won't invalidate this transaction but simple ignores it.
  66. if sender.Balance().Cmp(totAmount) < 0 {
  67. return fmt.Errorf("Insufficient amount in sender's (%x) account", tx.From())
  68. }
  69. */
  70. return nil
  71. }
  72. func (self *TxPool) addTx(tx *types.Transaction) {
  73. self.txs[string(tx.Hash())] = tx
  74. }
  75. func (self *TxPool) Add(tx *types.Transaction) error {
  76. if self.txs[string(tx.Hash())] != nil {
  77. return fmt.Errorf("Known transaction (%x)", tx.Hash()[0:4])
  78. }
  79. err := self.ValidateTransaction(tx)
  80. if err != nil {
  81. return err
  82. }
  83. self.addTx(tx)
  84. var to string
  85. if len(tx.To()) > 0 {
  86. to = ethutil.Bytes2Hex(tx.To()[:4])
  87. } else {
  88. to = "[NEW_CONTRACT]"
  89. }
  90. var from string
  91. if len(tx.From()) > 0 {
  92. from = ethutil.Bytes2Hex(tx.From()[:4])
  93. } else {
  94. from = "INVALID"
  95. }
  96. txplogger.Debugf("(t) %x => %s (%v) %x\n", from, to, tx.Value, tx.Hash())
  97. // Notify the subscribers
  98. go self.eventMux.Post(TxPreEvent{tx})
  99. return nil
  100. }
  101. func (self *TxPool) Size() int {
  102. return len(self.txs)
  103. }
  104. func (self *TxPool) AddTransactions(txs []*types.Transaction) {
  105. for _, tx := range txs {
  106. if err := self.Add(tx); err != nil {
  107. txplogger.Infoln(err)
  108. } else {
  109. txplogger.Infof("tx %x\n", tx.Hash()[0:4])
  110. }
  111. }
  112. }
  113. func (self *TxPool) GetTransactions() (txs types.Transactions) {
  114. txs = make(types.Transactions, self.Size())
  115. i := 0
  116. for _, tx := range self.txs {
  117. txs[i] = tx
  118. i++
  119. }
  120. return
  121. }
  122. func (pool *TxPool) RemoveInvalid(query StateQuery) {
  123. var removedTxs types.Transactions
  124. for _, tx := range pool.txs {
  125. sender := query.GetAccount(tx.From())
  126. err := pool.ValidateTransaction(tx)
  127. fmt.Println(err, sender.Nonce, tx.Nonce())
  128. if err != nil || sender.Nonce >= tx.Nonce() {
  129. removedTxs = append(removedTxs, tx)
  130. }
  131. }
  132. pool.RemoveSet(removedTxs)
  133. }
  134. func (self *TxPool) RemoveSet(txs types.Transactions) {
  135. for _, tx := range txs {
  136. delete(self.txs, string(tx.Hash()))
  137. }
  138. }
  139. func (pool *TxPool) Flush() []*types.Transaction {
  140. txList := pool.GetTransactions()
  141. pool.txs = make(map[string]*types.Transaction)
  142. return txList
  143. }
  144. func (pool *TxPool) Start() {
  145. }
  146. func (pool *TxPool) Stop() {
  147. pool.Flush()
  148. txplogger.Infoln("Stopped")
  149. }