@@ -155,7 +155,7 @@ type TxPool struct {
155155 gasPrice * big.Int
156156 eventMux * event.TypeMux
157157 events * event.TypeMuxSubscription
158- locals * txSet
158+ locals * accountSet
159159 signer types.Signer
160160 mu sync.RWMutex
161161
@@ -176,12 +176,12 @@ type TxPool struct {
176176func NewTxPool (config TxPoolConfig , chainconfig * params.ChainConfig , eventMux * event.TypeMux , currentStateFn stateFn , gasLimitFn func () * big.Int ) * TxPool {
177177 // Sanitize the input to ensure no vulnerable gas prices are set
178178 config = (& config ).sanitize ()
179-
179+ signer := types . NewEIP155Signer ( chainconfig . ChainId )
180180 // Create the transaction pool with its initial settings
181181 pool := & TxPool {
182182 config : config ,
183183 chainconfig : chainconfig ,
184- signer : types . NewEIP155Signer ( chainconfig . ChainId ) ,
184+ signer : signer ,
185185 pending : make (map [common.Address ]* txList ),
186186 queue : make (map [common.Address ]* txList ),
187187 beats : make (map [common.Address ]time.Time ),
@@ -191,7 +191,7 @@ func NewTxPool(config TxPoolConfig, chainconfig *params.ChainConfig, eventMux *e
191191 gasLimit : gasLimitFn ,
192192 gasPrice : new (big.Int ).SetUint64 (config .PriceLimit ),
193193 pendingState : nil ,
194- locals : newTxSet ( ),
194+ locals : newAccountSet ( signer ),
195195 events : eventMux .Subscribe (ChainHeadEvent {}, RemovedTransactionEvent {}),
196196 quit : make (chan struct {}),
197197 }
@@ -376,13 +376,19 @@ func (pool *TxPool) Pending() (map[common.Address]types.Transactions, error) {
376376func (pool * TxPool ) SetLocal (tx * types.Transaction ) {
377377 pool .mu .Lock ()
378378 defer pool .mu .Unlock ()
379- pool .locals .add (tx . Hash () )
379+ pool .locals .add (tx )
380380}
381381
382382// validateTx checks whether a transaction is valid according
383383// to the consensus rules.
384384func (pool * TxPool ) validateTx (tx * types.Transaction ) error {
385- local := pool .locals .contains (tx .Hash ())
385+
386+ from , err := types .Sender (pool .signer , tx )
387+ if err != nil {
388+ return ErrInvalidSender
389+ }
390+
391+ local := pool .locals .containsAddress (from )
386392 // Drop transactions under our own minimal accepted gas price
387393 if ! local && pool .gasPrice .Cmp (tx .GasPrice ()) > 0 {
388394 return ErrUnderpriced
@@ -393,10 +399,6 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
393399 return err
394400 }
395401
396- from , err := types .Sender (pool .signer , tx )
397- if err != nil {
398- return ErrInvalidSender
399- }
400402 // Last but not least check for nonce errors
401403 if currentState .GetNonce (from ) > tx .Nonce () {
402404 return ErrNonceTooLow
@@ -748,14 +750,8 @@ func (pool *TxPool) promoteExecutables(state *state.StateDB, accounts []common.A
748750 spammers := prque .New ()
749751 for addr , list := range pool .pending {
750752 // Only evict transactions from high rollers
751- if uint64 (list .Len ()) > pool .config .AccountSlots {
752- // Skip local accounts as pools should maintain backlogs for themselves
753- for _ , tx := range list .txs .items {
754- if ! pool .locals .contains (tx .Hash ()) {
755- spammers .Push (addr , float32 (list .Len ()))
756- }
757- break // Checking on transaction for locality is enough
758- }
753+ if ! pool .locals .containsAddress (addr ) && uint64 (list .Len ()) > pool .config .AccountSlots {
754+ spammers .Push (addr , float32 (list .Len ()))
759755 }
760756 }
761757 // Gradually drop transactions from offenders
@@ -929,48 +925,41 @@ func (a addresssByHeartbeat) Len() int { return len(a) }
929925func (a addresssByHeartbeat ) Less (i , j int ) bool { return a [i ].heartbeat .Before (a [j ].heartbeat ) }
930926func (a addresssByHeartbeat ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
931927
932- // txSet represents a set of transaction hashes in which entries
933- // are automatically dropped after txSetDuration time
934- type txSet struct {
935- txMap map [common.Hash ]struct {}
936- txOrd map [uint64 ]txOrdType
937- addPtr , delPtr uint64
928+ // accountSet is simply a map of addresses, and a signer, to be able
929+ // to determine the address from a tx
930+ type accountSet struct {
931+ accounts map [common.Address ]struct {}
932+ signer types.Signer
938933}
939934
940- const txSetDuration = time .Hour * 2
941-
942- // txOrdType represents an entry in the time-ordered list of transaction hashes
943- type txOrdType struct {
944- hash common.Hash
945- time time.Time
935+ func newAccountSet (signer types.Signer ) * accountSet {
936+ return & accountSet {
937+ accounts : make (map [common.Address ]struct {}),
938+ signer : signer ,
939+ }
946940}
947941
948- // newTxSet creates a new transaction set
949- func newTxSet () * txSet {
950- return & txSet {
951- txMap : make (map [common.Hash ]struct {}),
952- txOrd : make (map [uint64 ]txOrdType ),
953- }
942+ // containsAddress checks if a given address is within the set
943+ func (as * accountSet ) containsAddress (address common.Address ) bool {
944+ _ , exist := as .accounts [address ]
945+ return exist
954946}
955947
956- // contains returns true if the set contains the given transaction hash
957- // (not thread safe, should be called from a locked environment)
958- func (ts * txSet ) contains (hash common.Hash ) bool {
959- _ , ok := ts .txMap [hash ]
960- return ok
948+ // contains checks if the sender of a given tx is within the set
949+ func (as * accountSet ) contains (tx * types.Transaction ) bool {
950+ if address , err := types .Sender (as .signer , tx ); err == nil {
951+ return as .containsAddress (address )
952+ }
953+ return false
961954}
962955
963- // add adds a transaction hash to the set, then removes entries older than txSetDuration
964- // (not thread safe, should be called from a locked environment)
965- func (ts * txSet ) add (hash common.Hash ) {
966- ts .txMap [hash ] = struct {}{}
967- now := time .Now ()
968- ts .txOrd [ts .addPtr ] = txOrdType {hash : hash , time : now }
969- ts .addPtr ++
970- delBefore := now .Add (- txSetDuration )
971- for ts .delPtr < ts .addPtr && ts .txOrd [ts .delPtr ].time .Before (delBefore ) {
972- delete (ts .txMap , ts .txOrd [ts .delPtr ].hash )
973- delete (ts .txOrd , ts .delPtr )
974- ts .delPtr ++
956+ // add a transaction sender to the set
957+ // if sender can't be derived, this is a no-op (no errors returned)
958+ func (as * accountSet ) add (tx * types.Transaction ) {
959+ if address , err := types .Sender (as .signer , tx ); err == nil {
960+ if _ , exist := as .accounts [address ]; ! exist {
961+ as .accounts [address ] = struct {}{}
962+ }
975963 }
964+
976965}
0 commit comments