forked from filecoin-project/go-state-types
/
manifest.go
156 lines (126 loc) · 3.03 KB
/
manifest.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
package manifest
import (
"context"
"fmt"
"io"
actorstypes "github.com/CluEleSsUK/go-state-types/actors"
"github.com/CluEleSsUK/go-state-types/builtin/v8/util/adt"
"github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
)
const (
AccountKey = "account"
CronKey = "cron"
InitKey = "init"
MarketKey = "storagemarket"
MinerKey = "storageminer"
MultisigKey = "multisig"
PaychKey = "paymentchannel"
PowerKey = "storagepower"
RewardKey = "reward"
SystemKey = "system"
VerifregKey = "verifiedregistry"
DatacapKey = "datacap"
EvmKey = "evm"
EamKey = "eam"
PlaceholderKey = "placeholder"
EthAccountKey = "ethaccount"
TimelockKey = "timelock"
)
func GetBuiltinActorsKeys(av actorstypes.Version) []string {
keys := []string{
AccountKey,
CronKey,
InitKey,
MarketKey,
MinerKey,
MultisigKey,
PaychKey,
PowerKey,
RewardKey,
SystemKey,
VerifregKey,
}
if av >= 9 {
keys = append(keys, DatacapKey)
}
if av >= 10 {
keys = append(keys, EvmKey, EamKey, PlaceholderKey, EthAccountKey, TimelockKey)
}
return keys
}
type Manifest struct {
Version uint64 // this is really u32, but cbor-gen can't deal with it
Data cid.Cid
entries map[string]cid.Cid
}
type ManifestEntry struct {
Name string
Code cid.Cid
}
type ManifestData struct {
Entries []ManifestEntry
}
func (m *Manifest) Load(ctx context.Context, store adt.Store) error {
if m.Version != 1 {
return fmt.Errorf("unknown manifest version %d", m.Version)
}
data := ManifestData{}
if err := store.Get(ctx, m.Data, &data); err != nil {
return err
}
m.entries = make(map[string]cid.Cid)
for _, e := range data.Entries {
m.entries[e.Name] = e.Code
}
return nil
}
func (m *Manifest) Get(name string) (cid.Cid, bool) {
c, ok := m.entries[name]
return c, ok
}
// this is a flat tuple, so we need to write these by hand
func (d *ManifestData) UnmarshalCBOR(r io.Reader) error {
*d = ManifestData{}
br := cbg.GetPeeker(r)
scratch := make([]byte, 8)
maj, extra, err := cbg.CborReadHeaderBuf(br, scratch)
if err != nil {
return err
}
if maj != cbg.MajArray {
return fmt.Errorf("cbor input should be of type array")
}
if extra > cbg.MaxLength {
return fmt.Errorf("too many manifest entries")
}
entries := int(extra)
d.Entries = make([]ManifestEntry, 0, entries)
for i := 0; i < entries; i++ {
entry := ManifestEntry{}
if err := entry.UnmarshalCBOR(r); err != nil {
return fmt.Errorf("error unmarsnalling manifest entry: %w", err)
}
d.Entries = append(d.Entries, entry)
}
return nil
}
func (d *ManifestData) MarshalCBOR(w io.Writer) error {
if d == nil {
_, err := w.Write(cbg.CborNull)
return err
}
scratch := make([]byte, 9)
if len(d.Entries) > cbg.MaxLength {
return fmt.Errorf("too many manifest entries")
}
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(d.Entries))); err != nil {
return err
}
for _, v := range d.Entries {
if err := v.MarshalCBOR(w); err != nil {
return err
}
}
return nil
}