Skip to content

Return status and error code #32

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

Merged
merged 7 commits into from
Nov 11, 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
52 changes: 52 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Pull Request

on:
push:
workflow_dispatch:

jobs:
publish-dev-build:
name: Publish dev build docker image to dockerhub
runs-on: 'ubuntu-latest'
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
# This is a dedicated repository to house the development/preview build
images: |
avaprotocol/avs-dev
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=raw,value={{sha}},enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}

- name: Build and push avs-dev docker image
uses: docker/build-push-action@v6
with:
build-args: |
RELEASE_TAG=${{ inputs.tag || github.ref_name }}
platforms: linux/amd64,linux/arm64
context: .
file: dockerfiles/operator.Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
20 changes: 11 additions & 9 deletions aggregator/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/ethereum/go-ethereum/ethclient"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"

Expand Down Expand Up @@ -43,7 +45,7 @@ func (r *RpcServer) GetNonce(ctx context.Context, payload *avsproto.NonceRequest

nonce, err := aa.GetNonce(r.smartWalletRpc, ownerAddress, big.NewInt(0))
if err != nil {
return nil, err
return nil, status.Errorf(codes.Code(avsproto.Error_SmartWalletRpcError), "cannot determine nonce for smart wallet")
}

return &avsproto.NonceResp{
Expand All @@ -56,21 +58,21 @@ func (r *RpcServer) GetSmartAccountAddress(ctx context.Context, payload *avsprot
ownerAddress := common.HexToAddress(payload.Owner)
salt := big.NewInt(0)
sender, err := aa.GetSenderAddress(r.smartWalletRpc, ownerAddress, salt)
nonce, err := aa.GetNonce(r.smartWalletRpc, *sender, salt)

if err != nil {
return nil, err
return nil, status.Errorf(codes.Code(avsproto.Error_SmartWalletNotFoundError), "cannot determine smart wallet address")
}
return &avsproto.AddressResp{
Nonce: nonce.String(),
SmartAccountAddress: sender.String(),
// TODO: return the right salt
Salt: big.NewInt(0).String(),
}, nil
}

func (r *RpcServer) CancelTask(ctx context.Context, taskID *avsproto.UUID) (*wrapperspb.BoolValue, error) {
user, err := r.verifyAuth(ctx)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Unauthenticated, "invalid authentication key")
}

r.config.Logger.Info("Process Cancel Task",
Expand All @@ -90,7 +92,7 @@ func (r *RpcServer) CancelTask(ctx context.Context, taskID *avsproto.UUID) (*wra
func (r *RpcServer) DeleteTask(ctx context.Context, taskID *avsproto.UUID) (*wrapperspb.BoolValue, error) {
user, err := r.verifyAuth(ctx)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Unauthenticated, "invalid authentication key")
}

r.config.Logger.Info("Process Delete Task",
Expand All @@ -110,7 +112,7 @@ func (r *RpcServer) DeleteTask(ctx context.Context, taskID *avsproto.UUID) (*wra
func (r *RpcServer) CreateTask(ctx context.Context, taskPayload *avsproto.CreateTaskReq) (*avsproto.CreateTaskResp, error) {
user, err := r.verifyAuth(ctx)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Unauthenticated, "invalid authentication key")
}

task, err := r.engine.CreateTask(user, taskPayload)
Expand All @@ -126,7 +128,7 @@ func (r *RpcServer) CreateTask(ctx context.Context, taskPayload *avsproto.Create
func (r *RpcServer) ListTasks(ctx context.Context, _ *avsproto.ListTasksReq) (*avsproto.ListTasksResp, error) {
user, err := r.verifyAuth(ctx)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Unauthenticated, "invalid authentication key")
}

r.config.Logger.Info("Process List Task",
Expand All @@ -142,7 +144,7 @@ func (r *RpcServer) ListTasks(ctx context.Context, _ *avsproto.ListTasksReq) (*a
func (r *RpcServer) GetTask(ctx context.Context, taskID *avsproto.UUID) (*avsproto.Task, error) {
user, err := r.verifyAuth(ctx)
if err != nil {
return nil, err
return nil, status.Errorf(codes.Unauthenticated, "invalid authentication key")
}

r.config.Logger.Info("Process Get Task",
Expand Down
22 changes: 15 additions & 7 deletions core/taskengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/AvaProtocol/ap-avs/storage"
sdklogging "github.com/Layr-Labs/eigensdk-go/logging"
"github.com/ethereum/go-ethereum/ethclient"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"

avsproto "github.com/AvaProtocol/ap-avs/protobuf"
)
Expand Down Expand Up @@ -135,12 +137,12 @@ func (n *Engine) CreateTask(user *model.User, taskPayload *avsproto.CreateTaskRe
user.SmartAccountAddress, err = aa.GetSenderAddress(rpcConn, user.Address, salt)

if err != nil {
return nil, err
return nil, grpcstatus.Errorf(codes.Code(avsproto.Error_SmartWalletRpcError), "cannot get smart wallet address")
}

taskID, err := n.NewTaskID()
if err != nil {
return nil, fmt.Errorf("cannot create task right now. storage unavailable")
return nil, grpcstatus.Errorf(codes.Code(avsproto.Error_StorageUnavailable), "cannot create task right now. storage unavailable")
}

task, err := model.NewTaskFromProtobuf(taskID, user, taskPayload)
Expand Down Expand Up @@ -264,7 +266,7 @@ func (n *Engine) ListTasksByUser(user *model.User) ([]*avsproto.ListTasksResp_Ta
taskIDs, err := n.db.GetByPrefix([]byte(fmt.Sprintf("u:%s", user.Address.String())))

if err != nil {
return nil, err
return nil, grpcstatus.Errorf(codes.Code(avsproto.Error_StorageUnavailable), "storage is not ready")
}

tasks := make([]*avsproto.ListTasksResp_TaskItemResp, len(taskIDs))
Expand All @@ -288,6 +290,9 @@ func (n *Engine) GetTaskByUser(user *model.User, taskID string) (*model.Task, er

// Get Task Status
rawStatus, err := n.db.GetKey([]byte(TaskUserKey(task)))
if err != nil {
return nil, grpcstatus.Errorf(codes.NotFound, "task not found")
}
status, _ := strconv.Atoi(string(rawStatus))

taskRawByte, err := n.db.GetKey([]byte(
Expand All @@ -299,19 +304,22 @@ func (n *Engine) GetTaskByUser(user *model.User, taskID string) (*model.Task, er
TaskStorageKey(taskID, avsproto.TaskStatus_Executing),
))
if err != nil {
return nil, err
return nil, grpcstatus.Errorf(codes.Code(avsproto.Error_TaskDataCorrupted), "task data storage is corrupted")
}
}

err = task.FromStorageData(taskRawByte)
return task, err
if err != nil {
return nil, grpcstatus.Errorf(codes.Code(avsproto.Error_TaskDataCorrupted), "task data storage is corrupted")
}
return task, nil
}

func (n *Engine) DeleteTaskByUser(user *model.User, taskID string) (bool, error) {
task, err := n.GetTaskByUser(user, taskID)

if err != nil {
return false, err
return false, grpcstatus.Errorf(codes.NotFound, "task not found")
}

if task.Status == avsproto.TaskStatus_Executing {
Expand All @@ -328,7 +336,7 @@ func (n *Engine) CancelTaskByUser(user *model.User, taskID string) (bool, error)
task, err := n.GetTaskByUser(user, taskID)

if err != nil {
return false, err
return false, grpcstatus.Errorf(codes.NotFound, "task not found")
}

if task.Status != avsproto.TaskStatus_Active {
Expand Down
18 changes: 14 additions & 4 deletions examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ const config = {
TEST_TRANSFER_TOKEN: "0x2e8bdb63d09ef989a0018eeb1c47ef84e3e61f7b",
TEST_TRANSFER_TO: "0xe0f7D11FD714674722d325Cd86062A5F1882E13a",
ORACLE_PRICE_CONTRACT: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
RPC_PROVIDER: "https://sepolia.gateway.tenderly.co",
},

staging: {
AP_AVS_RPC: "aggregator-holesky.avaprotocol.org:2206",
TEST_TRANSFER_TOKEN: "0x2e8bdb63d09ef989a0018eeb1c47ef84e3e61f7b",
TEST_TRANSFER_TO: "0xe0f7D11FD714674722d325Cd86062A5F1882E13a",
ORACLE_PRICE_CONTRACT: "0x694AA1769357215DE4FAC081bf1f309aDC325306",
RPC_PROVIDER: "https://rpc.sepolia.avaprotocol.org",
RPC_PROVIDER: "https://sepolia.gateway.tenderly.co",
},

minato: {
Expand Down Expand Up @@ -215,13 +216,13 @@ async function getWallet(owner, token) {
return result;
}

(async () => {
const main = async (cmd) => {
// 1. Generate the api token to interact with aggregator
const { owner, token } = await generateApiToken();

let taskCondition = "";

switch (process.argv[2]) {
switch (cmd) {
case "schedule":
// ETH-USD pair on sepolia
// https://sepolia.etherscan.io/address/0x694AA1769357215DE4FAC081bf1f309aDC325306#code
Expand Down Expand Up @@ -304,7 +305,7 @@ async function getWallet(owner, token) {
cancel <task-id>: to cancel a task
delete <task-id>: to completely remove a task`);
}
})();
}

function getTaskData() {
let ABI = ["function transfer(address to, uint amount)"];
Expand Down Expand Up @@ -409,3 +410,12 @@ async function scheduleTimeTransfer(owner, token) {

console.log("Expression Task ID is:", result);
}


(async () => {
try {
main(process.argv[2]);
} catch (e) {
console.log("error from grpc", e.code, "detail", e.message);
}
})();
Loading
Loading