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

refactor(crosschain/precompile): split into method and abi #797

Merged
merged 1 commit into from
Oct 31, 2024
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
23 changes: 23 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
zakir-code marked this conversation as resolved.
Show resolved Hide resolved

type AppKeepers struct {
// keys to access the substores
keys map[string]*storetypes.KVStoreKey
Expand Down
8 changes: 4 additions & 4 deletions contract/crosschain_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions x/crosschain/keeper/bridge_call_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
}
Expand All @@ -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: "",
Expand Down
4 changes: 2 additions & 2 deletions x/crosschain/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
63 changes: 10 additions & 53 deletions x/crosschain/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()})
Expand All @@ -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,
Expand All @@ -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, &params))
}

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")
}
}
47 changes: 5 additions & 42 deletions x/crosschain/keeper/keeper_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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})
}
}

Expand All @@ -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() {
Expand Down
18 changes: 9 additions & 9 deletions x/crosschain/keeper/pending_execute_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -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,
},
},
}
Expand Down
30 changes: 20 additions & 10 deletions x/crosschain/precompile/bridge_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -102,26 +100,38 @@ 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
}
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
}
Loading