Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TxnType is not returned in JSON-RPC response #1440

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions jsonrpc/eth_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,59 @@ func TestEth_GetNextNonce(t *testing.T) {
}
}

func TestEth_TxnType(t *testing.T) {
flavor-town marked this conversation as resolved.
Show resolved Hide resolved
// Set up the mock accounts
accounts := []struct {
address types.Address
account *Account
}{
{
types.StringToAddress("123"),
&Account{
Nonce: 5,
},
},
}

// Set up the mock store
store := newMockStore()
for _, acc := range accounts {
store.SetAccount(acc.address, acc.account)
}

// Setup Txn
args := &txnArgs{
From: &addr1,
To: &addr2,
Gas: toArgUint64Ptr(21000),
GasPrice: toArgBytesPtr(big.NewInt(10000).Bytes()),
GasTipCap: toArgBytesPtr(big.NewInt(10000).Bytes()),
GasFeeCap: toArgBytesPtr(big.NewInt(10000).Bytes()),
Value: toArgBytesPtr(oneEther.Bytes()),
Data: nil,
Nonce: toArgUint64Ptr(0),
Type: toArgUint64Ptr(uint64(types.DynamicFeeTx)),
}

expectedRes := &types.Transaction{
From: addr1,
To: &addr2,
Gas: 21000,
GasPrice: big.NewInt(10000),
GasTipCap: big.NewInt(10000),
GasFeeCap: big.NewInt(10000),
Value: oneEther,
Input: []byte{},
Nonce: 0,
Type: types.DynamicFeeTx,
}
res, err := DecodeTxn(args, store)

expectedRes.ComputeHash()
assert.NoError(t, err)
assert.Equal(t, expectedRes, res)
}

func newTestEthEndpoint(store testStore) *Eth {
return &Eth{
hclog.NewNullLogger(), store, 100, nil, 0,
Expand Down
6 changes: 6 additions & 0 deletions jsonrpc/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ func DecodeTxn(arg *txnArgs, store nonceGetter) (*types.Transaction, error) {
arg.Gas = argUintPtr(0)
}

txType := types.LegacyTx
if arg.Type != nil {
txType = types.TxType(*arg.Type)
}

txn := &types.Transaction{
From: *arg.From,
Gas: uint64(*arg.Gas),
Expand All @@ -225,6 +230,7 @@ func DecodeTxn(arg *txnArgs, store nonceGetter) (*types.Transaction, error) {
Value: new(big.Int).SetBytes(*arg.Value),
Input: input,
Nonce: uint64(*arg.Nonce),
Type: txType,
}

if arg.To != nil {
Expand Down
3 changes: 2 additions & 1 deletion jsonrpc/testsuite/block-with-txn-bodies.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"from": "0x0300000000000000000000000000000000000000",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": "0x1",
"transactionIndex": "0x2"
"transactionIndex": "0x2",
"type": "0x0"
}
],
"uncles": null,
Expand Down
3 changes: 2 additions & 1 deletion jsonrpc/testsuite/transaction-eip1559.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
"from": "0x0300000000000000000000000000000000000000",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": "0x1",
"transactionIndex": "0x2"
"transactionIndex": "0x2",
"type": "0x2"
}
3 changes: 2 additions & 1 deletion jsonrpc/testsuite/transaction-pending.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"from": "0x0300000000000000000000000000000000000000",
"blockHash": null,
"blockNumber": null,
"transactionIndex": null
"transactionIndex": null,
"type": "0x0"
}
3 changes: 2 additions & 1 deletion jsonrpc/testsuite/transaction-sealed.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"from": "0x0300000000000000000000000000000000000000",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"blockNumber": "0x1",
"transactionIndex": "0x2"
"transactionIndex": "0x2",
"type": "0x0"
}
3 changes: 3 additions & 0 deletions jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type transaction struct {
BlockHash *types.Hash `json:"blockHash"`
BlockNumber *argUint64 `json:"blockNumber"`
TxIndex *argUint64 `json:"transactionIndex"`
Type argUint64 `json:"type"`
}

func (t transaction) getHash() types.Hash { return t.Hash }
Expand Down Expand Up @@ -66,6 +67,7 @@ func toTransaction(
S: argBig(*t.S),
Hash: t.Hash,
From: t.From,
Type: argUint64(t.Type),
}

if t.GasTipCap != nil {
Expand Down Expand Up @@ -331,6 +333,7 @@ type txnArgs struct {
Data *argBytes
Input *argBytes
Nonce *argUint64
Type *argUint64
}

type progression struct {
Expand Down
2 changes: 2 additions & 0 deletions jsonrpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func mockTxn() *transaction {
BlockHash: &types.ZeroHash,
BlockNumber: argUintPtr(1),
TxIndex: argUintPtr(2),
Type: argUint64(types.LegacyTx),
}

return tt
Expand Down Expand Up @@ -256,6 +257,7 @@ func TestTransaction_Encoding(t *testing.T) {
tt := mockTxn()
tt.GasTipCap = &gasTipCap
tt.GasFeeCap = &gasFeeCap
tt.Type = argUint64(types.DynamicFeeTx)

testTransaction("testsuite/transaction-eip1559.json", tt)
})
Expand Down