Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Jul 2, 2024
1 parent 07ee972 commit c6ed0a5
Show file tree
Hide file tree
Showing 10 changed files with 2,429 additions and 2,431 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test-ledger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
on:
push:
paths:
- clients/ledger/**
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.22.1'
- run: make test-ledger
2 changes: 1 addition & 1 deletion .github/workflows/test-registry.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
paths:
- registry/**
- clients/registry/**
jobs:
test:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ test:
test-registry:
REGISTRY_TEST_ENABLE=true go test -v -cover ./clients/registry/...

test-ledger:
REGISTRY_TEST_ENABLE=true go test -v -cover ./clients/ledger/...

check-moc:
find ic -type f -name '*.mo' -print0 | xargs -0 $(shell dfx cache show)/moc --check

Expand Down
37 changes: 33 additions & 4 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/aviate-labs/agent-go/candid/idl"
"net/url"
"reflect"
"time"

"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/certification"
"github.com/aviate-labs/agent-go/certification/hashtree"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/principal"
"github.com/fxamacker/cbor/v2"
"google.golang.org/protobuf/proto"
)

// DefaultConfig is the default configuration for an Agent.
Expand Down Expand Up @@ -101,12 +102,13 @@ type APIRequest[In, Out any] struct {
data []byte
}

func CreateAPIRequest[In, Out any](
func createAPIRequest[In, Out any](
a *Agent,
marshal func(In) ([]byte, error),
unmarshal func([]byte, Out) error,
typ RequestType,
canisterID principal.Principal,
effectiveCanisterID principal.Principal,
methodName string,
in In,
) (*APIRequest[In, Out], error) {
Expand Down Expand Up @@ -135,7 +137,7 @@ func CreateAPIRequest[In, Out any](
unmarshal: unmarshal,
typ: typ,
methodName: methodName,
effectiveCanisterID: canisterID,
effectiveCanisterID: effectiveCanisterID,
requestID: *requestID,
data: data,
}, nil
Expand Down Expand Up @@ -216,17 +218,42 @@ func (a Agent) Client() *Client {

// CreateCandidAPIRequest creates a new api request to the given canister and method.
func (a *Agent) CreateCandidAPIRequest(typ RequestType, canisterID principal.Principal, methodName string, args ...any) (*CandidAPIRequest, error) {
return CreateAPIRequest[[]any, []any](
return createAPIRequest[[]any, []any](
a,
idl.Marshal,
idl.Unmarshal,
typ,
canisterID,
effectiveCanisterID(canisterID, args),
methodName,
args,
)
}

// CreateProtoAPIRequest creates a new api request to the given canister and method.
func (a *Agent) CreateProtoAPIRequest(typ RequestType, canisterID principal.Principal, methodName string, message proto.Message) (*ProtoAPIRequest, error) {
return createAPIRequest[proto.Message, proto.Message](
a,
func(m proto.Message) ([]byte, error) {
raw, err := proto.Marshal(m)
if err != nil {
return nil, err
}
if len(raw) == 0 {
// Protobuf arg are not allowed to be empty.
return []byte{}, nil
}
return raw, nil
},
proto.Unmarshal,
typ,
canisterID,
canisterID,
methodName,
message,
)
}

// GetCanisterControllers returns the list of principals that can control the given canister.
func (a Agent) GetCanisterControllers(canisterID principal.Principal) ([]principal.Principal, error) {
resp, err := a.GetCanisterInfo(canisterID, "controllers")
Expand Down Expand Up @@ -486,3 +513,5 @@ type Config struct {
// DisableSignedQueryVerification disables the verification of signed queries.
DisableSignedQueryVerification bool
}

type ProtoAPIRequest = APIRequest[proto.Message, proto.Message]
22 changes: 2 additions & 20 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,9 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, in []any,

// CallProto calls a method on a canister and unmarshals the result into the given proto message.
func (a Agent) CallProto(canisterID principal.Principal, methodName string, in, out proto.Message) error {
payload, err := proto.Marshal(in)
call, err := a.CreateProtoAPIRequest(RequestTypeCall, canisterID, methodName, in)
if err != nil {
return err
}
requestID, data, err := a.sign(Request{
Type: RequestTypeCall,
Sender: a.Sender(),
IngressExpiry: a.expiryDate(),
CanisterID: canisterID,
MethodName: methodName,
Arguments: payload,
})
if err != nil {
return err
}
if _, err := a.call(canisterID, data); err != nil {
return err
}
raw, err := a.poll(canisterID, *requestID)
if err != nil {
return err
}
return proto.Unmarshal(raw, out)
return call.CallAndWait(out)
}
155 changes: 78 additions & 77 deletions clients/registry/proto/v1/local.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c6ed0a5

Please sign in to comment.