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 6f1b321 commit 0c748a7
Show file tree
Hide file tree
Showing 26 changed files with 634 additions and 910 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,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
10 changes: 5 additions & 5 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
"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"
}
],
"name": "AVSInfoRegisterOrDeregister",
"name": "AVSAction",
"outputs": [
{
"internalType": "bool",
Expand Down
6 changes: 3 additions & 3 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ 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,
string memory avsAddress,
string memory operatorAddress,
uint64 action

) external returns (bool success);
Expand Down
156 changes: 156 additions & 0 deletions precompiles/avs/avs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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
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),
)
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
}
2 changes: 1 addition & 1 deletion precompiles/avs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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.
Expand Down
28 changes: 20 additions & 8 deletions precompiles/avs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,45 @@ import (
"golang.org/x/xerrors"
)

func (p Precompile) GetAVSParamsFromInputs(ctx sdk.Context, args []interface{}) (*keeper.AVSParams, error) {
if len(args) != 4 {
func (p Precompile) GetAVSParamsFromInputs(ctx sdk.Context, args []interface{}) (*keeper.AVSRegisterOrDeregisterParams, error) {
if len(args) != 6 {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, 4, len(args))
}
avsParams := &keeper.AVSParams{}
avsParams := &keeper.AVSRegisterOrDeregisterParams{}
avsName, ok := args[0].(string)
if !ok {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", 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

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 != keeper.RegisterAction && action != keeper.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.

24 changes: 12 additions & 12 deletions proto/exocore/avs/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ package exocore.avs;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "exocore/avs/params.proto";
import "exocore/avs/tx.proto";

option go_package = "github.com/ExocoreNetwork/exocore/x/avs/types";

message QueryAVSInfoReq {
string avs_address = 1 [(gogoproto.customname) = "AVSAddres"];
}

message QueryAVSInfoResponse {
AVSInfo info = 1;
}


// Query defines the gRPC querier service.
service Query {
// Parameters queries the parameters of the module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/ExocoreNetwork/exocore/avs/params";
rpc QueryAVSInfo(QueryAVSInfoReq) returns (QueryAVSInfoResponse) {
option (google.api.http).get = "/ExocoreNetwork/exocore/avs/QueryAVSInfo";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is response type for the Query/Params RPC method.
message QueryParamsResponse {
// params holds all the parameters of this module.
Params params = 1 [(gogoproto.nullable) = false];
}
Loading

0 comments on commit 0c748a7

Please sign in to comment.