Skip to content

Commit

Permalink
Include 0 values for v,r,s and gasPrice on deposit transactions (ethe…
Browse files Browse the repository at this point in the history
…reum#48)

When serializing deposit transactions for RPC responses, provide 0 values for v, r, s and gasPrice instead of null to improve compatibility with existing parsing code.
  • Loading branch information
ajsutton authored Feb 23, 2023
1 parent 5ff481f commit 2864f90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
29 changes: 7 additions & 22 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,28 +1438,6 @@ type RPCTransaction struct {
func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber uint64, index uint64, baseFee *big.Int, config *params.ChainConfig) *RPCTransaction {
signer := types.MakeSigner(config, new(big.Int).SetUint64(blockNumber))
from, _ := types.Sender(signer, tx)
if tx.Type() == types.DepositTxType {
srcHash := tx.SourceHash()
isSystemTx := tx.IsSystemTx()
result := &RPCTransaction{
Type: hexutil.Uint64(tx.Type()),
From: from,
Gas: hexutil.Uint64(tx.Gas()),
Hash: tx.Hash(),
Input: hexutil.Bytes(tx.Data()),
To: tx.To(),
Value: (*hexutil.Big)(tx.Value()),
Mint: (*hexutil.Big)(tx.Mint()),
SourceHash: &srcHash,
IsSystemTx: &isSystemTx,
}
if blockHash != (common.Hash{}) {
result.BlockHash = &blockHash
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
result.TransactionIndex = (*hexutil.Uint64)(&index)
}
return result
}
v, r, s := tx.RawSignatureValues()
result := &RPCTransaction{
Type: hexutil.Uint64(tx.Type()),
Expand All @@ -1480,7 +1458,14 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
result.TransactionIndex = (*hexutil.Uint64)(&index)
}

switch tx.Type() {
case types.DepositTxType:
srcHash := tx.SourceHash()
isSystemTx := tx.IsSystemTx()
result.SourceHash = &srcHash
result.IsSystemTx = &isSystemTx
result.Mint = (*hexutil.Big)(tx.Mint())
case types.LegacyTxType:
// if a legacy transaction has an EIP-155 chain id, include it explicitly
if id := tx.ChainId(); id.Sign() != 0 {
Expand Down
20 changes: 19 additions & 1 deletion internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ import (
"github.com/stretchr/testify/require"
)

func TestNewRPCTransactionDepositTx(t *testing.T) {
tx := types.NewTx(&types.DepositTx{
SourceHash: common.HexToHash("0x1234"),
IsSystemTransaction: true,
Mint: big.NewInt(34),
})
got := newRPCTransaction(tx, common.Hash{}, uint64(12), uint64(1), big.NewInt(0), &params.ChainConfig{})
// Should provide zero values for unused fields that are required in other transactions
require.Equal(t, got.GasPrice, (*hexutil.Big)(big.NewInt(0)), "newRPCTransaction().GasPrice = %v, want 0x0", got.GasPrice)
require.Equal(t, got.V, (*hexutil.Big)(big.NewInt(0)), "newRPCTransaction().V = %v, want 0x0", got.V)
require.Equal(t, got.R, (*hexutil.Big)(big.NewInt(0)), "newRPCTransaction().R = %v, want 0x0", got.R)
require.Equal(t, got.S, (*hexutil.Big)(big.NewInt(0)), "newRPCTransaction().S = %v, want 0x0", got.S)

// Should include deposit tx specific fields
require.Equal(t, *got.SourceHash, tx.SourceHash(), "newRPCTransaction().SourceHash = %v, want %v", got.SourceHash, tx.SourceHash())
require.Equal(t, *got.IsSystemTx, tx.IsSystemTx(), "newRPCTransaction().IsSystemTx = %v, want %v", got.IsSystemTx, tx.IsSystemTx())
require.Equal(t, got.Mint, (*hexutil.Big)(tx.Mint()), "newRPCTransaction().Mint = %v, want %v", got.Mint, tx.Mint())
}

func TestUnmarshalRpcDepositTx(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -83,7 +102,6 @@ func TestUnmarshalRpcDepositTx(t *testing.T) {
test.modifier(rpcTx)
json, err := json.Marshal(rpcTx)
require.NoError(t, err, "marshalling failed: %w", err)
println(string(json))
parsed := &types.Transaction{}
err = parsed.UnmarshalJSON(json)
if test.valid {
Expand Down

0 comments on commit 2864f90

Please sign in to comment.