-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposal.go
470 lines (402 loc) · 10.2 KB
/
proposal.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package wire
import (
"bytes"
"encoding/binary"
"errors"
"io"
"github.com/golang/protobuf/proto"
"github.com/fatzero/mass-core/interfaces"
wirepb "github.com/fatzero/mass-core/wire/pb"
)
const (
ProposalVersion = 1
HeadersPerProposal = 2
HeaderSizePerPlaceHolder = 500
PlaceHolderSize = HeadersPerProposal * HeaderSizePerPlaceHolder
)
type ProposalType uint32
const (
typeFaultPubKey ProposalType = iota
typePlaceHolder
typeAnyMessage
)
// General Proposal Interface
type Proposal interface {
Bytes(CodecMode) ([]byte, error)
SetBytes([]byte, CodecMode) error
Hash(int) Hash
}
func GetProposalHash(proposal Proposal, index int) Hash {
var b8 [8]byte
binary.LittleEndian.PutUint64(b8[:], uint64(index))
buf, _ := proposal.Bytes(ID)
return DoubleHashH(append(buf, b8[:]...))
}
// FaultPubKey implements Proposal, representing for punishment-double-mining-pubKey
type FaultPubKey struct {
version uint32
PubKey interfaces.PublicKey
Testimony [HeadersPerProposal]*BlockHeader
}
func (fpk *FaultPubKey) Version() uint32 {
return fpk.version
}
func (fpk *FaultPubKey) Type() ProposalType {
return typeFaultPubKey
}
func (fpk *FaultPubKey) Encode(w io.Writer, mode CodecMode) (n int, err error) { //TODO:
pb := fpk.ToProto()
switch mode {
case Packet, DB:
buf, err := proto.Marshal(pb)
if err != nil {
return 0, err
}
return w.Write(buf)
case Plain, ID:
return pb.Write(w)
default:
return n, ErrInvalidCodecMode
}
}
func (fpk *FaultPubKey) Decode(r io.Reader, mode CodecMode) (n int, err error) {
switch mode {
case Packet, DB:
var buf bytes.Buffer
n64, err := buf.ReadFrom(r)
n = int(n64)
if err != nil {
return n, err
}
pb := new(wirepb.Punishment)
if err := proto.Unmarshal(buf.Bytes(), pb); err != nil {
return n, err
}
return n, fpk.FromProto(pb)
default:
return n, ErrInvalidCodecMode
}
}
func (fpk *FaultPubKey) ToProto() *wirepb.Punishment {
return &wirepb.Punishment{
Version: fpk.Version(),
Type: uint32(typeFaultPubKey),
TestimonyA: fpk.Testimony[0].ToProto(),
TestimonyB: fpk.Testimony[1].ToProto(),
}
}
func (fpk *FaultPubKey) FromProto(pb *wirepb.Punishment) error {
if pb == nil {
return errors.New("nil proto punishment")
}
fpk.version = pb.Version
headerA, headerB := new(BlockHeader), new(BlockHeader)
if err := headerA.FromProto(pb.TestimonyA); err != nil {
return err
}
if err := headerB.FromProto(pb.TestimonyB); err != nil {
return err
}
fpk.Testimony = [HeadersPerProposal]*BlockHeader{headerA, headerB}
fpk.PubKey = fpk.Testimony[0].PublicKey()
return nil
}
func (fpk *FaultPubKey) Bytes(mode CodecMode) ([]byte, error) {
//TODO: mass/chia
return getBytes(fpk, mode)
}
func (fpk *FaultPubKey) SetBytes(bs []byte, mode CodecMode) error {
return setFromBytes(fpk, bs, mode)
}
// PlainSize returns the number of bytes the FaultPubKey contains.
func (fpk *FaultPubKey) PlainSize() int {
return getPlainSize(fpk)
}
func (fpk *FaultPubKey) Hash(index int) Hash {
return GetProposalHash(fpk, index)
}
func (fpk *FaultPubKey) IsValid() error {
if fpk.PubKey == nil {
return errFaultPubKeyNoPubKey
}
if len(fpk.Testimony) < 2 {
return errFaultPubKeyNoTestimony
}
h0 := fpk.Testimony[0]
h1 := fpk.Testimony[1]
// check validity of testimony
if h0.Height != h1.Height {
return errFaultPubKeyWrongHeight
}
if h0.Proof.BitLength() != h1.Proof.BitLength() {
return errFaultPubKeyWrongBigLength
}
if h0.BlockHash() == h1.BlockHash() {
return errFaultPubKeySameBlock
}
fpkbytes := fpk.PubKey.SerializeUncompressed()
if !bytes.Equal(fpkbytes, h0.PublicKey().SerializeUncompressed()) ||
!bytes.Equal(fpkbytes, h1.PublicKey().SerializeUncompressed()) {
return errFaultPubKeyWrongPubKey
}
pass0, err0 := h0.VerifySig()
if err0 != nil {
return err0
}
pass1, err1 := h1.VerifySig()
if err1 != nil {
return err1
}
if !(pass0 && pass1) {
return errFaultPubKeyWrongSignature
}
return nil
}
func NewEmptyFaultPubKey() *FaultPubKey {
var t [HeadersPerProposal]*BlockHeader
for i := 0; i < HeadersPerProposal; i++ {
bh := NewEmptyBlockHeader()
t[i] = bh
}
return &FaultPubKey{
version: ProposalVersion,
PubKey: nil,
Testimony: t,
}
}
func NewFaultPubKeyFromBytes(bs []byte, mode CodecMode) (*FaultPubKey, error) {
fpk := new(FaultPubKey)
err := fpk.SetBytes(bs, mode)
if err != nil {
return nil, err
}
return fpk, nil
}
// NormalProposal implements Proposal, representing for all proposals
// with no consensus proposalType.
type NormalProposal struct {
version uint32
proposalType ProposalType
content []byte
}
func (np *NormalProposal) Version() uint32 {
return np.version
}
func (np *NormalProposal) Type() ProposalType {
return np.proposalType
}
func (np *NormalProposal) Content() []byte {
return np.content
}
func (np *NormalProposal) Encode(w io.Writer, mode CodecMode) (n int, err error) {
pb := np.ToProto()
switch mode {
case Packet, DB:
buf, err := proto.Marshal(pb)
if err != nil {
return n, err
}
return w.Write(buf)
case Plain, ID:
return pb.Write(w)
default:
return n, ErrInvalidCodecMode
}
}
func (np *NormalProposal) Decode(r io.Reader, mode CodecMode) (n int, err error) {
switch mode {
case Packet, DB:
var buf bytes.Buffer
n64, err := buf.ReadFrom(r)
n = int(n64)
if err != nil {
return n, err
}
pb := new(wirepb.Proposal)
if err = proto.Unmarshal(buf.Bytes(), pb); err != nil {
return n, err
}
if err = np.FromProto(pb); err != nil {
return n, err
}
return n, nil
default:
return n, ErrInvalidCodecMode
}
}
func (np *NormalProposal) ToProto() *wirepb.Proposal {
return &wirepb.Proposal{
Version: np.Version(),
Type: uint32(np.Type()),
Content: np.Content(),
}
}
func (np *NormalProposal) FromProto(pb *wirepb.Proposal) error {
if pb == nil {
return errors.New("nil proto proposal")
}
np.version = pb.Version
np.proposalType = ProposalType(pb.Type)
np.content = MoveBytes(pb.Content)
return nil
}
func (np *NormalProposal) Bytes(mode CodecMode) ([]byte, error) {
return getBytes(np, mode)
}
func (np *NormalProposal) SetBytes(bs []byte, mode CodecMode) error {
return setFromBytes(np, bs, mode)
}
func (np *NormalProposal) Hash(index int) Hash {
return GetProposalHash(np, index)
}
func (np *NormalProposal) PlainSize() int {
return getPlainSize(np)
}
// PlaceHolder prevents miner from eliminating all punishment proposals,
// because that is more profitable to save block size for transactions.
func NewPlaceHolder() *NormalProposal {
return &NormalProposal{
version: ProposalVersion,
proposalType: typePlaceHolder,
content: make([]byte, 2*HeaderSizePerPlaceHolder),
}
}
// ProposalArea represents for proposals in blocks.
type ProposalArea struct {
PunishmentArea []*FaultPubKey
OtherArea []*NormalProposal
}
func (pa *ProposalArea) Count() int {
return len(pa.PunishmentArea) + len(pa.OtherArea)
}
func (pa *ProposalArea) PunishmentCount() int {
return len(pa.PunishmentArea)
}
func (pa *ProposalArea) OtherCount() int {
return len(pa.OtherArea)
}
func (pa *ProposalArea) Encode(w io.Writer, mode CodecMode) (n int, err error) {
pb, err := pa.ToProto()
if err != nil {
return n, err
}
switch mode {
case Packet, DB:
buf, err := proto.Marshal(pb)
if err != nil {
return n, err
}
return w.Write(buf)
case Plain:
return pb.Write(w)
default:
return n, ErrInvalidCodecMode
}
}
func (pa *ProposalArea) Decode(r io.Reader, mode CodecMode) (n int, err error) {
switch mode {
case Packet, DB:
var buf bytes.Buffer
n64, err := buf.ReadFrom(r)
n = int(n64)
if err != nil {
return n, err
}
pb := new(wirepb.ProposalArea)
err = proto.Unmarshal(buf.Bytes(), pb)
if err != nil {
return n, err
}
err = pa.FromProto(pb)
return n, err
default:
return n, ErrInvalidCodecMode
}
}
func (pa *ProposalArea) Bytes(mode CodecMode) ([]byte, error) {
return getBytes(pa, mode)
}
func (pa *ProposalArea) SetBytes(bs []byte, mode CodecMode) error {
return setFromBytes(pa, bs, mode)
}
func (pa *ProposalArea) PlainSize() int {
size := getPlainSize(pa)
if pa.PunishmentCount() == 0 {
size += PlaceHolderSize
}
return size
}
func newEmptyProposalArea() *ProposalArea {
return &ProposalArea{
PunishmentArea: []*FaultPubKey{},
OtherArea: []*NormalProposal{},
}
}
func NewProposalArea(punishmentArea []*FaultPubKey, otherArea []*NormalProposal) (*ProposalArea, error) {
for _, other := range otherArea {
if other.proposalType < typeAnyMessage {
return nil, errWrongProposalType
}
}
return &ProposalArea{
PunishmentArea: punishmentArea,
OtherArea: otherArea,
}, nil
}
// ToProto get proto ProposalArea from wire ProposalArea
func (pa *ProposalArea) ToProto() (*wirepb.ProposalArea, error) {
punishments := make([]*wirepb.Punishment, pa.PunishmentCount())
for i, fpk := range pa.PunishmentArea {
punishments[i] = fpk.ToProto()
}
others := make([]*wirepb.Proposal, len(pa.OtherArea))
for i, proposal := range pa.OtherArea {
if proposal.proposalType < typeAnyMessage {
return nil, errWrongProposalType
}
others[i] = proposal.ToProto()
}
return &wirepb.ProposalArea{
Punishments: punishments,
OtherProposals: others,
}, nil
}
// FromProto load proto ProposalArea into wire ProposalArea,
// if error happens, old content is still immutable
func (pa *ProposalArea) FromProto(pb *wirepb.ProposalArea) error {
if pb == nil {
return errors.New("nil proto proposal_area")
}
var err error
punishments := make([]*FaultPubKey, len(pb.Punishments))
for i, v := range pb.Punishments {
fpk := new(FaultPubKey)
if err = fpk.FromProto(v); err != nil {
return err
}
punishments[i] = fpk
}
others := make([]*NormalProposal, len(pb.OtherProposals))
for i, v := range pb.OtherProposals {
if ProposalType(v.Type) < typeAnyMessage {
return errWrongProposalType
}
np := new(NormalProposal)
if err = np.FromProto(v); err != nil {
return err
}
others[i] = np
}
pa.PunishmentArea = punishments
pa.OtherArea = others
return nil
}
// NewProposalAreaFromProto get wire ProposalArea from proto ProposalArea
func NewProposalAreaFromProto(pb *wirepb.ProposalArea) (*ProposalArea, error) {
pa := new(ProposalArea)
err := pa.FromProto(pb)
if err != nil {
return nil, err
}
return pa, nil
}