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 task manager #20

Closed
wants to merge 10 commits into from
Closed
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
6 changes: 6 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
"github.com/ExocoreNetwork/exocore/x/reward"
rewardKeeper "github.com/ExocoreNetwork/exocore/x/reward/keeper"
rewardTypes "github.com/ExocoreNetwork/exocore/x/reward/types"
taskKeeper "github.com/ExocoreNetwork/exocore/x/taskmanageravs/keeper"
tasktype "github.com/ExocoreNetwork/exocore/x/taskmanageravs/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 @@ -358,6 +360,7 @@ type ExocoreApp struct {
WithdrawKeeper withdrawKeeper.Keeper
RewardKeeper rewardKeeper.Keeper
OperatorKeeper operatorKeeper.Keeper
TaskKeeper taskKeeper.Keeper

ExoSlashKeeper slashKeeper.Keeper
// the module manager
Expand Down Expand Up @@ -630,6 +633,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.TaskKeeper = taskKeeper.NewKeeper(appCodec, keys[tasktype.StoreKey], tasktype.AvsKeeper{})

// We call this after setting the hooks to ensure that the hooks are set on the keeper
evmKeeper.WithPrecompiles(
evmkeeper.AvailablePrecompiles(
Expand All @@ -645,6 +650,7 @@ func NewExocoreApp(
app.WithdrawKeeper,
app.ExoSlashKeeper,
app.RewardKeeper,
app.TaskKeeper,
),
)
epochsKeeper := epochskeeper.NewKeeper(appCodec, keys[epochstypes.StoreKey])
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/ethereum/go-ethereum v1.11.5
github.com/evmos/evmos/v14 v14.0.0-rc4
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/onsi/ginkgo/v2 v2.15.0
Expand Down Expand Up @@ -123,7 +124,6 @@ require (
github.com/google/orderedcode v0.0.1 // indirect
github.com/google/pprof v0.0.0-20230228050547-1710fef4ab10 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
Expand Down
36 changes: 36 additions & 0 deletions precompiles/avs/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"inputs": [
{
"internalType": "string",
"name": "avsName",
"type": "string"
},
{
"internalType": "bytes",
"name": "avsAddress",
"type": "bytes"
},
{
"internalType": "bytes",
"name": "operatorAddress",
"type": "bytes"
},
{
"internalType": "uint64",
"name": "action",
"type": "uint64"
}
],
"name": "AVSInfoRegisterOrDeregister",
"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 AVSInfoRegisterOrDeregister(
string memory avsName,
bytes memory avsAddress,
bytes memory operatorAddress,
uint64 action

) external returns (bool success);

}
35 changes: 35 additions & 0 deletions precompiles/avs/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package avs

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

const (
// AVSRegister defines the ABI method name for the avs
// related transaction.
MethodAVSAction = "avsAction"
)

// AVSInfoRegister register the avs related information and change the state in avs keeper module.
func (p Precompile) AVSInfoRegisterOrDeregister(
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, err
}
err = p.avsKeeper.AVSInfoUpdate(ctx, avsParams)
if err != nil {
return nil, err
}
return method.Outputs.Pack(true)
}
40 changes: 40 additions & 0 deletions precompiles/avs/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package avs

import (
exocmn "github.com/ExocoreNetwork/exocore/precompiles/common"
"github.com/ExocoreNetwork/exocore/x/avs/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
cmn "github.com/evmos/evmos/v14/precompiles/common"
"golang.org/x/xerrors"
)

func (p Precompile) GetAVSParamsFromInputs(ctx sdk.Context, args []interface{}) (*keeper.AVSParams, error) {
if len(args) != 4 {
return nil, xerrors.Errorf(cmn.ErrInvalidNumberOfArgs, 4, len(args))
}
avsParams := &keeper.AVSParams{}
avsName, ok := args[0].(string)
if !ok {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 0, "string", avsName)
}
avsParams.AVSName = avsName

avsAddress, ok := args[1].([]byte)
if !ok || avsAddress == nil {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", avsAddress)
}
avsParams.AVSAddress = avsAddress

operatorAddress, ok := args[2].([]byte)
if !ok || operatorAddress == nil {
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", operatorAddress)
}
avsParams.OperatorAddress = operatorAddress

action, ok := args[3].(uint64)
if !ok || action != keeper.RegisterAction || action != keeper.DeRegisterAction {

Check failure on line 35 in precompiles/avs/types.go

View workflow job for this annotation

GitHub Actions / test-unit-cover

suspect or: action != keeper.RegisterAction || action != keeper.DeRegisterAction
return nil, xerrors.Errorf(exocmn.ErrContractInputParaOrType, 3, "uint64", action)
}
avsParams.Action = action
return avsParams, nil
}
31 changes: 31 additions & 0 deletions precompiles/avsTask/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"inputs":[
{
"internalType":"uint256",
"name":"numberToBeSquared",
"type":"uint256"
},
{
"internalType":"bytes",
"name":"quorumThresholdPercentage",
"type":"bytes"
},
{
"internalType":"bytes",
"name":"quorumNumbers",
"type":"bytes"
}
],
"name":"createNewTask",
"outputs":[
{
"internalType":"bool",
"name":"success",
"type":"bool"
}
],
"stateMutability":"nonpayable",
"type":"function"
}
]
Loading
Loading