Skip to content

Commit

Permalink
fix: support accesslist type tx and handle rpc transaction (#73)
Browse files Browse the repository at this point in the history
* feat: json rpc access list type transaction

* fix: test missed argument

* fix: delete test code

* fix: test to cover transation with  none access list

* fix: set yparity null in legacy tx and can be access list in dynamic tx

* fix: handle rpc transaction with tx type

* chore: refactoring

* fix: typo

* fix: remove duplicated code

* small refactor

---------

Co-authored-by: beer-1 <[email protected]>
  • Loading branch information
djm07073 and beer-1 authored Oct 21, 2024
1 parent 729fa81 commit c7da47d
Show file tree
Hide file tree
Showing 25 changed files with 1,158 additions and 209 deletions.
16 changes: 8 additions & 8 deletions app/ibc-hooks/ack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_onAckIcs20Packet_memo(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -87,7 +87,7 @@ func Test_onAckIcs20Packet_memo(t *testing.T) {
// check the contract state
queryInputBz, err := abi.Pack("count")
require.NoError(t, err)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(0).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -102,7 +102,7 @@ func Test_onAckIcs20Packet_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99 if ack is success
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(99).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -114,7 +114,7 @@ func Test_onAckIcs20Packet_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 1 if ack is failed
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(100).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand Down Expand Up @@ -157,7 +157,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -197,7 +197,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
// check the contract state
queryInputBz, err := abi.Pack("count")
require.NoError(t, err)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(0).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -212,7 +212,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99 if ack is success
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(99).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -224,7 +224,7 @@ func Test_onAckPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 1 if ack is failed
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(100).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand Down
18 changes: 9 additions & 9 deletions app/ibc-hooks/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -105,7 +105,7 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, err := input.EVMKeeper.EVMStaticCall(ctx, evmAddr, contractAddr, queryInputBz)
queryRes, err := input.EVMKeeper.EVMStaticCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(1).Bytes32(), [32]byte(queryRes))

Expand All @@ -114,7 +114,7 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
require.NoError(t, err)
queryInputBz, err = input.EVMKeeper.ERC20Keeper().GetERC20ABI().Pack("allowance", common.BytesToAddress(intermediateSender.Bytes()), contractAddr)
require.NoError(t, err)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc20Addr, queryInputBz)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc20Addr, queryInputBz, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(10000).Bytes32(), [32]byte(queryRes))
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func Test_onReceivePacket_memo_ICS721(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -222,7 +222,7 @@ func Test_onReceivePacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(1).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -234,7 +234,7 @@ func Test_onReceivePacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)
queryInputBz, err = input.EVMKeeper.ERC721Keeper().GetERC721ABI().Pack("getApproved", tokenId)
require.NoError(t, err)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc721Addr, queryInputBz)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc721Addr, queryInputBz, nil)
require.NoError(t, err)
require.Equal(t, contractAddr.Bytes(), common.HexToAddress(hexutil.Encode(queryRes)).Bytes())
}
Expand All @@ -248,7 +248,7 @@ func Test_onReceivePacket_memo_ICS721_Wasm(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -316,7 +316,7 @@ func Test_onReceivePacket_memo_ICS721_Wasm(t *testing.T) {
require.NoError(t, err)

// check the contract state
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(1).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -328,7 +328,7 @@ func Test_onReceivePacket_memo_ICS721_Wasm(t *testing.T) {
require.NoError(t, err)
queryInputBz, err = input.EVMKeeper.ERC721Keeper().GetERC721ABI().Pack("getApproved", tokenId)
require.NoError(t, err)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc721Addr, queryInputBz)
queryRes, err = input.EVMKeeper.EVMStaticCall(ctx, evmtypes.StdAddress, erc721Addr, queryInputBz, nil)
require.NoError(t, err)
require.Equal(t, contractAddr.Bytes(), common.HexToAddress(hexutil.Encode(queryRes)).Bytes())
}
12 changes: 6 additions & 6 deletions app/ibc-hooks/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_onTimeoutIcs20Packet_memo(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -81,7 +81,7 @@ func Test_onTimeoutIcs20Packet_memo(t *testing.T) {
// check the contract state
queryInputBz, err := abi.Pack("count")
require.NoError(t, err)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(0).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -96,7 +96,7 @@ func Test_onTimeoutIcs20Packet_memo(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(99).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand Down Expand Up @@ -136,7 +136,7 @@ func Test_onTimeoutPacket_memo_ICS721(t *testing.T) {
codeBz, err := hexutil.Decode(counter.CounterBin)
require.NoError(t, err)

_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil)
_, contractAddr, _, err := input.EVMKeeper.EVMCreate(ctx, evmAddr, codeBz, nil, nil)
require.NoError(t, err)

abi, err := counter.CounterMetaData.GetAbi()
Expand Down Expand Up @@ -169,7 +169,7 @@ func Test_onTimeoutPacket_memo_ICS721(t *testing.T) {
// check the contract state
queryInputBz, err := abi.Pack("count")
require.NoError(t, err)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err := input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(0).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand All @@ -184,7 +184,7 @@ func Test_onTimeoutPacket_memo_ICS721(t *testing.T) {
require.NoError(t, err)

// check the contract state; increased by 99
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil)
queryRes, logs, err = input.EVMKeeper.EVMCall(ctx, evmAddr, contractAddr, queryInputBz, nil, nil)
require.NoError(t, err)
require.Equal(t, uint256.NewInt(99).Bytes32(), [32]byte(queryRes))
require.Empty(t, logs)
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/erc721_transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (suite *KeeperTestSuite) CreateNftClass(
inputBz, err := nftKeeper.ABI.Pack("", name, name)
suite.Require().NoError(err)

_, contractAddr, _, err := evmKeeper.EVMCreate(ctx, createAccountAddr, append(nftKeeper.ERC721Bin, inputBz...), nil)
_, contractAddr, _, err := evmKeeper.EVMCreate(ctx, createAccountAddr, append(nftKeeper.ERC721Bin, inputBz...), nil, nil)
suite.Require().NoError(err)

classId, err := evmtypes.ClassIdFromCollectionAddress(endpoint.Chain.GetContext(), nftKeeper, contractAddr)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (suite *KeeperTestSuite) MintNft(
contractAddr, err := evmtypes.ContractAddressFromClassId(ctx, nftKeeper, classId)
suite.Require().NoError(err)

_, _, err = nftKeeper.EVMCall(ctx, createAccountAddr, contractAddr, inputBz, nil)
_, _, err = nftKeeper.EVMCall(ctx, createAccountAddr, contractAddr, inputBz, nil, nil)
suite.Require().NoError(err)
}

Expand Down
79 changes: 65 additions & 14 deletions jsonrpc/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ func NewRPCTransaction(tx *coretypes.Transaction, blockHash common.Hash, blockNu
from, _ := coretypes.Sender(signer, tx)
v, r, s := tx.RawSignatureValues()
al := tx.AccessList()
yparity := hexutil.Uint64(v.Sign())

result := &RPCTransaction{
Type: hexutil.Uint64(tx.Type()),
From: from,
Expand All @@ -58,27 +56,80 @@ func NewRPCTransaction(tx *coretypes.Transaction, blockHash common.Hash, blockNu
S: (*hexutil.Big)(s),
ChainID: (*hexutil.Big)(chainID),
Accesses: &al,
YParity: &yparity,
}

if blockHash != (common.Hash{}) {
result.BlockHash = &blockHash
result.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
result.TransactionIndex = (*hexutil.Uint64)(&index)
}

switch tx.Type() {
case coretypes.LegacyTxType: // Legacy type returns nil on yparity
default: // Dynamic and Access List type returns yParity
yParityValue := (v.Uint64())
result.YParity = (*hexutil.Uint64)(&yParityValue)
}

return result
}

func (rpcTx RPCTransaction) ToTransaction() *coretypes.Transaction {
return coretypes.NewTx(&coretypes.LegacyTx{
Nonce: uint64(rpcTx.Nonce),
GasPrice: rpcTx.GasPrice.ToInt(),
Gas: uint64(rpcTx.Gas),
To: rpcTx.To,
Value: rpcTx.Value.ToInt(),
Data: rpcTx.Input,
V: rpcTx.V.ToInt(),
R: rpcTx.R.ToInt(),
S: rpcTx.S.ToInt(),
})
gasPrice := func() *big.Int {
if rpcTx.GasPrice != nil {
return rpcTx.GasPrice.ToInt()
}
return big.NewInt(0)
}()
accessList := func() coretypes.AccessList {
if rpcTx.Accesses != nil {
return *rpcTx.Accesses
}
return nil
}()
switch rpcTx.Type {
case coretypes.LegacyTxType:
return coretypes.NewTx(&coretypes.LegacyTx{
Nonce: uint64(rpcTx.Nonce),
GasPrice: gasPrice,
Gas: uint64(rpcTx.Gas),
To: rpcTx.To,
Value: rpcTx.Value.ToInt(),
Data: rpcTx.Input,
V: rpcTx.V.ToInt(),
R: rpcTx.R.ToInt(),
S: rpcTx.S.ToInt(),
})
case coretypes.AccessListTxType:
return coretypes.NewTx(&coretypes.AccessListTx{
ChainID: rpcTx.ChainID.ToInt(),
Nonce: uint64(rpcTx.Nonce),
GasPrice: rpcTx.GasPrice.ToInt(),
Gas: uint64(rpcTx.Gas),
To: rpcTx.To,
Value: rpcTx.Value.ToInt(),
Data: rpcTx.Input,
AccessList: accessList,
V: rpcTx.V.ToInt(),
R: rpcTx.R.ToInt(),
S: rpcTx.S.ToInt(),
})
case coretypes.DynamicFeeTxType:
return coretypes.NewTx((&coretypes.DynamicFeeTx{
ChainID: rpcTx.ChainID.ToInt(),
Nonce: uint64(rpcTx.Nonce),
GasTipCap: rpcTx.GasTipCap.ToInt(),
GasFeeCap: rpcTx.GasFeeCap.ToInt(),
Gas: uint64(rpcTx.Gas),
To: rpcTx.To,
Value: rpcTx.Value.ToInt(),
Data: rpcTx.Input,
AccessList: accessList,
V: rpcTx.V.ToInt(),
R: rpcTx.R.ToInt(),
S: rpcTx.S.ToInt(),
}))
default:
return nil
}
}
Loading

0 comments on commit c7da47d

Please sign in to comment.