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: add avs manager #19

Merged
merged 9 commits into from
Apr 10, 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
35 changes: 22 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"path/filepath"
"sort"

"github.com/ExocoreNetwork/exocore/x/avs"
"github.com/ExocoreNetwork/exocore/x/operator"
operatorKeeper "github.com/ExocoreNetwork/exocore/x/operator/keeper"

exoslash "github.com/ExocoreNetwork/exocore/x/slash"

avsManagerKeeper "github.com/ExocoreNetwork/exocore/x/avs/keeper"
avsManagerTypes "github.com/ExocoreNetwork/exocore/x/avs/types"
slashKeeper "github.com/ExocoreNetwork/exocore/x/slash/keeper"
exoslashTypes "github.com/ExocoreNetwork/exocore/x/slash/types"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
Expand All @@ -32,18 +43,12 @@ import (
depositKeeper "github.com/ExocoreNetwork/exocore/x/deposit/keeper"
depositTypes "github.com/ExocoreNetwork/exocore/x/deposit/types"

"github.com/ExocoreNetwork/exocore/x/operator"
operatorKeeper "github.com/ExocoreNetwork/exocore/x/operator/keeper"
operatorTypes "github.com/ExocoreNetwork/exocore/x/operator/types"

"github.com/ExocoreNetwork/exocore/x/reward"
rewardKeeper "github.com/ExocoreNetwork/exocore/x/reward/keeper"
rewardTypes "github.com/ExocoreNetwork/exocore/x/reward/types"

exoslash "github.com/ExocoreNetwork/exocore/x/slash"
slashKeeper "github.com/ExocoreNetwork/exocore/x/slash/keeper"
exoslashTypes "github.com/ExocoreNetwork/exocore/x/slash/types"

"github.com/ExocoreNetwork/exocore/x/withdraw"
withdrawKeeper "github.com/ExocoreNetwork/exocore/x/withdraw/keeper"
withdrawTypes "github.com/ExocoreNetwork/exocore/x/withdraw/types"
Expand Down Expand Up @@ -268,6 +273,7 @@ var (
withdraw.AppModuleBasic{},
reward.AppModuleBasic{},
exoslash.AppModuleBasic{},
avs.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -350,8 +356,8 @@ type ExocoreApp struct {
WithdrawKeeper withdrawKeeper.Keeper
RewardKeeper rewardKeeper.Keeper
OperatorKeeper operatorKeeper.Keeper

ExoSlashKeeper slashKeeper.Keeper
ExoSlashKeeper slashKeeper.Keeper
AVSManagerKeeper avsManagerKeeper.Keeper
// the module manager
mm *module.Manager

Expand Down Expand Up @@ -434,6 +440,7 @@ func NewExocoreApp(
rewardTypes.StoreKey,
exoslashTypes.StoreKey,
operatorTypes.StoreKey,
avsManagerTypes.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -649,11 +656,8 @@ func NewExocoreApp(

app.WithdrawKeeper = *withdrawKeeper.NewKeeper(appCodec, keys[withdrawTypes.StoreKey], app.AssetsKeeper, app.DepositKeeper)
app.RewardKeeper = *rewardKeeper.NewKeeper(appCodec, keys[rewardTypes.StoreKey], app.AssetsKeeper)
app.ExoSlashKeeper = slashKeeper.NewKeeper(
appCodec,
keys[exoslashTypes.StoreKey],
app.AssetsKeeper,
)
app.ExoSlashKeeper = slashKeeper.NewKeeper(appCodec, keys[exoslashTypes.StoreKey], app.AssetsKeeper)
app.AVSManagerKeeper = *avsManagerKeeper.NewKeeper(appCodec, keys[avsManagerTypes.StoreKey], &app.OperatorKeeper, app.AssetsKeeper)
// We call this after setting the hooks to ensure that the hooks are set on the keeper
evmKeeper.WithPrecompiles(
evmkeeper.AvailablePrecompiles(
Expand All @@ -667,6 +671,7 @@ func NewExocoreApp(
app.WithdrawKeeper,
app.ExoSlashKeeper,
app.RewardKeeper,
app.AVSManagerKeeper,
),
)
epochsKeeper := epochskeeper.NewKeeper(appCodec, keys[epochstypes.StoreKey])
Expand Down Expand Up @@ -839,6 +844,7 @@ func NewExocoreApp(
withdraw.NewAppModule(appCodec, app.WithdrawKeeper),
reward.NewAppModule(appCodec, app.RewardKeeper),
exoslash.NewAppModule(appCodec, app.ExoSlashKeeper),
avs.NewAppModule(appCodec, app.AVSManagerKeeper),
)

// During begin block slashing happens after reward.BeginBlocker so that
Expand Down Expand Up @@ -882,6 +888,7 @@ func NewExocoreApp(
withdrawTypes.ModuleName,
rewardTypes.ModuleName,
exoslashTypes.ModuleName,
avsManagerTypes.ModuleName,
)

// NOTE: fee market module must go last in order to retrieve the block gas used.
Expand Down Expand Up @@ -920,6 +927,7 @@ func NewExocoreApp(
withdrawTypes.ModuleName,
rewardTypes.ModuleName,
exoslashTypes.ModuleName,
avsManagerTypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -964,6 +972,7 @@ func NewExocoreApp(
// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
consensusparamtypes.ModuleName,
avsManagerTypes.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down
46 changes: 46 additions & 0 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"inputs": [
{
"internalType": "string",
"name": "avsName",
"type": "string"
},
{
"internalType": "string",
"name": "avsAddress",
"type": "string"
},
{
"internalType": "string",
"name": "operatorAddress",
"type": "string"
},
{
"internalType": "uint64",
"name": "action",
"type": "uint64"
},
{
"internalType": "string",
"name": "avsOwnerAddress",
"type": "string"
},
{
"internalType": "string",
"name": "assetID",
"type": "string"
}
],
"name": "AVSAction",
"outputs": [
{
"internalType": "bool",
"name": "success",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
116 changes: 116 additions & 0 deletions precompiles/avs/avs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package avs

import (
"bytes"
"embed"
"fmt"

avsKeeper "github.com/ExocoreNetwork/exocore/x/avs/keeper"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
"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"
)

var _ vm.PrecompiledContract = &Precompile{}

// Embed abi json file to the executable binary. Needed when importing as dependency.
//
//go:embed abi.json
var f embed.FS

// Precompile defines the precompiled contract for avs.
type Precompile struct {
cmn.Precompile
avsKeeper avsKeeper.Keeper
}

// NewPrecompile creates a new avs Precompile instance as a
// PrecompiledContract interface.
func NewPrecompile(
avsKeeper avsKeeper.Keeper,
authzKeeper authzkeeper.Keeper,
) (*Precompile, error) {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
return nil, fmt.Errorf("error loading the avs ABI %s", err)
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
if err != nil {
return nil, fmt.Errorf(cmn.ErrInvalidABI, err)
}

return &Precompile{
Precompile: cmn.Precompile{
ABI: newAbi,
AuthzKeeper: authzKeeper,
KvGasConfig: storetypes.KVGasConfig(),
TransientKVGasConfig: storetypes.TransientGasConfig(),
ApprovalExpiration: cmn.DefaultExpirationDuration,
},
avsKeeper: avsKeeper,
}, nil
}

// Address defines the address of the avs compile contract.
// address: 0x0000000000000000000000000000000000000902
func (p Precompile) Address() common.Address {
return common.HexToAddress("0x0000000000000000000000000000000000000902")
}

// RequiredGas calculates the precompiled contract's base gas rate.
func (p Precompile) RequiredGas(input []byte) uint64 {
methodID := input[:4]

method, err := p.MethodById(methodID)
if err != nil {
// This should never happen since this method is going to fail during Run
return 0
}
return p.Precompile.RequiredGas(input, p.IsTransaction(method.Name))
}

// Run executes the precompiled contract AVSInfoRegisterOrDeregister 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 {
return nil, err
}

// This handles any out of gas errors that may occur during the execution of a precompile tx or query.
// 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)
}

if err != nil {
ctx.Logger().Error("call avs precompile error", "module", "avs precompile", "err", err)
return nil, err
}

cost := ctx.GasMeter().GasConsumed() - initialGas

if !contract.UseGas(cost) {
return nil, vm.ErrOutOfGas
}

return bz, nil
}

// IsTransaction checks if the given methodID corresponds to a transaction or query.
//
// Available avs transactions are:
// - AVSRegister
func (Precompile) IsTransaction(methodID string) bool {
switch methodID {
case MethodAVSAction:
return true
default:
return false
}
}
24 changes: 24 additions & 0 deletions precompiles/avs/avs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
pragma solidity >=0.8.17;

/// @dev The avs-manager contract's address.
address constant AVSMANAGER_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000902;

/// @dev The avs-manager contract's instance.
IAVSManager constant AVSMANAGER_CONTRACT = IAVSManager(
AVSMANAGER_PRECOMPILE_ADDRESS
);

/// @author Exocore Team
/// @title AVS-Manager Precompile Contract
/// @dev The interface through which solidity contracts will interact with AVS-Manager
/// @custom:address 0x0000000000000000000000000000000000000902
interface IAVSManager {
function AVSAction(
string memory avsName,
string memory avsAddress,
string memory operatorAddress,
uint64 action,
string memory avsOwnerAddress,
string memory assetID
) external returns (bool success);
}
Loading
Loading