internal/ethapi: refactor RPC tx formatter (#33582)
Some checks are pending
/ Linux Build (push) Waiting to run
/ Linux Build (arm) (push) Waiting to run
/ Keeper Build (push) Waiting to run
/ Windows Build (push) Waiting to run
/ Docker Image (push) Waiting to run

This commit is contained in:
0xcharry 2026-01-12 07:31:45 +01:00 committed by GitHub
parent c890637af9
commit 31d5d82ce5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -186,6 +186,15 @@ func NewTxPoolAPI(b Backend) *TxPoolAPI {
return &TxPoolAPI{b} return &TxPoolAPI{b}
} }
// flattenTxs builds the RPC transaction map keyed by nonce for a set of pool txs.
func flattenTxs(txs types.Transactions, header *types.Header, cfg *params.ChainConfig) map[string]*RPCTransaction {
dump := make(map[string]*RPCTransaction, len(txs))
for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, header, cfg)
}
return dump
}
// Content returns the transactions contained within the transaction pool. // Content returns the transactions contained within the transaction pool.
func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction { func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction {
pending, queue := api.b.TxPoolContent() pending, queue := api.b.TxPoolContent()
@ -196,19 +205,11 @@ func (api *TxPoolAPI) Content() map[string]map[string]map[string]*RPCTransaction
curHeader := api.b.CurrentHeader() curHeader := api.b.CurrentHeader()
// Flatten the pending transactions // Flatten the pending transactions
for account, txs := range pending { for account, txs := range pending {
dump := make(map[string]*RPCTransaction, len(txs)) content["pending"][account.Hex()] = flattenTxs(txs, curHeader, api.b.ChainConfig())
for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
}
content["pending"][account.Hex()] = dump
} }
// Flatten the queued transactions // Flatten the queued transactions
for account, txs := range queue { for account, txs := range queue {
dump := make(map[string]*RPCTransaction, len(txs)) content["queued"][account.Hex()] = flattenTxs(txs, curHeader, api.b.ChainConfig())
for _, tx := range txs {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
}
content["queued"][account.Hex()] = dump
} }
return content return content
} }
@ -220,18 +221,10 @@ func (api *TxPoolAPI) ContentFrom(addr common.Address) map[string]map[string]*RP
curHeader := api.b.CurrentHeader() curHeader := api.b.CurrentHeader()
// Build the pending transactions // Build the pending transactions
dump := make(map[string]*RPCTransaction, len(pending)) content["pending"] = flattenTxs(pending, curHeader, api.b.ChainConfig())
for _, tx := range pending {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
}
content["pending"] = dump
// Build the queued transactions // Build the queued transactions
dump = make(map[string]*RPCTransaction, len(queue)) content["queued"] = flattenTxs(queue, curHeader, api.b.ChainConfig())
for _, tx := range queue {
dump[fmt.Sprintf("%d", tx.Nonce())] = NewRPCPendingTransaction(tx, curHeader, api.b.ChainConfig())
}
content["queued"] = dump
return content return content
} }