/
message_root.go
55 lines (43 loc) · 960 Bytes
/
message_root.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
package types
import (
"bytes"
"github.com/filecoin-project/go-state-types/abi"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
)
type MessageRoot struct {
BlsRoot cid.Cid
SecpkRoot cid.Cid
}
func (mr *MessageRoot) Serialize() ([]byte, error) {
buf := new(bytes.Buffer)
if err := mr.MarshalCBOR(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (mr *MessageRoot) SerializeWithCid() (cid.Cid, []byte, error) {
data, err := mr.Serialize()
if err != nil {
return cid.Undef, nil, err
}
c, err := abi.CidBuilder.Sum(data)
if err != nil {
return cid.Undef, nil, err
}
return c, data, nil
}
func (mr *MessageRoot) ToStorageBlock() (blocks.Block, error) {
c, data, err := mr.SerializeWithCid()
if err != nil {
return nil, err
}
return blocks.NewBlockWithCid(data, c)
}
func (mr *MessageRoot) Cid() cid.Cid {
c, _, err := mr.SerializeWithCid()
if err != nil {
panic(err)
}
return c
}