Skip to content

Commit

Permalink
refine the unconvitional code identified by code review in the procom…
Browse files Browse the repository at this point in the history
…pile
  • Loading branch information
TimmyExogenous committed Mar 14, 2024
1 parent 032c5eb commit 03b5c9e
Show file tree
Hide file tree
Showing 25 changed files with 97 additions and 103 deletions.
10 changes: 10 additions & 0 deletions precompiles/common/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common

const (
ErrContractInputParaOrType = "the contract input parameter type or value error,arg index:%d, type is:%s,value:%v"
ErrContractCaller = "the caller doesn't have the permission to call this function"

ErrInputClientChainAddrLength = "the length of input client chain addr doesn't match,input:%d,need:%d"

ErrInputOperatorAddrLength = "mismatched length of the input operator address,actual is:%d,expect:%v"
)
22 changes: 11 additions & 11 deletions precompiles/delegation/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func paddingClientChainAddress(input []byte, outputLength int) []byte {
// TestRun tests DelegateToThroughClientChain method through calling Run function.
func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
// deposit params for test
ExocoreLzAppAddress := "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"
ExocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
exocoreLzAppAddress := "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"
exocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
usdtAddress := common.FromHex("0xdAC17F958D2ee523a2206206994597C13D831ec7")
opAccAddr := "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr"
clientChainLzID := 101
Expand Down Expand Up @@ -127,7 +127,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
returnBytes []byte
}{
{
name: "fail - delegateToThroughClientChain transaction will fail because the ExocoreLzAppAddress haven't been stored",
name: "fail - delegateToThroughClientChain transaction will fail because the exocoreLzAppAddress haven't been stored",
malleate: func() (common.Address, []byte) {
return commonMalleate()
},
Expand All @@ -139,8 +139,8 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
name: "fail - delegateToThroughClientChain transaction will fail because the contract caller isn't the exoCoreLzAppAddr",
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: ExocoreLzAppAddress,
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppAddress: exocoreLzAppAddress,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -155,7 +155,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -170,7 +170,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -186,7 +186,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -203,7 +203,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand Down Expand Up @@ -291,7 +291,7 @@ func (s *DelegationPrecompileSuite) TestRunDelegateToThroughClientChain() {
// TestRun tests DelegateToThroughClientChain method through calling Run function.
func (s *DelegationPrecompileSuite) TestRunUnDelegateFromThroughClientChain() {
// deposit params for test
ExocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
exocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
usdtAddress := common.FromHex("0xdAC17F958D2ee523a2206206994597C13D831ec7")
operatorAddr := "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr"
clientChainLzID := 101
Expand Down Expand Up @@ -373,7 +373,7 @@ func (s *DelegationPrecompileSuite) TestRunUnDelegateFromThroughClientChain() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand Down
6 changes: 1 addition & 5 deletions precompiles/delegation/error.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package delegation

const (
ErrContractInputParaOrType = "the contract input parameter type or value error,arg index:%d, type is:%s,value:%v"
ErrContractCaller = "the caller doesn't have the permission to call this function"
ErrCtxTxHash = "ctx TxHash type error or is nil,type is:%v,value:%v"

ErrInputOperatorAddrLength = "mismatched length of the input operator address,actual is:%d,expect:%v"
ErrCtxTxHash = "ctx TxHash type error or is nil,type is:%v,value:%v"
)
6 changes: 4 additions & 2 deletions precompiles/delegation/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"reflect"

exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -36,7 +38,7 @@ func (p Precompile) DelegateToThroughClientChain(
// check the invalidation of caller contract
err := p.assetsKeeper.CheckExocoreLzAppAddr(ctx, contract.CallerAddress)
if err != nil {
return nil, errorsmod.Wrap(err, ErrContractCaller)
return nil, errorsmod.Wrap(err, exocmn.ErrContractCaller)
}

delegationParams, err := p.GetDelegationParamsFromInputs(ctx, args)
Expand All @@ -63,7 +65,7 @@ func (p Precompile) UndelegateFromThroughClientChain(
// check the invalidation of caller contract
err := p.assetsKeeper.CheckExocoreLzAppAddr(ctx, contract.CallerAddress)
if err != nil {
return nil, errorsmod.Wrap(err, ErrContractCaller)
return nil, errorsmod.Wrap(err, exocmn.ErrContractCaller)
}

UndelegationParams, err := p.GetDelegationParamsFromInputs(ctx, args)
Expand Down
20 changes: 10 additions & 10 deletions precompiles/delegation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/ExocoreNetwork/exocore/precompiles/deposit"
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/assets/types"
keeper2 "github.com/ExocoreNetwork/exocore/x/delegation/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -22,7 +22,7 @@ func (p Precompile) GetDelegationParamsFromInputs(ctx sdk.Context, args []interf
delegationParams := &keeper2.DelegationOrUndelegationParams{}
clientChainLzID, ok := args[0].(uint16)
if !ok {
return nil, fmt.Errorf(ErrContractInputParaOrType, 0, reflect.TypeOf(args[0]), clientChainLzID)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, reflect.TypeOf(args[0]), clientChainLzID)
}
delegationParams.ClientChainLzID = uint64(clientChainLzID)

Expand All @@ -34,36 +34,36 @@ func (p Precompile) GetDelegationParamsFromInputs(ctx sdk.Context, args []interf

txLzNonce, ok := args[1].(uint64)
if !ok {
return nil, fmt.Errorf(ErrContractInputParaOrType, 1, reflect.TypeOf(args[1]), txLzNonce)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, reflect.TypeOf(args[1]), txLzNonce)
}
delegationParams.LzNonce = txLzNonce

// the length of client chain address inputted by caller is 32, so we need to check the length and remove the padding according to the actual length.
assetAddr, ok := args[2].([]byte)
if !ok || assetAddr == nil {
return nil, fmt.Errorf(ErrContractInputParaOrType, 2, reflect.TypeOf(args[2]), assetAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 2, reflect.TypeOf(args[2]), assetAddr)
}
if len(assetAddr) != types.GeneralClientChainAddrLength {
return nil, fmt.Errorf(deposit.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
return nil, fmt.Errorf(exocmn.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
}
delegationParams.AssetsAddress = assetAddr[:clientChainAddrLength]

stakerAddr, ok := args[3].([]byte)
if !ok || stakerAddr == nil {
return nil, fmt.Errorf(ErrContractInputParaOrType, 3, reflect.TypeOf(args[3]), stakerAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, reflect.TypeOf(args[3]), stakerAddr)
}
if len(assetAddr) != types.GeneralClientChainAddrLength {
return nil, fmt.Errorf(deposit.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
return nil, fmt.Errorf(exocmn.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
}
delegationParams.StakerAddress = stakerAddr[:clientChainAddrLength]

// the input operator address is cosmos accAddress type,so we need to check the length and decode it through Bench32
operatorAddr, ok := args[4].([]byte)
if !ok || operatorAddr == nil {
return nil, fmt.Errorf(ErrContractInputParaOrType, 4, reflect.TypeOf(args[4]), operatorAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 4, reflect.TypeOf(args[4]), operatorAddr)
}
if len(operatorAddr) != types.ExoCoreOperatorAddrLength {
return nil, fmt.Errorf(ErrInputOperatorAddrLength, len(operatorAddr), types.ExoCoreOperatorAddrLength)
return nil, fmt.Errorf(exocmn.ErrInputOperatorAddrLength, len(operatorAddr), types.ExoCoreOperatorAddrLength)
}

opAccAddr, err := sdk.AccAddressFromBech32(string(operatorAddr))
Expand All @@ -74,7 +74,7 @@ func (p Precompile) GetDelegationParamsFromInputs(ctx sdk.Context, args []interf

opAmount, ok := args[5].(*big.Int)
if !ok || opAmount == nil || opAmount.Cmp(big.NewInt(0)) == 0 {
return nil, fmt.Errorf(ErrContractInputParaOrType, 5, reflect.TypeOf(args[5]), opAmount)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 5, reflect.TypeOf(args[5]), opAmount)
}
delegationParams.OpAmount = sdkmath.NewIntFromBigInt(opAmount)
return delegationParams, nil
Expand Down
14 changes: 7 additions & 7 deletions precompiles/deposit/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func paddingClientChainAddress(input []byte, outputLength int) []byte {
// TestRunDepositTo tests DepositTo method through calling Run function..
func (s *DepositPrecompileSuite) TestRunDepositTo() {
// deposit params for test
ExocoreLzAppAddress := "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"
ExocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
exocoreLzAppAddress := "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"
exocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
usdtAddress := paddingClientChainAddress(common.FromHex("0xdAC17F958D2ee523a2206206994597C13D831ec7"), assetstype.GeneralClientChainAddrLength)
usdcAddress := paddingClientChainAddress(common.FromHex("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), assetstype.GeneralClientChainAddrLength)
clientChainLzID := 101
Expand Down Expand Up @@ -87,7 +87,7 @@ func (s *DepositPrecompileSuite) TestRunDepositTo() {
returnBytes []byte
}{
{
name: "fail - depositTo transaction will fail because the ExocoreLzAppAddress haven't been stored",
name: "fail - depositTo transaction will fail because the exocoreLzAppAddress haven't been stored",
malleate: func() (common.Address, []byte) {
return commonMalleate()
},
Expand All @@ -99,8 +99,8 @@ func (s *DepositPrecompileSuite) TestRunDepositTo() {
name: "fail - depositTo transaction will fail because the contract caller isn't the exoCoreLzAppAddr",
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: ExocoreLzAppAddress,
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppAddress: exocoreLzAppAddress,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -115,7 +115,7 @@ func (s *DepositPrecompileSuite) TestRunDepositTo() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
Expand All @@ -131,7 +131,7 @@ func (s *DepositPrecompileSuite) TestRunDepositTo() {
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: ExocoreLzAppEventTopic,
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
assetAddr = usdtAddress
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
Expand Down
7 changes: 0 additions & 7 deletions precompiles/deposit/error.go

This file was deleted.

3 changes: 2 additions & 1 deletion precompiles/deposit/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deposit

import (
errorsmod "cosmossdk.io/errors"
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/assets/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -27,7 +28,7 @@ func (p Precompile) DepositTo(
// check the invalidation of caller contract,the caller must be exoCore LzApp contract
err := p.assetsKeeper.CheckExocoreLzAppAddr(ctx, contract.CallerAddress)
if err != nil {
return nil, errorsmod.Wrap(err, ErrContractCaller)
return nil, errorsmod.Wrap(err, exocmn.ErrContractCaller)
}
// parse the depositTo input params
depositParams, err := p.GetDepositToParamsFromInputs(ctx, args)
Expand Down
13 changes: 7 additions & 6 deletions precompiles/deposit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

sdkmath "cosmossdk.io/math"
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/assets/types"
"github.com/ExocoreNetwork/exocore/x/deposit/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,7 +19,7 @@ func (p Precompile) GetDepositToParamsFromInputs(ctx sdk.Context, args []interfa
depositParams := &keeper.DepositParams{}
clientChainLzID, ok := args[0].(uint16)
if !ok {
return nil, xerrors.Errorf(ErrContractInputParaOrType, 0, "uint16", clientChainLzID)
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 0, "uint16", clientChainLzID)
}
depositParams.ClientChainLzID = uint64(clientChainLzID)

Expand All @@ -31,25 +32,25 @@ func (p Precompile) GetDepositToParamsFromInputs(ctx sdk.Context, args []interfa
// the length of client chain address inputted by caller is 32, so we need to check the length and remove the padding according to the actual length.
assetAddr, ok := args[1].([]byte)
if !ok || assetAddr == nil {
return nil, xerrors.Errorf(ErrContractInputParaOrType, 1, "[]byte", assetAddr)
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", assetAddr)
}
if len(assetAddr) != types.GeneralAssetsAddrLength {
return nil, xerrors.Errorf(ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
return nil, xerrors.Errorf(exocmn.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
}
depositParams.AssetsAddress = assetAddr[:clientChainAddrLength]

stakerAddr, ok := args[2].([]byte)
if !ok || stakerAddr == nil {
return nil, xerrors.Errorf(ErrContractInputParaOrType, 2, "[]byte", stakerAddr)
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", stakerAddr)
}
if len(stakerAddr) != types.GeneralClientChainAddrLength {
return nil, xerrors.Errorf(ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
return nil, xerrors.Errorf(exocmn.ErrInputClientChainAddrLength, len(assetAddr), types.GeneralClientChainAddrLength)
}
depositParams.StakerAddress = stakerAddr[:clientChainAddrLength]

opAmount, ok := args[3].(*big.Int)
if !ok || opAmount == nil || opAmount.Cmp(big.NewInt(0)) == 0 {
return nil, xerrors.Errorf(ErrContractInputParaOrType, 3, "*big.Int", opAmount)
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 3, "*big.Int", opAmount)
}
depositParams.OpAmount = sdkmath.NewIntFromBigInt(opAmount)

Expand Down
7 changes: 0 additions & 7 deletions precompiles/reward/error.go

This file was deleted.

3 changes: 2 additions & 1 deletion precompiles/reward/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reward

import (
errorsmod "cosmossdk.io/errors"
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/assets/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand All @@ -27,7 +28,7 @@ func (p Precompile) Reward(
// check the invalidation of caller contract
err := p.assetsKeeper.CheckExocoreLzAppAddr(ctx, contract.CallerAddress)
if err != nil {
return nil, errorsmod.Wrap(err, ErrContractCaller)
return nil, errorsmod.Wrap(err, exocmn.ErrContractCaller)
}

rewardParam, err := p.GetRewardParamsFromInputs(ctx, args)
Expand Down
Loading

0 comments on commit 03b5c9e

Please sign in to comment.