mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
ethclient/lightclient: pending transaction debug
This commit is contained in:
parent
84f1e31909
commit
37950cad34
3 changed files with 65 additions and 7 deletions
|
|
@ -169,6 +169,20 @@ loop:
|
||||||
}
|
}
|
||||||
testState(common.Address{})
|
testState(common.Address{})
|
||||||
testState(common.HexToAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")) // WETH contract
|
testState(common.HexToAddress("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2")) // WETH contract
|
||||||
|
|
||||||
|
if txs, err := client.RandomPendingTxs(ctx, 5); err == nil {
|
||||||
|
log.Info("Tracking random pending transactions", "count", len(txs))
|
||||||
|
for _, tx := range txs {
|
||||||
|
client.TrackTransaction(tx)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Error("Tracking random pending transactions", "error", err)
|
||||||
|
}
|
||||||
|
if count, err := client.PendingTransactionCount(ctx); err == nil {
|
||||||
|
log.Info("PendingTransactionCount ", "count", count)
|
||||||
|
} else {
|
||||||
|
log.Error("PendingTransactionCount ", "error", err)
|
||||||
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -313,7 +313,7 @@ func (c *Client) PendingNonceAt(ctx context.Context, account common.Address) (ui
|
||||||
if len(pendingTxs) == 0 {
|
if len(pendingTxs) == 0 {
|
||||||
return headNonce, nil
|
return headNonce, nil
|
||||||
}
|
}
|
||||||
return pendingTxs[len(pendingTxs)-1].Nonce(), nil
|
return pendingTxs[len(pendingTxs)-1].Nonce() + 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
|
func (c *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
|
||||||
|
|
@ -325,8 +325,16 @@ func (c *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
|
||||||
countCh := make(chan uint, len(allSenders))
|
countCh := make(chan uint, len(allSenders))
|
||||||
errCh := make(chan error, len(allSenders))
|
errCh := make(chan error, len(allSenders))
|
||||||
for _, sender := range allSenders {
|
for _, sender := range allSenders {
|
||||||
|
sender := sender
|
||||||
go func() {
|
go func() {
|
||||||
_, pendingTxs, err := c.nonceAndPendingTxs(ctx, head, sender)
|
nonce, pendingTxs, err := c.nonceAndPendingTxs(ctx, head, sender)
|
||||||
|
if len(pendingTxs) > 0 {
|
||||||
|
txNonces := make([]uint64, len(pendingTxs))
|
||||||
|
for i, tx := range pendingTxs {
|
||||||
|
txNonces[i] = tx.Nonce()
|
||||||
|
}
|
||||||
|
log.Info("Pending transactions", "sender", sender, "head nonce", nonce, "tx nonces", txNonces)
|
||||||
|
}
|
||||||
errCh <- err
|
errCh <- err
|
||||||
countCh <- uint(len(pendingTxs))
|
countCh <- uint(len(pendingTxs))
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
|
@ -359,10 +360,6 @@ func (c *Client) cacheBlockTxPositions(block *types.Block) {
|
||||||
// sendTransaction sends a transaction to the RPC server and adds it to the set of
|
// sendTransaction sends a transaction to the RPC server and adds it to the set of
|
||||||
// tracked transactions.
|
// tracked transactions.
|
||||||
func (c *Client) sendTransaction(ctx context.Context, tx *types.Transaction) error {
|
func (c *Client) sendTransaction(ctx context.Context, tx *types.Transaction) error {
|
||||||
sender, err := types.Sender(c.signer, tx)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
data, err := tx.MarshalBinary()
|
data, err := tx.MarshalBinary()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -370,6 +367,14 @@ func (c *Client) sendTransaction(ctx context.Context, tx *types.Transaction) err
|
||||||
if err := c.client.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data)); err != nil {
|
if err := c.client.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return c.TrackTransaction(tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) TrackTransaction(tx *types.Transaction) error {
|
||||||
|
sender, err := types.Sender(c.signer, tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
c.trackedTxLock.Lock()
|
c.trackedTxLock.Lock()
|
||||||
c.markTxAsSeen(sender, tx)
|
c.markTxAsSeen(sender, tx)
|
||||||
c.trackedTxLock.Unlock()
|
c.trackedTxLock.Unlock()
|
||||||
|
|
@ -400,10 +405,11 @@ func (c *Client) nonceAndPendingTxs(ctx context.Context, head *btypes.ExecutionH
|
||||||
resultCh := make(chan pendingResult, len(senderTxs))
|
resultCh := make(chan pendingResult, len(senderTxs))
|
||||||
var reqCount int
|
var reqCount int
|
||||||
for txHash, trackedTx := range senderTxs {
|
for txHash, trackedTx := range senderTxs {
|
||||||
if trackedTx.nonce <= proof.Nonce {
|
if trackedTx.nonce < proof.Nonce {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reqCount++
|
reqCount++
|
||||||
|
txHash := txHash
|
||||||
go func() {
|
go func() {
|
||||||
tx, isPending, err := c.getUncachedTxByHash(ctx, txHash, head.BlockNumber())
|
tx, isPending, err := c.getUncachedTxByHash(ctx, txHash, head.BlockNumber())
|
||||||
if err == nil && isPending {
|
if err == nil && isPending {
|
||||||
|
|
@ -480,3 +486,33 @@ func (c *Client) markTxAsSeen(sender common.Address, tx *types.Transaction) {
|
||||||
}
|
}
|
||||||
senderTxs[tx.Hash()] = trackedTx{nonce: tx.Nonce(), lastSeen: c.headCounter}
|
senderTxs[tx.Hash()] = trackedTx{nonce: tx.Nonce(), lastSeen: c.headCounter}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) RandomPendingTxs(ctx context.Context, count int) (types.Transactions, error) {
|
||||||
|
var txc hexutil.Uint
|
||||||
|
err := c.client.CallContext(ctx, &txc, "eth_getBlockTransactionCountByNumber", "pending")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
txCount := int(txc)
|
||||||
|
if txCount == 0 {
|
||||||
|
return nil, errors.New("no pending transactions")
|
||||||
|
}
|
||||||
|
txs := make(types.Transactions, count)
|
||||||
|
for i := range txs {
|
||||||
|
var json *rpcTransaction
|
||||||
|
index := rand.Intn(txCount)
|
||||||
|
err := c.client.CallContext(ctx, &json, "eth_getTransactionByBlockNumberAndIndex", "pending", hexutil.Uint64(index))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if json == nil {
|
||||||
|
return nil, ethereum.NotFound
|
||||||
|
} else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
|
||||||
|
return nil, errors.New("server returned transaction without signature")
|
||||||
|
}
|
||||||
|
if json.From != nil && json.BlockHash != nil {
|
||||||
|
setSenderFromServer(json.tx, *json.From, *json.BlockHash)
|
||||||
|
}
|
||||||
|
txs[i] = json.tx
|
||||||
|
}
|
||||||
|
return txs, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue