Skip to content

Commit

Permalink
fix: some refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebraver committed Apr 10, 2024
1 parent 10b4db5 commit bbd2a05
Show file tree
Hide file tree
Showing 62 changed files with 743 additions and 4,299 deletions.
8 changes: 1 addition & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ import (
depositKeeper "github.com/ExocoreNetwork/exocore/x/deposit/keeper"
depositTypes "github.com/ExocoreNetwork/exocore/x/deposit/types"

"github.com/ExocoreNetwork/exocore/x/operator"
operatorKeeper "github.com/ExocoreNetwork/exocore/x/operator/keeper"
operatorTypes "github.com/ExocoreNetwork/exocore/x/operator/types"

"github.com/ExocoreNetwork/exocore/x/reward"
rewardKeeper "github.com/ExocoreNetwork/exocore/x/reward/keeper"
rewardTypes "github.com/ExocoreNetwork/exocore/x/reward/types"

exoslash "github.com/ExocoreNetwork/exocore/x/slash"
slashKeeper "github.com/ExocoreNetwork/exocore/x/slash/keeper"
exoslashTypes "github.com/ExocoreNetwork/exocore/x/slash/types"

"github.com/ExocoreNetwork/exocore/x/withdraw"
withdrawKeeper "github.com/ExocoreNetwork/exocore/x/withdraw/keeper"
withdrawTypes "github.com/ExocoreNetwork/exocore/x/withdraw/types"
Expand Down Expand Up @@ -663,7 +657,7 @@ func NewExocoreApp(
app.WithdrawKeeper = *withdrawKeeper.NewKeeper(appCodec, keys[withdrawTypes.StoreKey], app.AssetsKeeper, app.DepositKeeper)
app.RewardKeeper = *rewardKeeper.NewKeeper(appCodec, keys[rewardTypes.StoreKey], app.AssetsKeeper)
app.ExoSlashKeeper = slashKeeper.NewKeeper(appCodec, keys[exoslashTypes.StoreKey], app.AssetsKeeper)
app.AVSManagerKeeper = *avsManagerKeeper.NewKeeper(appCodec, keys[avsManagerTypes.StoreKey], &app.OperatorKeeper)
app.AVSManagerKeeper = *avsManagerKeeper.NewKeeper(appCodec, keys[avsManagerTypes.StoreKey], &app.OperatorKeeper, app.AssetsKeeper)
// We call this after setting the hooks to ensure that the hooks are set on the keeper
evmKeeper.WithPrecompiles(
evmkeeper.AvailablePrecompiles(
Expand Down
20 changes: 15 additions & 5 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,32 @@
"type": "string"
},
{
"internalType": "bytes",
"internalType": "string",
"name": "avsAddress",
"type": "bytes"
"type": "string"
},
{
"internalType": "bytes",
"internalType": "string",
"name": "operatorAddress",
"type": "bytes"
"type": "string"
},
{
"internalType": "uint64",
"name": "action",
"type": "uint64"
},
{
"internalType": "string",
"name": "avsOwnerAddress",
"type": "string"
},
{
"internalType": "string",
"name": "assetID",
"type": "string"
}
],
"name": "AVSInfoRegisterOrDeregister",
"name": "AVSAction",
"outputs": [
{
"internalType": "bool",
Expand Down
12 changes: 6 additions & 6 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ IAVSManager constant AVSMANAGER_CONTRACT = IAVSManager(
/// @dev The interface through which solidity contracts will interact with AVS-Manager
/// @custom:address 0x0000000000000000000000000000000000000902
interface IAVSManager {
function AVSInfoRegisterOrDeregister(
function AVSAction(
string memory avsName,
bytes memory avsAddress,
bytes memory operatorAddress,
uint64 action

string memory avsAddress,
string memory operatorAddress,
uint64 action,
string memory avsOwnerAddress,
string memory assetID
) external returns (bool success);

}
159 changes: 159 additions & 0 deletions precompiles/avs/avs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package avs_test

import (
"math/big"

"github.com/ExocoreNetwork/exocore/app"
"github.com/ExocoreNetwork/exocore/precompiles/avs"
avskeeper "github.com/ExocoreNetwork/exocore/x/avs/keeper"
operatortypes "github.com/ExocoreNetwork/exocore/x/operator/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
evmtypes "github.com/evmos/evmos/v14/x/evm/types"
)

func (s *AVSManagerPrecompileSuite) TestIsTransaction() {
testCases := []struct {
name string
method string
isTx bool
}{
{
avs.MethodAVSAction,
s.precompile.Methods[avs.MethodAVSAction].Name,
true,
},
{
"invalid",
"invalid",
false,
},
}

for _, tc := range testCases {
s.Run(tc.name, func() {
s.Require().Equal(s.precompile.IsTransaction(tc.method), tc.isTx)
})
}
}

func (s *AVSManagerPrecompileSuite) TestAVSManager() {
avsName, avsAddres, operatorAddress := "avsTest", "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr", "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr"
avsAction := avskeeper.RegisterAction
avsOwnderAddress, assetID := "0x123", "0x1"
registerOperator := func() {
registerReq := &operatortypes.RegisterOperatorReq{
FromAddress: operatorAddress,
Info: &operatortypes.OperatorInfo{
EarningsAddr: operatorAddress,
},
}
_, err := s.App.OperatorKeeper.RegisterOperator(s.Ctx, registerReq)
s.NoError(err)
}
commonMalleate := func() (common.Address, []byte) {
// prepare the call input for delegation test
input, err := s.precompile.Pack(
avs.MethodAVSAction,
avsName,
avsAddres,
operatorAddress,
uint64(avsAction),
avsOwnderAddress,
assetID,
)
s.Require().NoError(err, "failed to pack input")
return s.Address, input
}
successRet, err := s.precompile.Methods[avs.MethodAVSAction].Outputs.Pack(true)
s.Require().NoError(err)

testcases := []struct {
name string
malleate func() (common.Address, []byte)
readOnly bool
expPass bool
errContains string
returnBytes []byte
}{
{
name: "pass for avs-manager",
malleate: func() (common.Address, []byte) {
registerOperator()
return commonMalleate()
},
readOnly: false,
expPass: true,
returnBytes: successRet,
},
}

for _, tc := range testcases {
tc := tc
s.Run(tc.name, func() {
// setup basic test suite
s.SetupTest()

baseFee := s.App.FeeMarketKeeper.GetBaseFee(s.Ctx)

// malleate testcase
caller, input := tc.malleate()

contract := vm.NewPrecompile(vm.AccountRef(caller), s.precompile, big.NewInt(0), uint64(1e6))
contract.Input = input

contractAddr := contract.Address()
// Build and sign Ethereum transaction
txArgs := evmtypes.EvmTxArgs{
ChainID: s.App.EvmKeeper.ChainID(),
Nonce: 0,
To: &contractAddr,
Amount: nil,
GasLimit: 100000,
GasPrice: app.MainnetMinGasPrices.BigInt(),
GasFeeCap: baseFee,
GasTipCap: big.NewInt(1),
Accesses: &ethtypes.AccessList{},
}
msgEthereumTx := evmtypes.NewTx(&txArgs)

msgEthereumTx.From = s.Address.String()
err := msgEthereumTx.Sign(s.EthSigner, s.Signer)
s.Require().NoError(err, "failed to sign Ethereum message")

// Instantiate config
proposerAddress := s.Ctx.BlockHeader().ProposerAddress
cfg, err := s.App.EvmKeeper.EVMConfig(s.Ctx, proposerAddress, s.App.EvmKeeper.ChainID())
s.Require().NoError(err, "failed to instantiate EVM config")

msg, err := msgEthereumTx.AsMessage(s.EthSigner, baseFee)
s.Require().NoError(err, "failed to instantiate Ethereum message")

// Instantiate EVM
evm := s.App.EvmKeeper.NewEVM(
s.Ctx, msg, cfg, nil, s.StateDB,
)

params := s.App.EvmKeeper.GetParams(s.Ctx)
activePrecompiles := params.GetActivePrecompilesAddrs()
precompileMap := s.App.EvmKeeper.Precompiles(activePrecompiles...)
err = vm.ValidatePrecompiles(precompileMap, activePrecompiles)
s.Require().NoError(err, "invalid precompiles", activePrecompiles)
evm.WithPrecompiles(precompileMap, activePrecompiles)

// Run precompiled contract
bz, err := s.precompile.Run(evm, contract, tc.readOnly)

// Check results
if tc.expPass {
s.Require().NoError(err, "expected no error when running the precompile")
s.Require().Equal(tc.returnBytes, bz, "the return doesn't match the expected result")
} else {
s.Require().Error(err, "expected error to be returned when running the precompile")
s.Require().Nil(bz, "expected returned bytes to be nil")
s.Require().ErrorContains(err, tc.errContains)
}
})
}
}
35 changes: 35 additions & 0 deletions precompiles/avs/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package avs_test

import (
"testing"

"github.com/ExocoreNetwork/exocore/precompiles/avs"
"github.com/ExocoreNetwork/exocore/testutil"
"github.com/stretchr/testify/suite"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var s *AVSManagerPrecompileSuite

type AVSManagerPrecompileSuite struct {
testutil.BaseTestSuite
precompile *avs.Precompile
}

func TestPrecompileTestSuite(t *testing.T) {
s = new(AVSManagerPrecompileSuite)
suite.Run(t, s)

// Run Ginkgo integration tests
RegisterFailHandler(Fail)
RunSpecs(t, "AVSManager Precompile Suite")
}

func (s *AVSManagerPrecompileSuite) SetupTest() {
s.DoSetupTest()
precompile, err := avs.NewPrecompile(s.App.AVSManagerKeeper, s.App.AuthzKeeper)
s.Require().NoError(err)
s.precompile = precompile
}
4 changes: 2 additions & 2 deletions precompiles/avs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
const (
// AVSRegister defines the ABI method name for the avs
// related transaction.
MethodAVSAction = "avsAction"
MethodAVSAction = "AVSAction"
)

// AVSInfoRegister register the avs related information and change the state in avs keeper module.
func (p Precompile) AVSInfoRegisterOrDeregister(
ctx sdk.Context,
_ common.Address,
contract *vm.Contract,
_ *vm.Contract,
_ vm.StateDB,
method *abi.Method,
args []interface{},
Expand Down
36 changes: 24 additions & 12 deletions precompiles/avs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,51 @@ package avs

import (
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/avs/keeper"
avstypes "github.com/ExocoreNetwork/exocore/x/avs/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
cmn "github.com/evmos/evmos/v14/precompiles/common"
"golang.org/x/xerrors"
)

func (p Precompile) GetAVSParamsFromInputs(ctx sdk.Context, args []interface{}) (*keeper.AVSParams, error) {
if len(args) != 4 {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, 4, len(args))
func (p Precompile) GetAVSParamsFromInputs(_ sdk.Context, args []interface{}) (*avstypes.AVSRegisterOrDeregisterParams, error) {
if len(args) != 6 {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, 6, len(args))
}
avsParams := &keeper.AVSParams{}
avsParams := &avstypes.AVSRegisterOrDeregisterParams{}
avsName, ok := args[0].(string)
if !ok {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", avsName)
}
avsParams.AVSName = avsName
avsParams.AvsName = avsName

avsAddress, ok := args[1].([]byte)
if !ok || avsAddress == nil {
avsAddress, ok := args[1].(string)
if !ok {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", avsAddress)
}
avsParams.AVSAddress = avsAddress
avsParams.AvsAddress = avsAddress

operatorAddress, ok := args[2].([]byte)
if !ok || operatorAddress == nil {
operatorAddress, ok := args[2].(string)
if !ok || operatorAddress == "" {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", operatorAddress)
}
avsParams.OperatorAddress = operatorAddress

action, ok := args[3].(uint64)
if !ok || action != keeper.RegisterAction || action != keeper.DeRegisterAction {
if !ok || (action != avstypes.RegisterAction && action != avstypes.DeRegisterAction) {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 3, "uint64", action)
}
avsParams.Action = action

avsOwnerAddress, ok := args[4].(string)
if !ok || avsOwnerAddress == "" {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 4, "string", avsOwnerAddress)
}
avsParams.AvsOwnerAddress = avsOwnerAddress

assetID, ok := args[5].(string)
if !ok || assetID == "" {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 3, "uint64", action)
}
avsParams.AssetID = assetID
return avsParams, nil
}
12 changes: 0 additions & 12 deletions proto/exocore/avs/genesis.proto

This file was deleted.

12 changes: 0 additions & 12 deletions proto/exocore/avs/params.proto

This file was deleted.

Loading

0 comments on commit bbd2a05

Please sign in to comment.