-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfilecoin_test.go
185 lines (152 loc) · 4.66 KB
/
filecoin_test.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
package hamt_test
import (
"encoding/hex"
"fmt"
"io"
"os"
"path"
"runtime"
"testing"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
// fhamt "github.com/filecoin-project/go-hamt-ipld/v3"
qt "github.com/frankban/quicktest"
hamt "github.com/ipld/go-ipld-adl-hamt"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/bindnode"
cbg "github.com/whyrusleeping/cbor-gen"
)
// TODO: do we really need this type?
type cborString []byte
func (c cborString) MarshalCBOR(w io.Writer) error {
if err := cbg.WriteMajorTypeHeader(w, cbg.MajByteString, uint64(len(c))); err != nil {
return err
}
_, err := w.Write(c)
return err
}
func TestFilecoinBasic(t *testing.T) {
_ = cborString("") // don't let the type go unused
// Below is the code we used with go-hamt-ipld to produce a HAMT encoded
// with dag-cbor.
// The version of go-hamt-ipld used was v3.0.0-20201223215115-47873c31a853.
// We keep the code commented out to not have the go-ipld-adl-hamt
// module depend on go-hamt-ipld.
// In the future, we should find a better way, like a nested module or a
// test case generator.
// ctx := context.Background()
// fnode := fhamt.NewNode(nil,
// fhamt.UseTreeBitWidth(5),
// fhamt.UseHashFunction(func(key []byte) []byte {
// hasher := sha256.New()
// hasher.Write(key)
// return hasher.Sum(nil)
// }),
// )
// qt.Assert(t, fnode.Set(ctx, "foo", cborString("bar")), qt.IsNil)
// qt.Assert(t, fnode.MarshalCBOR(buf), qt.IsNil)
// fenc := buf.Bytes()
// t.Logf("go-hamt-ipld: %x", fenc)
fenc, err := hex.DecodeString("82412081818243666f6f43626172")
qt.Assert(t, err, qt.IsNil)
builder := hamt.FilecoinV3Prototype{}.NewBuilder()
assembler, err := builder.BeginMap(0)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, assembler.AssembleKey().AssignString("foo"), qt.IsNil)
qt.Assert(t, assembler.AssembleValue().AssignBytes([]byte("bar")), qt.IsNil)
qt.Assert(t, assembler.Finish(), qt.IsNil)
// TODO: can we do better than these type asserts?
node := builder.Build().(*hamt.Node)
enc, err := ipld.Encode(node.Substrate(), dagcbor.Encode)
qt.Assert(t, err, qt.IsNil)
t.Logf("go-ipld-adl-hamt: %x", fenc)
qt.Assert(t, enc, qt.DeepEquals, fenc)
}
func generateFilecoinStateTreeStore() (cid.Cid, linking.LinkSystem) {
// The fixtures below were generated by running 40 iterations of
// https://github.com/filecoin-project/specs-actors/blob/d8d9867f68a3c299295efdc6d1b3421c9b63df57/actors/states/tree_test.go#L79-L94
_, filename, _, _ := runtime.Caller(0)
files, err := os.ReadDir(path.Join(path.Dir(filename), "fixtures"))
if err != nil {
panic(err)
}
store := cidlink.Memory{
Bag: make(map[string][]byte),
}
// preload the fixtures in the bag
for _, file := range files {
k, err := cid.Decode(file.Name())
if err != nil {
panic(err)
}
f, err := os.Open(path.Join(path.Dir(filename), "fixtures", file.Name()))
if err != nil {
panic(err)
}
data, err := io.ReadAll(f)
if err != nil {
panic(err)
}
store.Bag[string(k.Hash())] = data
}
ls := cidlink.DefaultLinkSystem()
ls.StorageReadOpener = store.OpenRead
ls.StorageWriteOpener = store.OpenWrite
// The Map root
root, err := cid.Decode("bafy2bzaceco2ynykenmzjht6mtmu2enaw5nfaxa5jhtpqjclecb7aqdygzqok")
if err != nil {
panic(err)
}
return root, ls
}
func TestFilecoinAdtMap(t *testing.T) {
root, ls := generateFilecoinStateTreeStore()
ndr, err := ls.Load(ipld.LinkContext{}, cidlink.Link{Cid: root}, hamt.HashMapNodePrototype.Representation())
if err != nil {
t.Fatal(err)
}
builder := hamt.FilecoinV3Prototype{}.NewBuilder().(*hamt.Builder)
builder = builder.WithLinking(ls, cidlink.LinkPrototype{})
nd := builder.Build().(*hamt.Node)
hmn := bindnode.Unwrap(ndr).(*hamt.HashMapNode)
if hmn == nil {
t.Fatal("invalid hashmap node")
}
nd.Hamt = *hmn
testCases := []struct {
input string
expected int64
}{
// id address #39, which is the first element in a leaf, one step from the root of this fixture.
{"0027", 39},
// Middle of the other leaf.
{"0001", 1},
// At the end of the root block.
{"000c", 12},
}
for _, tc := range testCases {
tc := tc
t.Run(fmt.Sprintf("testing for id: %d", tc.expected), func(t *testing.T) {
addrKey, err := hex.DecodeString(tc.input)
if err != nil {
t.Fatal(err)
}
val, err := nd.LookupByString(string(addrKey))
if err != nil {
t.Fatal(err)
}
// Actor CallSeqNum
sn, err := val.LookupByIndex(2)
if err != nil {
t.Fatal(err)
}
num, err := sn.AsInt()
if err != nil {
t.Fatal(err)
}
qt.Assert(t, tc.expected, qt.Equals, num)
})
}
}