aboutsummaryrefslogtreecommitdiffstats
path: root/core/transaction_pool.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-01-06 00:10:42 +0800
committerobscuren <geffobscura@gmail.com>2015-01-06 00:10:42 +0800
commit6abf8ef78f1474fdeb7a6a6ce084bf994cc055f2 (patch)
treeb898711590694cfaa6f10dbc2a4c8591232954ef /core/transaction_pool.go
parentb0854fbff5c3d588134f577918a39d08002235dc (diff)
downloadgo-tangerine-6abf8ef78f1474fdeb7a6a6ce084bf994cc055f2.tar.gz
go-tangerine-6abf8ef78f1474fdeb7a6a6ce084bf994cc055f2.tar.zst
go-tangerine-6abf8ef78f1474fdeb7a6a6ce084bf994cc055f2.zip
Merge
Diffstat (limited to 'core/transaction_pool.go')
-rw-r--r--core/transaction_pool.go54
1 files changed, 23 insertions, 31 deletions
diff --git a/core/transaction_pool.go b/core/transaction_pool.go
index fa284e52d..ff6c21aa9 100644
--- a/core/transaction_pool.go
+++ b/core/transaction_pool.go
@@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
- "gopkg.in/fatih/set.v0"
)
var txplogger = logger.NewLogger("TXP")
@@ -38,7 +37,7 @@ type TxPool struct {
quit chan bool
// The actual pool
//pool *list.List
- pool *set.Set
+ txs map[string]*types.Transaction
SecondaryProcessor TxProcessor
@@ -49,21 +48,19 @@ type TxPool struct {
func NewTxPool(eventMux *event.TypeMux) *TxPool {
return &TxPool{
- pool: set.New(),
+ txs: make(map[string]*types.Transaction),
queueChan: make(chan *types.Transaction, txPoolQueueSize),
quit: make(chan bool),
eventMux: eventMux,
}
}
-func (pool *TxPool) addTransaction(tx *types.Transaction) {
- pool.pool.Add(tx)
-
- // Broadcast the transaction to the rest of the peers
- pool.eventMux.Post(TxPreEvent{tx})
-}
-
func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
+ hash := tx.Hash()
+ if pool.txs[string(hash)] != nil {
+ return fmt.Errorf("Known transaction (%x)", hash[0:4])
+ }
+
if len(tx.To()) != 0 && len(tx.To()) != 20 {
return fmt.Errorf("Invalid recipient. len = %d", len(tx.To()))
}
@@ -95,18 +92,17 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
return nil
}
-func (self *TxPool) Add(tx *types.Transaction) error {
- hash := tx.Hash()
- if self.pool.Has(tx) {
- return fmt.Errorf("Known transaction (%x)", hash[0:4])
- }
+func (self *TxPool) addTx(tx *types.Transaction) {
+ self.txs[string(tx.Hash())] = tx
+}
+func (self *TxPool) Add(tx *types.Transaction) error {
err := self.ValidateTransaction(tx)
if err != nil {
return err
}
- self.addTransaction(tx)
+ self.addTx(tx)
var to string
if len(tx.To()) > 0 {
@@ -124,7 +120,7 @@ func (self *TxPool) Add(tx *types.Transaction) error {
}
func (self *TxPool) Size() int {
- return self.pool.Size()
+ return len(self.txs)
}
func (self *TxPool) AddTransactions(txs []*types.Transaction) {
@@ -137,43 +133,39 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) {
}
}
-func (pool *TxPool) GetTransactions() []*types.Transaction {
- txList := make([]*types.Transaction, pool.Size())
+func (self *TxPool) GetTransactions() (txs types.Transactions) {
+ txs = make(types.Transactions, self.Size())
i := 0
- pool.pool.Each(func(v interface{}) bool {
- txList[i] = v.(*types.Transaction)
+ for _, tx := range self.txs {
+ txs[i] = tx
i++
+ }
- return true
- })
-
- return txList
+ return
}
func (pool *TxPool) RemoveInvalid(query StateQuery) {
var removedTxs types.Transactions
- pool.pool.Each(func(v interface{}) bool {
- tx := v.(*types.Transaction)
+ for _, tx := range pool.txs {
sender := query.GetAccount(tx.From())
err := pool.ValidateTransaction(tx)
if err != nil || sender.Nonce >= tx.Nonce() {
removedTxs = append(removedTxs, tx)
}
+ }
- return true
- })
pool.RemoveSet(removedTxs)
}
func (self *TxPool) RemoveSet(txs types.Transactions) {
for _, tx := range txs {
- self.pool.Remove(tx)
+ delete(self.txs, string(tx.Hash()))
}
}
func (pool *TxPool) Flush() []*types.Transaction {
txList := pool.GetTransactions()
- pool.pool.Clear()
+ pool.txs = make(map[string]*types.Transaction)
return txList
}