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

feat(avs): add precompile for VotingPower #163

Merged
merged 11 commits into from
Aug 19, 2024
3 changes: 1 addition & 2 deletions app/ante/cosmos/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cosmos
import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/authz"
Expand All @@ -28,7 +27,7 @@ func NewAuthzLimiterDecorator(disabledMsgTypes ...string) AuthzLimiterDecorator

func (ald AuthzLimiterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if err := ald.checkDisabledMsgs(tx.GetMsgs(), false, 1); err != nil {
return ctx, errorsmod.Wrapf(errortypes.ErrUnauthorized, err.Error())
return ctx, errortypes.ErrUnauthorized.Wrap(err.Error())
}
return next(ctx, tx, simulate)
}
Expand Down
43 changes: 43 additions & 0 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,49 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "avsAddr",
"type": "address"
}
],
"name": "getAVSUSDValue",
"outputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "avsAddr",
"type": "address"
},
{
"internalType": "string",
"name": "operatorAddr",
"type": "string"
}
],
"name": "getOperatorOptedUSDValue",
"outputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
46 changes: 45 additions & 1 deletion precompiles/avs/avs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,68 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
switch method.Name {
case MethodRegisterAVS:
bz, err = p.RegisterAVS(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
trestinlsd marked this conversation as resolved.
Show resolved Hide resolved
}
case MethodDeregisterAVS:
bz, err = p.DeregisterAVS(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodUpdateAVS:
bz, err = p.UpdateAVS(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodRegisterOperatorToAVS:
bz, err = p.BindOperatorToAVS(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodDeregisterOperatorFromAVS:
bz, err = p.UnbindOperatorToAVS(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodCreateAVSTask:
bz, err = p.CreateAVSTask(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodRegisterBLSPublicKey:
bz, err = p.RegisterBLSPublicKey(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodGetOptinOperators:
bz, err = p.GetOptedInOperatorAccAddrs(ctx, contract, method, args)
case MethodSubmitProof:
bz, err = p.SubmitProof(ctx, evm.Origin, contract, stateDB, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(false)
}
case MethodGetRegisteredPubkey:
bz, err = p.GetRegisteredPubkey(ctx, contract, method, args)
case MethodGetAVSUSDValue:
bz, err = p.GetAVSUSDValue(ctx, contract, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(common.Big0)
}
case MethodGetOperatorOptedUSDValue:
bz, err = p.GetOperatorOptedUSDValue(ctx, contract, method, args)
if err != nil {
ctx.Logger().Error("internal error when calling avs precompile", "module", "avs precompile", "method", method.Name, "err", err)
bz, err = method.Outputs.Pack(common.Big0)
}
}

if err != nil {
Expand All @@ -129,7 +173,7 @@ func (Precompile) IsTransaction(methodID string) bool {
case MethodRegisterAVS, MethodDeregisterAVS, MethodUpdateAVS, MethodRegisterOperatorToAVS,
MethodDeregisterOperatorFromAVS, MethodCreateAVSTask, MethodRegisterBLSPublicKey, MethodSubmitProof:
return true
case MethodGetRegisteredPubkey, MethodGetOptinOperators:
case MethodGetRegisteredPubkey, MethodGetOptinOperators, MethodGetAVSUSDValue, MethodGetOperatorOptedUSDValue:
return false
default:
return false
Expand Down
16 changes: 16 additions & 0 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ interface IAVSManager {
/// @param avsAddress avs address
function getOptInOperators(address avsAddress) external returns (string[] calldata operators);

/// @dev getAVSUSDValue is a function to retrieve the USD share of specified Avs.
/// @param avsAddr The address of the avs
/// @return amount The total USD share of specified operator and Avs.
function getAVSUSDValue(
address avsAddr
) external view returns (uint256 amount);

/// @dev getOperatorOptedUSDValue is a function to retrieve the USD share of specified operator and Avs.
/// @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
) external view returns (uint256 amount);

/// @dev RegisterBLSPublicKey Emitted when `operator` registers with the public keys `pubKey`.
/// @param operator the address of the delegator
/// @param pubKey the address of the validator
Expand Down
Loading
Loading