From cfcf281acb63735b3bc1f3f6a760dd5b5516340e Mon Sep 17 00:00:00 2001 From: dustinxie Date: Tue, 16 Apr 2024 11:03:48 -0700 Subject: [PATCH] [action] remove DecodeRawTx() (#4235) --- .github/CODEOWNERS | 8 ++--- action/rlp_tx.go | 35 -------------------- action/rlp_tx_test.go | 73 ------------------------------------------ api/web3server.go | 29 ++++++----------- api/web3server_test.go | 3 -- 5 files changed, 14 insertions(+), 134 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d798176e37..f96390b8bb 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -11,10 +11,10 @@ /config/ @CoderZhi @dustinxie @Liuhaai @envestcc @millken /p2p/ @CoderZhi @dustinxie @Liuhaai @envestcc /action/ @CoderZhi @dustinxie @Liuhaai @envestcc -/blockchain/ @CoderZhi @dustinxie @Liuhaai -/blockindex/ @CoderZhi @dustinxie @Liuhaai -/crypto/ @CoderZhi @dustinxie @Liuhaai -/db/ @CoderZhi @dustinxie @Liuhaai +/blockchain/ @CoderZhi @dustinxie @Liuhaai @envestcc +/blockindex/ @CoderZhi @dustinxie @Liuhaai @envestcc +/crypto/ @CoderZhi @dustinxie @Liuhaai @envestcc +/db/ @CoderZhi @dustinxie @Liuhaai @envestcc /dispatcher/ @CoderZhi @dustinxie @Liuhaai @envestcc /pkg/ @CoderZhi @dustinxie @Liuhaai /server/ @CoderZhi @dustinxie @Liuhaai diff --git a/action/rlp_tx.go b/action/rlp_tx.go index 4be4aa8c10..08b280438c 100644 --- a/action/rlp_tx.go +++ b/action/rlp_tx.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/rlp" "github.com/iotexproject/go-pkgs/crypto" "github.com/iotexproject/go-pkgs/hash" "github.com/iotexproject/iotex-proto/golang/iotextypes" @@ -45,40 +44,6 @@ func RawTxToSignedTx(rawTx *types.Transaction, signer types.Signer, sig []byte) return signedTx, nil } -// DecodeRawTx decodes raw data string into eth tx -func DecodeRawTx(rawData string, chainID uint32) (tx *types.Transaction, sig []byte, pubkey crypto.PublicKey, err error) { - //remove Hex prefix and decode string to byte - rawData = strings.Replace(rawData, "0x", "", -1) - rawData = strings.Replace(rawData, "0X", "", -1) - var dataInString []byte - dataInString, err = hex.DecodeString(rawData) - if err != nil { - return - } - - // decode raw data into rlp tx - tx = &types.Transaction{} - err = rlp.DecodeBytes(dataInString, tx) - if err != nil { - return - } - - // extract signature and recover pubkey - v, r, s := tx.RawSignatureValues() - recID := uint32(v.Int64()) - 2*chainID - 8 - sig = make([]byte, 65) - rSize := len(r.Bytes()) - copy(sig[32-rSize:32], r.Bytes()) - sSize := len(s.Bytes()) - copy(sig[64-sSize:], s.Bytes()) - sig[64] = byte(recID) - - // recover public key - rawHash := types.NewEIP155Signer(big.NewInt(int64(chainID))).Hash(tx) - pubkey, err = crypto.RecoverPubkey(rawHash[:], sig) - return -} - // NewEthSigner returns the proper signer for Eth-compatible tx func NewEthSigner(txType iotextypes.Encoding, chainID uint32) (types.Signer, error) { switch txType { diff --git a/action/rlp_tx_test.go b/action/rlp_tx_test.go index 646224542b..c0b922418b 100644 --- a/action/rlp_tx_test.go +++ b/action/rlp_tx_test.go @@ -8,7 +8,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/iotexproject/go-pkgs/crypto" "github.com/iotexproject/go-pkgs/hash" "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" @@ -89,68 +88,6 @@ func TestGenerateRlp(t *testing.T) { } } -func TestRlpDecodeVerify(t *testing.T) { - require := require.New(t) - - oldTests := rlpTests[:len(rlpTests)-1] - for _, v := range oldTests { - // decode received RLP tx - tx, sig, pubkey, err := DecodeRawTx(v.raw, _evmNetworkID) - require.NoError(err) - require.EqualValues(types.LegacyTxType, tx.Type()) - require.True(tx.Protected()) - require.EqualValues(v.chainID, tx.ChainId().Uint64()) - require.Equal(v.pubkey, pubkey.HexString()) - require.Equal(v.pkhash, hex.EncodeToString(pubkey.Hash())) - - // convert to our Execution - pb := &iotextypes.Action{ - Encoding: iotextypes.Encoding_ETHEREUM_EIP155, - } - pb.Core = convertToNativeProto(tx, v.actType) - pb.SenderPubKey = pubkey.Bytes() - pb.Signature = sig - - // send on wire - bs, err := proto.Marshal(pb) - require.NoError(err) - - // receive from API - proto.Unmarshal(bs, pb) - selp := &SealedEnvelope{} - require.NoError(selp.loadProto(pb, _evmNetworkID)) - act, ok := selp.Action().(EthCompatibleAction) - require.True(ok) - rlpTx, err := act.ToEthTx(_evmNetworkID) - require.NoError(err) - - // verify against original tx - require.Equal(v.nonce, rlpTx.Nonce()) - require.Equal(v.price, rlpTx.GasPrice().String()) - require.Equal(v.limit, rlpTx.Gas()) - if v.to == "" { - require.Nil(rlpTx.To()) - } else { - require.Equal(v.to, rlpTx.To().Hex()) - } - require.Equal(v.amount, rlpTx.Value().String()) - require.Equal(v.dataLen, len(rlpTx.Data())) - h, err := selp.Hash() - require.NoError(err) - require.Equal(v.hash, hex.EncodeToString(h[:])) - require.Equal(pubkey, selp.SrcPubkey()) - require.True(bytes.Equal(sig, selp.signature)) - raw, err := selp.envelopeHash() - require.NoError(err) - signer, err := NewEthSigner(iotextypes.Encoding_ETHEREUM_RLP, _evmNetworkID) - require.NoError(err) - rawHash := signer.Hash(tx) - require.True(bytes.Equal(rawHash[:], raw[:])) - require.NotEqual(raw, h) - require.NoError(selp.VerifySignature()) - } -} - var ( // deterministic deployment: https://github.com/Arachnid/deterministic-deployment-proxy // see example at https://etherscan.io/tx/0xeddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26 @@ -594,13 +531,3 @@ func TestIssue3944(t *testing.T) { r.Equal("9415", v.String()) // this is the correct V value corresponding to chainID = 4690 r.Equal(hash, tx1.Hash().Hex()) } - -func TestBackwardComp(t *testing.T) { - r := require.New(t) - for _, chainID := range []uint32{_evmNetworkID, _evmNetworkID + 1, 31337, 0} { - _, _, _, err := DecodeRawTx(deterministicDeploymentTx, chainID) - r.Equal(crypto.ErrInvalidKey, err) - _, _, _, err = DecodeRawTx(accessListTx, chainID) - r.ErrorContains(err, "typed transaction too short") - } -} diff --git a/api/web3server.go b/api/web3server.go index a39868c797..fb48d62493 100644 --- a/api/web3server.go +++ b/api/web3server.go @@ -465,25 +465,16 @@ func (svr *web3Handler) sendRawTransaction(in *gjson.Result) (interface{}, error pubkey crypto.PublicKey err error ) - if g := cs.Genesis(); g.IsSumatra(cs.TipHeight()) { - tx, err = action.DecodeEtherTx(dataStr.String()) - if err != nil { - return nil, err - } - if tx.Protected() && tx.ChainId().Uint64() != uint64(cs.EVMNetworkID()) { - return nil, errors.Wrapf(errInvalidEvmChainID, "expect chainID = %d, got %d", cs.EVMNetworkID(), tx.ChainId().Uint64()) - } - encoding, sig, pubkey, err = action.ExtractTypeSigPubkey(tx) - if err != nil { - return nil, err - } - } else { - tx, sig, pubkey, err = action.DecodeRawTx(dataStr.String(), cs.EVMNetworkID()) - if err != nil { - return nil, err - } - // before Sumatra height, all tx are EIP-155 format - encoding = iotextypes.Encoding_ETHEREUM_EIP155 + tx, err = action.DecodeEtherTx(dataStr.String()) + if err != nil { + return nil, err + } + if tx.Protected() && tx.ChainId().Uint64() != uint64(cs.EVMNetworkID()) { + return nil, errors.Wrapf(errInvalidEvmChainID, "expect chainID = %d, got %d", cs.EVMNetworkID(), tx.ChainId().Uint64()) + } + encoding, sig, pubkey, err = action.ExtractTypeSigPubkey(tx) + if err != nil { + return nil, err } elp, err := svr.ethTxToEnvelope(tx) if err != nil { diff --git a/api/web3server_test.go b/api/web3server_test.go index 4327f1a4e9..dde1b5783a 100644 --- a/api/web3server_test.go +++ b/api/web3server_test.go @@ -29,7 +29,6 @@ import ( "github.com/iotexproject/iotex-core/action" apitypes "github.com/iotexproject/iotex-core/api/types" "github.com/iotexproject/iotex-core/blockchain/block" - "github.com/iotexproject/iotex-core/blockchain/genesis" "github.com/iotexproject/iotex-core/test/identityset" "github.com/iotexproject/iotex-core/test/mock/mock_apicoreservice" mock_apitypes "github.com/iotexproject/iotex-core/test/mock/mock_apiresponder" @@ -434,8 +433,6 @@ func TestSendRawTransaction(t *testing.T) { defer ctrl.Finish() core := mock_apicoreservice.NewMockCoreService(ctrl) web3svr := &web3Handler{core, nil, _defaultBatchRequestLimit} - core.EXPECT().Genesis().Return(genesis.Default) - core.EXPECT().TipHeight().Return(uint64(0)) core.EXPECT().EVMNetworkID().Return(uint32(1)) core.EXPECT().ChainID().Return(uint32(1)) core.EXPECT().Account(gomock.Any()).Return(&iotextypes.AccountMeta{IsContract: true}, nil, nil)