-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
69 lines (54 loc) · 1.54 KB
/
store.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
package adt
import (
"context"
"github.com/chenjianmei111/go-state-types/cbor"
"github.com/chenjianmei111/go-state-types/exitcode"
cid "github.com/ipfs/go-cid"
ipldcbor "github.com/ipfs/go-ipld-cbor"
vmr "github.com/chenjianmei111/specs-actors/v2/actors/runtime"
)
// Store defines an interface required to back the ADTs in this package.
type Store interface {
Context() context.Context
ipldcbor.IpldStore
}
// Adapts a vanilla IPLD store as an ADT store.
func WrapStore(ctx context.Context, store ipldcbor.IpldStore) Store {
return &wstore{
ctx: ctx,
IpldStore: store,
}
}
type wstore struct {
ctx context.Context
ipldcbor.IpldStore
}
var _ Store = &wstore{}
func (s *wstore) Context() context.Context {
return s.ctx
}
// Adapter for a Runtime as an ADT Store.
// Adapts a Runtime as an ADT store.
func AsStore(rt vmr.Runtime) Store {
return rtStore{rt}
}
type rtStore struct {
vmr.Runtime
}
var _ Store = &rtStore{}
func (r rtStore) Context() context.Context {
return r.Runtime.Context()
}
func (r rtStore) Get(_ context.Context, c cid.Cid, out interface{}) error {
// The Go context is (un/fortunately?) dropped here.
// See https://github.com/chenjianmei111/specs-actors/issues/140
if !r.StoreGet(c, out.(cbor.Unmarshaler)) {
r.Abortf(exitcode.ErrNotFound, "not found")
}
return nil
}
func (r rtStore) Put(_ context.Context, v interface{}) (cid.Cid, error) {
// The Go context is (un/fortunately?) dropped here.
// See https://github.com/chenjianmei111/specs-actors/issues/140
return r.StorePut(v.(cbor.Marshaler)), nil
}