From b2fa97c13a2d5b99912fbbaa983b5f615574eeba Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:32:53 +0800 Subject: [PATCH] refactor(crosschain/precompile): split into method and abi --- app/keepers/keepers.go | 23 ++++ contract/crosschain_precompile.go | 8 +- x/crosschain/keeper/bridge_call_out_test.go | 6 +- x/crosschain/keeper/grpc_query_test.go | 4 +- x/crosschain/keeper/keeper_test.go | 63 ++--------- x/crosschain/keeper/keeper_v1_test.go | 47 +------- .../keeper/pending_execute_claim_test.go | 18 +-- x/crosschain/precompile/bridge_call.go | 30 +++-- x/crosschain/precompile/bridge_call_test.go | 40 +++---- x/crosschain/precompile/bridge_coin_amount.go | 26 +++-- .../precompile/bridge_coin_amount_test.go | 6 +- x/crosschain/precompile/contract_test.go | 105 ++++-------------- x/crosschain/precompile/crosschain.go | 36 ++++-- x/crosschain/precompile/crosschain_test.go | 8 +- x/crosschain/precompile/execute_claim.go | 32 ++++-- x/crosschain/precompile/execute_claim_test.go | 10 +- x/crosschain/precompile/has_oracle.go | 26 +++-- x/crosschain/precompile/has_oracle_test.go | 14 +-- x/crosschain/precompile/is_oracle_online.go | 16 ++- .../precompile/is_oracle_online_test.go | 23 ++-- 20 files changed, 239 insertions(+), 302 deletions(-) diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index 0b64973d..4db00781 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -105,6 +105,29 @@ func (c CrosschainKeepers) ToSlice() []crosschainkeeper.Keeper { } } +func (c CrosschainKeepers) GetKeeper(moduleName string) crosschainkeeper.Keeper { + switch moduleName { + case bsctypes.ModuleName: + return c.BscKeeper + case polygontypes.ModuleName: + return c.PolygonKeeper + case trontypes.ModuleName: + return c.TronKeeper + case ethtypes.ModuleName: + return c.EthKeeper + case avalanchetypes.ModuleName: + return c.AvalancheKeeper + case arbitrumtypes.ModuleName: + return c.ArbitrumKeeper + case optimismtypes.ModuleName: + return c.OptimismKeeper + case layer2types.ModuleName: + return c.Layer2Keeper + default: + panic("invalid chain name") + } +} + type AppKeepers struct { // keys to access the substores keys map[string]*storetypes.KVStoreKey diff --git a/contract/crosschain_precompile.go b/contract/crosschain_precompile.go index f25abef1..9832f339 100644 --- a/contract/crosschain_precompile.go +++ b/contract/crosschain_precompile.go @@ -37,21 +37,21 @@ func (k CrosschainPrecompileKeeper) BridgeCoinAmount(ctx context.Context, args B } func (k CrosschainPrecompileKeeper) HasOracle(ctx context.Context, args HasOracleArgs) (bool, error) { - res := struct{ HasOracle bool }{} + res := struct{ Has bool }{} err := k.QueryContract(ctx, common.Address{}, k.contractAddr, k.abi, "hasOracle", &res, args.Chain, args.ExternalAddress) if err != nil { return false, err } - return res.HasOracle, nil + return res.Has, nil } func (k CrosschainPrecompileKeeper) IsOracleOnline(ctx context.Context, args IsOracleOnlineArgs) (bool, error) { - res := struct{ IsOracleOnline bool }{} + res := struct{ Online bool }{} err := k.QueryContract(ctx, common.Address{}, k.contractAddr, k.abi, "isOracleOnline", &res, args.Chain, args.ExternalAddress) if err != nil { return false, err } - return res.IsOracleOnline, nil + return res.Online, nil } func (k CrosschainPrecompileKeeper) BridgeCall(ctx context.Context, from common.Address, args BridgeCallArgs) (*evmtypes.MsgEthereumTxResponse, *big.Int, error) { diff --git a/x/crosschain/keeper/bridge_call_out_test.go b/x/crosschain/keeper/bridge_call_out_test.go index 10ad4a30..b2a392b2 100644 --- a/x/crosschain/keeper/bridge_call_out_test.go +++ b/x/crosschain/keeper/bridge_call_out_test.go @@ -20,12 +20,12 @@ func (s *KeeperMockSuite) TestKeeper_BridgeCallResultHandler() { for _, tt := range tests { s.Run(tt.name, func() { msg := &types.MsgBridgeCallResultClaim{ - ChainName: s.moduleName, + ChainName: s.chainName, BridgerAddress: helpers.GenAccAddress().String(), EventNonce: 1, BlockHeight: 1, Nonce: 1, - TxOrigin: helpers.GenExternalAddr(s.moduleName), + TxOrigin: helpers.GenExternalAddr(s.chainName), Success: true, Cause: "", } @@ -39,7 +39,7 @@ func (s *KeeperMockSuite) TestKeeper_BridgeCallResultHandler() { s.erc20Keeper.EXPECT().DeleteCache(gomock.Any(), gomock.Any()).Times(1) s.crosschainKeeper.SetOutgoingBridgeCall(s.ctx, &types.OutgoingBridgeCall{ - Sender: helpers.GenExternalAddr(s.moduleName), + Sender: helpers.GenExternalAddr(s.chainName), Refund: "", Tokens: nil, To: "", diff --git a/x/crosschain/keeper/grpc_query_test.go b/x/crosschain/keeper/grpc_query_test.go index a3436937..7ec6c91f 100644 --- a/x/crosschain/keeper/grpc_query_test.go +++ b/x/crosschain/keeper/grpc_query_test.go @@ -21,7 +21,7 @@ func (s *KeeperMockSuite) TestQueryServer_BridgeCalls() { s.crosschainKeeper.SetOutgoingBridgeCall(s.ctx, &data1) s.crosschainKeeper.SetOutgoingBridgeCall(s.ctx, &data2) actual, err := s.queryClient.BridgeCalls(s.ctx, &types.QueryBridgeCallsRequest{ - ChainName: s.moduleName, + ChainName: s.chainName, Pagination: &query.PageRequest{ Offset: 0, Limit: 1, @@ -32,7 +32,7 @@ func (s *KeeperMockSuite) TestQueryServer_BridgeCalls() { s.Equal(len(actual.BridgeCalls), 1) actual, err = s.queryClient.BridgeCalls(s.ctx, &types.QueryBridgeCallsRequest{ - ChainName: s.moduleName, + ChainName: s.chainName, Pagination: &query.PageRequest{ Offset: 0, Limit: 2, diff --git a/x/crosschain/keeper/keeper_test.go b/x/crosschain/keeper/keeper_test.go index 13038ae9..538b2884 100644 --- a/x/crosschain/keeper/keeper_test.go +++ b/x/crosschain/keeper/keeper_test.go @@ -17,24 +17,18 @@ import ( "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/testutil" "github.com/functionx/fx-core/v8/testutil/helpers" - arbitrumtypes "github.com/functionx/fx-core/v8/x/arbitrum/types" - avalanchetypes "github.com/functionx/fx-core/v8/x/avalanche/types" - bsctypes "github.com/functionx/fx-core/v8/x/bsc/types" crosschainkeeper "github.com/functionx/fx-core/v8/x/crosschain/keeper" "github.com/functionx/fx-core/v8/x/crosschain/mock" "github.com/functionx/fx-core/v8/x/crosschain/types" ethtypes "github.com/functionx/fx-core/v8/x/eth/types" - layer2types "github.com/functionx/fx-core/v8/x/layer2/types" - optimismtypes "github.com/functionx/fx-core/v8/x/optimism/types" - polygontypes "github.com/functionx/fx-core/v8/x/polygon/types" trontypes "github.com/functionx/fx-core/v8/x/tron/types" ) type KeeperMockSuite struct { suite.Suite - ctx sdk.Context - moduleName string + ctx sdk.Context + chainName string queryClient types.QueryClient // msgServer types.MsgServer @@ -51,30 +45,20 @@ type KeeperMockSuite struct { } func TestKeeperTestSuite(t *testing.T) { - mustTestModule := []string{ + modules := []string{ trontypes.ModuleName, ethtypes.ModuleName, } - subModules := mustTestModule if os.Getenv("TEST_CROSSCHAIN") == "true" { - subModules = append(subModules, []string{ - bsctypes.ModuleName, - polygontypes.ModuleName, - avalanchetypes.ModuleName, - arbitrumtypes.ModuleName, - optimismtypes.ModuleName, - layer2types.ModuleName, - }...) + modules = types.GetSupportChains() } - for _, moduleName := range subModules { - suite.Run(t, &KeeperMockSuite{ - moduleName: moduleName, - }) + for _, moduleName := range modules { + suite.Run(t, &KeeperMockSuite{chainName: moduleName}) } } func (s *KeeperMockSuite) SetupTest() { - key := storetypes.NewKVStoreKey(s.moduleName) + key := storetypes.NewKVStoreKey(s.chainName) testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test")) s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: tmtime.Now()}) @@ -98,11 +82,11 @@ func (s *KeeperMockSuite) SetupTest() { s.accountKeeper = mock.NewMockAccountKeeper(ctrl) s.evmKeeper = mock.NewMockEVMKeeper(ctrl) - s.accountKeeper.EXPECT().GetModuleAddress(s.moduleName).Return(authtypes.NewEmptyModuleAccount(s.moduleName).GetAddress()).Times(1) + s.accountKeeper.EXPECT().GetModuleAddress(s.chainName).Return(authtypes.NewEmptyModuleAccount(s.chainName).GetAddress()).Times(1) s.crosschainKeeper = crosschainkeeper.NewKeeper( myApp.AppCodec(), - s.moduleName, + s.chainName, key, s.stakingKeeper, s.stakingMsgServer, @@ -117,42 +101,15 @@ func (s *KeeperMockSuite) SetupTest() { ) crosschainRouter := crosschainkeeper.NewRouter() - crosschainRouter.AddRoute(s.moduleName, crosschainkeeper.NewModuleHandler(s.crosschainKeeper)) + crosschainRouter.AddRoute(s.chainName, crosschainkeeper.NewModuleHandler(s.crosschainKeeper)) crosschainRouterKeeper := crosschainkeeper.NewRouterKeeper(crosschainRouter) queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, myApp.InterfaceRegistry()) types.RegisterQueryServer(queryHelper, crosschainRouterKeeper) s.queryClient = types.NewQueryClient(queryHelper) // s.msgServer = crosschainkeeper.NewMsgServerRouterImpl(crosschainRouterKeeper) - - params := s.CrosschainParams() - params.EnableSendToExternalPending = true - s.NoError(s.crosschainKeeper.SetParams(s.ctx, ¶ms)) } func (s *KeeperMockSuite) SetupSubTest() { s.SetupTest() } - -func (s *KeeperMockSuite) CrosschainParams() types.Params { - switch s.moduleName { - case ethtypes.ModuleName: - return ethtypes.DefaultGenesisState().Params - case bsctypes.ModuleName: - return bsctypes.DefaultGenesisState().Params - case polygontypes.ModuleName: - return polygontypes.DefaultGenesisState().Params - case trontypes.ModuleName: - return trontypes.DefaultGenesisState().Params - case avalanchetypes.ModuleName: - return avalanchetypes.DefaultGenesisState().Params - case optimismtypes.ModuleName: - return optimismtypes.DefaultGenesisState().Params - case arbitrumtypes.ModuleName: - return arbitrumtypes.DefaultGenesisState().Params - case layer2types.ModuleName: - return layer2types.DefaultGenesisState().Params - default: - panic("module not support") - } -} diff --git a/x/crosschain/keeper/keeper_v1_test.go b/x/crosschain/keeper/keeper_v1_test.go index 68e3404a..900bd405 100644 --- a/x/crosschain/keeper/keeper_v1_test.go +++ b/x/crosschain/keeper/keeper_v1_test.go @@ -18,15 +18,9 @@ import ( "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" - arbitrumtypes "github.com/functionx/fx-core/v8/x/arbitrum/types" - avalanchetypes "github.com/functionx/fx-core/v8/x/avalanche/types" - bsctypes "github.com/functionx/fx-core/v8/x/bsc/types" "github.com/functionx/fx-core/v8/x/crosschain/keeper" "github.com/functionx/fx-core/v8/x/crosschain/types" ethtypes "github.com/functionx/fx-core/v8/x/eth/types" - layer2types "github.com/functionx/fx-core/v8/x/layer2/types" - optimismtypes "github.com/functionx/fx-core/v8/x/optimism/types" - polygontypes "github.com/functionx/fx-core/v8/x/polygon/types" trontypes "github.com/functionx/fx-core/v8/x/tron/types" ) @@ -40,27 +34,15 @@ type KeeperTestSuite struct { } func TestCrosschainKeeperTestSuite(t *testing.T) { - mustTestModule := []string{ + modules := []string{ trontypes.ModuleName, ethtypes.ModuleName, } - - subModules := mustTestModule if os.Getenv("TEST_CROSSCHAIN") == "true" { - subModules = append(subModules, []string{ - bsctypes.ModuleName, - polygontypes.ModuleName, - avalanchetypes.ModuleName, - arbitrumtypes.ModuleName, - optimismtypes.ModuleName, - layer2types.ModuleName, - }...) + modules = types.GetSupportChains() } - - for _, moduleName := range subModules { - suite.Run(t, &KeeperTestSuite{ - chainName: moduleName, - }) + for _, moduleName := range modules { + suite.Run(t, &KeeperTestSuite{chainName: moduleName}) } } @@ -75,26 +57,7 @@ func (suite *KeeperTestSuite) QueryClient() types.QueryClient { } func (suite *KeeperTestSuite) Keeper() keeper.Keeper { - switch suite.chainName { - case bsctypes.ModuleName: - return suite.App.BscKeeper - case polygontypes.ModuleName: - return suite.App.PolygonKeeper - case trontypes.ModuleName: - return suite.App.TronKeeper - case ethtypes.ModuleName: - return suite.App.EthKeeper - case avalanchetypes.ModuleName: - return suite.App.AvalancheKeeper - case arbitrumtypes.ModuleName: - return suite.App.ArbitrumKeeper - case optimismtypes.ModuleName: - return suite.App.OptimismKeeper - case layer2types.ModuleName: - return suite.App.Layer2Keeper - default: - panic("invalid chain name") - } + return suite.App.CrosschainKeepers.GetKeeper(suite.chainName) } func (suite *KeeperTestSuite) SetupTest() { diff --git a/x/crosschain/keeper/pending_execute_claim_test.go b/x/crosschain/keeper/pending_execute_claim_test.go index 94ffb6de..6bd43b4a 100644 --- a/x/crosschain/keeper/pending_execute_claim_test.go +++ b/x/crosschain/keeper/pending_execute_claim_test.go @@ -15,15 +15,15 @@ func (s *KeeperMockSuite) TestKeeper_SavePendingExecuteClaim() { { name: "msg bridge call claim", claim: &types.MsgBridgeCallClaim{ - ChainName: s.moduleName, + ChainName: s.chainName, BridgerAddress: helpers.GenAccAddress().String(), EventNonce: 1, BlockHeight: 100, - Sender: helpers.GenExternalAddr(s.moduleName), - Refund: helpers.GenExternalAddr(s.moduleName), - TokenContracts: []string{helpers.GenExternalAddr(s.moduleName)}, + Sender: helpers.GenExternalAddr(s.chainName), + Refund: helpers.GenExternalAddr(s.chainName), + TokenContracts: []string{helpers.GenExternalAddr(s.chainName)}, Amounts: []sdkmath.Int{sdkmath.NewInt(1)}, - To: helpers.GenExternalAddr(s.moduleName), + To: helpers.GenExternalAddr(s.chainName), Data: "", Value: sdkmath.NewInt(0), Memo: "", @@ -35,13 +35,13 @@ func (s *KeeperMockSuite) TestKeeper_SavePendingExecuteClaim() { claim: &types.MsgSendToFxClaim{ EventNonce: 1, BlockHeight: 100, - TokenContract: helpers.GenExternalAddr(s.moduleName), + TokenContract: helpers.GenExternalAddr(s.chainName), Amount: sdkmath.NewInt(1), - Sender: helpers.GenExternalAddr(s.moduleName), - Receiver: helpers.GenExternalAddr(s.moduleName), + Sender: helpers.GenExternalAddr(s.chainName), + Receiver: helpers.GenExternalAddr(s.chainName), TargetIbc: "", BridgerAddress: helpers.GenAccAddress().String(), - ChainName: s.moduleName, + ChainName: s.chainName, }, }, } diff --git a/x/crosschain/precompile/bridge_call.go b/x/crosschain/precompile/bridge_call.go index 307a9b16..378a099a 100644 --- a/x/crosschain/precompile/bridge_call.go +++ b/x/crosschain/precompile/bridge_call.go @@ -18,15 +18,13 @@ import ( type BridgeCallMethod struct { *Keeper - abi.Method - abi.Event + BridgeCallABI } func NewBridgeCallMethod(keeper *Keeper) *BridgeCallMethod { return &BridgeCallMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["bridgeCall"], - Event: crosschainABI.Events["BridgeCallEvent"], + Keeper: keeper, + BridgeCallABI: NewBridgeCallABI(), } } @@ -102,11 +100,23 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro return result, err } -func (m *BridgeCallMethod) NewBridgeCallEvent(args *fxcontract.BridgeCallArgs, sender, origin common.Address, eventNonce *big.Int) (data []byte, topic []common.Hash, err error) { +type BridgeCallABI struct { + abi.Method + abi.Event +} + +func NewBridgeCallABI() BridgeCallABI { + return BridgeCallABI{ + Method: crosschainABI.Methods["bridgeCall"], + Event: crosschainABI.Events["BridgeCallEvent"], + } +} + +func (m BridgeCallABI) NewBridgeCallEvent(args *fxcontract.BridgeCallArgs, sender, origin common.Address, eventNonce *big.Int) (data []byte, topic []common.Hash, err error) { return evmtypes.PackTopicData(m.Event, []common.Hash{sender.Hash(), args.Refund.Hash(), args.To.Hash()}, origin, args.Value, eventNonce, args.DstChain, args.Tokens, args.Amounts, args.Data, args.Memo) } -func (m *BridgeCallMethod) UnpackInput(data []byte) (*fxcontract.BridgeCallArgs, error) { +func (m BridgeCallABI) UnpackInput(data []byte) (*fxcontract.BridgeCallArgs, error) { args := new(fxcontract.BridgeCallArgs) if err := evmtypes.ParseMethodArgs(m.Method, args, data[4:]); err != nil { return nil, err @@ -114,14 +124,14 @@ func (m *BridgeCallMethod) UnpackInput(data []byte) (*fxcontract.BridgeCallArgs, return args, nil } -func (m *BridgeCallMethod) PackOutput(nonceNonce *big.Int) ([]byte, error) { +func (m BridgeCallABI) PackOutput(nonceNonce *big.Int) ([]byte, error) { return m.Method.Outputs.Pack(nonceNonce) } -func (m *BridgeCallMethod) PackInput(args fxcontract.BridgeCallArgs) ([]byte, error) { +func (m BridgeCallABI) PackInput(args fxcontract.BridgeCallArgs) ([]byte, error) { arguments, err := m.Method.Inputs.Pack(args.DstChain, args.Refund, args.Tokens, args.Amounts, args.To, args.Data, args.Value, args.Memo) if err != nil { return nil, err } - return append(m.GetMethodId(), arguments...), nil + return append(m.Method.ID, arguments...), nil } diff --git a/x/crosschain/precompile/bridge_call_test.go b/x/crosschain/precompile/bridge_call_test.go index 6bc9105c..36157851 100644 --- a/x/crosschain/precompile/bridge_call_test.go +++ b/x/crosschain/precompile/bridge_call_test.go @@ -15,20 +15,20 @@ import ( ) func TestBridgeCallABI(t *testing.T) { - bridgeCall := precompile.NewBridgeCallMethod(nil) + bridgeCallABI := precompile.NewBridgeCallABI() - require.Len(t, bridgeCall.Method.Inputs, 8) - require.Len(t, bridgeCall.Method.Outputs, 1) + require.Len(t, bridgeCallABI.Method.Inputs, 8) + require.Len(t, bridgeCallABI.Method.Outputs, 1) } func TestContract_BridgeCall_Input(t *testing.T) { - bridgeCall := precompile.NewBridgeCallMethod(nil) + bridgeCallABI := precompile.NewBridgeCallABI() - assert.Equal(t, `bridgeCall(string,address,address[],uint256[],address,bytes,uint256,bytes)`, bridgeCall.Method.Sig) - assert.Equal(t, "payable", bridgeCall.Method.StateMutability) - assert.Len(t, bridgeCall.Method.Inputs, 8) + assert.Equal(t, `bridgeCall(string,address,address[],uint256[],address,bytes,uint256,bytes)`, bridgeCallABI.Method.Sig) + assert.Equal(t, "payable", bridgeCallABI.Method.StateMutability) + assert.Len(t, bridgeCallABI.Method.Inputs, 8) - inputs := bridgeCall.Method.Inputs + inputs := bridgeCallABI.Method.Inputs type Args struct { DstChain string Refund common.Address @@ -78,10 +78,10 @@ func TestContract_BridgeCall_Input(t *testing.T) { } func TestContract_BridgeCall_Output(t *testing.T) { - bridgeCall := precompile.NewBridgeCallMethod(nil) - assert.Len(t, bridgeCall.Method.Outputs, 1) + bridgeCallABI := precompile.NewBridgeCallABI() + assert.Len(t, bridgeCallABI.Method.Outputs, 1) - outputs := bridgeCall.Method.Outputs + outputs := bridgeCallABI.Method.Outputs eventNonce := big.NewInt(1) outputData, err := outputs.Pack(eventNonce) require.NoError(t, err) @@ -95,16 +95,16 @@ func TestContract_BridgeCall_Output(t *testing.T) { } func TestContract_BridgeCall_Event(t *testing.T) { - bridgeCall := precompile.NewBridgeCallMethod(nil) + bridgeCallABI := precompile.NewBridgeCallABI() - assert.Equal(t, `BridgeCallEvent(address,address,address,address,uint256,uint256,string,address[],uint256[],bytes,bytes)`, bridgeCall.Event.Sig) - assert.Equal(t, "0x4a9b24da6150ef33e7c41038842b7c94fe89a4fff22dccb2c3fd79f0176062c6", bridgeCall.Event.ID.String()) - assert.Len(t, bridgeCall.Event.Inputs, 11) - assert.Len(t, bridgeCall.Event.Inputs.NonIndexed(), 8) + assert.Equal(t, `BridgeCallEvent(address,address,address,address,uint256,uint256,string,address[],uint256[],bytes,bytes)`, bridgeCallABI.Event.Sig) + assert.Equal(t, "0x4a9b24da6150ef33e7c41038842b7c94fe89a4fff22dccb2c3fd79f0176062c6", bridgeCallABI.Event.ID.String()) + assert.Len(t, bridgeCallABI.Event.Inputs, 11) + assert.Len(t, bridgeCallABI.Event.Inputs.NonIndexed(), 8) for i := 0; i < 3; i++ { - assert.True(t, bridgeCall.Event.Inputs[i].Indexed) + assert.True(t, bridgeCallABI.Event.Inputs[i].Indexed) } - inputs := bridgeCall.Event.Inputs + inputs := bridgeCallABI.Event.Inputs args := contract.ICrosschainBridgeCallEvent{ TxOrigin: helpers.GenHexAddress(), @@ -144,7 +144,7 @@ func TestContract_BridgeCall_Event(t *testing.T) { } func TestContract_BridgeCall_NewBridgeCallEvent(t *testing.T) { - bridgeCall := precompile.NewBridgeCallMethod(nil) + bridgeCallABI := precompile.NewBridgeCallABI() sender := common.BytesToAddress([]byte{0x1}) origin := common.BytesToAddress([]byte{0x2}) @@ -159,7 +159,7 @@ func TestContract_BridgeCall_NewBridgeCallEvent(t *testing.T) { Value: big.NewInt(100), Memo: []byte{0x1, 0x2, 0x3}, } - dataNew, topicNew, err := bridgeCall.NewBridgeCallEvent(args, sender, origin, nonce) + dataNew, topicNew, err := bridgeCallABI.NewBridgeCallEvent(args, sender, origin, nonce) require.NoError(t, err) expectData := "0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000240000000000000000000000000000000000000000000000000000000000000000365746800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000007b00000000000000000000000000000000000000000000000000000000000001c80000000000000000000000000000000000000000000000000000000000000003010203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030102030000000000000000000000000000000000000000000000000000000000" require.EqualValues(t, expectData, hex.EncodeToString(dataNew)) diff --git a/x/crosschain/precompile/bridge_coin_amount.go b/x/crosschain/precompile/bridge_coin_amount.go index f74a5354..d9e4c365 100644 --- a/x/crosschain/precompile/bridge_coin_amount.go +++ b/x/crosschain/precompile/bridge_coin_amount.go @@ -15,13 +15,13 @@ import ( type BridgeCoinAmountMethod struct { *Keeper - abi.Method + BridgeCoinAmountABI } func NewBridgeCoinAmountMethod(keeper *Keeper) *BridgeCoinAmountMethod { return &BridgeCoinAmountMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["bridgeCoinAmount"], + Keeper: keeper, + BridgeCoinAmountABI: NewBridgeCoinAmountABI(), } } @@ -57,15 +57,25 @@ func (m *BridgeCoinAmountMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte return m.PackOutput(totalSupply.Amount.BigInt()) } -func (m *BridgeCoinAmountMethod) PackInput(args fxcontract.BridgeCoinAmountArgs) ([]byte, error) { +type BridgeCoinAmountABI struct { + abi.Method +} + +func NewBridgeCoinAmountABI() BridgeCoinAmountABI { + return BridgeCoinAmountABI{ + Method: crosschainABI.Methods["bridgeCoinAmount"], + } +} + +func (m BridgeCoinAmountABI) PackInput(args fxcontract.BridgeCoinAmountArgs) ([]byte, error) { arguments, err := m.Method.Inputs.Pack(args.Token, args.Target) if err != nil { return nil, err } - return append(m.GetMethodId(), arguments...), nil + return append(m.Method.ID, arguments...), nil } -func (m *BridgeCoinAmountMethod) UnpackInput(data []byte) (*fxcontract.BridgeCoinAmountArgs, error) { +func (m BridgeCoinAmountABI) UnpackInput(data []byte) (*fxcontract.BridgeCoinAmountArgs, error) { args := new(fxcontract.BridgeCoinAmountArgs) if err := evmtypes.ParseMethodArgs(m.Method, args, data[4:]); err != nil { return nil, err @@ -73,11 +83,11 @@ func (m *BridgeCoinAmountMethod) UnpackInput(data []byte) (*fxcontract.BridgeCoi return args, nil } -func (m *BridgeCoinAmountMethod) PackOutput(amount *big.Int) ([]byte, error) { +func (m BridgeCoinAmountABI) PackOutput(amount *big.Int) ([]byte, error) { return m.Method.Outputs.Pack(amount) } -func (m *BridgeCoinAmountMethod) UnpackOutput(data []byte) (*big.Int, error) { +func (m BridgeCoinAmountABI) UnpackOutput(data []byte) (*big.Int, error) { amount, err := m.Method.Outputs.Unpack(data) if err != nil { return nil, err diff --git a/x/crosschain/precompile/bridge_coin_amount_test.go b/x/crosschain/precompile/bridge_coin_amount_test.go index 5b4f9cfd..4bb67c9b 100644 --- a/x/crosschain/precompile/bridge_coin_amount_test.go +++ b/x/crosschain/precompile/bridge_coin_amount_test.go @@ -9,7 +9,7 @@ import ( ) func TestBridgeCoinAmountMethod_ABI(t *testing.T) { - bridgeCoinAmount := precompile.NewBridgeCoinAmountMethod(nil).Method - assert.Len(t, bridgeCoinAmount.Inputs, 2) - assert.Len(t, bridgeCoinAmount.Outputs, 1) + bridgeCoinAmountABI := precompile.NewBridgeCoinAmountABI() + assert.Len(t, bridgeCoinAmountABI.Inputs, 2) + assert.Len(t, bridgeCoinAmountABI.Outputs, 1) } diff --git a/x/crosschain/precompile/contract_test.go b/x/crosschain/precompile/contract_test.go index 3a99c1e1..bd0f517b 100644 --- a/x/crosschain/precompile/contract_test.go +++ b/x/crosschain/precompile/contract_test.go @@ -1,10 +1,7 @@ package precompile_test import ( - "reflect" - "strings" "testing" - "time" sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" @@ -17,7 +14,6 @@ import ( testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" - crosschainkeeper "github.com/functionx/fx-core/v8/x/crosschain/keeper" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" ) @@ -26,6 +22,7 @@ type CrosschainPrecompileTestSuite struct { signer *helpers.Signer crosschainAddr common.Address + chainName string helpers.CrosschainPrecompileSuite } @@ -54,93 +51,29 @@ func (suite *CrosschainPrecompileTestSuite) SetupTest() { } suite.CrosschainPrecompileSuite = helpers.NewCrosschainPrecompileSuite(suite.Require(), suite.signer, suite.App.EvmKeeper, suite.crosschainAddr) + + chainNames := crosschaintypes.GetSupportChains() + suite.chainName = chainNames[tmrand.Intn(len(chainNames))] } func (suite *CrosschainPrecompileTestSuite) SetupSubTest() { suite.SetupTest() } -func (suite *CrosschainPrecompileTestSuite) Commit() { - header := suite.Ctx.BlockHeader() - _, err := suite.App.EndBlocker(suite.Ctx) - suite.Require().NoError(err) - _, err = suite.App.Commit() - suite.Require().NoError(err) - // after commit Ctx header - header.Height++ - - // begin block - header.Time = time.Now().UTC() - header.Height++ - suite.Ctx = suite.Ctx.WithBlockHeader(header) - _, err = suite.App.BeginBlocker(suite.Ctx) - suite.Require().NoError(err) - suite.Ctx = suite.Ctx.WithBlockHeight(header.Height) -} - -func (suite *CrosschainPrecompileTestSuite) CrosschainKeepers() map[string]crosschainkeeper.Keeper { - value := reflect.ValueOf(suite.App.CrosschainKeepers) - keepers := make(map[string]crosschainkeeper.Keeper) - for i := 0; i < value.NumField(); i++ { - res := value.Field(i).MethodByName("GetGravityID").Call([]reflect.Value{reflect.ValueOf(suite.Ctx)}) - gravityID := res[0].String() - chainName := strings.TrimSuffix(strings.TrimPrefix(gravityID, "fx-"), "-bridge") - if chainName == "bridge-eth" { - keepers["eth"] = value.Field(i).Interface().(crosschainkeeper.Keeper) - } else { - keepers[chainName] = value.Field(i).Interface().(crosschainkeeper.Keeper) - } +func (suite *CrosschainPrecompileTestSuite) SetOracle(online bool) crosschaintypes.Oracle { + oracle := crosschaintypes.Oracle{ + OracleAddress: helpers.GenAccAddress().String(), + BridgerAddress: helpers.GenAccAddress().String(), + ExternalAddress: helpers.GenExternalAddr(suite.chainName), + DelegateAmount: sdkmath.NewInt(1e18).MulRaw(1000), + StartHeight: 1, + Online: online, + DelegateValidator: sdk.ValAddress(helpers.GenAccAddress()).String(), + SlashTimes: 0, } - return keepers -} - -func (suite *CrosschainPrecompileTestSuite) GenerateModuleName() string { - keepers := suite.CrosschainKeepers() - modules := make([]string, 0, len(keepers)) - for m := range keepers { - modules = append(modules, m) - } - if len(modules) == 0 { - return "" - } - return modules[tmrand.Intn(len(modules))] -} - -func (suite *CrosschainPrecompileTestSuite) GenerateOracles(moduleName string, online bool, num int) []Oracle { - keeper := suite.CrosschainKeepers()[moduleName] - oracles := make([]Oracle, 0, num) - for i := 0; i < num; i++ { - oracle := crosschaintypes.Oracle{ - OracleAddress: helpers.GenAccAddress().String(), - BridgerAddress: helpers.GenAccAddress().String(), - ExternalAddress: helpers.GenExternalAddr(moduleName), - DelegateAmount: sdkmath.NewInt(1e18).MulRaw(1000), - StartHeight: 1, - Online: online, - DelegateValidator: sdk.ValAddress(helpers.GenAccAddress()).String(), - SlashTimes: 0, - } - keeper.SetOracle(suite.Ctx, oracle) - keeper.SetOracleAddrByExternalAddr(suite.Ctx, oracle.ExternalAddress, oracle.GetOracle()) - keeper.SetOracleAddrByBridgerAddr(suite.Ctx, oracle.GetBridger(), oracle.GetOracle()) - oracles = append(oracles, Oracle{ - moduleName: moduleName, - oracle: oracle, - }) - } - return oracles -} - -func (suite *CrosschainPrecompileTestSuite) GenerateRandOracle(moduleName string, online bool) Oracle { - oracles := suite.GenerateOracles(moduleName, online, 1) - return oracles[0] -} - -type Oracle struct { - moduleName string - oracle crosschaintypes.Oracle -} - -func (o Oracle) GetExternalHexAddr() common.Address { - return crosschaintypes.ExternalAddrToHexAddr(o.moduleName, o.oracle.ExternalAddress) + keeper := suite.App.CrosschainKeepers.GetKeeper(suite.chainName) + keeper.SetOracle(suite.Ctx, oracle) + keeper.SetOracleAddrByExternalAddr(suite.Ctx, oracle.ExternalAddress, oracle.GetOracle()) + keeper.SetOracleAddrByBridgerAddr(suite.Ctx, oracle.GetBridger(), oracle.GetOracle()) + return oracle } diff --git a/x/crosschain/precompile/crosschain.go b/x/crosschain/precompile/crosschain.go index 47b9a790..59a189f1 100644 --- a/x/crosschain/precompile/crosschain.go +++ b/x/crosschain/precompile/crosschain.go @@ -16,19 +16,17 @@ import ( evmtypes "github.com/functionx/fx-core/v8/x/evm/types" ) -// Deprecated: please use BridgeCallMethod +// Deprecated: After the upgrade to v8 type CrosschainMethod struct { *Keeper - abi.Method - abi.Event + CrosschainABI } -// Deprecated: please use BridgeCallMethod +// Deprecated: After the upgrade to v8 func NewCrosschainMethod(keeper *Keeper) *CrosschainMethod { return &CrosschainMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["crossChain"], - Event: crosschainABI.Events["CrossChain"], + Keeper: keeper, + CrosschainABI: NewCrosschainABI(), } } @@ -111,11 +109,25 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro return m.PackOutput(true) } -func (m *CrosschainMethod) NewCrosschainEvent(sender, token common.Address, denom, receipt string, amount, fee *big.Int, target [32]byte, memo string) (data []byte, topic []common.Hash, err error) { +// Deprecated: After the upgrade to v8 +type CrosschainABI struct { + abi.Method + abi.Event +} + +// Deprecated: After the upgrade to v8 +func NewCrosschainABI() CrosschainABI { + return CrosschainABI{ + Method: crosschainABI.Methods["crossChain"], + Event: crosschainABI.Events["CrossChain"], + } +} + +func (m CrosschainABI) NewCrosschainEvent(sender, token common.Address, denom, receipt string, amount, fee *big.Int, target [32]byte, memo string) (data []byte, topic []common.Hash, err error) { return evmtypes.PackTopicData(m.Event, []common.Hash{sender.Hash(), token.Hash()}, denom, receipt, amount, fee, target, memo) } -func (m *CrosschainMethod) UnpackInput(data []byte) (*fxcontract.CrosschainArgs, error) { +func (m CrosschainABI) UnpackInput(data []byte) (*fxcontract.CrosschainArgs, error) { args := new(fxcontract.CrosschainArgs) if err := evmtypes.ParseMethodArgs(m.Method, args, data[4:]); err != nil { return nil, err @@ -123,14 +135,14 @@ func (m *CrosschainMethod) UnpackInput(data []byte) (*fxcontract.CrosschainArgs, return args, nil } -func (m *CrosschainMethod) PackInput(args fxcontract.CrosschainArgs) ([]byte, error) { +func (m CrosschainABI) PackInput(args fxcontract.CrosschainArgs) ([]byte, error) { data, err := m.Method.Inputs.Pack(args.Token, args.Receipt, args.Amount, args.Fee, args.Target, args.Memo) if err != nil { return nil, err } - return append(m.GetMethodId(), data...), nil + return append(m.Method.ID, data...), nil } -func (m *CrosschainMethod) PackOutput(success bool) ([]byte, error) { +func (m CrosschainABI) PackOutput(success bool) ([]byte, error) { return m.Method.Outputs.Pack(success) } diff --git a/x/crosschain/precompile/crosschain_test.go b/x/crosschain/precompile/crosschain_test.go index d0d5147d..9da7e49a 100644 --- a/x/crosschain/precompile/crosschain_test.go +++ b/x/crosschain/precompile/crosschain_test.go @@ -9,10 +9,10 @@ import ( ) func TestCrosschainABI(t *testing.T) { - crosschain := precompile.NewCrosschainMethod(nil) + crosschainABI := precompile.NewCrosschainABI() - require.Len(t, crosschain.Method.Inputs, 6) - require.Len(t, crosschain.Method.Outputs, 1) + require.Len(t, crosschainABI.Method.Inputs, 6) + require.Len(t, crosschainABI.Method.Outputs, 1) - require.Len(t, crosschain.Event.Inputs, 8) + require.Len(t, crosschainABI.Event.Inputs, 8) } diff --git a/x/crosschain/precompile/execute_claim.go b/x/crosschain/precompile/execute_claim.go index d5fa3383..0fd4de67 100644 --- a/x/crosschain/precompile/execute_claim.go +++ b/x/crosschain/precompile/execute_claim.go @@ -17,15 +17,13 @@ var _ fxcontract.PrecompileMethod = (*ExecuteClaimMethod)(nil) type ExecuteClaimMethod struct { *Keeper - abi.Method - abi.Event + ExecuteClaimABI } func NewExecuteClaimMethod(keeper *Keeper) *ExecuteClaimMethod { return &ExecuteClaimMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["executeClaim"], - Event: crosschainABI.Events["ExecuteClaimEvent"], + Keeper: keeper, + ExecuteClaimABI: NewExecuteClaimABI(), } } @@ -73,19 +71,31 @@ func (m *ExecuteClaimMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er return m.PackOutput(true) } -func (m *ExecuteClaimMethod) NewExecuteClaimEvent(sender common.Address, eventNonce *big.Int, dstChain string) (data []byte, topic []common.Hash, err error) { +type ExecuteClaimABI struct { + abi.Method + abi.Event +} + +func NewExecuteClaimABI() ExecuteClaimABI { + return ExecuteClaimABI{ + Method: crosschainABI.Methods["executeClaim"], + Event: crosschainABI.Events["ExecuteClaimEvent"], + } +} + +func (m ExecuteClaimABI) NewExecuteClaimEvent(sender common.Address, eventNonce *big.Int, dstChain string) (data []byte, topic []common.Hash, err error) { return evmtypes.PackTopicData(m.Event, []common.Hash{sender.Hash()}, eventNonce, dstChain) } -func (m *ExecuteClaimMethod) PackInput(args fxcontract.ExecuteClaimArgs) ([]byte, error) { +func (m ExecuteClaimABI) PackInput(args fxcontract.ExecuteClaimArgs) ([]byte, error) { arguments, err := m.Method.Inputs.Pack(args.Chain, args.EventNonce) if err != nil { return nil, err } - return append(m.GetMethodId(), arguments...), nil + return append(m.Method.ID, arguments...), nil } -func (m *ExecuteClaimMethod) UnpackInput(data []byte) (*fxcontract.ExecuteClaimArgs, error) { +func (m ExecuteClaimABI) UnpackInput(data []byte) (*fxcontract.ExecuteClaimArgs, error) { args := new(fxcontract.ExecuteClaimArgs) if err := evmtypes.ParseMethodArgs(m.Method, args, data[4:]); err != nil { return nil, err @@ -93,11 +103,11 @@ func (m *ExecuteClaimMethod) UnpackInput(data []byte) (*fxcontract.ExecuteClaimA return args, nil } -func (m *ExecuteClaimMethod) PackOutput(success bool) ([]byte, error) { +func (m ExecuteClaimABI) PackOutput(success bool) ([]byte, error) { return m.Method.Outputs.Pack(success) } -func (m *ExecuteClaimMethod) UnpackOutput(data []byte) (bool, error) { +func (m ExecuteClaimABI) UnpackOutput(data []byte) (bool, error) { success, err := m.Method.Outputs.Unpack(data) if err != nil { return false, err diff --git a/x/crosschain/precompile/execute_claim_test.go b/x/crosschain/precompile/execute_claim_test.go index f22b5130..66128f7b 100644 --- a/x/crosschain/precompile/execute_claim_test.go +++ b/x/crosschain/precompile/execute_claim_test.go @@ -14,18 +14,18 @@ import ( ) func TestExecuteClaimMethod_ABI(t *testing.T) { - executeClaim := precompile.NewExecuteClaimMethod(nil) + executeClaimABI := precompile.NewExecuteClaimABI() methodStr := `function executeClaim(string _chain, uint256 _eventNonce) returns(bool _result)` - assert.Equal(t, methodStr, executeClaim.Method.String()) + assert.Equal(t, methodStr, executeClaimABI.Method.String()) eventStr := `event ExecuteClaimEvent(address indexed _sender, uint256 _eventNonce, string _chain)` - assert.Equal(t, eventStr, executeClaim.Event.String()) + assert.Equal(t, eventStr, executeClaimABI.Event.String()) } func TestExecuteClaimMethod_PackInput(t *testing.T) { - executeClaim := precompile.NewExecuteClaimMethod(nil) - input, err := executeClaim.PackInput(contract.ExecuteClaimArgs{ + executeClaimABI := precompile.NewExecuteClaimABI() + input, err := executeClaimABI.PackInput(contract.ExecuteClaimArgs{ Chain: ethtypes.ModuleName, EventNonce: big.NewInt(1), }) diff --git a/x/crosschain/precompile/has_oracle.go b/x/crosschain/precompile/has_oracle.go index 6042467d..61ee9590 100644 --- a/x/crosschain/precompile/has_oracle.go +++ b/x/crosschain/precompile/has_oracle.go @@ -13,13 +13,13 @@ import ( type HasOracleMethod struct { *Keeper - abi.Method + HasOracleABI } func NewHasOracleMethod(keeper *Keeper) *HasOracleMethod { return &HasOracleMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["hasOracle"], + Keeper: keeper, + HasOracleABI: NewHasOracleABI(), } } @@ -50,25 +50,35 @@ func (m *HasOracleMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error return m.PackOutput(hasOracle) } -func (m *HasOracleMethod) PackInput(args contract.HasOracleArgs) ([]byte, error) { +type HasOracleABI struct { + abi.Method +} + +func NewHasOracleABI() HasOracleABI { + return HasOracleABI{ + Method: crosschainABI.Methods["hasOracle"], + } +} + +func (m HasOracleABI) PackInput(args contract.HasOracleArgs) ([]byte, error) { arguments, err := m.Method.Inputs.Pack(args.Chain, args.ExternalAddress) if err != nil { return nil, err } - return append(m.GetMethodId(), arguments...), nil + return append(m.Method.ID, arguments...), nil } -func (m *HasOracleMethod) UnpackInput(data []byte) (*contract.HasOracleArgs, error) { +func (m HasOracleABI) UnpackInput(data []byte) (*contract.HasOracleArgs, error) { args := new(contract.HasOracleArgs) err := types.ParseMethodArgs(m.Method, args, data[4:]) return args, err } -func (m *HasOracleMethod) PackOutput(result bool) ([]byte, error) { +func (m HasOracleABI) PackOutput(result bool) ([]byte, error) { return m.Method.Outputs.Pack(result) } -func (m *HasOracleMethod) UnpackOutput(data []byte) (bool, error) { +func (m HasOracleABI) UnpackOutput(data []byte) (bool, error) { result, err := m.Method.Outputs.Unpack(data) if err != nil { return false, err diff --git a/x/crosschain/precompile/has_oracle_test.go b/x/crosschain/precompile/has_oracle_test.go index ce5ba631..10bc825d 100644 --- a/x/crosschain/precompile/has_oracle_test.go +++ b/x/crosschain/precompile/has_oracle_test.go @@ -9,13 +9,14 @@ import ( "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/crosschain/precompile" + "github.com/functionx/fx-core/v8/x/crosschain/types" ethtypes "github.com/functionx/fx-core/v8/x/eth/types" ) func TestCrosschainHasOracleABI(t *testing.T) { - method := precompile.NewHasOracleMethod(nil) - require.Len(t, method.Method.Inputs, 2) - require.Len(t, method.Method.Outputs, 1) + hasOracleABI := precompile.NewHasOracleABI() + require.Len(t, hasOracleABI.Method.Inputs, 2) + require.Len(t, hasOracleABI.Method.Outputs, 1) } func (suite *CrosschainPrecompileTestSuite) TestHasOracle() { @@ -27,11 +28,10 @@ func (suite *CrosschainPrecompileTestSuite) TestHasOracle() { { name: "has oracle", malleate: func() (contract.HasOracleArgs, error) { - moduleName := suite.GenerateModuleName() - oracle := suite.GenerateRandOracle(moduleName, true) + oracle := suite.SetOracle(true) return contract.HasOracleArgs{ - Chain: moduleName, - ExternalAddress: oracle.GetExternalHexAddr(), + Chain: suite.chainName, + ExternalAddress: types.ExternalAddrToHexAddr(suite.chainName, oracle.ExternalAddress), }, nil }, result: true, diff --git a/x/crosschain/precompile/is_oracle_online.go b/x/crosschain/precompile/is_oracle_online.go index 791bfdd9..2bd80d4b 100644 --- a/x/crosschain/precompile/is_oracle_online.go +++ b/x/crosschain/precompile/is_oracle_online.go @@ -13,13 +13,13 @@ import ( type IsOracleOnlineMethod struct { *Keeper - abi.Method + IsOracleOnlineABI } func NewIsOracleOnlineMethod(keeper *Keeper) *IsOracleOnlineMethod { return &IsOracleOnlineMethod{ - Keeper: keeper, - Method: crosschainABI.Methods["isOracleOnline"], + Keeper: keeper, + IsOracleOnlineABI: NewIsOracleOnlineABI(), } } @@ -56,6 +56,16 @@ func (i *IsOracleOnlineMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, return i.PackOutput(has && oracle.Online) } +type IsOracleOnlineABI struct { + abi.Method +} + +func NewIsOracleOnlineABI() IsOracleOnlineABI { + return IsOracleOnlineABI{ + Method: crosschainABI.Methods["isOracleOnline"], + } +} + func (i *IsOracleOnlineMethod) PackInput(args contract.IsOracleOnlineArgs) ([]byte, error) { arguments, err := i.Method.Inputs.Pack(args.Chain, args.ExternalAddress) if err != nil { diff --git a/x/crosschain/precompile/is_oracle_online_test.go b/x/crosschain/precompile/is_oracle_online_test.go index 6b44216c..93458ce9 100644 --- a/x/crosschain/precompile/is_oracle_online_test.go +++ b/x/crosschain/precompile/is_oracle_online_test.go @@ -9,13 +9,14 @@ import ( "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/crosschain/precompile" + "github.com/functionx/fx-core/v8/x/crosschain/types" ethtypes "github.com/functionx/fx-core/v8/x/eth/types" ) func TestCrosschainIsOracleOnlineABI(t *testing.T) { - method := precompile.NewIsOracleOnlineMethod(nil) - require.Len(t, method.Method.Inputs, 2) - require.Len(t, method.Method.Outputs, 1) + isOracleOnlineABI := precompile.NewIsOracleOnlineABI() + require.Len(t, isOracleOnlineABI.Method.Inputs, 2) + require.Len(t, isOracleOnlineABI.Method.Outputs, 1) } func (suite *CrosschainPrecompileTestSuite) TestIsOracleOnline() { @@ -27,11 +28,10 @@ func (suite *CrosschainPrecompileTestSuite) TestIsOracleOnline() { { name: "oracle online", malleate: func() (contract.IsOracleOnlineArgs, error) { - moduleName := suite.GenerateModuleName() - oracle := suite.GenerateRandOracle(moduleName, true) + oracle := suite.SetOracle(true) return contract.IsOracleOnlineArgs{ - Chain: moduleName, - ExternalAddress: oracle.GetExternalHexAddr(), + Chain: suite.chainName, + ExternalAddress: types.ExternalAddrToHexAddr(suite.chainName, oracle.ExternalAddress), }, nil }, result: true, @@ -39,11 +39,10 @@ func (suite *CrosschainPrecompileTestSuite) TestIsOracleOnline() { { name: "oracle offline", malleate: func() (contract.IsOracleOnlineArgs, error) { - moduleName := suite.GenerateModuleName() - oracle := suite.GenerateRandOracle(moduleName, false) + oracle := suite.SetOracle(false) return contract.IsOracleOnlineArgs{ - Chain: moduleName, - ExternalAddress: oracle.GetExternalHexAddr(), + Chain: suite.chainName, + ExternalAddress: types.ExternalAddrToHexAddr(suite.chainName, oracle.ExternalAddress), }, nil }, result: false, @@ -75,7 +74,7 @@ func (suite *CrosschainPrecompileTestSuite) TestIsOracleOnline() { args, expectErr := tc.malleate() result := suite.WithError(expectErr).IsOracleOnline(suite.Ctx, args) - suite.Require().Equal(tc.result, result) + suite.Require().Equal(tc.result, result, suite.chainName) }) } }