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: privatize precompiled cross-chain contract addresses and ABI #782

Merged
merged 1 commit into from
Oct 25, 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
38 changes: 32 additions & 6 deletions tests/crosschain_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"

"github.com/functionx/fx-core/v8/client"
"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v8/types"
"github.com/functionx/fx-core/v8/x/crosschain/precompile"
Expand All @@ -33,6 +34,8 @@ type CrosschainTestSuite struct {
externalPrivKey *ecdsa.PrivateKey
privKey cryptotypes.PrivKey
executeClaimPrivKey cryptotypes.PrivKey

crosschainAddr common.Address
}

func NewCrosschainWithTestSuite(chainName string, ts *TestSuite) CrosschainTestSuite {
Expand All @@ -48,6 +51,7 @@ func NewCrosschainWithTestSuite(chainName string, ts *TestSuite) CrosschainTestS
externalPrivKey: externalPrivKey,
privKey: helpers.NewEthPrivKey(),
executeClaimPrivKey: helpers.NewEthPrivKey(),
crosschainAddr: common.HexToAddress(contract.CrosschainAddress),
}
}

Expand Down Expand Up @@ -80,8 +84,8 @@ func (suite *CrosschainTestSuite) ExecuteClaimAccAddress() sdk.AccAddress {
return suite.executeClaimPrivKey.PubKey().Address().Bytes()
}

func (suite *CrosschainTestSuite) HexAddress() gethcommon.Address {
return gethcommon.BytesToAddress(suite.privKey.PubKey().Address())
func (suite *CrosschainTestSuite) HexAddress() common.Address {
return common.BytesToAddress(suite.privKey.PubKey().Address())
}

func (suite *CrosschainTestSuite) HexAddressString() string {
Expand Down Expand Up @@ -450,7 +454,7 @@ func (suite *CrosschainTestSuite) AddBridgeToken(md banktypes.Metadata) (string,
}
}

func (suite *CrosschainTestSuite) FormatAddress(address gethcommon.Address) string {
func (suite *CrosschainTestSuite) FormatAddress(address common.Address) string {
return crosschaintypes.ExternalAddrToStr(suite.chainName, address.Bytes())
}

Expand Down Expand Up @@ -524,8 +528,7 @@ func (suite *CrosschainTestSuite) ExecuteClaim() *ethtypes.Transaction {
})
suite.Require().NoError(err)

address := crosschaintypes.GetAddress()
ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), suite.executeClaimPrivKey, &address, nil, pack)
ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), suite.executeClaimPrivKey, &suite.crosschainAddr, nil, pack)
suite.Require().NoError(err)

suite.SendTransaction(ethTx)
Expand Down Expand Up @@ -560,3 +563,26 @@ func (suite *CrosschainTestSuite) UpdateParams(opts ...func(params *crosschainty
}
return suite.BroadcastProposalTx2([]sdk.Msg{msg}, "UpdateParams", "UpdateParams")
}

func (suite *CrosschainTestSuite) Crosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction {
privateKey := suite.privKey
crosschainContract := suite.crosschainAddr
suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee))

beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes()))
pack, err := precompile.NewCrosschainMethod(nil).PackInput(crosschaintypes.CrosschainArgs{
Token: token,
Receipt: recipient,
Amount: amount,
Fee: fee,
Target: fxtypes.MustStrToByte32(target),
Memo: "",
})
suite.Require().NoError(err)
ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &crosschainContract, nil, pack)
suite.Require().NoError(err, target)
suite.SendTransaction(ethTx)
afterBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes()))
suite.Require().True(new(big.Int).Sub(beforeBalanceOf, afterBalanceOf).Cmp(new(big.Int).Add(amount, fee)) == 0)
return ethTx
}
51 changes: 0 additions & 51 deletions tests/precompile_suite.go

This file was deleted.

9 changes: 5 additions & 4 deletions x/crosschain/precompile/bridge_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

fxcontract "github.com/functionx/fx-core/v8/contract"
fxtypes "github.com/functionx/fx-core/v8/types"
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types"
evmtypes "github.com/functionx/fx-core/v8/x/evm/types"
Expand All @@ -24,8 +25,8 @@ type BridgeCallMethod struct {
func NewBridgeCallMethod(keeper *Keeper) *BridgeCallMethod {
return &BridgeCallMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["bridgeCall"],
Event: crosschaintypes.GetABI().Events["BridgeCallEvent"],
Method: crosschainABI.Methods["bridgeCall"],
Event: crosschainABI.Events["BridgeCallEvent"],
}
}

