created new api ContentByAccount in txpool namespace

This commit is contained in:
barryz 2021-05-31 16:37:02 +08:00
parent 8479f28b0b
commit 23cf9324cc
5 changed files with 56 additions and 0 deletions

View file

@ -482,6 +482,27 @@ func (pool *TxPool) Content() (map[common.Address]types.Transactions, map[common
return pending, queued
}
// ContentByAccount retrieves the data content of the transaction pool with a specific account,
// returning the pending as well as queued transaction.
func (pool *TxPool) ContentByAccount(addr common.Address) (pending, queued types.Transactions) {
pool.mu.Lock()
defer pool.mu.Unlock()
list, ok := pool.pending[addr]
if !ok {
return nil, nil
}
pending = list.Flatten()
list, ok = pool.queue[addr]
if !ok {
return nil, nil
}
queued = list.Flatten()
return
}
// Pending retrieves all currently processable transactions, grouped by origin
// account and sorted by nonce. The returned transaction set is a copy and can be
// freely modified by calling code.

View file

@ -263,6 +263,10 @@ func (b *EthAPIBackend) TxPoolContent() (map[common.Address]types.Transactions,
return b.eth.TxPool().Content()
}
func (b *EthAPIBackend) TxPoolContentByAccount(addr common.Address) (pending, queued types.Transactions) {
return b.eth.TxPool().ContentByAccount(addr)
}
func (b *EthAPIBackend) TxPool() *core.TxPool {
return b.eth.TxPool()
}

View file

@ -99,6 +99,31 @@ func NewPublicTxPoolAPI(b Backend) *PublicTxPoolAPI {
return &PublicTxPoolAPI{b}
}
// ContentByAccount returns the transactions contained within the tx pool by given account address.
func (s *PublicTxPoolAPI) ContentByAccount(addr common.Address) map[string]map[string]*RPCTransaction {
// map[(queued|pending)]map[tx.Nonce()]tx
content := map[string]map[string]*RPCTransaction{
"pending": make(map[string]*RPCTransaction),
"queued": make(map[string]*RPCTransaction),
}
pending, queued := s.b.TxPoolContentByAccount(addr)
dump := make(map[string]*RPCTransaction)
for _, tx := range pending {
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx)
}
content["pending"] = dump
dump = make(map[string]*RPCTransaction)
for _, tx := range queued {
dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx)
}
content["queued"] = dump
return content
}
// Content returns the transactions contained within the transaction pool.
func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
content := map[string]map[string]map[string]*RPCTransaction{

View file

@ -76,6 +76,7 @@ type Backend interface {
GetPoolNonce(ctx context.Context, addr common.Address) (uint64, error)
Stats() (pending int, queued int)
TxPoolContent() (map[common.Address]types.Transactions, map[common.Address]types.Transactions)
TxPoolContentByAccount(address common.Address) (pending, queued types.Transactions)
SubscribeNewTxsEvent(chan<- core.NewTxsEvent) event.Subscription
// Filter API

View file

@ -212,6 +212,11 @@ func (b *LesApiBackend) TxPoolContent() (map[common.Address]types.Transactions,
return b.eth.txPool.Content()
}
func (b *LesApiBackend) TxPoolContentByAccount(addr common.Address) (pending, queued types.Transactions) {
// TODO(barryz) not implement for light node.
return nil, nil
}
func (b *LesApiBackend) SubscribeNewTxsEvent(ch chan<- core.NewTxsEvent) event.Subscription {
return b.eth.txPool.SubscribeNewTxsEvent(ch)
}