-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprepare.go
84 lines (76 loc) · 1.99 KB
/
preprepare.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
package qbfttypes
import (
"fmt"
"io"
"math/big"
"github.com/electroneum/electroneum-sc/consensus/istanbul"
"github.com/electroneum/electroneum-sc/core/types"
"github.com/electroneum/electroneum-sc/rlp"
)
type Preprepare struct {
CommonPayload
Proposal istanbul.Proposal
JustificationRoundChanges []*SignedRoundChangePayload
JustificationPrepares []*Prepare
}
func NewPreprepare(sequence *big.Int, round *big.Int, proposal istanbul.Proposal) *Preprepare {
return &Preprepare{
CommonPayload: CommonPayload{
code: PreprepareCode,
Sequence: sequence,
Round: round,
},
Proposal: proposal,
}
}
func (m *Preprepare) EncodePayloadForSigning() ([]byte, error) {
return rlp.EncodeToBytes(
[]interface{}{
m.Code(),
[]interface{}{m.Sequence, m.Round, m.Proposal},
})
}
func (m *Preprepare) EncodeRLP(w io.Writer) error {
return rlp.Encode(
w,
[]interface{}{
[]interface{}{
[]interface{}{m.Sequence, m.Round, m.Proposal},
m.signature,
},
[]interface{}{
m.JustificationRoundChanges,
m.JustificationPrepares,
},
})
}
func (m *Preprepare) DecodeRLP(stream *rlp.Stream) error {
var message struct {
SignedPayload struct {
Payload struct {
Sequence *big.Int
Round *big.Int
Proposal *types.Block
}
Signature []byte
}
Justification struct {
RoundChanges []*SignedRoundChangePayload
Prepares []*Prepare
}
}
if err := stream.Decode(&message); err != nil {
return err
}
m.code = PreprepareCode
m.Sequence = message.SignedPayload.Payload.Sequence
m.Round = message.SignedPayload.Payload.Round
m.Proposal = message.SignedPayload.Payload.Proposal
m.signature = message.SignedPayload.Signature
m.JustificationPrepares = message.Justification.Prepares
m.JustificationRoundChanges = message.Justification.RoundChanges
return nil
}
func (m *Preprepare) String() string {
return fmt.Sprintf("code: %d, sequence: %d, round: %d, proposal: %v", m.code, m.Sequence, m.Round, m.Proposal.Hash().Hex())
}