Expand Down Expand Up @@ -61,7 +62,7 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
originTokenAmount := sdkmath.ZeroInt()
if value.Sign() > 0 {
baseCoin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(value))
if err = m.bankKeeper.SendCoins(ctx, crosschaintypes.GetAddress().Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil {
if err = m.bankKeeper.SendCoins(ctx, crosschainAddress.Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil {
return err
}
baseCoins = append(baseCoins, baseCoin)
Expand Down Expand Up @@ -93,7 +94,7 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
if err != nil {
return err
}
EmitEvent(evm, data, topic)
fxcontract.EmitEvent(evm, crosschainAddress, data, topic)

result, err = m.PackOutput(eventNonce)
return err
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/precompile/bridge_coin_amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type BridgeCoinAmountMethod struct {
func NewBridgeCoinAmountMethod(keeper *Keeper) *BridgeCoinAmountMethod {
return &BridgeCoinAmountMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["bridgeCoinAmount"],
Method: crosschainABI.Methods["bridgeCoinAmount"],
}
}

Expand Down
18 changes: 6 additions & 12 deletions x/crosschain/precompile/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"errors"

"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/functionx/fx-core/v8/contract"
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types"
evmtypes "github.com/functionx/fx-core/v8/x/evm/types"
)

var (
crosschainABI = contract.MustABIJson(contract.ICrosschainMetaData.ABI)
crosschainAddress = common.HexToAddress(contract.CrosschainAddress)
)

type Contract struct {
methods []contract.PrecompileMethod
govKeeper GovKeeper
Expand Down Expand Up @@ -42,7 +45,7 @@ func NewPrecompiledContract(
}

func (c *Contract) Address() common.Address {
return crosschaintypes.GetAddress()
return crosschainAddress
}

func (c *Contract) IsStateful() bool {
Expand Down Expand Up @@ -86,12 +89,3 @@ func (c *Contract) Run(evm *vm.EVM, vmContract *vm.Contract, readonly bool) (ret
}
return contract.PackRetErrV2(errors.New("unknown method"))
}

func EmitEvent(evm *vm.EVM, data []byte, topics []common.Hash) {
evm.StateDB.AddLog(&ethtypes.Log{
Address: crosschaintypes.GetAddress(),
Topics: topics,
Data: data,
BlockNumber: evm.Context.BlockNumber.Uint64(),
})
}
6 changes: 4 additions & 2 deletions x/crosschain/precompile/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type PrecompileTestSuite struct {
helpers.BaseSuite

// signer *helpers.Signer
// crosschain common.Address
crosschainAddr common.Address
}

func TestPrecompileTestSuite(t *testing.T) {
Expand All @@ -57,7 +57,9 @@ func (suite *PrecompileTestSuite) SetupTest() {

// crosschainContract, err := suite.App.EvmKeeper.DeployContract(suite.Ctx, suite.signer.Address(), contract.MustABIJson(testscontract.CrosschainTestMetaData.ABI), contract.MustDecodeHex(testscontract.CrosschainTestMetaData.Bin))
// suite.Require().NoError(err)
// suite.crosschain = crosschainContract
// suite.crosschainAddrTest = crosschainContract

suite.crosschainAddr = common.HexToAddress(contract.CrosschainAddress)
}

func (suite *PrecompileTestSuite) SetupSubTest() {
Expand Down
8 changes: 4 additions & 4 deletions x/crosschain/precompile/crosschain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type CrosschainMethod struct {
func NewCrosschainMethod(keeper *Keeper) *CrosschainMethod {
return &CrosschainMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["crossChain"],
Event: crosschaintypes.GetABI().Events["CrossChain"],
Method: crosschainABI.Methods["crossChain"],
Event: crosschainABI.Events["CrossChain"],
Comment on lines +30 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Based on the gathered information, I can now provide a final response:

Migration from CrosschainMethod to BridgeCallMethod is in progress but incomplete

The codebase shows that:

  • BridgeCallMethod is fully implemented in bridge_call.go with all necessary methods and tests
  • Both methods are currently registered in contract.go
  • CrosschainMethod is properly marked as deprecated with a reference to BridgeCallMethod
  • However, there are still active usages in tests (crosschain_test.go and crosschain_suite.go) that need to be migrated
  • tests/crosschain_suite.go: Uses deprecated NewCrosschainMethod for packing input
  • x/crosschain/precompile/crosschain_test.go: Contains test cases using the deprecated method
🔗 Analysis chain

Verify migration plan for deprecated CrosschainMethod.

While the ABI reference changes look good, this struct is marked as deprecated in favor of BridgeCallMethod. Let's ensure there's a plan to migrate all existing usages.


Let me check the actual usage context of CrosschainMethod in these files to better understand the migration status.


Let me check if BridgeCallMethod is already implemented and being used as the replacement, to ensure there's a proper migration path.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for remaining usages of CrosschainMethod to ensure complete migration
rg -l "CrosschainMethod" --type go

Length of output: 177


Script:

#!/bin/bash
# Check the context of CrosschainMethod usage in each file
echo "=== crosschain.go ==="
rg "CrosschainMethod" x/crosschain/precompile/crosschain.go -B 2 -A 2

echo -e "\n=== crosschain_test.go ==="
rg "CrosschainMethod" x/crosschain/precompile/crosschain_test.go -B 2 -A 2

echo -e "\n=== contract.go ==="
rg "CrosschainMethod" x/crosschain/precompile/contract.go -B 2 -A 2

echo -e "\n=== crosschain_suite.go ==="
rg "CrosschainMethod" tests/crosschain_suite.go -B 2 -A 2

Length of output: 2572


Script:

#!/bin/bash
# Check BridgeCallMethod implementation and usage
echo "=== BridgeCallMethod Implementation ==="
rg "BridgeCallMethod" x/crosschain/precompile/crosschain.go -B 2 -A 2

echo -e "\n=== BridgeCallMethod Usage ==="
rg "BridgeCallMethod" -g '!*.md' --type go -B 2 -A 2

Length of output: 6809

}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
}

baseCoin = sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(totalAmount))
if err = m.bankKeeper.SendCoins(ctx, crosschaintypes.GetAddress().Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil {
if err = m.bankKeeper.SendCoins(ctx, crosschainAddress.Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil {
return err
}
} else {
Expand All @@ -101,7 +101,7 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
if err != nil {
return err
}
EmitEvent(evm, data, topic)
fxcontract.EmitEvent(evm, crosschainAddress, data, topic)

return nil
}); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions x/crosschain/precompile/execute_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"

"github.com/functionx/fx-core/v8/contract"
fxcontract "github.com/functionx/fx-core/v8/contract"
crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types"
evmtypes "github.com/functionx/fx-core/v8/x/evm/types"
)

var _ contract.PrecompileMethod = (*ExecuteClaimMethod)(nil)
var _ fxcontract.PrecompileMethod = (*ExecuteClaimMethod)(nil)

type ExecuteClaimMethod struct {
*Keeper
Expand All @@ -25,8 +25,8 @@ type ExecuteClaimMethod struct {
func NewExecuteClaimMethod(keeper *Keeper) *ExecuteClaimMethod {
return &ExecuteClaimMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["executeClaim"],
Event: crosschaintypes.GetABI().Events["ExecuteClaimEvent"],
Method: crosschainABI.Methods["executeClaim"],
Event: crosschainABI.Events["ExecuteClaimEvent"],
}
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func (m *ExecuteClaimMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er
if err != nil {
return err
}
EmitEvent(evm, data, topic)
fxcontract.EmitEvent(evm, crosschainAddress, data, topic)
return nil
}); err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/precompile/has_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type HasOracleMethod struct {
func NewHasOracleMethod(keeper *Keeper) *HasOracleMethod {
return &HasOracleMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["hasOracle"],
Method: crosschainABI.Methods["hasOracle"],
}
}

Expand Down
4 changes: 1 addition & 3 deletions x/crosschain/precompile/has_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func (suite *PrecompileTestSuite) TestHasOracle() {
packData, err := hasOracle.PackInput(args)
suite.Require().NoError(err)

contractAddr := crosschaintypes.GetAddress()

res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &contractAddr, nil, packData, false)
res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &suite.crosschainAddr, nil, packData, false)
if err != nil {
suite.Require().EqualError(err, expectErr.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/precompile/is_oracle_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type IsOracleOnlineMethod struct {
func NewIsOracleOnlineMethod(keeper *Keeper) *IsOracleOnlineMethod {
return &IsOracleOnlineMethod{
Keeper: keeper,
Method: crosschaintypes.GetABI().Methods["isOracleOnline"],
Method: crosschainABI.Methods["isOracleOnline"],
}
}

Expand Down
4 changes: 1 addition & 3 deletions x/crosschain/precompile/is_oracle_online_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (suite *PrecompileTestSuite) TestIsOracleOnline() {
packData, err := hasOracle.PackInput(args)
suite.Require().NoError(err)

contractAddr := crosschaintypes.GetAddress()

res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &contractAddr, nil, packData, false)
res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &suite.crosschainAddr, nil, packData, false)
if err != nil {
suite.Require().EqualError(err, expectErr.Error())
return
Expand Down
14 changes: 0 additions & 14 deletions x/crosschain/types/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,11 @@ import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"

"github.com/functionx/fx-core/v8/contract"
)

var (
crosschainAddress = common.HexToAddress(contract.CrosschainAddress)
crosschainABI = contract.MustABIJson(contract.ICrosschainMetaData.ABI)
)

func GetAddress() common.Address {
return crosschainAddress
}

func GetABI() abi.ABI {
return crosschainABI
}

type BridgeCoinAmountArgs struct {
Token common.Address `abi:"_token"`
Target [32]byte `abi:"_target"`
Expand Down