Skip to content

Commit

Permalink
feat(avs):implement whitelist feature for AVS (imua-xyz#262)
Browse files Browse the repository at this point in the history
* implement whitelist feature for AVS
  • Loading branch information
trestinlsd authored Jan 7, 2025
1 parent de571b1 commit 7d5114d
Show file tree
Hide file tree
Showing 23 changed files with 424 additions and 275 deletions.
24 changes: 12 additions & 12 deletions precompiles/assets/decode_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (ta *TypedArgs) GetUint8(index int) (uint8, error) {
}
val, ok := ta.args[index].(uint8)
if !ok {
return 0, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "uint8", ta.args[index])
return 0, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "uint8", ta.args[index])
}
return val, nil
}
Expand All @@ -44,7 +44,7 @@ func (ta *TypedArgs) GetUint32(index int) (uint32, error) {
}
val, ok := ta.args[index].(uint32)
if !ok {
return 0, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "uint32", ta.args[index])
return 0, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "uint32", ta.args[index])
}
return val, nil
}
Expand All @@ -55,7 +55,7 @@ func (ta *TypedArgs) GetBytes(index int) ([]byte, error) {
}
val, ok := ta.args[index].([]byte)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "[]byte", ta.args[index])
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "[]byte", ta.args[index])
}
return val, nil
}
Expand All @@ -66,7 +66,7 @@ func (ta *TypedArgs) GetString(index int) (string, error) {
}
val, ok := ta.args[index].(string)
if !ok {
return "", fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "string", ta.args[index])
return "", fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "string", ta.args[index])
}
return val, nil
}
Expand All @@ -77,7 +77,7 @@ func (ta *TypedArgs) GetBigInt(index int) (*big.Int, error) {
}
val, ok := ta.args[index].(*big.Int)
if !ok || val == nil {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "*big.Int", ta.args[index])
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "*big.Int", ta.args[index])
}
return val, nil
}
Expand All @@ -88,7 +88,7 @@ func (ta *TypedArgs) GetEVMAddress(index int) (common.Address, error) {
}
val, ok := ta.args[index].(common.Address)
if !ok {
return common.Address{}, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "address", ta.args[index])
return common.Address{}, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "address", ta.args[index])
}
return val, nil
}
Expand All @@ -99,7 +99,7 @@ func (ta *TypedArgs) GetEVMAddressSlice(index int) ([]common.Address, error) {
}
val, ok := ta.args[index].([]common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "[]common.Address", ta.args[index])
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "[]common.Address", ta.args[index])
}
return val, nil
}
Expand All @@ -110,7 +110,7 @@ func (ta *TypedArgs) GetPositiveUint8(index int) (uint8, error) {
return 0, err
}
if val == 0 {
return 0, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "uint8", ta.args[index])
return 0, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "uint8", ta.args[index])
}
return val, nil
}
Expand All @@ -121,7 +121,7 @@ func (ta *TypedArgs) GetPositiveUint32(index int) (uint32, error) {
return 0, err
}
if val == 0 {
return 0, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "uint32", ta.args[index])
return 0, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "uint32", ta.args[index])
}
return val, nil
}
Expand All @@ -132,7 +132,7 @@ func (ta *TypedArgs) GetRequiredBytes(index int) ([]byte, error) {
return nil, err
}
if len(val) == 0 {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "[]byte", ta.args[index])
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "[]byte", ta.args[index])
}
return val, nil
}
Expand All @@ -143,7 +143,7 @@ func (ta *TypedArgs) GetRequiredString(index int) (string, error) {
return "", err
}
if len(val) == 0 {
return "", fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "string", ta.args[index])
return "", fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "string", ta.args[index])
}
return val, nil
}
Expand All @@ -154,7 +154,7 @@ func (ta *TypedArgs) GetPositiveBigInt(index int) (*big.Int, error) {
return nil, err
}
if val.Sign() <= 0 {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, index, "*big.Int", ta.args[index])
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, index, "*big.Int", ta.args[index])
}
return val, nil
}
Expand Down
14 changes: 9 additions & 5 deletions precompiles/avs/IAVSManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ interface IAVSManager {
/// @param slashAddr The slash address of AVS.
/// @param rewardAddr The reward address of AVS.
/// @param avsOwnerAddress The owners who have permission for AVS.
/// @param whitelistAddress The whitelist address of the operator.
/// @param assetIds The basic asset information of AVS.
/// @param avsUnbondingPeriod The unbonding duration of AVS.
/// @param minSelfDelegation The minimum delegation amount for an operator.
Expand All @@ -64,7 +65,8 @@ interface IAVSManager {
address taskAddr,
address slashAddr,
address rewardAddr,
string[] memory avsOwnerAddress,
address[] memory avsOwnerAddress,
address[] memory whitelistAddress,
string[] memory assetIds,
uint64 avsUnbondingPeriod,
uint64 minSelfDelegation,
Expand All @@ -80,6 +82,7 @@ interface IAVSManager {
/// @param slashAddr The slash address of AVS.
/// @param rewardAddr The reward address of AVS.
/// @param avsOwnerAddress The owners who have permission for AVS.
/// @param whitelistAddress The whitelist address of the operator.
/// @param assetIds The basic asset information of AVS.
/// @param avsUnbondingPeriod The unbonding duration of AVS.
/// @param minSelfDelegation The minimum delegation amount for an operator.
Expand All @@ -95,7 +98,8 @@ interface IAVSManager {
address taskAddr,
address slashAddr,
address rewardAddr,
string[] memory avsOwnerAddress,
address[] memory avsOwnerAddress,
address[] memory whitelistAddress,
string[] memory assetIds,
uint64 avsUnbondingPeriod,
uint64 minSelfDelegation,
Expand Down Expand Up @@ -145,7 +149,7 @@ interface IAVSManager {
bytes calldata taskHash,
uint64 taskID,
bytes calldata taskResponseHash,
string memory operatorAddress
address operatorAddress
) external returns (bool success);

/// @dev Called by the avs manager service register an operator as the owner of a BLS public key.
Expand Down Expand Up @@ -183,7 +187,7 @@ interface IAVSManager {
/// QUERIES
/// @dev Returns the pubkey and pubkey hash of an operator
/// @param operator is the operator for whom the key is being registered
function getRegisteredPubkey(string memory operator) external view returns (bytes memory pubkey);
function getRegisteredPubkey(address operator) external view returns (bytes memory pubkey);

/// @dev Returns the operators of all opt-in in the current avs
/// @param avsAddress avs address
Expand All @@ -198,7 +202,7 @@ interface IAVSManager {
/// @param avsAddr The address of the avs
/// @param operatorAddr The address of the operator
/// @return amount The total USD share of specified operator and Avs.
function getOperatorOptedUSDValue(address avsAddr, string memory operatorAddr)
function getOperatorOptedUSDValue(address avsAddr, address operatorAddr)
external
view
returns (uint256 amount);
Expand Down
30 changes: 20 additions & 10 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@
"type": "bytes"
},
{
"internalType": "string",
"internalType": "address",
"name": "operatorAddress",
"type": "string"
"type": "address"
}
],
"name": "challenge",
Expand Down Expand Up @@ -468,9 +468,9 @@
"type": "address"
},
{
"internalType": "string",
"internalType": "address",
"name": "operatorAddr",
"type": "string"
"type": "address"
}
],
"name": "getOperatorOptedUSDValue",
Expand Down Expand Up @@ -506,9 +506,9 @@
{
"inputs": [
{
"internalType": "string",
"internalType": "address",
"name": "operator",
"type": "string"
"type": "address"
}
],
"name": "getRegisteredPubkey",
Expand Down Expand Up @@ -642,9 +642,14 @@
"type": "address"
},
{
"internalType": "string[]",
"internalType": "address[]",
"name": "avsOwnerAddress",
"type": "string[]"
"type": "address[]"
},
{
"internalType": "address[]",
"name": "whitelistAddress",
"type": "address[]"
},
{
"internalType": "string[]",
Expand Down Expand Up @@ -774,9 +779,14 @@
"type": "address"
},
{
"internalType": "string[]",
"internalType": "address[]",
"name": "avsOwnerAddress",
"type": "string[]"
"type": "address[]"
},
{
"internalType": "address[]",
"name": "whitelistAddress",
"type": "address[]"
},
{
"internalType": "string[]",
Expand Down
28 changes: 19 additions & 9 deletions precompiles/avs/avs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ func (suite *AVSManagerPrecompileSuite) TestIsTransaction() {

func (suite *AVSManagerPrecompileSuite) TestRegisterAVS() {
avsName, slashAddress, rewardAddress := "avsTest", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB"
avsOwnerAddress := []string{
sdk.AccAddress(suite.Address.Bytes()).String(),
sdk.AccAddress(utiltx.GenerateAddress().Bytes()).String(),
sdk.AccAddress(utiltx.GenerateAddress().Bytes()).String(),
avsOwnerAddress := []common.Address{
suite.Address,
utiltx.GenerateAddress(),
utiltx.GenerateAddress(),
}
exoWhiteListAddress := []common.Address{
utiltx.GenerateAddress(),
utiltx.GenerateAddress(),
}
assetID := suite.AssetIDs
minStakeAmount, taskAddr := uint64(3), "0xDF907c29719154eb9872f021d21CAE6E5025d7aB"
Expand All @@ -108,6 +112,7 @@ func (suite *AVSManagerPrecompileSuite) TestRegisterAVS() {
common.HexToAddress(slashAddress),
common.HexToAddress(rewardAddress),
avsOwnerAddress,
exoWhiteListAddress,
assetID,
avsUnbondingPeriod,
minSelfDelegation,
Expand Down Expand Up @@ -344,10 +349,14 @@ func (suite *AVSManagerPrecompileSuite) TestDeregisterAVS() {

func (suite *AVSManagerPrecompileSuite) TestUpdateAVS() {
avsName, slashAddress, rewardAddress := "avsTest", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB", "0xDF907c29719154eb9872f021d21CAE6E5025d7aB"
avsOwnerAddress := []string{
sdk.AccAddress(suite.Address.Bytes()).String(),
sdk.AccAddress(utiltx.GenerateAddress().Bytes()).String(),
sdk.AccAddress(utiltx.GenerateAddress().Bytes()).String(),
avsOwnerAddress := []common.Address{
suite.Address,
utiltx.GenerateAddress(),
utiltx.GenerateAddress(),
}
exoWhiteAddresses := []common.Address{
utiltx.GenerateAddress(),
utiltx.GenerateAddress(),
}
assetID := suite.AssetIDs
minStakeAmount, taskAddr := uint64(3), "0x3e108c058e8066DA635321Dc3018294cA82ddEdf"
Expand All @@ -364,6 +373,7 @@ func (suite *AVSManagerPrecompileSuite) TestUpdateAVS() {
common.HexToAddress(slashAddress),
common.HexToAddress(rewardAddress),
avsOwnerAddress,
exoWhiteAddresses,
assetID,
avsUnbondingPeriod,
minSelfDelegation,
Expand All @@ -382,7 +392,7 @@ func (suite *AVSManagerPrecompileSuite) TestUpdateAVS() {
AvsAddress: suite.Address.String(),
SlashAddr: slashAddress,
RewardAddr: rewardAddress,
AvsOwnerAddress: avsOwnerAddress,
AvsOwnerAddress: []string{sdk.AccAddress(suite.Address.Bytes()).String()},
AssetIDs: assetID,
AvsUnbondingPeriod: avsUnbondingPeriod,
MinSelfDelegation: minSelfDelegation,
Expand Down
32 changes: 17 additions & 15 deletions precompiles/avs/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ func (p Precompile) GetRegisteredPubkey(
if len(args) != len(p.ABI.Methods[MethodGetRegisteredPubkey].Inputs) {
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, len(p.ABI.Methods[MethodGetRegisteredPubkey].Inputs), len(args))
}
addr, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", addr)
addr, ok := args[0].(common.Address)
if !ok || addr == (common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", addr)
}
blsPubKeyInfo, err := p.avsKeeper.GetOperatorPubKey(ctx, addr)
var accAddr sdk.AccAddress = addr[:]
blsPubKeyInfo, err := p.avsKeeper.GetOperatorPubKey(ctx, accAddr.String())
if err != nil {
if errors.Is(err, avstype.ErrNoKeyInTheStore) {
return method.Outputs.Pack([]byte{})
Expand All @@ -64,7 +65,7 @@ func (p Precompile) GetOptedInOperatorAccAddrs(

addr, ok := args[0].(common.Address)
if !ok || addr == (common.Address{}) {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", addr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", addr)
}

list, err := p.avsKeeper.GetOperatorKeeper().GetOptedInOperatorListByAVS(ctx, strings.ToLower(addr.String()))
Expand All @@ -86,7 +87,7 @@ func (p Precompile) GetAVSUSDValue(
}
addr, ok := args[0].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", addr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", addr)
}
amount, err := p.avsKeeper.GetOperatorKeeper().GetAVSUSDValue(ctx, addr.String())
if err != nil {
Expand All @@ -110,13 +111,14 @@ func (p Precompile) GetOperatorOptedUSDValue(
}
avsAddr, ok := args[0].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", avsAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", avsAddr)
}
operatorAddr, ok := args[1].(string)
addr, ok := args[1].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "string", operatorAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 1, "common.Address", addr)
}
amount, err := p.avsKeeper.GetOperatorKeeper().GetOperatorOptedUSDValue(ctx, strings.ToLower(avsAddr.String()), operatorAddr)
var operatorAddr sdk.AccAddress = addr[:]
amount, err := p.avsKeeper.GetOperatorKeeper().GetOperatorOptedUSDValue(ctx, strings.ToLower(avsAddr.String()), operatorAddr.String())
if err != nil {
if errors.Is(err, avstype.ErrNoKeyInTheStore) {
return method.Outputs.Pack(common.Big0)
Expand All @@ -137,7 +139,7 @@ func (p Precompile) GetAVSEpochIdentifier(
}
addr, ok := args[0].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", addr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", addr)
}

avs, err := p.avsKeeper.GetAVSInfo(ctx, addr.String())
Expand All @@ -163,7 +165,7 @@ func (p Precompile) IsOperator(
}
operatorAddr, ok := args[0].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", operatorAddr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", operatorAddr)
}

param := operatorAddr[:]
Expand All @@ -183,11 +185,11 @@ func (p Precompile) GetTaskInfo(
}
addr, ok := args[0].(common.Address)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "common.Address", addr)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "common.Address", addr)
}
taskID, ok := args[1].(uint64)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "uint64", taskID)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 1, "uint64", taskID)
}

task, err := p.avsKeeper.GetTaskInfo(ctx, strconv.FormatUint(taskID, 10), addr.String())
Expand Down Expand Up @@ -215,7 +217,7 @@ func (p Precompile) GetCurrentEpoch(
}
epochIdentifier, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", epochIdentifier)
return nil, fmt.Errorf(exocmn.ErrContractInputParamOrType, 0, "string", epochIdentifier)
}
epoch, flag := p.avsKeeper.GetEpochKeeper().GetEpochInfo(ctx, epochIdentifier)
if !flag {
Expand Down
Loading

0 comments on commit 7d5114d

Please sign in to comment.