-
Notifications
You must be signed in to change notification settings - Fork 12
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 precmpile contract #579
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package contract | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
) | ||
|
||
type PrecompileMethod interface { | ||
GetMethodId() []byte | ||
RequiredGas() uint64 | ||
IsReadonly() bool | ||
Run(ctx sdk.Context, evm *vm.EVM, contract *vm.Contract) ([]byte, error) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package legacy | ||
|
||
import ( | ||
"math/big" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/cosmos/cosmos-sdk/telemetry" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
// Fip20CrossChainEvents use for fip20 cross chain | ||
// Deprecated | ||
func Fip20CrossChainEvents(ctx sdk.Context, from, token common.Address, recipient, target, denom string, amount, fee *big.Int) { | ||
ctx.EventManager().EmitEvent(sdk.NewEvent( | ||
EventTypeRelayTransferCrossChain, | ||
sdk.NewAttribute(AttributeKeyFrom, from.String()), | ||
sdk.NewAttribute(AttributeKeyRecipient, recipient), | ||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), | ||
sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()), | ||
sdk.NewAttribute(AttributeKeyTarget, target), | ||
sdk.NewAttribute(AttributeKeyTokenAddress, token.String()), | ||
sdk.NewAttribute(AttributeKeyDenom, denom), | ||
)) | ||
|
||
telemetry.IncrCounterWithLabels( | ||
[]string{"relay_transfer_cross_chain"}, | ||
1, | ||
[]metrics.Label{ | ||
telemetry.NewLabel("erc20", token.String()), | ||
telemetry.NewLabel("denom", denom), | ||
telemetry.NewLabel("target", target), | ||
}, | ||
) | ||
} | ||
|
||
// CrossChainEvents | ||
// Deprecated | ||
func CrossChainEvents(ctx sdk.Context, from, token common.Address, recipient, target, denom, memo string, amount, fee *big.Int) { | ||
ctx.EventManager().EmitEvent(sdk.NewEvent( | ||
EventTypeCrossChain, | ||
sdk.NewAttribute(AttributeKeyFrom, from.String()), | ||
sdk.NewAttribute(AttributeKeyRecipient, recipient), | ||
sdk.NewAttribute(sdk.AttributeKeyAmount, amount.String()), | ||
sdk.NewAttribute(sdk.AttributeKeyFee, fee.String()), | ||
sdk.NewAttribute(AttributeKeyTarget, target), | ||
sdk.NewAttribute(AttributeKeyTokenAddress, token.String()), | ||
sdk.NewAttribute(AttributeKeyDenom, denom), | ||
sdk.NewAttribute(AttributeKeyMemo, memo), | ||
)) | ||
} | ||
Comment on lines
+39
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Deprecated function check The
Analysis chainDeprecated function check The Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify that the deprecated function `CrossChainEvents` is not used elsewhere in the codebase.
# Test: Search for the function usage. Expect: No occurrences outside this file.
rg --type go $'CrossChainEvents'
Length of output: 800 |
||
|
||
const ( | ||
// EventTypeRelayTransferCrossChain | ||
// Deprecated | ||
EventTypeRelayTransferCrossChain = "relay_transfer_cross_chain" | ||
// EventTypeCrossChain new cross chain event type | ||
EventTypeCrossChain = "cross_chain" | ||
|
||
AttributeKeyDenom = "coin" | ||
AttributeKeyTokenAddress = "token_address" | ||
AttributeKeyFrom = "from" | ||
AttributeKeyRecipient = "recipient" | ||
AttributeKeyTarget = "target" | ||
AttributeKeyMemo = "memo" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,28 @@ import ( | |
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/functionx/fx-core/v7/contract" | ||
"github.com/functionx/fx-core/v7/testutil/helpers" | ||
"github.com/functionx/fx-core/v7/x/crosschain/types" | ||
"github.com/functionx/fx-core/v7/x/crosschain/precompile" | ||
) | ||
|
||
func TestBridgeCallABI(t *testing.T) { | ||
bridgeCall := precompile.NewBridgeCallMethod(nil) | ||
|
||
require.Equal(t, 8, len(bridgeCall.Method.Inputs)) | ||
require.Equal(t, 1, len(bridgeCall.Method.Outputs)) | ||
} | ||
Comment on lines
+16
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve Test Coverage The tests for + func TestBridgeCallMethod_ErrorHandling(t *testing.T) {
+ _, err := bridgeCall.Run(ctx, nil, nil)
+ require.Error(t, err, "should handle nil contexts and contracts")
+ }
|
||
|
||
func TestContract_BridgeCall_Input(t *testing.T) { | ||
assert.Equal(t, `bridgeCall(string,address,address[],uint256[],address,bytes,uint256,bytes)`, types.BridgeCallMethod.Sig) | ||
assert.Equal(t, "payable", types.BridgeCallMethod.StateMutability) | ||
assert.Equal(t, 8, len(types.BridgeCallMethod.Inputs)) | ||
bridgeCall := precompile.NewBridgeCallMethod(nil) | ||
|
||
inputs := types.BridgeCallMethod.Inputs | ||
assert.Equal(t, `bridgeCall(string,address,address[],uint256[],address,bytes,uint256,bytes)`, bridgeCall.Method.Sig) | ||
assert.Equal(t, "payable", bridgeCall.Method.StateMutability) | ||
assert.Equal(t, 8, len(bridgeCall.Method.Inputs)) | ||
|
||
inputs := bridgeCall.Method.Inputs | ||
type Args struct { | ||
DstChain string | ||
Refund common.Address | ||
|
@@ -67,9 +77,10 @@ func TestContract_BridgeCall_Input(t *testing.T) { | |
} | ||
|
||
func TestContract_BridgeCall_Output(t *testing.T) { | ||
assert.Equal(t, 1, len(types.BridgeCallMethod.Outputs)) | ||
bridgeCall := precompile.NewBridgeCallMethod(nil) | ||
assert.Equal(t, 1, len(bridgeCall.Method.Outputs)) | ||
|
||
outputs := types.BridgeCallMethod.Outputs | ||
outputs := bridgeCall.Method.Outputs | ||
eventNonce := big.NewInt(1) | ||
outputData, err := outputs.Pack(eventNonce) | ||
assert.NoError(t, err) | ||
|
@@ -83,14 +94,16 @@ func TestContract_BridgeCall_Output(t *testing.T) { | |
} | ||
|
||
func TestContract_BridgeCall_Event(t *testing.T) { | ||
assert.Equal(t, `BridgeCallEvent(address,address,address,address,uint256,uint256,string,address[],uint256[],bytes,bytes)`, types.BridgeCallEvent.Sig) | ||
assert.Equal(t, "0x4a9b24da6150ef33e7c41038842b7c94fe89a4fff22dccb2c3fd79f0176062c6", types.BridgeCallEvent.ID.String()) | ||
assert.Equal(t, 11, len(types.BridgeCallEvent.Inputs)) | ||
assert.Equal(t, 8, len(types.BridgeCallEvent.Inputs.NonIndexed())) | ||
bridgeCall := precompile.NewBridgeCallMethod(nil) | ||
|
||
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.Equal(t, 11, len(bridgeCall.Event.Inputs)) | ||
assert.Equal(t, 8, len(bridgeCall.Event.Inputs.NonIndexed())) | ||
for i := 0; i < 3; i++ { | ||
assert.Equal(t, true, types.BridgeCallEvent.Inputs[i].Indexed) | ||
assert.Equal(t, true, bridgeCall.Event.Inputs[i].Indexed) | ||
} | ||
inputs := types.BridgeCallEvent.Inputs | ||
inputs := bridgeCall.Event.Inputs | ||
|
||
args := contract.ICrossChainBridgeCallEvent{ | ||
TxOrigin: helpers.GenHexAddress(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
Deprecated function usage
The function
Fip20CrossChainEvents
is marked as deprecated but is still in use inx/crosschain/precompile/fip20_crosschain.go
. Consider replacing it with theCrossChainEvents
function found in the same file, if it serves the same purpose. This will help in cleaning up the legacy code.x/crosschain/precompile/fip20_crosschain.go
legacy.Fip20CrossChainEvents(ctx, args.Sender, tokenPair.GetERC20Contract(), args.Receipt, fxtypes.Byte32ToString(args.Target), tokenPair.GetDenom(), args.Amount, args.Fee)
Analysis chain
Deprecated function usage
The function
Fip20CrossChainEvents
is marked as deprecated. Ensure that it is no longer used in the codebase or consider removing it to clean up the legacy code.Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 434
Script:
Length of output: 943
Script:
Length of output: 381