mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-23 12:07:17 +00:00
eedabfca85
* txpool_content * txpool_content * txpool_content * txpool_content * Squashed 'interfaces/' content from commit 1e916d013 git-subtree-dir: interfaces git-subtree-split: 1e916d0137aff7bd9e9356cc203cea5f0ea9c7f1 * add test * add test * add test * merge devel * save
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package mock
|
|
|
|
import (
|
|
"sort"
|
|
"sync"
|
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
"github.com/ledgerwatch/erigon/core"
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
"github.com/ledgerwatch/erigon/event"
|
|
)
|
|
|
|
// TestTxPool is a mock transaction pool that blindly accepts all transactions.
|
|
// Its goal is to get around setting up a valid statedb for the balance and nonce
|
|
// checks.
|
|
type TestTxPool struct {
|
|
pool map[common.Hash]types.Transaction // Hash map of collected transactions
|
|
|
|
txFeed event.Feed // Notification feed to allow waiting for inclusion
|
|
lock sync.RWMutex // Protects the transaction pool
|
|
}
|
|
|
|
// NewTestTxPool creates a mock transaction pool.
|
|
func NewTestTxPool() *TestTxPool {
|
|
return &TestTxPool{
|
|
pool: make(map[common.Hash]types.Transaction),
|
|
}
|
|
}
|
|
|
|
// Has returns an indicator whether txpool has a transaction
|
|
// cached with the given hash.
|
|
func (p *TestTxPool) Has(hash common.Hash) bool {
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
|
|
return p.pool[hash] != nil
|
|
}
|
|
|
|
// Get retrieves the transaction from local txpool with given
|
|
// tx hash.
|
|
func (p *TestTxPool) Get(hash common.Hash) types.Transaction {
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
|
|
return p.pool[hash]
|
|
}
|
|
|
|
func (p *TestTxPool) add(txs []types.Transaction) {
|
|
p.lock.Lock()
|
|
defer p.lock.Unlock()
|
|
|
|
for _, tx := range txs {
|
|
p.pool[tx.Hash()] = tx
|
|
}
|
|
p.txFeed.Send(core.NewTxsEvent{Txs: txs})
|
|
}
|
|
|
|
// AddRemotes appends a batch of transactions to the pool, and notifies any
|
|
// listeners if the addition channel is non nil
|
|
func (p *TestTxPool) AddRemotes(txs []types.Transaction) []error {
|
|
p.add(txs)
|
|
return make([]error, len(txs))
|
|
}
|
|
|
|
func (p *TestTxPool) AddLocals(txs []types.Transaction) []error {
|
|
p.add(txs)
|
|
return make([]error, len(txs))
|
|
}
|
|
|
|
// Pending returns all the transactions known to the pool
|
|
func (p *TestTxPool) Pending() (types.TransactionsGroupedBySender, error) {
|
|
p.lock.RLock()
|
|
defer p.lock.RUnlock()
|
|
|
|
batches := make(map[common.Address]types.Transactions)
|
|
for _, tx := range p.pool {
|
|
from, _ := tx.Sender(*types.LatestSignerForChainID(nil))
|
|
batches[from] = append(batches[from], tx)
|
|
}
|
|
groups := types.TransactionsGroupedBySender{}
|
|
for _, batch := range batches {
|
|
sort.Sort(types.TxByNonce(batch))
|
|
groups = append(groups, batch)
|
|
}
|
|
return groups, nil
|
|
}
|
|
|
|
// Content returns all the transactions known to the pool
|
|
func (p *TestTxPool) Content() (map[common.Address]types.Transactions, map[common.Address]types.Transactions) {
|
|
p.lock.RLock()
|
|
defer p.lock.RUnlock()
|
|
|
|
batches := make(map[common.Address]types.Transactions)
|
|
for _, tx := range p.pool {
|
|
from, _ := tx.Sender(*types.LatestSignerForChainID(nil))
|
|
batches[from] = append(batches[from], tx)
|
|
}
|
|
return batches, nil
|
|
}
|
|
|
|
// SubscribeNewTxsEvent should return an event subscription of NewTxsEvent and
|
|
// send events to the given channel.
|
|
func (p *TestTxPool) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
|
|
return p.txFeed.Subscribe(ch)
|
|
}
|