Skip to content

Commit

Permalink
refactor: privatize precompiled cross-chain contract addresses and ABI
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code committed Oct 24, 2024
1 parent bfb4a1a commit 8d923ca
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 107 deletions.
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"],
}
}

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

0 comments on commit 8d923ca

Please sign in to comment.