Skip to content

Commit

Permalink
replace evm.origin with sender address
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Sep 5, 2024
1 parent 8b2a247 commit 33dc0fc
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 104 deletions.
16 changes: 16 additions & 0 deletions precompiles/avs/IAVSManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ IAVSManager constant AVSMANAGER_CONTRACT = IAVSManager(
/// @custom:address 0x0000000000000000000000000000000000000901
interface IAVSManager {
/// @dev Register AVS contract to EXO.
/// @param sender The external address for calling this method.
/// @param avsName The name of AVS.
/// @param minStakeAmount The minimum amount of funds staked by each operator.
/// @param taskAddr The task address of AVS.
Expand All @@ -29,6 +30,7 @@ interface IAVSManager {
///3.avsReward The proportion of reward for AVS.
///4.avsSlash The proportion of slash for AVS.
function registerAVS(
address sender,
string memory avsName,
uint64 minStakeAmount,
address taskAddr,
Expand All @@ -43,6 +45,7 @@ interface IAVSManager {
) external returns (bool success);

/// @dev Update AVS info to EXO.
/// @param sender The external address for calling this method.
/// @param avsName The name of AVS.
/// @param minStakeAmount The minimum amount of funds staked by each operator.
/// @param taskAddr The task address of AVS.
Expand All @@ -58,6 +61,7 @@ interface IAVSManager {
///3.avsReward The proportion of reward for AVS.
///4.avsSlash The proportion of slash for AVS.
function updateAVS(
address sender,
string memory avsName,
uint64 minStakeAmount,
address taskAddr,
Expand All @@ -72,28 +76,36 @@ interface IAVSManager {
) external returns (bool success);

/// @dev Deregister avs from exo
/// @param sender The external address for calling this method.
/// @param avsName The name of AVS.
function deregisterAVS(
address sender,
string memory avsName
) external returns (bool success);

/// @dev RegisterOperatorToAVS operator opt in current avs
/// @param sender The external address for calling this method.
function registerOperatorToAVS(
address sender
) external returns (bool success);

/// @dev DeregisterOperatorFromAVS operator opt out current avs
/// @param sender The external address for calling this method.
function deregisterOperatorFromAVS(
address sender
) external returns (bool success);


/// @dev CreateTask , avs owner create a new task
/// @param sender The external address for calling this method.
/// @param name The name of the task.
/// @param hash The data supplied by the contract, usually ABI-encoded.
/// @param taskResponsePeriod The deadline for task response.
/// @param taskChallengePeriod The challenge period for the task.
/// @param thresholdPercentage The signature threshold percentage.
/// @param taskStatisticalPeriod The statistical period for the task.
function createTask(
address sender,
string memory name,
bytes calldata hash,
uint64 taskResponsePeriod,
Expand All @@ -103,11 +115,13 @@ interface IAVSManager {
) external returns (bool success);

/// @dev challenge , this function enables a challenger to raise and resolve a challenge.
/// @param sender The external address for calling this method.
/// @param taskHash The data supplied by the contract, usually ABI-encoded.
/// @param taskID The id of task.
/// @param taskResponseHash The hash of task response.
/// @param operatorAddress operator address.
function challenge(
address sender,
bytes calldata taskHash,
uint64 taskID,
bytes calldata taskResponseHash,
Expand All @@ -132,11 +146,13 @@ interface IAVSManager {


/// @dev Called by the avs manager service register an operator as the owner of a BLS public key.
/// @param sender The external address for calling this method.
/// @param name the name of public keys
/// @param pubKey the public keys of the operator
/// @param pubkeyRegistrationSignature the public keys of the operator
/// @param pubkeyRegistrationMessageHash the public keys of the operator
function registerBLSPublicKey(
address sender,
string calldata name,
bytes calldata pubKey,
bytes calldata pubkeyRegistrationSignature,
Expand Down
99 changes: 62 additions & 37 deletions precompiles/avs/tx.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:dupl
package avs

import (
Expand Down Expand Up @@ -31,7 +32,7 @@ const (
// AVSInfoRegister register the avs related information and change the state in avs keeper module.
func (p Precompile) RegisterAVS(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
Expand All @@ -42,7 +43,7 @@ func (p Precompile) RegisterAVS(
if err != nil {
return nil, errorsmod.Wrap(err, "parse args error")
}
if !slices.Contains(avsParams.AvsOwnerAddress, sdk.AccAddress(origin.Bytes()).String()) {
if !slices.Contains(avsParams.AvsOwnerAddress, avsParams.CallerAddress) {
return nil, errorsmod.Wrap(err, "not qualified to registerOrDeregister")
}
// The AVS registration is done by the calling contract.
Expand All @@ -60,7 +61,7 @@ func (p Precompile) RegisterAVS(

func (p Precompile) DeregisterAVS(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
Expand All @@ -70,17 +71,20 @@ func (p Precompile) DeregisterAVS(
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodDeregisterAVS].Inputs), len(args))
}
avsParams := &avstypes.AVSRegisterOrDeregisterParams{}

avsName, ok := args[0].(string)
callerAddress, ok := args[0].(common.Address)
if !ok || (callerAddress == common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", callerAddress)
}
avsParams.CallerAddress = sdk.AccAddress(callerAddress[:]).String()
avsName, ok := args[1].(string)
if !ok || avsName == "" {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", avsName)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "string", avsName)
}
avsParams.AvsName = avsName

avsParams.AvsAddress = contract.CallerAddress.String()
avsParams.Action = avskeeper.DeRegisterAction
// validates that this is owner
avsParams.CallerAddress = sdk.AccAddress(origin[:]).String()

err := p.avsKeeper.UpdateAVSInfo(ctx, avsParams)
if err != nil {
Expand All @@ -91,7 +95,7 @@ func (p Precompile) DeregisterAVS(

func (p Precompile) UpdateAVS(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
Expand All @@ -104,7 +108,6 @@ func (p Precompile) UpdateAVS(
}

avsParams.AvsAddress = contract.CallerAddress.String()
avsParams.CallerAddress = sdk.AccAddress(origin[:]).String()
avsParams.Action = avskeeper.UpdateAction
previousAVSInfo, err := p.avsKeeper.GetAVSInfo(ctx, avsParams.AvsAddress)
if err != nil {
Expand All @@ -124,14 +127,22 @@ func (p Precompile) UpdateAVS(

func (p Precompile) BindOperatorToAVS(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
_ []interface{},
args []interface{},
) ([]byte, error) {
if len(args) != len(p.ABI.Methods[MethodRegisterOperatorToAVS].Inputs) {
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodRegisterOperatorToAVS].Inputs), len(args))
}
callerAddress, ok := args[0].(common.Address)
if !ok || (callerAddress == common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", callerAddress)
}

operatorParams := &avskeeper.OperatorOptParams{}
operatorParams.OperatorAddress = sdk.AccAddress(origin[:]).String()
operatorParams.OperatorAddress = sdk.AccAddress(callerAddress[:]).String()
operatorParams.AvsAddress = contract.CallerAddress.String()
operatorParams.Action = avskeeper.RegisterAction
err := p.avsKeeper.OperatorOptAction(ctx, operatorParams)
Expand All @@ -143,14 +154,21 @@ func (p Precompile) BindOperatorToAVS(

func (p Precompile) UnbindOperatorToAVS(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
_ []interface{},
args []interface{},
) ([]byte, error) {
if len(args) != len(p.ABI.Methods[MethodRegisterOperatorToAVS].Inputs) {
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodRegisterOperatorToAVS].Inputs), len(args))
}
callerAddress, ok := args[0].(common.Address)
if !ok || (callerAddress == common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", callerAddress)
}
operatorParams := &avskeeper.OperatorOptParams{}
operatorParams.OperatorAddress = sdk.AccAddress(origin[:]).String()
operatorParams.OperatorAddress = sdk.AccAddress(callerAddress[:]).String()
operatorParams.AvsAddress = contract.CallerAddress.String()
operatorParams.Action = avskeeper.DeRegisterAction
err := p.avsKeeper.OperatorOptAction(ctx, operatorParams)
Expand All @@ -163,7 +181,7 @@ func (p Precompile) UnbindOperatorToAVS(
// CreateAVSTask Middleware uses exocore's default avstask template to create tasks in avstask module.
func (p Precompile) CreateAVSTask(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
stateDB vm.StateDB,
method *abi.Method,
Expand All @@ -174,7 +192,6 @@ func (p Precompile) CreateAVSTask(
return nil, err
}
params.TaskContractAddress = contract.CallerAddress.String()
params.CallerAddress = sdk.AccAddress(origin[:]).String()
params.TaskID = p.avsKeeper.GetTaskID(ctx, common.HexToAddress(params.TaskContractAddress))
err = p.avsKeeper.CreateAVSTask(ctx, params)
if err != nil {
Expand All @@ -189,7 +206,7 @@ func (p Precompile) CreateAVSTask(
// Challenge Middleware uses exocore's default avstask template to create tasks in avstask module.
func (p Precompile) Challenge(
ctx sdk.Context,
origin common.Address,
_ common.Address,
contract *vm.Contract,
_ vm.StateDB,
method *abi.Method,
Expand All @@ -200,29 +217,33 @@ func (p Precompile) Challenge(
}
challengeParams := &avskeeper.ChallengeParams{}
challengeParams.TaskContractAddress = contract.CallerAddress
challengeParams.CallerAddress = origin
callerAddress, ok := args[0].(common.Address)
if !ok || (callerAddress == common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", callerAddress)
}
challengeParams.CallerAddress = sdk.AccAddress(callerAddress[:]).String()

taskHash, ok := args[0].([]byte)
taskHash, ok := args[1].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "[]byte", taskHash)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", taskHash)
}
challengeParams.TaskHash = taskHash

taskID, ok := args[1].(uint64)
taskID, ok := args[2].(uint64)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "uint64", taskID)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 2, "uint64", taskID)
}
challengeParams.TaskID = taskID

taskResponseHash, ok := args[2].([]byte)
taskResponseHash, ok := args[3].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", taskResponseHash)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, "[]byte", taskResponseHash)
}
challengeParams.TaskResponseHash = taskResponseHash

operatorAddress, ok := args[3].(string)
operatorAddress, ok := args[4].(string)
if !ok || operatorAddress == "" {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, "string", operatorAddress)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 4, "string", operatorAddress)
}
operator, err := sdk.AccAddressFromBech32(operatorAddress)
if err != nil {
Expand All @@ -241,7 +262,7 @@ func (p Precompile) Challenge(
// RegisterBLSPublicKey
func (p Precompile) RegisterBLSPublicKey(
ctx sdk.Context,
origin common.Address,
_ common.Address,
_ *vm.Contract,
_ vm.StateDB,
method *abi.Method,
Expand All @@ -251,28 +272,32 @@ func (p Precompile) RegisterBLSPublicKey(
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodRegisterBLSPublicKey].Inputs), len(args))
}
blsParams := &avskeeper.BlsParams{}
blsParams.Operator = sdk.AccAddress(origin[:]).String()
name, ok := args[0].(string)
callerAddress, ok := args[0].(common.Address)
if !ok || (callerAddress == common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", callerAddress)
}
blsParams.Operator = sdk.AccAddress(callerAddress[:]).String()
name, ok := args[1].(string)
if !ok || name == "" {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", name)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "string", name)
}
blsParams.Name = name

pubkeyBz, ok := args[1].([]byte)
pubkeyBz, ok := args[2].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", pubkeyBz)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, "[]byte", pubkeyBz)
}
blsParams.PubKey = pubkeyBz

pubkeyRegistrationSignature, ok := args[2].([]byte)
pubkeyRegistrationSignature, ok := args[3].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", pubkeyRegistrationSignature)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, "[]byte", pubkeyRegistrationSignature)
}
blsParams.PubkeyRegistrationSignature = pubkeyRegistrationSignature

pubkeyRegistrationMessageHash, ok := args[3].([]byte)
pubkeyRegistrationMessageHash, ok := args[4].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 3, "[]byte", pubkeyRegistrationMessageHash)
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 4, "[]byte", pubkeyRegistrationMessageHash)
}
blsParams.PubkeyRegistrationMessageHash = pubkeyRegistrationMessageHash

Expand Down
Loading

0 comments on commit 33dc0fc

Please sign in to comment.