forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserde.go
94 lines (79 loc) · 2.04 KB
/
serde.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
package header
import (
core "github.com/tendermint/tendermint/types"
"github.com/celestiaorg/celestia-app/pkg/da"
header_pb "github.com/celestiaorg/celestia-node/header/pb"
)
// MarshalExtendedHeader serializes given ExtendedHeader to bytes using protobuf.
// Paired with UnmarshalExtendedHeader.
func MarshalExtendedHeader(in *ExtendedHeader) (_ []byte, err error) {
out := &header_pb.ExtendedHeader{
Header: in.RawHeader.ToProto(),
Commit: in.Commit.ToProto(),
}
out.ValidatorSet, err = in.ValidatorSet.ToProto()
if err != nil {
return nil, err
}
out.Dah, err = in.DAH.ToProto()
if err != nil {
return nil, err
}
return out.Marshal()
}
// UnmarshalExtendedHeader deserializes given data into a new ExtendedHeader using protobuf.
// Paired with MarshalExtendedHeader.
func UnmarshalExtendedHeader(data []byte) (*ExtendedHeader, error) {
in := &header_pb.ExtendedHeader{}
err := in.Unmarshal(data)
if err != nil {
return nil, err
}
out := &ExtendedHeader{}
out.RawHeader, err = core.HeaderFromProto(in.Header)
if err != nil {
return nil, err
}
out.Commit, err = core.CommitFromProto(in.Commit)
if err != nil {
return nil, err
}
out.ValidatorSet, err = core.ValidatorSetFromProto(in.ValidatorSet)
if err != nil {
return nil, err
}
out.DAH, err = da.DataAvailabilityHeaderFromProto(in.Dah)
if err != nil {
return nil, err
}
return out, out.ValidateBasic()
}
func ExtendedHeaderToProto(eh *ExtendedHeader) (*header_pb.ExtendedHeader, error) {
pb := &header_pb.ExtendedHeader{
Header: eh.RawHeader.ToProto(),
Commit: eh.Commit.ToProto(),
}
valSet, err := eh.ValidatorSet.ToProto()
if err != nil {
return nil, err
}
pb.ValidatorSet = valSet
dah, err := eh.DAH.ToProto()
if err != nil {
return nil, err
}
pb.Dah = dah
return pb, nil
}
func ProtoToExtendedHeader(pb *header_pb.ExtendedHeader) (*ExtendedHeader, error) {
bin, err := pb.Marshal()
if err != nil {
return nil, err
}
header := new(ExtendedHeader)
err = header.UnmarshalBinary(bin)
if err != nil {
return nil, err
}
return header, nil
}