forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization.go
229 lines (204 loc) · 5.88 KB
/
serialization.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package types
import (
"errors"
abci "github.com/lazyledger/lazyledger-core/abci/types"
pb "github.com/lazyledger/optimint/types/pb/optimint"
)
// MarshalBinary encodes Block into binary form and returns it.
func (b *Block) MarshalBinary() ([]byte, error) {
return b.ToProto().Marshal()
}
// MarshalBinary encodes Header into binary form and returns it.
func (h *Header) MarshalBinary() ([]byte, error) {
return h.ToProto().Marshal()
}
// MarshalBinary encodes Data into binary form and returns it.
func (d *Data) MarshalBinary() ([]byte, error) {
return d.ToProto().Marshal()
}
// UnmarshalBinary decodes binary form of Block into object.
func (b *Block) UnmarshalBinary(data []byte) error {
var pBlock pb.Block
err := pBlock.Unmarshal(data)
if err != nil {
return err
}
err = b.FromProto(&pBlock)
return err
}
// MarshalBinary encodes Commit into binary form and returns it.
func (c *Commit) MarshalBinary() ([]byte, error) {
return c.ToProto().Marshal()
}
// UnmarshalBinary decodes binary form of Commit into object.
func (c *Commit) UnmarshalBinary(data []byte) error {
var pCommit pb.Commit
err := pCommit.Unmarshal(data)
if err != nil {
return err
}
err = c.FromProto(&pCommit)
return err
}
// ToProto converts Header into protobuf representation and returns it.
func (h *Header) ToProto() *pb.Header {
return &pb.Header{
Version: &pb.Version{
Block: h.Version.Block,
App: h.Version.App,
},
NamespaceId: h.NamespaceID[:],
Height: h.Height,
Time: h.Time,
LastHeaderHash: h.LastHeaderHash[:],
LastCommitHash: h.LastCommitHash[:],
DataHash: h.DataHash[:],
ConsensusHash: h.ConsensusHash[:],
AppHash: h.AppHash[:],
LastResultsHash: h.LastResultsHash[:],
ProposerAddress: h.ProposerAddress[:],
}
}
// FromProto fills Header with data from its protobuf representation.
func (h *Header) FromProto(other *pb.Header) error {
h.Version.Block = other.Version.Block
h.Version.App = other.Version.App
if !safeCopy(h.NamespaceID[:], other.NamespaceId) {
return errors.New("invalid length of 'NamespaceId'")
}
h.Height = other.Height
h.Time = other.Time
if !safeCopy(h.LastHeaderHash[:], other.LastHeaderHash) {
return errors.New("invalid length of 'InvalidLastHeaderHash'")
}
if !safeCopy(h.LastCommitHash[:], other.LastCommitHash) {
return errors.New("invalid length of 'InvalidLastCommitHash'")
}
if !safeCopy(h.DataHash[:], other.DataHash) {
return errors.New("invalid length of 'InvalidDataHash'")
}
if !safeCopy(h.ConsensusHash[:], other.ConsensusHash) {
return errors.New("invalid length of 'InvalidConsensusHash'")
}
if !safeCopy(h.AppHash[:], other.AppHash) {
return errors.New("invalid length of 'InvalidAppHash'")
}
if !safeCopy(h.LastResultsHash[:], other.LastResultsHash) {
return errors.New("invalid length of 'InvalidLastResultsHash'")
}
if len(other.ProposerAddress) > 0 {
h.ProposerAddress = make([]byte, len(other.ProposerAddress))
copy(h.ProposerAddress, other.ProposerAddress)
}
return nil
}
// safeCopy copies bytes from src slice into dst slice if both have same size.
// It returns true if sizes of src and dst are the same.
func safeCopy(dst, src []byte) bool {
if len(src) != len(dst) {
return false
}
_ = copy(dst, src)
return true
}
// ToProto converts Block into protobuf representation and returns it.
func (b *Block) ToProto() *pb.Block {
return &pb.Block{
Header: b.Header.ToProto(),
Data: b.Data.ToProto(),
LastCommit: b.LastCommit.ToProto(),
}
}
// ToProto converts Data into protobuf representation and returns it.
func (d *Data) ToProto() *pb.Data {
return &pb.Data{
Txs: txsToByteSlices(d.Txs),
IntermediateStateRoots: d.IntermediateStateRoots.RawRootsList,
Evidence: evidenceToProto(d.Evidence),
}
}
// FromProto fills Block with data from its protobuf representation.
func (b *Block) FromProto(other *pb.Block) error {
err := b.Header.FromProto(other.Header)
if err != nil {
return err
}
b.Data.Txs = byteSlicesToTxs(other.Data.Txs)
b.Data.IntermediateStateRoots.RawRootsList = other.Data.IntermediateStateRoots
b.Data.Evidence = evidenceFromProto(other.Data.Evidence)
if other.LastCommit != nil {
err := b.LastCommit.FromProto(other.LastCommit)
if err != nil {
return err
}
}
return nil
}
// ToProto converts Commit into protobuf representation and returns it.
func (c *Commit) ToProto() *pb.Commit {
return &pb.Commit{
Height: c.Height,
HeaderHash: c.HeaderHash[:],
Signatures: signaturesToByteSlices(c.Signatures),
}
}
// FromProto fills Commit with data from its protobuf representation.
func (c *Commit) FromProto(other *pb.Commit) error {
c.Height = other.Height
if !safeCopy(c.HeaderHash[:], other.HeaderHash) {
return errors.New("invalid length of HeaderHash")
}
c.Signatures = byteSlicesToSignatures(other.Signatures)
return nil
}
func txsToByteSlices(txs Txs) [][]byte {
bytes := make([][]byte, len(txs))
for i := range txs {
bytes[i] = txs[i]
}
return bytes
}
func byteSlicesToTxs(bytes [][]byte) Txs {
if len(bytes) == 0 {
return nil
}
txs := make(Txs, len(bytes))
for i := range txs {
txs[i] = bytes[i]
}
return txs
}
func evidenceToProto(evidence EvidenceData) []*abci.Evidence {
var ret []*abci.Evidence
for _, e := range evidence.Evidence {
for _, ae := range e.ABCI() {
ret = append(ret, &ae)
}
}
return ret
}
func evidenceFromProto(evidence []*abci.Evidence) EvidenceData {
var ret EvidenceData
// TODO(tzdybal): right now Evidence is just an interface without implementations
return ret
}
func signaturesToByteSlices(sigs []Signature) [][]byte {
if sigs == nil {
return nil
}
bytes := make([][]byte, len(sigs))
for i := range sigs {
bytes[i] = sigs[i]
}
return bytes
}
func byteSlicesToSignatures(bytes [][]byte) []Signature {
if bytes == nil {
return nil
}
sigs := make([]Signature, len(bytes))
for i := range bytes {
sigs[i] = bytes[i]
}
return sigs
}