-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstances.go
165 lines (153 loc) · 3.76 KB
/
instances.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package pocketic
import (
"fmt"
"github.com/aviate-labs/agent-go/principal"
"net/http"
"time"
)
type InstanceConfig struct {
InstanceID int `json:"instance_id"`
Topology map[string]Topology `json:"topology"`
}
// CreateInstance creates a new PocketIC instance.
func (pic PocketIC) CreateInstance(config SubnetConfigSet) (*InstanceConfig, error) {
var a createResponse[InstanceConfig]
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/instances", pic.server.URL()),
config,
&a,
); err != nil {
return nil, err
}
if a.Error != nil {
return nil, a.Error
}
return a.Created, nil
}
// DeleteInstance deletes a PocketIC instance.
func (pic PocketIC) DeleteInstance(instanceID int) error {
return pic.do(
http.MethodDelete,
fmt.Sprintf("%s/instances/%d", pic.server.URL(), instanceID),
nil,
nil,
)
}
// GetCycles returns the cycles of a canister.
func (pic PocketIC) GetCycles(canisterID principal.Principal) (int, error) {
var cycles rawCycles
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/read/get_cycles", pic.InstanceURL()),
&RawCanisterID{CanisterID: canisterID.Raw},
&cycles,
); err != nil {
return 0, err
}
return cycles.Cycles, nil
}
// GetInstances lists all PocketIC instance availabilities.
func (pic PocketIC) GetInstances() ([]string, error) {
var instances []string
if err := pic.do(
http.MethodGet,
fmt.Sprintf("%s/instances", pic.server.URL()),
nil,
&instances,
); err != nil {
return nil, err
}
return instances, nil
}
// GetStableMemory returns the stable memory of a canister.
func (pic PocketIC) GetStableMemory(canisterID principal.Principal) ([]byte, error) {
var data rawStableMemory
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/read/get_stable_memory", pic.InstanceURL()),
&RawCanisterID{CanisterID: canisterID.Raw},
&data,
); err != nil {
return nil, err
}
return data.Blob, nil
}
// GetSubnet returns the subnet of a canister.
func (pic PocketIC) GetSubnet(canisterID principal.Principal) (*principal.Principal, error) {
var subnetID SubnetID
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/read/get_subnet", pic.InstanceURL()),
&RawCanisterID{CanisterID: canisterID.Raw},
&subnetID,
); err != nil {
return nil, err
}
return &principal.Principal{Raw: subnetID.SubnetID}, nil
}
// GetTime returns the current time of the PocketIC instance.
func (pic PocketIC) GetTime() (*time.Time, error) {
var t rawTime
if err := pic.do(
http.MethodGet,
fmt.Sprintf("%s/read/get_time", pic.InstanceURL()),
nil,
&t,
); err != nil {
return nil, err
}
now := time.Unix(0, t.NanosSinceEpoch)
return &now, nil
}
// RootKey returns the root key of the NNS subnet.
func (pic PocketIC) RootKey() ([]byte, error) {
var subnetID *principal.Principal
for k, v := range pic.topology {
if v.SubnetKind == NNSSubnet {
id, err := principal.Decode(k)
if err != nil {
return nil, err
}
subnetID = &id
break
}
}
if subnetID == nil {
return nil, fmt.Errorf("no NNS subnet found")
}
var key []byte
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/read/pub_key", pic.InstanceURL()),
&SubnetID{SubnetID: subnetID.Raw},
&key,
); err != nil {
return nil, err
}
return key, nil
}
// SetStableMemory sets the stable memory of a canister.
func (pic PocketIC) SetStableMemory(canisterID principal.Principal, data []byte, gzipCompression bool) error {
blobID, err := pic.UploadBlob(data, gzipCompression)
if err != nil {
return err
}
return pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/set_stable_memory", pic.InstanceURL()),
rawSetStableMemory{
CanisterID: canisterID.Raw,
BlobID: blobID,
},
nil,
)
}
func (pic PocketIC) Tick() error {
return pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/tick", pic.InstanceURL()),
nil,
nil,
)
}