light: make transaction pool receiver names consistent (#19000)

This commit is contained in:
Matthew Halpern 2019-02-07 02:53:00 -08:00 committed by Péter Szilágyi
parent 4097ba6a00
commit 7c339ff442

View File

@ -390,81 +390,81 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error
// add validates a new transaction and sets its state pending if processable.
// It also updates the locally stored nonce if necessary.
func (self *TxPool) add(ctx context.Context, tx *types.Transaction) error {
func (pool *TxPool) add(ctx context.Context, tx *types.Transaction) error {
hash := tx.Hash()
if self.pending[hash] != nil {
if pool.pending[hash] != nil {
return fmt.Errorf("Known transaction (%x)", hash[:4])
}
err := self.validateTx(ctx, tx)
err := pool.validateTx(ctx, tx)
if err != nil {
return err
}
if _, ok := self.pending[hash]; !ok {
self.pending[hash] = tx
if _, ok := pool.pending[hash]; !ok {
pool.pending[hash] = tx
nonce := tx.Nonce() + 1
addr, _ := types.Sender(self.signer, tx)
if nonce > self.nonce[addr] {
self.nonce[addr] = nonce
addr, _ := types.Sender(pool.signer, tx)
if nonce > pool.nonce[addr] {
pool.nonce[addr] = nonce
}
// Notify the subscribers. This event is posted in a goroutine
// because it's possible that somewhere during the post "Remove transaction"
// gets called which will then wait for the global tx pool lock and deadlock.
go self.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}})
go pool.txFeed.Send(core.NewTxsEvent{Txs: types.Transactions{tx}})
}
// Print a log message if low enough level is set
log.Debug("Pooled new transaction", "hash", hash, "from", log.Lazy{Fn: func() common.Address { from, _ := types.Sender(self.signer, tx); return from }}, "to", tx.To())
log.Debug("Pooled new transaction", "hash", hash, "from", log.Lazy{Fn: func() common.Address { from, _ := types.Sender(pool.signer, tx); return from }}, "to", tx.To())
return nil
}
// Add adds a transaction to the pool if valid and passes it to the tx relay
// backend
func (self *TxPool) Add(ctx context.Context, tx *types.Transaction) error {
self.mu.Lock()
defer self.mu.Unlock()
func (pool *TxPool) Add(ctx context.Context, tx *types.Transaction) error {
pool.mu.Lock()
defer pool.mu.Unlock()
data, err := rlp.EncodeToBytes(tx)
if err != nil {
return err
}
if err := self.add(ctx, tx); err != nil {
if err := pool.add(ctx, tx); err != nil {
return err
}
//fmt.Println("Send", tx.Hash())
self.relay.Send(types.Transactions{tx})
pool.relay.Send(types.Transactions{tx})
self.chainDb.Put(tx.Hash().Bytes(), data)
pool.chainDb.Put(tx.Hash().Bytes(), data)
return nil
}
// AddTransactions adds all valid transactions to the pool and passes them to
// the tx relay backend
func (self *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) {
self.mu.Lock()
defer self.mu.Unlock()
func (pool *TxPool) AddBatch(ctx context.Context, txs []*types.Transaction) {
pool.mu.Lock()
defer pool.mu.Unlock()
var sendTx types.Transactions
for _, tx := range txs {
if err := self.add(ctx, tx); err == nil {
if err := pool.add(ctx, tx); err == nil {
sendTx = append(sendTx, tx)
}
}
if len(sendTx) > 0 {
self.relay.Send(sendTx)
pool.relay.Send(sendTx)
}
}
// GetTransaction returns a transaction if it is contained in the pool
// and nil otherwise.
func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
func (pool *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
// check the txs first
if tx, ok := tp.pending[hash]; ok {
if tx, ok := pool.pending[hash]; ok {
return tx
}
return nil
@ -472,13 +472,13 @@ func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction {
// GetTransactions returns all currently processable transactions.
// The returned slice may be modified by the caller.
func (self *TxPool) GetTransactions() (txs types.Transactions, err error) {
self.mu.RLock()
defer self.mu.RUnlock()
func (pool *TxPool) GetTransactions() (txs types.Transactions, err error) {
pool.mu.RLock()
defer pool.mu.RUnlock()
txs = make(types.Transactions, len(self.pending))
txs = make(types.Transactions, len(pool.pending))
i := 0
for _, tx := range self.pending {
for _, tx := range pool.pending {
txs[i] = tx
i++
}
@ -487,14 +487,14 @@ func (self *TxPool) GetTransactions() (txs types.Transactions, err error) {
// Content retrieves the data content of the transaction pool, returning all the
// pending as well as queued transactions, grouped by account and nonce.
func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
self.mu.RLock()
defer self.mu.RUnlock()
func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
pool.mu.RLock()
defer pool.mu.RUnlock()
// Retrieve all the pending transactions and sort by account and by nonce
pending := make(map[common.Address]types.Transactions)
for _, tx := range self.pending {
account, _ := types.Sender(self.signer, tx)
for _, tx := range pool.pending {
account, _ := types.Sender(pool.signer, tx)
pending[account] = append(pending[account], tx)
}
// There are no queued transactions in a light pool, just return an empty map
@ -503,20 +503,20 @@ func (self *TxPool) Content() (map[common.Address]types.Transactions, map[common
}
// RemoveTransactions removes all given transactions from the pool.
func (self *TxPool) RemoveTransactions(txs types.Transactions) {
self.mu.Lock()
defer self.mu.Unlock()
func (pool *TxPool) RemoveTransactions(txs types.Transactions) {
pool.mu.Lock()
defer pool.mu.Unlock()
var hashes []common.Hash
batch := self.chainDb.NewBatch()
batch := pool.chainDb.NewBatch()
for _, tx := range txs {
hash := tx.Hash()
delete(self.pending, hash)
delete(pool.pending, hash)
batch.Delete(hash.Bytes())
hashes = append(hashes, hash)
}
batch.Write()
self.relay.Discard(hashes)
pool.relay.Discard(hashes)
}
// RemoveTx removes the transaction with the given hash from the pool.