forked from elastos/Elastos.ELA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dposproposalvote.go
111 lines (89 loc) · 2 KB
/
dposproposalvote.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
// Copyright (c) 2017-2019 The Elastos Foundation
// Use of this source code is governed by an MIT
// license that can be found in the LICENSE file.
//
package payload
import (
"bytes"
"io"
"github.com/elastos/Elastos.ELA/common"
"github.com/elastos/Elastos.ELA/crypto"
)
type DPOSProposalVote struct {
ProposalHash common.Uint256
Signer []byte
Accept bool
Sign []byte
hash *common.Uint256
}
func (v *DPOSProposalVote) Data() []byte {
buf := new(bytes.Buffer)
if err := v.SerializeUnsigned(buf); err != nil {
return []byte{0}
}
return buf.Bytes()
}
func (v *DPOSProposalVote) SerializeUnsigned(w io.Writer) error {
if err := v.ProposalHash.Serialize(w); err != nil {
return err
}
if err := common.WriteVarBytes(w, v.Signer); err != nil {
return err
}
var acceptValue uint8
if v.Accept {
acceptValue = 1
} else {
acceptValue = 0
}
if err := common.WriteUint8(w, acceptValue); err != nil {
return err
}
return nil
}
func (v *DPOSProposalVote) Serialize(w io.Writer) error {
if err := v.SerializeUnsigned(w); err != nil {
return err
}
return common.WriteVarBytes(w, v.Sign)
}
func (v *DPOSProposalVote) DeserializeUnsigned(r io.Reader) error {
if err := v.ProposalHash.Deserialize(r); err != nil {
return err
}
signer, err := common.ReadVarBytes(r, crypto.NegativeBigLength, "public key")
if err != nil {
return err
}
v.Signer = signer
acceptValue, err := common.ReadUint8(r)
if err != nil {
return err
}
if acceptValue == 1 {
v.Accept = true
} else {
v.Accept = false
}
return nil
}
func (v *DPOSProposalVote) Deserialize(r io.Reader) error {
if err := v.DeserializeUnsigned(r); err != nil {
return err
}
sign, err := common.ReadVarBytes(r, crypto.SignatureLength, "sign data")
if err != nil {
return err
}
v.Sign = sign
return nil
}
func (v *DPOSProposalVote) Hash() common.Uint256 {
if v.hash == nil {
buf := new(bytes.Buffer)
v.SerializeUnsigned(buf)
hash := common.Uint256(common.Sha256D(buf.Bytes()))
v.hash = &hash
}
return *v.hash
}