77 "github.com/ethereum/go-ethereum/ethutil"
88 "github.com/ethereum/go-ethereum/event"
99 "github.com/ethereum/go-ethereum/logger"
10- "gopkg.in/fatih/set.v0"
1110)
1211
1312var txplogger = logger .NewLogger ("TXP" )
@@ -38,7 +37,7 @@ type TxPool struct {
3837 quit chan bool
3938 // The actual pool
4039 //pool *list.List
41- pool * set. Set
40+ txs map [ string ] * types. Transaction
4241
4342 SecondaryProcessor TxProcessor
4443
@@ -49,21 +48,19 @@ type TxPool struct {
4948
5049func NewTxPool (eventMux * event.TypeMux ) * TxPool {
5150 return & TxPool {
52- pool : set . New ( ),
51+ txs : make ( map [ string ] * types. Transaction ),
5352 queueChan : make (chan * types.Transaction , txPoolQueueSize ),
5453 quit : make (chan bool ),
5554 eventMux : eventMux ,
5655 }
5756}
5857
59- func (pool * TxPool ) addTransaction (tx * types.Transaction ) {
60- pool .pool .Add (tx )
61-
62- // Broadcast the transaction to the rest of the peers
63- pool .eventMux .Post (TxPreEvent {tx })
64- }
65-
6658func (pool * TxPool ) ValidateTransaction (tx * types.Transaction ) error {
59+ hash := tx .Hash ()
60+ if pool .txs [string (hash )] != nil {
61+ return fmt .Errorf ("Known transaction (%x)" , hash [0 :4 ])
62+ }
63+
6764 if len (tx .To ()) != 0 && len (tx .To ()) != 20 {
6865 return fmt .Errorf ("Invalid recipient. len = %d" , len (tx .To ()))
6966 }
@@ -95,18 +92,17 @@ func (pool *TxPool) ValidateTransaction(tx *types.Transaction) error {
9592 return nil
9693}
9794
98- func (self * TxPool ) Add (tx * types.Transaction ) error {
99- hash := tx .Hash ()
100- if self .pool .Has (tx ) {
101- return fmt .Errorf ("Known transaction (%x)" , hash [0 :4 ])
102- }
95+ func (self * TxPool ) addTx (tx * types.Transaction ) {
96+ self .txs [string (tx .Hash ())] = tx
97+ }
10398
99+ func (self * TxPool ) Add (tx * types.Transaction ) error {
104100 err := self .ValidateTransaction (tx )
105101 if err != nil {
106102 return err
107103 }
108104
109- self .addTransaction (tx )
105+ self .addTx (tx )
110106
111107 var to string
112108 if len (tx .To ()) > 0 {
@@ -124,7 +120,7 @@ func (self *TxPool) Add(tx *types.Transaction) error {
124120}
125121
126122func (self * TxPool ) Size () int {
127- return self .pool . Size ( )
123+ return len ( self .txs )
128124}
129125
130126func (self * TxPool ) AddTransactions (txs []* types.Transaction ) {
@@ -137,43 +133,39 @@ func (self *TxPool) AddTransactions(txs []*types.Transaction) {
137133 }
138134}
139135
140- func (pool * TxPool ) GetTransactions () [] * types.Transaction {
141- txList : = make ([] * types.Transaction , pool .Size ())
136+ func (self * TxPool ) GetTransactions () ( txs types.Transactions ) {
137+ txs = make (types.Transactions , self .Size ())
142138 i := 0
143- pool . pool . Each ( func ( v interface {}) bool {
144- txList [i ] = v .( * types. Transaction )
139+ for _ , tx := range self . txs {
140+ txs [i ] = tx
145141 i ++
142+ }
146143
147- return true
148- })
149-
150- return txList
144+ return
151145}
152146
153147func (pool * TxPool ) RemoveInvalid (query StateQuery ) {
154148 var removedTxs types.Transactions
155- pool .pool .Each (func (v interface {}) bool {
156- tx := v .(* types.Transaction )
149+ for _ , tx := range pool .txs {
157150 sender := query .GetAccount (tx .From ())
158151 err := pool .ValidateTransaction (tx )
159152 if err != nil || sender .Nonce >= tx .Nonce () {
160153 removedTxs = append (removedTxs , tx )
161154 }
155+ }
162156
163- return true
164- })
165157 pool .RemoveSet (removedTxs )
166158}
167159
168160func (self * TxPool ) RemoveSet (txs types.Transactions ) {
169161 for _ , tx := range txs {
170- self .pool . Remove (tx )
162+ delete ( self .txs , string (tx . Hash ()) )
171163 }
172164}
173165
174166func (pool * TxPool ) Flush () []* types.Transaction {
175167 txList := pool .GetTransactions ()
176- pool .pool . Clear ( )
168+ pool .txs = make ( map [ string ] * types. Transaction )
177169
178170 return txList
179171}
0 commit comments