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

fix:add missing fields #64

Merged
merged 4 commits into from
May 30, 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
44 changes: 34 additions & 10 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
[
{
"inputs": [
{
"internalType": "string[]",
"name": "avsOwnerAddress",
"type": "string[]"
},
{
"internalType": "string",
"name": "avsName",
"type": "string"
},
{
"internalType": "string",
"name": "avsAddress",
"name": "slashContractAddr",
"type": "string"
},
{
"internalType": "string",
"name": "operatorAddress",
"type": "string"
"internalType": "string[]",
"name": "assetID",
"type": "string[]"
},
{
"internalType": "uint64",
"name": "action",
"type": "uint64"
},
{
"internalType": "string",
"name": "avsOwnerAddress",
"type": "string"
"internalType": "uint64",
"name": "minSelfDelegation",
"type": "uint64"
},
{
"internalType": "string",
"name": "assetID",
"type": "string"
"internalType": "uint64",
"name": "unbondingPeriod",
"type": "uint64"
}
],
"name": "AVSAction",
Expand All @@ -42,5 +47,24 @@
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint64",
"name": "action",
"type": "uint64"
}
],
"name": "OperatorOptAction",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
11 changes: 7 additions & 4 deletions precompiles/avs/avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
return p.Precompile.RequiredGas(input, p.IsTransaction(method.Name))
}

