Skip to content

Commit

Permalink
Merge pull request #1 from permadao/feature/add-spawn
Browse files Browse the repository at this point in the history
feature: add spawn process and dryRun process
  • Loading branch information
outprog authored Aug 6, 2024
2 parents b398d04 + 4c1365e commit 3887738
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ See details in `client_test.go`.

- [x] Send: send message to ao process
- [x] Eval: send eval message to ao process
- [x] Spawn: create ao process
- [x] Result: request cu get process result
- [ ] Spawn
- [ ] Dry run
- [x] DryRun: dry run ao process
95 changes: 85 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"

"github.com/permadao/goao/schema"
"github.com/permadao/goar"
goarSchema "github.com/permadao/goar/schema"
"gopkg.in/h2non/gentleman.v2"
"net/http"
"strconv"
"time"
)

type Client struct {
Expand All @@ -36,10 +37,10 @@ func NewClient(muURL, cuURL string, signer interface{}) (*Client, error) {
}, nil
}

func (c *Client) Send(processId, data string, tags []goarSchema.Tag) (res schema.ResponseMu, err error) {
func (c *Client) Send(processId, data, msgType string, tags []goarSchema.Tag) (res schema.ResponseMu, err error) {
tags = append(tags, goarSchema.Tag{Name: "Data-Protocol", Value: schema.DataProtocol})
tags = append(tags, goarSchema.Tag{Name: "Variant", Value: schema.Variant})
tags = append(tags, goarSchema.Tag{Name: "Type", Value: schema.TypeMessage})
tags = append(tags, goarSchema.Tag{Name: "Type", Value: msgType})
tags = append(tags, goarSchema.Tag{Name: "SDK", Value: schema.SDK})

item, err := c.bundler.CreateAndSignItem([]byte(data), processId, "", tags)
Expand Down Expand Up @@ -67,13 +68,25 @@ func (c *Client) Send(processId, data string, tags []goarSchema.Tag) (res schema

func (c *Client) Eval(processId, code string) (res schema.ResponseMu, err error) {
return c.Send(
processId, code,
processId, code, schema.TypeMessage,
[]goarSchema.Tag{
goarSchema.Tag{Name: "Action", Value: "Eval"},
},
)
}

func (c *Client) Spawn(processName, appName, module, scheduler string) (res schema.ResponseMu, err error) {
return c.Send(
"", strconv.Itoa(int(time.Now().UnixNano())), schema.TypeProcess,
[]goarSchema.Tag{
{Name: "Name", Value: processName},
{Name: "App-Name", Value: appName},
{Name: "Module", Value: module},
{Name: "Scheduler", Value: scheduler},
},
)
}

func (c *Client) Result(processId, messageId string) (res schema.ResponseCu, err error) {
req := c.cuCli.Get()
req.AddPath(fmt.Sprintf("/result/%v", messageId))
Expand All @@ -88,20 +101,82 @@ func (c *Client) Result(processId, messageId string) (res schema.ResponseCu, err
return
}

defer resp.Close()
respBody := resp.Bytes()

// golang http not handle Temporary Redirect(307)
if resp.StatusCode == http.StatusTemporaryRedirect {
loc := resp.Header.Get("Location")
resp.Close()
resp, err = c.cuCli.Request().URL(loc).Send()
var redirectResp *gentleman.Response
redirectResp, err = c.cuCli.Request().URL(resp.Header.Get("Location")).Send()
if err != nil {
return
}
if !resp.Ok {
if !redirectResp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer redirectResp.Close()
respBody = redirectResp.Bytes()
}

err = json.Unmarshal(resp.Bytes(), &res)
err = json.Unmarshal(respBody, &res)
return
}

func (c *Client) DryRun(processId, data string, tags []goarSchema.Tag) (res schema.ResponseCu, err error) {
tags = append(tags, goarSchema.Tag{Name: "Data-Protocol", Value: schema.DataProtocol})
tags = append(tags, goarSchema.Tag{Name: "Variant", Value: schema.Variant})
tags = append(tags, goarSchema.Tag{Name: "Type", Value: schema.TypeMessage})
tags = append(tags, goarSchema.Tag{Name: "SDK", Value: schema.SDK})

item := struct {
Id string `json:"Id"`
Target string `json:"Target"`
Owner string `json:"Owner"`
Data string `json:"Data"`
Tags []goarSchema.Tag `json:"Tags"`
Anchor string `json:"Anchor"`
}{
Id: "0000000000000000000000000000000000000000001",
Target: processId,
Owner: "0000000000000000000000000000000000000000001",
Data: data,
Tags: tags,
}
payload, err := json.Marshal(item)
if err != nil {
return
}

req := c.cuCli.Post()
req.AddPath("/dry-run")
req.AddQuery("process-id", processId)
req.JSON(payload)
resp, err := req.Send()
if err != nil {
return
}
if !resp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer resp.Close()
respBody := resp.Bytes()

if resp.StatusCode == http.StatusTemporaryRedirect {
var redirectResp *gentleman.Response
redirectResp, err = gentleman.New().URL(resp.Header.Get("Location")).Post().JSON(payload).Send()
if err != nil {
return
}
if !redirectResp.Ok {
err = fmt.Errorf("invalid server response: %d", resp.StatusCode)
return
}
defer redirectResp.Close()
respBody = redirectResp.Bytes()
}

err = json.Unmarshal(respBody, &res)
return
}
54 changes: 48 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package goao

import (
"fmt"
"github.com/everFinance/goether"
goarSchema "github.com/permadao/goar/schema"
"testing"

"github.com/permadao/goar"
"github.com/stretchr/testify/assert"
)

var tClient *Client
var (
tClient *Client
eccClient *Client
)

func init() {
signer, err := goar.NewSignerFromPath("testKey.json")
Expand All @@ -23,29 +28,66 @@ func init() {
if err != nil {
panic(err)
}

eccSigner, _ := goether.NewSigner("4c3f9a1e5b234ce8f1ab58d82f849c0f70a4d5ceaf2b6e2d9a6c58b1f897ef0a")
eccClient, err = NewClient(
"https://mu.ao-testnet.xyz",
"https://cu.ao-testnet.xyz",
eccSigner)

if err != nil {
panic(err)
}
}

func TestSend(t *testing.T) {
func TestClient_Send(t *testing.T) {
// res, err := tClient.Send(
// "ya9XinY0qXeYyf7HXANqzOiKns8yiXZoDtFqUMXkX0Q",
// "",
// []schema.Tag{
// schema.Tag{Name: "Action", Value: "Claim"},
// schema.TypeMessage,
// []goarSchema.Tag{
// {Name: "Action", Value: "Claim"},
// })
// assert.NoError(t, err)
// fmt.Println(res)
}

func TestEval(t *testing.T) {
func TestClient_Eval(t *testing.T) {
// res, err := tClient.Eval(
// "ya9XinY0qXeYyf7HXANqzOiKns8yiXZoDtFqUMXkX0Q",
// "1+1")
// assert.NoError(t, err)
// fmt.Println(res)
}

func TestResult(t *testing.T) {
func TestClient_Spawn(t *testing.T) {
// res, err := tClient.Spawn(
// "test1", "goao-test",
// schema.DefaultModule, schema.DefaultScheduler)
// assert.NoError(t, err)
// t.Log(res)
//
// // 0x address
// res, err = eccClient.Spawn(
// "test2", "goao-test2",
// schema.DefaultModule, schema.DefaultScheduler)
// assert.NoError(t, err)
// t.Log(res)
}

func TestClient_Result(t *testing.T) {
res, err := tClient.Result("ya9XinY0qXeYyf7HXANqzOiKns8yiXZoDtFqUMXkX0Q", "5JtjkYy1hk0Zce5mP6gDWIOdt9rCSQAFX-K9jZnqniw")
assert.NoError(t, err)
fmt.Println(res)
}

func TestClient_DryRun(t *testing.T) {
processId := "xU9zFkq3X2ZQ6olwNVvr1vUWIjc3kXTWr7xKQD6dh10"
data := ""
tags := []goarSchema.Tag{
{Name: "Action", Value: "Info"},
}
res, err := tClient.DryRun(processId, data, tags)
assert.NoError(t, err)
t.Log(res)
}
14 changes: 9 additions & 5 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const (
TypeMessage = "Message"
TypeProcess = "Process"
SDK = "goao"

DefaultModule = "xT0ogTeagEGuySbKuUoo_NaWeeBv1fZ4MqgDdKVKY0U"
DefaultSqliteModule = "sFNHeYzhHfP9vV9CPpqZMU-4Zzq_qKGKwlwMZozWi2Y"
DefaultScheduler = "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA"
)

type ResponseMu struct {
Expand All @@ -14,9 +18,9 @@ type ResponseMu struct {
}

type ResponseCu struct {
Message string `json:"Message"`
Assignments []string `json:"Assignments"`
Spawns []string `json:"Spawns"`
Output interface{} `json:"Output"`
GasUsed int64 `json:"GasUsed"`
Messages []interface{} `json:"Messages"`
Assignments []interface{} `json:"Assignments"`
Spawns []interface{} `json:"Spawns"`
Output interface{} `json:"Output"`
GasUsed int64 `json:"GasUsed"`
}

0 comments on commit 3887738

Please sign in to comment.