Skip to content

Commit

Permalink
eth.Transaction: fix RequiredFields behavior for legacy transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanschneider committed Sep 25, 2023
1 parent 6b65a87 commit f14e3cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 1 addition & 2 deletions eth/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (t *Transaction) RequiredFields() error {
if t.GasPrice == nil {
fields = append(fields, "gasPrice")
}
return nil
case TransactionTypeAccessList:
if t.ChainId == nil {
fields = append(fields, "chainId")
Expand Down Expand Up @@ -160,7 +159,7 @@ func (t *Transaction) RequiredFields() error {
}

if len(fields) > 0 {
return fmt.Errorf("missing required field(s) %s for transaction type", strings.Join(fields, ","))
return fmt.Errorf("missing required field(s) %s for transaction type %d", strings.Join(fields, ","), t.TransactionType())
}

return nil
Expand Down
32 changes: 32 additions & 0 deletions eth/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ func TestTransactionTypeLegacy(t *testing.T) {
RequireEqualJSON(t, []byte(payload), j)
}

func TestTransactionTypeLegacy_RequiredFields(t *testing.T) {
// NOTE: gasPrice is intentionally removed from this payload
payload := `{
"type": "0x0",
"blockHash": "0x2b27fe2bbc8ce01ac7ae8bf74f793a197cf7edbe82727588811fa9a2c4776f81",
"blockNumber": "0x12b1d",
"from": "0x2b371c0262ceab27face32fbb5270ddc6aa01ba4",
"gas": "0x6bdf",
"hash": "0xbddbb685774d8a3df036ed9fb920b48f876090a57e9e90ee60921e0510ef7090",
"input": "0x9c0e3f7a0000000000000000000000000000000000000000000000000000000000000078000000000000000000000000000000000000000000000000000000000000002a",
"nonce": "0x1c",
"to": "0x8e730df7c70d33118d9e5f79ab81aed0be6f6635",
"transactionIndex": "0x2",
"value": "0x0",
"v": "0x1b",
"r": "0x2a98c51c2782f664d3ce571fef0491b48f5ebbc5845fa513192e6e6b24ecdaa1",
"s": "0x29b8e0c67aa9c11327e16556c591dc84a7aac2f6fc57c7f93901be8ee867aebc"
}`

tx := eth.Transaction{}
err := json.Unmarshal([]byte(payload), &tx)
require.NoError(t, err)
require.NotNil(t, tx.Type)
require.Equal(t, eth.TransactionTypeLegacy, tx.Type.Int64())
require.Nil(t, tx.AccessList)

// tx is missing gasPrice
require.Error(t, tx.RequiredFields())
}

func TestTransactionTypeAccessList_Empty(t *testing.T) {
payload := `{
"type": "0x1",
Expand All @@ -91,6 +121,7 @@ func TestTransactionTypeAccessList_Empty(t *testing.T) {
"v": "0x1b",
"r": "0x2a98c51c2782f664d3ce571fef0491b48f5ebbc5845fa513192e6e6b24ecdaa1",
"s": "0x29b8e0c67aa9c11327e16556c591dc84a7aac2f6fc57c7f93901be8ee867aebc",
"chainId": "0x66a",
"accessList": []
}`

Expand Down Expand Up @@ -126,6 +157,7 @@ func TestTransactionTypeAccessList_Populated(t *testing.T) {
"v": "0x1b",
"r": "0x2a98c51c2782f664d3ce571fef0491b48f5ebbc5845fa513192e6e6b24ecdaa1",
"s": "0x29b8e0c67aa9c11327e16556c591dc84a7aac2f6fc57c7f93901be8ee867aebc",
"chainId": "0x66a",
"accessList": [
{ "address": "0x2b371c0262ceab27face32fbb5270ddc6aa01ba4", "storageKeys": ["0x1122334455667788990011223344556677889900112233445566778899001122", "0x0000000000000000000000000000000000000000000000000000000000000000"] },
{ "address": "0x8e730df7c70d33118d9e5f79ab81aed0be6f6635", "storageKeys": [] }
Expand Down

0 comments on commit f14e3cf

Please sign in to comment.