forked from filecoin-project/specs-actors
/
vector.go
382 lines (327 loc) · 9.28 KB
/
vector.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package vm
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
gbig "math/big"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/bitchina-io/specs-actors/v5/actors/builtin"
"github.com/bitchina-io/specs-actors/v5/actors/util/adt"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipld/go-car"
cbg "github.com/whyrusleeping/cbor-gen"
)
// Update this when generating new vectors for a new filecoin network version
const defaultNetworkName = "hyperdrive"
type testVector struct {
// The name of the test generating this vector
ID string
// car file bytes of the vm state before applying the state transition capture by this vector
StartState []byte
// cid of the vm state tree root before applying the transition
StartStateTree cid.Cid
// on chain message defining the vector state transition
Message *ChainMessage
// receipt generated by vm through executing state transition
Receipt MessageResult
// cid of the vm state tree root after applying the transition
EndStateTree cid.Cid
// Runtime values
// chain epoch during execution
Epoch abi.ChainEpoch
// network version during execution
Version network.Version
// circulating supply during execution
CircSupply abi.TokenAmount
}
func (tv *testVector) MarshalJSON() ([]byte, error) {
tvs, err := newTestVectorSerial(tv)
if err != nil {
return nil, err
}
return json.Marshal(&tvs)
}
type Option func(tv *testVector) error
func SetID(id string) Option {
return func(tv *testVector) error {
tv.ID = id
return nil
}
}
func SetStartState(v *VM) Option {
return func(tv *testVector) error {
rawRoot, err := v.checkpoint()
if err != nil {
return err
}
root, err := flushTreeTopLevel(context.Background(), v.Store(), rawRoot)
if err != nil {
return err
}
tv.StartStateTree = root
getter := nodeGetterFromStore(v.Store())
carBytes, err := encodeCAR(getter, root)
if err != nil {
return err
}
tv.StartState = carBytes
return nil
}
}
func SetEpoch(e abi.ChainEpoch) Option {
return func(tv *testVector) error {
tv.Epoch = e
return nil
}
}
func SetNetworkVersion(nv network.Version) Option {
return func(tv *testVector) error {
tv.Version = nv
return nil
}
}
func SetCircSupply(circSupply big.Int) Option {
return func(tv *testVector) error {
tv.CircSupply = circSupply
return nil
}
}
func SetEndStateTree(rawRoot cid.Cid, store adt.Store) Option {
return func(tv *testVector) error {
root, err := flushTreeTopLevel(context.Background(), store, rawRoot)
if err != nil {
return err
}
tv.EndStateTree = root
return nil
}
}
func SetMessage(from, to address.Address, nonce uint64, value big.Int, method abi.MethodNum, params interface{}) Option {
return func(tv *testVector) error {
msg, err := makeChainMessage(from, to, nonce, value, method, params)
if err != nil {
return err
}
tv.Message = msg
return nil
}
}
func SetReceipt(res MessageResult) Option {
return func(tv *testVector) error {
tv.Receipt = res
return nil
}
}
func StartConditions(v *VM, id string) []Option {
var opts []Option
opts = append(opts, SetEpoch(v.GetEpoch()))
opts = append(opts, SetCircSupply(v.GetCirculatingSupply()))
opts = append(opts, SetNetworkVersion(v.networkVersion))
opts = append(opts, SetStartState(v))
opts = append(opts, SetID(id))
return opts
}
//
// Internal types for serialization
// Taken from https://github.com/filecoin-project/test-vectors/blob/master/schema/schema.go
//
type generationData struct {
Source string `json:"source"`
}
type metadata struct {
ID string `json:"id"`
Gen []generationData `json:"gen"`
}
type variant struct {
// ID of the variant, usually the codename of the upgrade.
ID string `json:"id"`
Epoch int64 `json:"epoch"`
NetworkVersion uint `json:"nv"`
}
type preconditions struct {
Variants []variant `json:"variants"`
StateTree *stateTreeSerial `json:"state_tree,omitempty"`
BaseFee *gbig.Int `json:"basefee,omitempty"`
CircSupply *gbig.Int `json:"circ_supply,omitempty"`
}
type base64EncodedBytes []byte
func (b base64EncodedBytes) String() string {
return base64.StdEncoding.EncodeToString(b)
}
// MarshalJSON implements json.Marshal for Base64EncodedBytes
func (b base64EncodedBytes) MarshalJSON() ([]byte, error) {
return json.Marshal(b.String())
}
type messageSerial struct {
Bytes base64EncodedBytes `json:"bytes"`
}
type stateTreeSerial struct {
RootCID cid.Cid `json:"root_cid"`
}
// Receipt represents a receipt to match against.
type receiptSerial struct {
// ExitCode must be interpreted by the driver as an exitcode.ExitCode
// in Lotus, or equivalent type in other implementations.
ExitCode int64 `json:"exit_code"`
ReturnValue base64EncodedBytes `json:"return"`
GasUsed int64 `json:"gas_used"`
}
// Postconditions contain a representation of VM state at th end of the test
type postconditions struct {
StateTree *stateTreeSerial `json:"state_tree"`
Receipts []*receiptSerial `json:"receipts"`
}
type testVectorSerial struct {
Class string `json:"class"`
Meta *metadata `json:"_meta"`
// CAR binary data to be loaded into the test environment. Should
// contain objects of entire state tree
CAR base64EncodedBytes `json:"car"`
Pre *preconditions `json:"preconditions"`
ApplyMessages []messageSerial `json:"apply_messages,omitempty"`
Post *postconditions `json:"postconditions"`
}
func newTestVectorSerial(tv *testVector) (*testVectorSerial, error) {
zero := big.Zero()
circSupply := tv.CircSupply
var msgBuf bytes.Buffer
if err := tv.Message.MarshalCBOR(&msgBuf); err != nil {
return nil, err
}
msgBytes := msgBuf.Bytes()
var retBuf bytes.Buffer
if err := tv.Receipt.Ret.MarshalCBOR(&retBuf); err != nil {
return nil, err
}
retBytes := retBuf.Bytes()
return &testVectorSerial{
Class: "message",
Meta: &metadata{
ID: tv.ID,
Gen: []generationData{
{Source: "specs-actors_test_auto_gen"},
},
},
CAR: tv.StartState,
Pre: &preconditions{
Variants: []variant{
{ID: defaultNetworkName, Epoch: int64(tv.Epoch), NetworkVersion: uint(tv.Version)},
},
StateTree: &stateTreeSerial{RootCID: tv.StartStateTree},
BaseFee: zero.Int,
CircSupply: circSupply.Int,
},
ApplyMessages: []messageSerial{
{Bytes: msgBytes},
},
Post: &postconditions{
StateTree: &stateTreeSerial{RootCID: tv.EndStateTree},
Receipts: []*receiptSerial{
{
ExitCode: int64(tv.Receipt.Code),
ReturnValue: retBytes,
GasUsed: tv.Receipt.GasCharged,
},
},
},
}, nil
}
// encodeCAR taken from https://github.com/filecoin-project/test-vectors/blob/master/gen/builders/car.go#L16
func encodeCAR(dagserv format.NodeGetter, roots ...cid.Cid) ([]byte, error) {
carWalkFn := func(nd format.Node) (out []*format.Link, err error) {
//fmt.Printf("%s: %x\n", nd.Cid(), nd.RawData())
for _, link := range nd.Links() {
// skip sector cids
if link.Cid.Prefix().Codec == cid.FilCommitmentSealed || link.Cid.Prefix().Codec == cid.FilCommitmentUnsealed {
continue
}
// skip builtin actor cids
if builtin.IsBuiltinActor(link.Cid) {
continue
}
out = append(out, link)
}
return out, nil
}
var (
out = new(bytes.Buffer)
gw = gzip.NewWriter(out)
)
if err := car.WriteCarWithWalker(context.Background(), dagserv, roots, gw, carWalkFn); err != nil {
return nil, err
}
if err := gw.Flush(); err != nil {
return nil, err
}
if err := gw.Close(); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// Get(context.Context, cid.Cid) (Node, error)
// // GetMany returns a channel of NodeOptions given a set of CIDs.
// GetMany(context.Context, []cid.Cid) <-chan *NodeOption
type adtNodeGetter struct {
store adt.Store
}
var _ format.NodeGetter = (*adtNodeGetter)(nil)
func (a *adtNodeGetter) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
d := cbg.Deferred{}
if err := a.store.Get(ctx, c, &d); err != nil {
return nil, err
}
b, err := blocks.NewBlockWithCid(d.Raw, c)
if err != nil {
return nil, err
}
return format.Decode(b)
}
func (a *adtNodeGetter) GetMany(ctx context.Context, cids []cid.Cid) <-chan *format.NodeOption {
ret := make(chan *format.NodeOption)
defer close(ret)
go func() {
for _, c := range cids {
nd, err := a.Get(ctx, c)
ret <- &format.NodeOption{
Node: nd,
Err: err,
}
}
}()
return ret
}
func nodeGetterFromStore(store adt.Store) format.NodeGetter {
return &adtNodeGetter{store: store}
}
// Top level state tree
const CurrentStateTreeVersion = 3
type StateTreeVersion uint64
type StateRoot struct {
// State tree version.
Version StateTreeVersion
// Actors tree. The structure depends on the state root version.
Actors cid.Cid
// Info. The structure depends on the state root version.
Info cid.Cid
}
type StateInfo0 struct{}
// Write top level object of state tree
func flushTreeTopLevel(ctx context.Context, store adt.Store, rawRoot cid.Cid) (cid.Cid, error) {
infoCid, err := store.Put(ctx, new(StateInfo0))
if err != nil {
return cid.Undef, err
}
top := &StateRoot{
Version: CurrentStateTreeVersion,
Actors: rawRoot,
Info: infoCid,
}
return store.Put(ctx, top)
}