-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus_payload.go
281 lines (233 loc) · 7.55 KB
/
consensus_payload.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package types
import (
"bytes"
"fmt"
"io"
"github.com/dnaproject2/DNA/common"
"github.com/dnaproject2/DNA/common/log"
"github.com/dnaproject2/DNA/common/serialization"
"github.com/dnaproject2/DNA/core/signature"
"github.com/dnaproject2/DNA/errors"
"github.com/ontio/ontology-crypto/keypair"
)
type ConsensusPayload struct {
Version uint32
PrevHash common.Uint256
Height uint32
BookkeeperIndex uint16
Timestamp uint32
Data []byte
Owner keypair.PublicKey
Signature []byte
PeerId uint64
hash common.Uint256
}
//get the consensus payload hash
func (this *ConsensusPayload) Hash() common.Uint256 {
return common.Uint256{}
}
//Check whether header is correct
func (this *ConsensusPayload) Verify() error {
buf := new(bytes.Buffer)
err := this.SerializeUnsigned(buf)
if err != nil {
return err
}
err = signature.Verify(this.Owner, buf.Bytes(), this.Signature)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetVerifyFail, fmt.Sprintf("signature verify error. buf:%v", buf))
}
return nil
}
//serialize the consensus payload
func (this *ConsensusPayload) ToArray() []byte {
b := new(bytes.Buffer)
err := this.Serialize(b)
if err != nil {
log.Errorf("consensus payload serialize error in ToArray(). payload:%v", this)
return nil
}
return b.Bytes()
}
//return inventory type
func (this *ConsensusPayload) InventoryType() common.InventoryType {
return common.CONSENSUS
}
func (this *ConsensusPayload) GetMessage() []byte {
//TODO: GetMessage
//return sig.GetHashData(cp)
return []byte{}
}
func (this *ConsensusPayload) Type() common.InventoryType {
//TODO:Temporary add for Interface signature.SignableData use.
return common.CONSENSUS
}
func (this *ConsensusPayload) Serialization(sink *common.ZeroCopySink) {
this.serializationUnsigned(sink)
buf := keypair.SerializePublicKey(this.Owner)
sink.WriteVarBytes(buf)
sink.WriteVarBytes(this.Signature)
}
//Serialize message payload
func (this *ConsensusPayload) Serialize(w io.Writer) error {
err := this.SerializeUnsigned(w)
if err != nil {
return err
}
buf := keypair.SerializePublicKey(this.Owner)
err = serialization.WriteVarBytes(w, buf)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write publickey error. publickey buf:%v", buf))
}
err = serialization.WriteVarBytes(w, this.Signature)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write Signature error. Signature:%v", this.Signature))
}
return nil
}
//Deserialize message payload
func (this *ConsensusPayload) Deserialization(source *common.ZeroCopySource) error {
err := this.deserializationUnsigned(source)
if err != nil {
return err
}
buf, _, irregular, eof := source.NextVarBytes()
if eof {
return io.ErrUnexpectedEOF
}
if irregular {
return common.ErrIrregularData
}
this.Owner, err = keypair.DeserializePublicKey(buf)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "deserialize publickey error")
}
this.Signature, _, irregular, eof = source.NextVarBytes()
if irregular {
return common.ErrIrregularData
}
if eof {
return io.ErrUnexpectedEOF
}
return nil
}
//Deserialize message payload
func (this *ConsensusPayload) Deserialize(r io.Reader) error {
err := this.DeserializeUnsigned(r)
if err != nil {
return err
}
buf, err := serialization.ReadVarBytes(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read buf error")
}
this.Owner, err = keypair.DeserializePublicKey(buf)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "deserialize publickey error")
}
this.Signature, err = serialization.ReadVarBytes(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Signature error")
}
return err
}
func (this *ConsensusPayload) serializationUnsigned(sink *common.ZeroCopySink) {
sink.WriteUint32(this.Version)
sink.WriteHash(this.PrevHash)
sink.WriteUint32(this.Height)
sink.WriteUint16(this.BookkeeperIndex)
sink.WriteUint32(this.Timestamp)
sink.WriteVarBytes(this.Data)
}
//Serialize message payload
func (this *ConsensusPayload) SerializeUnsigned(w io.Writer) error {
err := serialization.WriteUint32(w, this.Version)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. version:%v", this.Version))
}
err = this.PrevHash.Serialize(w)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("serialize error. PrevHash:%v", this.PrevHash))
}
err = serialization.WriteUint32(w, this.Height)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Height:%v", this.Height))
}
err = serialization.WriteUint16(w, this.BookkeeperIndex)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. BookkeeperIndex:%v", this.BookkeeperIndex))
}
err = serialization.WriteUint32(w, this.Timestamp)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Timestamp:%v", this.Timestamp))
}
err = serialization.WriteVarBytes(w, this.Data)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetPackFail, fmt.Sprintf("write error. Data:%v", this.Data))
}
return nil
}
func (this *ConsensusPayload) deserializationUnsigned(source *common.ZeroCopySource) error {
var irregular, eof bool
this.Version, eof = source.NextUint32()
this.PrevHash, eof = source.NextHash()
this.Height, eof = source.NextUint32()
this.BookkeeperIndex, eof = source.NextUint16()
this.Timestamp, eof = source.NextUint32()
this.Data, _, irregular, eof = source.NextVarBytes()
if eof {
return io.ErrUnexpectedEOF
}
if irregular {
return common.ErrIrregularData
}
return nil
}
//Deserialize message payload
func (this *ConsensusPayload) DeserializeUnsigned(r io.Reader) error {
var err error
this.Version, err = serialization.ReadUint32(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read version error")
}
preBlock := new(common.Uint256)
err = preBlock.Deserialize(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read preBlock error")
}
this.PrevHash = *preBlock
this.Height, err = serialization.ReadUint32(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Height error")
}
this.BookkeeperIndex, err = serialization.ReadUint16(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read BookkeeperIndex error")
}
this.Timestamp, err = serialization.ReadUint32(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Timestamp error")
}
this.Data, err = serialization.ReadVarBytes(r)
if err != nil {
return errors.NewDetailErr(err, errors.ErrNetUnPackFail, "read Data error")
}
return nil
}