Skip to content

Commit

Permalink
Add effective canister ids to mgmt canister interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 22, 2024
1 parent f40de9f commit b267e64
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 1,133 deletions.
15 changes: 13 additions & 2 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Agent struct {
identity identity.Identity
ingressExpiry time.Duration
rootKey []byte
logger Logger
}

// New returns a new Agent based on the given configuration.
Expand All @@ -56,13 +57,17 @@ func New(cfg Config) (*Agent, error) {
if cfg.Identity != nil {
id = cfg.Identity
}
var logger Logger = &defaultLogger{}
if cfg.Logger != nil {
logger = cfg.Logger
}
ccfg := ClientConfig{
Host: icp0,
}
if cfg.ClientConfig != nil {
ccfg = *cfg.ClientConfig
}
client := NewClient(ccfg)
client := NewClientWithLogger(ccfg, logger)
rootKey, _ := hex.DecodeString(certificate.RootKey)
if cfg.FetchRootKey {
status, err := client.Status()
Expand All @@ -76,6 +81,7 @@ func New(cfg Config) (*Agent, error) {
identity: id,
ingressExpiry: cfg.IngressExpiry,
rootKey: rootKey,
logger: logger,
}, nil
}

Expand All @@ -100,6 +106,7 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, args []an
if err != nil {
return err
}
a.logger.Printf("[AGENT] CALL %s %s", canisterID, methodName)
if _, err := a.call(canisterID, data); err != nil {
return err
}
Expand Down Expand Up @@ -191,6 +198,7 @@ func (a Agent) Query(canisterID principal.Principal, methodName string, args []a
if err != nil {
return err
}
a.logger.Printf("[AGENT] QUERY %s %s", canisterID, methodName)
resp, err := a.query(canisterID, data)
if err != nil {
return err
Expand All @@ -209,6 +217,7 @@ func (a Agent) Query(canisterID principal.Principal, methodName string, args []a

// RequestStatus returns the status of the request with the given ID.
func (a Agent) RequestStatus(canisterID principal.Principal, requestID RequestID) ([]byte, certificate.Node, error) {
a.logger.Printf("[AGENT] REQUEST STATUS %s", requestID)
path := [][]byte{[]byte("request_status"), requestID[:]}
c, err := a.readStateCertificate(canisterID, [][][]byte{path})
if err != nil {
Expand Down Expand Up @@ -251,6 +260,7 @@ func (a Agent) poll(canisterID principal.Principal, requestID RequestID, delay,
for {
select {
case <-ticker.C:
a.logger.Printf("[AGENT] POLL %s", requestID)
data, node, err := a.RequestStatus(canisterID, requestID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -301,7 +311,7 @@ func (a Agent) readStateCertificate(canisterID principal.Principal, paths [][][]
if err != nil {
return nil, err
}

a.logger.Printf("[AGENT] READ STATE %s", canisterID)
resp, err := a.readState(canisterID, data)
if err != nil {
return nil, err
Expand All @@ -328,4 +338,5 @@ type Config struct {
IngressExpiry time.Duration
ClientConfig *ClientConfig
FetchRootKey bool
Logger Logger
}
27 changes: 27 additions & 0 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"encoding/json"
"fmt"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/ic"
"github.com/aviate-labs/agent-go/ic/icpledger"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/mgmt"
"github.com/aviate-labs/agent-go/principal"
"testing"
)
Expand Down Expand Up @@ -67,6 +69,25 @@ func Example_query() {
// 0
}

func TestAgent_Call(t *testing.T) {
a, err := mgmt.NewAgent(agent.Config{
Logger: &testLogger{},
})
if err != nil {
t.Fatal(err)
}
r, err := a.BitcoinGetBalanceQuery(mgmt.BitcoinGetBalanceQueryArgs{
Address: "bc1qruu3xmfrt4nzkxax3lpxfmjega87jr3vqcwjn9",
Network: mgmt.BitcoinNetwork{
Mainnet: new(idl.Null),
},
})
if err != nil {
t.Fatal(err)
}
fmt.Println(r)
}

func TestICPLedger_queryBlocks(t *testing.T) {
a, err := icpledger.NewAgent(ic.LEDGER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
Expand All @@ -79,3 +100,9 @@ func TestICPLedger_queryBlocks(t *testing.T) {
t.Fatal(err)
}
}

type testLogger struct{}

func (l *testLogger) Printf(format string, v ...interface{}) {
fmt.Printf("[TEST]"+format+"\n", v...)
}
2 changes: 1 addition & 1 deletion candid/idl/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func Decode(bs []byte) ([]Type, []any, error) {
}
f, ok := v.(*FunctionType)
if !ok {
fmt.Println(reflect.TypeOf(v))
return nil, nil, fmt.Errorf("invalid method type: %s", reflect.TypeOf(v))
}
methods = append(methods, Method{
Name: string(name),
Expand Down
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,27 @@ import (
type Client struct {
client http.Client
config ClientConfig
logger Logger
}

// NewClient creates a new client based on the given configuration.
func NewClient(cfg ClientConfig) Client {
return Client{
client: http.Client{},
config: cfg,
logger: &defaultLogger{},
}
}

// NewClientWithLogger creates a new client based on the given configuration and logger.
func NewClientWithLogger(cfg ClientConfig, logger Logger) Client {
if logger == nil {
logger = &defaultLogger{}
}
return Client{
client: http.Client{},
config: cfg,
logger: logger,
}
}

Expand All @@ -38,6 +52,7 @@ func (c Client) Status() (*Status, error) {

func (c Client) call(canisterID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/canister/%s/call", canisterID.Encode()))
c.logger.Printf("[CLIENT] CALL %s", u)
resp, err := c.client.Post(u, "application/cbor", bytes.NewBuffer(data))
if err != nil {
return nil, err
Expand All @@ -59,6 +74,7 @@ func (c Client) call(canisterID principal.Principal, data []byte) ([]byte, error
}

func (c Client) get(path string) ([]byte, error) {
c.logger.Printf("[CLIENT] GET %s", c.url(path))
resp, err := c.client.Get(c.url(path))
if err != nil {
return nil, err
Expand All @@ -68,6 +84,7 @@ func (c Client) get(path string) ([]byte, error) {

func (c Client) post(path string, canisterID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/canister/%s/%s", canisterID.Encode(), path))
c.logger.Printf("[CLIENT] POST %s", u)
resp, err := c.client.Post(u, "application/cbor", bytes.NewBuffer(data))
if err != nil {
return nil, err
Expand Down
Empty file added didc
Empty file.
Loading

0 comments on commit b267e64

Please sign in to comment.