Skip to content

Commit

Permalink
refactor:refactor avs logic
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Jul 11, 2024
1 parent 7cd1cfd commit b638aca
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 23 deletions.
14 changes: 10 additions & 4 deletions precompiles/avs/avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
defer cmn.HandleGasError(ctx, contract, initialGas, &err)()

switch method.Name {
case MethodAVSAction:
bz, err = p.RegisterOrDeregisterAVSInfo(ctx, evm.Origin, contract, stateDB, method, args)
case MethodOperatorAction:
case MethodRegisterAVS:
bz, err = p.RegisterAVS(ctx, evm.Origin, contract, stateDB, method, args)
case MethodDeregisterAVS:
bz, err = p.DeregisterAVS(ctx, evm.Origin, contract, stateDB, method, args)
case MethodUpdateAVS:
bz, err = p.UpdateAVS(ctx, evm.Origin, contract, stateDB, method, args)
case MethodRegisterOperatorToAVS:
bz, err = p.BindOperatorToAVS(ctx, evm.Origin, contract, stateDB, method, args)
case MethodDeregisterOperatorFromAVS:
bz, err = p.UnbindOperatorToAVS(ctx, evm.Origin, contract, stateDB, method, args)
}

if err != nil {
Expand All @@ -111,7 +117,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, MethodOperatorAction:
case MethodRegisterAVS, MethodDeregisterAVS, MethodUpdateAVS, MethodRegisterOperatorToAVS, MethodDeregisterOperatorFromAVS:
return true
default:
return false
Expand Down
29 changes: 25 additions & 4 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,43 @@ IAVSManager constant AVSMANAGER_CONTRACT = IAVSManager(
/// @dev The interface through which solidity contracts will interact with AVS-Manager
/// @custom:address 0x0000000000000000000000000000000000000902
interface IAVSManager {
function AVSAction(
function RegisterAVS(
string[] memory avsOwnerAddress,
string memory avsName,
string memory rewardContractAddr,
string memory slashContractAddr,
string[] memory assetID,
uint64 minSelfDelegation,
uint64 unbondingPeriod,
string memory epochIdentifier
) external returns (bool success);

function UpdateAVS(
string[] memory avsOwnerAddress,
string memory avsName,
string memory rewardContractAddr,
string memory slashContractAddr,
string[] memory assetID,
uint64 action,
uint64 minSelfDelegation,
uint64 unbondingPeriod,
string memory epochIdentifier
) external returns (bool success);


function OperatorOptAction(
uint64 action
function DeregisterAVS(
string[] memory avsOwnerAddress,
string memory avsName
) external returns (bool success);

function RegisterOperatorToAVS(
) external returns (bool success);

function DeregisterOperatorFromAVS(
) external returns (bool success);




}


132 changes: 129 additions & 3 deletions precompiles/avs/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
)

const (
MethodAVSAction = "AVSAction"
MethodOperatorAction = "OperatorOptAction"
MethodRegisterOperatorToAVS = "RegisterOperatorToAVS"
MethodDeregisterOperatorFromAVS = "DeregisterOperatorFromAVS"
MethodRegisterAVS = "RegisterAVS"
MethodUpdateAVS = "UpdateAVS"
MethodDeregisterAVS = "DeregisterAVS"
)

// AVSInfoRegister register the avs related information and change the state in avs keeper module.
func (p Precompile) RegisterOrDeregisterAVSInfo(
func (p Precompile) RegisterAVS(
ctx sdk.Context,
_ common.Address,
contract *vm.Contract,
Expand Down Expand Up @@ -51,6 +54,88 @@ func (p Precompile) RegisterOrDeregisterAVSInfo(
}

avsParams.AvsAddress = avsAddress
avsParams.Action = avskeeper.RegisterAction

if err != nil {
return nil, err
}
err = p.avsKeeper.AVSInfoUpdate(ctx, avsParams)
if err != nil {
return nil, err
}
return method.Outputs.Pack(true)
}

func (p Precompile) DeregisterAVS(
ctx sdk.Context,
_ common.Address,
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
avsParams.Action = avskeeper.DeRegisterAction

if err != nil {
return nil, err
}
err = p.avsKeeper.AVSInfoUpdate(ctx, avsParams)
if err != nil {
return nil, err
}
return method.Outputs.Pack(true)
}

func (p Precompile) UpdateAVS(
ctx sdk.Context,
_ common.Address,
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
avsParams.Action = avskeeper.UpdateAction

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -91,6 +176,47 @@ func (p Precompile) BindOperatorToAVS(
return nil, errorsmod.Wrap(err, "parse avsAddress error")
}
operatorParams.AvsAddress = avsAddress
operatorParams.Action = avskeeper.RegisterAction
err = p.avsKeeper.AVSInfoUpdateWithOperator(ctx, operatorParams)
if err != nil {
return nil, err
}

return method.Outputs.Pack(true)
}

func (p Precompile) UnbindOperatorToAVS(
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
operatorParams.Action = avskeeper.DeRegisterAction

if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions precompiles/avs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

func (p Precompile) GetAVSParamsFromInputs(_ sdk.Context, args []interface{}) (*avstypes.AVSRegisterOrDeregisterParams, error) {
if len(args) != len(p.ABI.Methods[MethodAVSAction].Inputs) {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodAVSAction].Inputs), len(args))
if len(args) != len(p.ABI.Methods[MethodRegisterAVS].Inputs) {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodRegisterAVS].Inputs), len(args))
}
avsParams := &avstypes.AVSRegisterOrDeregisterParams{}
avsOwnerAddress, ok := args[0].([]string)
Expand Down
16 changes: 9 additions & 7 deletions proto/exocore/avs/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,29 @@ message AVSInfo {
string avs_address = 2;
// slash address of avs
string slash_addr = 3;
// reward address of avs
string reward_addr = 4;
// the owner who has permission for avs
repeated string avs_owner_address = 4;
repeated string avs_owner_address = 5;
// asset_basic_info is all the basic asset information of the avs.
repeated string asset_id = 5;
repeated string asset_id = 6;
// unbonding duration of avs.
uint32 avs_unbonding_period = 6;
uint32 avs_unbonding_period = 7;
// the operator minimum delegation amount.
string min_self_delegation = 7
string min_self_delegation = 8
[
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
//avs epoch ,Subsequently will be handled by the epochs module.
string epoch_identifier = 8;
string epoch_identifier = 9;
// registered operator of avs
repeated string operator_address = 9;
repeated string operator_address = 10;

// Effective current epoch, accounting for current_epoch + 1
// and current_epoch is the integer identifier of the epoch module
int64 starting_epoch = 10;
int64 starting_epoch = 11;
}

// RegisterAVSReq is requst to register avs
Expand Down
8 changes: 5 additions & 3 deletions x/avs/keeper/avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func (k *Keeper) GetAVSMinimumSelfDelegation(ctx sdk.Context, avsAddr string) (s
}

// GetEpochEndAVSs returns the AVS list where the current block marks the end of their epoch.
func (k *Keeper) GetEpochEndAVSs(ctx sdk.Context) ([]string, error) {
func (k *Keeper) GetEpochEndAVSs(ctx sdk.Context, epochIdentifier string, epochNumber int64) ([]string, error) {
var avsList []types.AVSInfo
k.IterateAVSInfo(ctx, func(_ int64, epochEndAVSInfo types.AVSInfo) (stop bool) {
avsList = append(avsList, epochEndAVSInfo)
k.IterateAVSInfo(ctx, func(_ int64, avsInfo types.AVSInfo) (stop bool) {
if epochIdentifier == avsInfo.EpochIdentifier && epochNumber > avsInfo.StartingEpoch {
avsList = append(avsList, avsInfo)
}
return false
})

Expand Down
1 change: 1 addition & 0 deletions x/avs/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ type OperatorOptParams struct {
const (
RegisterAction = 1
DeRegisterAction = 2
UpdateAction = 3
)

0 comments on commit b638aca

Please sign in to comment.