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(precompiles): consolidate precompiled contracts into a single package #806

Merged
merged 1 commit into from
Nov 6, 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
8 changes: 4 additions & 4 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ import (
"github.com/spf13/cast"

"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
stakingprecompile "github.com/functionx/fx-core/v8/precompiles/staking"
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"
crosschainkeeper "github.com/functionx/fx-core/v8/x/crosschain/keeper"
crosschainprecompile "github.com/functionx/fx-core/v8/x/crosschain/precompile"
erc20keeper "github.com/functionx/fx-core/v8/x/erc20/keeper"
erc20types "github.com/functionx/fx-core/v8/x/erc20/types"
ethtypes "github.com/functionx/fx-core/v8/x/eth/types"
Expand All @@ -83,7 +84,6 @@ import (
optimismtypes "github.com/functionx/fx-core/v8/x/optimism/types"
polygontypes "github.com/functionx/fx-core/v8/x/polygon/types"
fxstakingkeeper "github.com/functionx/fx-core/v8/x/staking/keeper"
stakingprecompile "github.com/functionx/fx-core/v8/x/staking/precompile"
trontypes "github.com/functionx/fx-core/v8/x/tron/types"
)

Expand Down Expand Up @@ -350,7 +350,7 @@ func NewAppKeeper(
)

// crosschain precompile
crosschainPrecompileRouter := crosschainprecompile.NewRouter()
crosschainPrecompileRouter := crosschain.NewRouter()
evmKeeper := evmkeeper.NewKeeper(
appCodec,
appKeepers.keys[evmtypes.StoreKey],
Expand All @@ -364,7 +364,7 @@ func NewAppKeeper(
appKeepers.GetSubspace(evmtypes.ModuleName),
[]evmkeeper.CustomContractFn{
func(_ sdk.Context, _ ethparams.Rules) vm.PrecompiledContract {
return crosschainprecompile.NewPrecompiledContract(
return crosschain.NewPrecompiledContract(
appKeepers.BankKeeper, appKeepers.GovKeeper, crosschainPrecompileRouter)
},
func(_ sdk.Context, _ ethparams.Rules) vm.PrecompiledContract {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile_test
package crosschain_test

import (
"encoding/hex"
Expand All @@ -10,19 +10,19 @@ import (
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
"github.com/functionx/fx-core/v8/testutil/helpers"
"github.com/functionx/fx-core/v8/x/crosschain/precompile"
)

func TestBridgeCallABI(t *testing.T) {
bridgeCallABI := precompile.NewBridgeCallABI()
bridgeCallABI := crosschain.NewBridgeCallABI()

require.Len(t, bridgeCallABI.Method.Inputs, 8)
require.Len(t, bridgeCallABI.Method.Outputs, 1)
}

func TestContract_BridgeCall_Input(t *testing.T) {
bridgeCallABI := precompile.NewBridgeCallABI()
bridgeCallABI := crosschain.NewBridgeCallABI()

assert.Equal(t, `bridgeCall(string,address,address[],uint256[],address,bytes,uint256,bytes)`, bridgeCallABI.Method.Sig)
assert.Equal(t, "payable", bridgeCallABI.Method.StateMutability)
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestContract_BridgeCall_Input(t *testing.T) {
}

func TestContract_BridgeCall_Output(t *testing.T) {
bridgeCallABI := precompile.NewBridgeCallABI()
bridgeCallABI := crosschain.NewBridgeCallABI()
assert.Len(t, bridgeCallABI.Method.Outputs, 1)

outputs := bridgeCallABI.Method.Outputs
Expand All @@ -95,7 +95,7 @@ func TestContract_BridgeCall_Output(t *testing.T) {
}

func TestContract_BridgeCall_Event(t *testing.T) {
bridgeCallABI := precompile.NewBridgeCallABI()
bridgeCallABI := crosschain.NewBridgeCallABI()

assert.Equal(t, `BridgeCallEvent(address,address,address,address,uint256,string,address[],uint256[],bytes,uint256,bytes)`, bridgeCallABI.Event.Sig)
assert.Equal(t, "0x96da1d63da4d424eb5848fc98c0361f8e970b1934ddc9c5529ba684171283a53", bridgeCallABI.Event.ID.String())
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestContract_BridgeCall_Event(t *testing.T) {
}

func TestContract_BridgeCall_NewBridgeCallEvent(t *testing.T) {
bridgeCallABI := precompile.NewBridgeCallABI()
bridgeCallABI := crosschain.NewBridgeCallABI()

sender := common.BytesToAddress([]byte{0x1})
origin := common.BytesToAddress([]byte{0x2})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package precompile_test
package crosschain_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/functionx/fx-core/v8/x/crosschain/precompile"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
)

func TestBridgeCoinAmountMethod_ABI(t *testing.T) {
bridgeCoinAmountABI := precompile.NewBridgeCoinAmountABI()
bridgeCoinAmountABI := crosschain.NewBridgeCoinAmountABI()
assert.Len(t, bridgeCoinAmountABI.Inputs, 2)
assert.Len(t, bridgeCoinAmountABI.Outputs, 1)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"bytes"
Expand All @@ -8,6 +8,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"

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

Expand All @@ -18,12 +19,12 @@ var (

type Contract struct {
methods []contract.PrecompileMethod
govKeeper GovKeeper
govKeeper types.GovKeeper
}

func NewPrecompiledContract(
bankKeeper BankKeeper,
govKeeper GovKeeper,
bankKeeper types.BankKeeper,
govKeeper types.GovKeeper,
router *Router,
) *Contract {
keeper := &Keeper{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile_test
package crosschain_test

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"errors"
Expand All @@ -17,32 +17,32 @@ import (
)

// Deprecated: After the upgrade to v8
type CrosschainMethod struct {
type LegacyCrosschainMethod struct {
*Keeper
CrosschainABI
LegacyCrosschainABI
}

// Deprecated: After the upgrade to v8
func NewCrosschainMethod(keeper *Keeper) *CrosschainMethod {
return &CrosschainMethod{
Keeper: keeper,
CrosschainABI: NewCrosschainABI(),
func NewCrosschainMethod(keeper *Keeper) *LegacyCrosschainMethod {
return &LegacyCrosschainMethod{
Keeper: keeper,
LegacyCrosschainABI: NewCrosschainABI(),
}
}

func (m *CrosschainMethod) IsReadonly() bool {
func (m *LegacyCrosschainMethod) IsReadonly() bool {
return false
}

func (m *CrosschainMethod) GetMethodId() []byte {
func (m *LegacyCrosschainMethod) GetMethodId() []byte {
return m.Method.ID
}

func (m *CrosschainMethod) RequiredGas() uint64 {
func (m *LegacyCrosschainMethod) RequiredGas() uint64 {
return 40_000
}

func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) {
func (m *LegacyCrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) {
args, err := m.UnpackInput(contract.Input)
if err != nil {
return nil, err
Expand Down Expand Up @@ -110,39 +110,39 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
}

// Deprecated: After the upgrade to v8
type CrosschainABI struct {
type LegacyCrosschainABI struct {
abi.Method
abi.Event
}

// Deprecated: After the upgrade to v8
func NewCrosschainABI() CrosschainABI {
return CrosschainABI{
func NewCrosschainABI() LegacyCrosschainABI {
return LegacyCrosschainABI{
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) {
func (m LegacyCrosschainABI) 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 CrosschainABI) UnpackInput(data []byte) (*fxcontract.CrosschainArgs, error) {
func (m LegacyCrosschainABI) UnpackInput(data []byte) (*fxcontract.CrosschainArgs, error) {
args := new(fxcontract.CrosschainArgs)
if err := evmtypes.ParseMethodArgs(m.Method, args, data[4:]); err != nil {
return nil, err
}
return args, nil
}

func (m CrosschainABI) PackInput(args fxcontract.CrosschainArgs) ([]byte, error) {
func (m LegacyCrosschainABI) 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.Method.ID, data...), nil
}

func (m CrosschainABI) PackOutput(success bool) ([]byte, error) {
func (m LegacyCrosschainABI) PackOutput(success bool) ([]byte, error) {
return m.Method.Outputs.Pack(success)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package precompile_test
package crosschain_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/x/crosschain/precompile"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
)

func TestCrosschainABI(t *testing.T) {
crosschainABI := precompile.NewCrosschainABI()
crosschainABI := crosschain.NewCrosschainABI()

require.Len(t, crosschainABI.Method.Inputs, 6)
require.Len(t, crosschainABI.Method.Outputs, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile_test
package crosschain_test

import (
"encoding/hex"
Expand All @@ -9,12 +9,12 @@ import (
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/x/crosschain/precompile"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
ethtypes "github.com/functionx/fx-core/v8/x/eth/types"
)

func TestExecuteClaimMethod_ABI(t *testing.T) {
executeClaimABI := precompile.NewExecuteClaimABI()
executeClaimABI := crosschain.NewExecuteClaimABI()

methodStr := `function executeClaim(string _chain, uint256 _eventNonce) returns(bool _result)`
assert.Equal(t, methodStr, executeClaimABI.Method.String())
Expand All @@ -24,7 +24,7 @@ func TestExecuteClaimMethod_ABI(t *testing.T) {
}

func TestExecuteClaimMethod_PackInput(t *testing.T) {
executeClaimABI := precompile.NewExecuteClaimABI()
executeClaimABI := crosschain.NewExecuteClaimABI()
input, err := executeClaimABI.PackInput(contract.ExecuteClaimArgs{
Chain: ethtypes.ModuleName,
EventNonce: big.NewInt(1),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile_test
package crosschain_test

import (
"fmt"
Expand All @@ -7,14 +7,14 @@ import (
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
"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) {
hasOracleABI := precompile.NewHasOracleABI()
hasOracleABI := crosschain.NewHasOracleABI()
require.Len(t, hasOracleABI.Method.Inputs, 2)
require.Len(t, hasOracleABI.Method.Outputs, 1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile_test
package crosschain_test

import (
"fmt"
Expand All @@ -7,14 +7,14 @@ import (
"github.com/stretchr/testify/require"

"github.com/functionx/fx-core/v8/contract"
"github.com/functionx/fx-core/v8/precompiles/crosschain"
"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) {
isOracleOnlineABI := precompile.NewIsOracleOnlineABI()
isOracleOnlineABI := crosschain.NewIsOracleOnlineABI()
require.Len(t, isOracleOnlineABI.Method.Inputs, 2)
require.Len(t, isOracleOnlineABI.Method.Outputs, 1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package precompile
package crosschain

import (
"fmt"
Expand All @@ -11,16 +11,17 @@ import (
"github.com/ethereum/go-ethereum/core/vm"

"github.com/functionx/fx-core/v8/contract"
types2 "github.com/functionx/fx-core/v8/precompiles/types"
fxtypes "github.com/functionx/fx-core/v8/types"
erc20types "github.com/functionx/fx-core/v8/x/erc20/types"
)

type Keeper struct {
router *Router
bankKeeper BankKeeper
bankKeeper types2.BankKeeper
}

func (c *Keeper) EvmTokenToBaseCoin(ctx sdk.Context, evm *vm.EVM, crosschainKeeper CrosschainKeeper, holder, tokenAddr common.Address, amount *big.Int) (sdk.Coin, error) {
func (c *Keeper) EvmTokenToBaseCoin(ctx sdk.Context, evm *vm.EVM, crosschainKeeper types2.CrosschainKeeper, holder, tokenAddr common.Address, amount *big.Int) (sdk.Coin, error) {
erc20Token, err := crosschainKeeper.GetBaseDenomByErc20(ctx, tokenAddr)
if err != nil {
return sdk.Coin{}, err
Expand Down
Loading