Skip to content

Commit

Permalink
fix: Update transaction validation logic in ethclient
Browse files Browse the repository at this point in the history
- Treat the block as valid if it contains a single sync transaction and the header's txHash is `EmptyTxsHash`.
- Consider a transaction as a sync transaction and skip error handling if either the `from` or `to` address is `zeroAddress`.
  • Loading branch information
Leekyungun committed Feb 4, 2025
1 parent b24237b commit 4ff9604
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,15 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
return nil, errors.New("server returned empty uncle list but block header indicates uncles")
}

if head.TxHash == types.EmptyTxsHash && len(body.Transactions) > 0 {
if !HasOnlySystemTransaction(body) {
// If there is only one transaction in the block and the header's txHash is `EmptyTxsHash`,
// it indicates a sync transaction. No error handling is required in this case.
tx := body.Transactions[0]
zeroAddress := common.HexToAddress("0x0000000000000000000000000000000000000000")
if (tx.From != nil && *tx.From != zeroAddress) || (tx.tx.To() != nil && *tx.tx.To() != zeroAddress) {
return nil, errors.New("server returned non-empty transaction list but block header indicates no transactions")
}
}

if head.TxHash != types.EmptyTxsHash && len(body.Transactions) == 0 {
return nil, errors.New("server returned empty transaction list but block header indicates transactions")
}
Expand Down Expand Up @@ -217,28 +219,6 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface
}), nil
}

// isZeroAddress checks if a given address is the zero address.
func isZeroAddress(address *common.Address) bool {
return address != nil && address.String() == "0x0000000000000000000000000000000000000000"
}

// isSystemTransaction
// A system transaction is identified when:
// - The "from" and "to" addresses in the transaction are both zero addresses.
func isSystemTransaction(tx rpcTransaction) bool {
return isZeroAddress(tx.From) && isZeroAddress(tx.tx.To())
}

// HasOnlySystemTransaction checks if a block contains only system transactions.
func HasOnlySystemTransaction(block rpcBlock) bool {
for _, transaction := range block.Transactions {
if !isSystemTransaction(transaction) {
return false
}
}
return true
}

// HeaderByHash returns the block header with the given hash.
func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
var head *types.Header
Expand Down

0 comments on commit 4ff9604

Please sign in to comment.