// Run executes the precompiled contract AVSInfoRegisterOrDeregister methods defined in the ABI.
// Run executes the precompiled contract RegisterOrDeregisterAVSInfo methods defined in the ABI.
func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz []byte, err error) {
ctx, stateDB, method, initialGas, args, err := p.RunSetup(evm, contract, readOnly, p.IsTransaction)
if err != nil {
Expand All @@ -84,8 +84,11 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
// It avoids panics and returns the out of gas error so the EVM can continue gracefully.
defer cmn.HandleGasError(ctx, contract, initialGas, &err)()

if method.Name == MethodAVSAction {
bz, err = p.AVSInfoRegisterOrDeregister(ctx, evm.Origin, contract, stateDB, method, args)
switch method.Name {
case MethodAVSAction:
bz, err = p.RegisterOrDeregisterAVSInfo(ctx, evm.Origin, contract, stateDB, method, args)
case MethodOperatorAction:
bz, err = p.BindOperatorToAVS(ctx, evm.Origin, contract, stateDB, method, args)
}

if err != nil {
Expand All @@ -108,7 +111,7 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
// - AVSRegister
func (Precompile) IsTransaction(methodID string) bool {
switch methodID {
case MethodAVSAction:
case MethodAVSAction, MethodOperatorAction:
return true
default:
return false
Expand Down
19 changes: 14 additions & 5 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ IAVSManager constant AVSMANAGER_CONTRACT = IAVSManager(
/// @custom:address 0x0000000000000000000000000000000000000902
interface IAVSManager {
function AVSAction(
string[] memory avsOwnerAddress,
string memory avsName,
string memory avsAddress,
string memory operatorAddress,
string memory slashContractAddr,
string[] memory assetID,
uint64 action,
string memory avsOwnerAddress,
string memory assetID
uint64 minSelfDelegation,
uint64 unbondingPeriod
) external returns (bool success);
}


function OperatorOptAction(
uint64 action
) external returns (bool success);

}


28 changes: 16 additions & 12 deletions precompiles/avs/avs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ func (s *AVSManagerPrecompileSuite) TestIsTransaction() {
isTx bool
}{
{
avs.MethodAVSAction,
s.precompile.Methods[avs.MethodAVSAction].Name,
avs.MethodOperatorAction,
s.precompile.Methods[avs.MethodOperatorAction].Name,
true,
},
{
"invalid",
"invalid",
false,
avs.MethodAVSAction,
s.precompile.Methods[avs.MethodAVSAction].Name,
true,
},
}

Expand All @@ -39,9 +39,12 @@ func (s *AVSManagerPrecompileSuite) TestIsTransaction() {
}

func (s *AVSManagerPrecompileSuite) TestAVSManager() {
avsName, avsAddres, operatorAddress := "avsTest", "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr", "exo13h6xg79g82e2g2vhjwg7j4r2z2hlncelwutkjr"
avsName, operatorAddress, slashAddress := "avsTest", "exo18cggcpvwspnd5c6ny8wrqxpffj5zmhklprtnph", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB"

avsAction := avskeeper.RegisterAction
avsOwnderAddress, assetID := "0x123", "0x1"
from := s.Address
avsOwnerAddress := []string{"0x3e108c058e8066DA635321Dc3018294cA82ddEdf", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB", from.String()}
assetID := []string{"11", "22", "33"}
registerOperator := func() {
registerReq := &operatortypes.RegisterOperatorReq{
FromAddress: operatorAddress,
Expand All @@ -56,12 +59,13 @@ func (s *AVSManagerPrecompileSuite) TestAVSManager() {
// prepare the call input for delegation test
input, err := s.precompile.Pack(
avs.MethodAVSAction,
avsOwnerAddress,
avsName,
avsAddres,
operatorAddress,
uint64(avsAction),
avsOwnderAddress,
slashAddress,
assetID,
uint64(avsAction),
uint64(10),
uint64(7),
)
s.Require().NoError(err, "failed to pack input")
return s.Address, input
Expand Down Expand Up @@ -99,9 +103,9 @@ func (s *AVSManagerPrecompileSuite) TestAVSManager() {

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

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

contractAddr := contract.Address()
// Build and sign Ethereum transaction
Expand Down
78 changes: 73 additions & 5 deletions precompiles/avs/tx.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
package avs

import (
"fmt"
"slices"

errorsmod "cosmossdk.io/errors"

exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
util "github.com/ExocoreNetwork/exocore/utils"
avskeeper "github.com/ExocoreNetwork/exocore/x/avs/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
cmn "github.com/evmos/evmos/v14/precompiles/common"
"golang.org/x/xerrors"
)

const (
// AVSRegister defines the ABI method name for the avs
// related transaction.
MethodAVSAction = "AVSAction"
MethodAVSAction = "AVSAction"
MethodOperatorAction = "OperatorOptAction"
)

// AVSInfoRegister register the avs related information and change the state in avs keeper module.
func (p Precompile) AVSInfoRegisterOrDeregister(
func (p Precompile) RegisterOrDeregisterAVSInfo(
ctx sdk.Context,
_ common.Address,
_ *vm.Contract,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
args []interface{},
) ([]byte, error) {
// parse the avs input params first.
avsParams, err := p.GetAVSParamsFromInputs(ctx, args)
if err != nil {
return nil, errorsmod.Wrap(err, "parse args error")
}
avsAddress, err := util.ProcessAddress(contract.Address().String())
if err != nil {
return nil, errorsmod.Wrap(err, "parse avsAddress error")
}

callerAddress, err := util.ProcessAddress(contract.CallerAddress.String())
if err != nil {
return nil, errorsmod.Wrap(err, "parse callerAddress error")
}

if !slices.Contains(avsParams.AvsOwnerAddress, callerAddress) {
return nil, errorsmod.Wrap(err, "not qualified to registerOrDeregister")
}

avsParams.AvsAddress = avsAddress
if err != nil {
return nil, err
}
Expand All @@ -33,3 +60,44 @@ func (p Precompile) AVSInfoRegisterOrDeregister(
}
return method.Outputs.Pack(true)
}

func (p Precompile) BindOperatorToAVS(
ctx sdk.Context,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
args []interface{},
) ([]byte, error) {
if len(args) != 2 {
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 2, len(args))
}
operatorParams := &avskeeper.OperatorOptParams{}
action, ok := args[0].(uint64)
if !ok || (action != avskeeper.RegisterAction && action != avskeeper.DeRegisterAction) {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 0, "uint64", action)
}
operatorParams.Action = action

callerAddress, err := util.ProcessAddress(contract.CallerAddress.String())
if err != nil {
return nil, errorsmod.Wrap(err, "parse callerAddress error")
}

operatorParams.OperatorAddress = callerAddress

avsAddress, err := util.ProcessAddress(contract.Address().String())
if err != nil {
return nil, errorsmod.Wrap(err, "parse avsAddress error")
}
operatorParams.AvsAddress = avsAddress
if err != nil {
return nil, err
}
err = p.avsKeeper.AVSInfoUpdateWithOperator(ctx, operatorParams)
if err != nil {
return nil, err
}

return method.Outputs.Pack(true)
}
Loading
Loading