-
Notifications
You must be signed in to change notification settings - Fork 460
/
cid.go
68 lines (56 loc) · 1.99 KB
/
cid.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
package testhelpers
import (
"context"
"testing"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/filecoin-project/venus/pkg/constants"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
)
// EmptyMessagesCID is the cid of an empty collection of messages.
var EmptyMessagesCID cid.Cid
// EmptyReceiptsCID is the cid of an empty collection of receipts.
var EmptyReceiptsCID cid.Cid
// EmptyTxMetaCID is the cid of a TxMeta wrapping empty cids
var EmptyTxMetaCID cid.Cid
func init() {
tmpCst := cbor.NewCborStore(blockstoreutil.NewBlockstore(datastore.NewMapDatastore()))
emptyAmt := adt.MakeEmptyArray(adt.WrapStore(context.Background(), tmpCst))
emptyAMTCid, err := emptyAmt.Root()
if err != nil {
panic("could not create CID for empty AMT")
}
EmptyMessagesCID = emptyAMTCid
EmptyReceiptsCID = emptyAMTCid
EmptyTxMetaCID, err = tmpCst.Put(context.Background(), &types.MessageRoot{SecpkRoot: EmptyMessagesCID, BlsRoot: EmptyMessagesCID})
if err != nil {
panic("could not create CID for empty TxMeta")
}
}
// CidFromString generates Cid from string input
func CidFromString(t *testing.T, input string) cid.Cid {
c, err := constants.DefaultCidBuilder.Sum([]byte(input))
require.NoError(t, err)
return c
}
// HasCid allows two values with CIDs to be compared.
type HasCid interface {
Cid() cid.Cid
}
// AssertHaveSameCid asserts that two values have identical CIDs.
func AssertHaveSameCid(t *testing.T, m HasCid, n HasCid) {
if !m.Cid().Equals(n.Cid()) {
assert.Fail(t, "CIDs don't match", "not equal %v %v", m.Cid(), n.Cid())
}
}
// AssertCidsEqual asserts that two CIDS are identical.
func AssertCidsEqual(t *testing.T, m cid.Cid, n cid.Cid) {
if !m.Equals(n) {
assert.Fail(t, "CIDs don't match", "not equal %v %v", m, n)
}
}