-
Notifications
You must be signed in to change notification settings - Fork 77
/
payload.go
244 lines (208 loc) · 6.32 KB
/
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
package consensus
import (
"fmt"
"github.com/nspcc-dev/dbft/payload"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/io"
npayload "github.com/nspcc-dev/neo-go/pkg/network/payload"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
)
type (
messageType byte
message struct {
Type messageType
BlockIndex uint32
ValidatorIndex byte
ViewNumber byte
payload io.Serializable
// stateRootEnabled specifies if state root is exchanged during consensus.
stateRootEnabled bool
}
// Payload is a type for consensus-related messages.
Payload struct {
npayload.Extensible
message
network netmode.Magic
}
)
const (
changeViewType messageType = 0x00
prepareRequestType messageType = 0x20
prepareResponseType messageType = 0x21
commitType messageType = 0x30
recoveryRequestType messageType = 0x40
recoveryMessageType messageType = 0x41
payloadGasLimit = 2000000 // 0.02 GAS
)
// ViewNumber implements payload.ConsensusPayload interface.
func (p Payload) ViewNumber() byte {
return p.message.ViewNumber
}
// SetViewNumber implements payload.ConsensusPayload interface.
func (p *Payload) SetViewNumber(view byte) {
p.message.ViewNumber = view
}
// Type implements payload.ConsensusPayload interface.
func (p Payload) Type() payload.MessageType {
return payload.MessageType(p.message.Type)
}
// SetType implements payload.ConsensusPayload interface.
func (p *Payload) SetType(t payload.MessageType) {
p.message.Type = messageType(t)
}
// Payload implements payload.ConsensusPayload interface.
func (p Payload) Payload() interface{} {
return p.payload
}
// SetPayload implements payload.ConsensusPayload interface.
func (p *Payload) SetPayload(pl interface{}) {
p.payload = pl.(io.Serializable)
}
// GetChangeView implements payload.ConsensusPayload interface.
func (p Payload) GetChangeView() payload.ChangeView { return p.payload.(payload.ChangeView) }
// GetPrepareRequest implements payload.ConsensusPayload interface.
func (p Payload) GetPrepareRequest() payload.PrepareRequest {
return p.payload.(payload.PrepareRequest)
}
// GetPrepareResponse implements payload.ConsensusPayload interface.
func (p Payload) GetPrepareResponse() payload.PrepareResponse {
return p.payload.(payload.PrepareResponse)
}
// GetCommit implements payload.ConsensusPayload interface.
func (p Payload) GetCommit() payload.Commit { return p.payload.(payload.Commit) }
// GetRecoveryRequest implements payload.ConsensusPayload interface.
func (p Payload) GetRecoveryRequest() payload.RecoveryRequest {
return p.payload.(payload.RecoveryRequest)
}
// GetRecoveryMessage implements payload.ConsensusPayload interface.
func (p Payload) GetRecoveryMessage() payload.RecoveryMessage {
return p.payload.(payload.RecoveryMessage)
}
// ValidatorIndex implements payload.ConsensusPayload interface.
func (p Payload) ValidatorIndex() uint16 {
return uint16(p.message.ValidatorIndex)
}
// SetValidatorIndex implements payload.ConsensusPayload interface.
func (p *Payload) SetValidatorIndex(i uint16) {
p.message.ValidatorIndex = byte(i)
}
// Height implements payload.ConsensusPayload interface.
func (p Payload) Height() uint32 {
return p.message.BlockIndex
}
// SetHeight implements payload.ConsensusPayload interface.
func (p *Payload) SetHeight(h uint32) {
p.message.BlockIndex = h
}
// EncodeBinary implements io.Serializable interface.
func (p *Payload) EncodeBinary(w *io.BinWriter) {
p.encodeData()
p.Extensible.EncodeBinary(w)
}
// Sign signs payload using the private key.
// It also sets corresponding verification and invocation scripts.
func (p *Payload) Sign(key *privateKey) error {
p.encodeData()
sig := key.PrivateKey.SignHashable(uint32(p.network), &p.Extensible)
buf := io.NewBufBinWriter()
emit.Bytes(buf.BinWriter, sig)
p.Witness.InvocationScript = buf.Bytes()
p.Witness.VerificationScript = key.PublicKey().GetVerificationScript()
return nil
}
// Hash implements payload.ConsensusPayload interface.
func (p *Payload) Hash() util.Uint256 {
if p.Extensible.Data == nil {
p.encodeData()
}
return p.Extensible.Hash()
}
// DecodeBinary implements io.Serializable interface.
func (p *Payload) DecodeBinary(r *io.BinReader) {
p.Extensible.DecodeBinary(r)
if r.Err == nil {
r.Err = p.decodeData()
}
}
// EncodeBinary implements io.Serializable interface.
func (m *message) EncodeBinary(w *io.BinWriter) {
w.WriteB(byte(m.Type))
w.WriteU32LE(m.BlockIndex)
w.WriteB(m.ValidatorIndex)
w.WriteB(m.ViewNumber)
m.payload.EncodeBinary(w)
}
// DecodeBinary implements io.Serializable interface.
func (m *message) DecodeBinary(r *io.BinReader) {
m.Type = messageType(r.ReadB())
m.BlockIndex = r.ReadU32LE()
m.ValidatorIndex = r.ReadB()
m.ViewNumber = r.ReadB()
switch m.Type {
case changeViewType:
cv := new(changeView)
// newViewNumber is not marshaled
cv.newViewNumber = m.ViewNumber + 1
m.payload = cv
case prepareRequestType:
r := new(prepareRequest)
if m.stateRootEnabled {
r.stateRootEnabled = true
}
m.payload = r
case prepareResponseType:
m.payload = new(prepareResponse)
case commitType:
m.payload = new(commit)
case recoveryRequestType:
m.payload = new(recoveryRequest)
case recoveryMessageType:
r := new(recoveryMessage)
if m.stateRootEnabled {
r.stateRootEnabled = true
}
m.payload = r
default:
r.Err = fmt.Errorf("invalid type: 0x%02x", byte(m.Type))
return
}
m.payload.DecodeBinary(r)
}
// String implements fmt.Stringer interface.
func (t messageType) String() string {
switch t {
case changeViewType:
return "ChangeView"
case prepareRequestType:
return "PrepareRequest"
case prepareResponseType:
return "PrepareResponse"
case commitType:
return "Commit"
case recoveryRequestType:
return "RecoveryRequest"
case recoveryMessageType:
return "RecoveryMessage"
default:
return fmt.Sprintf("UNKNOWN(0x%02x)", byte(t))
}
}
func (p *Payload) encodeData() {
if p.Extensible.Data == nil {
p.Extensible.ValidBlockStart = 0
p.Extensible.ValidBlockEnd = p.BlockIndex
bw := io.NewBufBinWriter()
p.message.EncodeBinary(bw.BinWriter)
p.Extensible.Data = bw.Bytes()
}
}
// decode data of payload into its message.
func (p *Payload) decodeData() error {
br := io.NewBinReaderFromBuf(p.Extensible.Data)
p.message.DecodeBinary(br)
if br.Err != nil {
return fmt.Errorf("can't decode message: %w", br.Err)
}
return nil
}