-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare.go
78 lines (69 loc) · 1.61 KB
/
prepare.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
package qbfttypes
import (
"fmt"
"io"
"math/big"
"github.com/electroneum/electroneum-sc/common"
"github.com/electroneum/electroneum-sc/rlp"
)
// A QBFT PREPARE message.
type Prepare struct {
CommonPayload
Digest common.Hash
}
func NewPrepare(sequence *big.Int, round *big.Int, digest common.Hash) *Prepare {
return &Prepare{
CommonPayload: CommonPayload{
code: PrepareCode,
Sequence: sequence,
Round: round,
},
Digest: digest,
}
}
func NewPrepareWithSigAndSource(sequence *big.Int, round *big.Int, digest common.Hash, signature []byte, source common.Address) *Prepare {
prepare := NewPrepare(sequence, round, digest)
prepare.signature = signature
prepare.source = source
return prepare
}
func (p *Prepare) String() string {
return fmt.Sprintf("Prepare {seq=%v, round=%v, digest=%v}", p.Sequence, p.Round, p.Digest.Hex())
}
func (p *Prepare) EncodePayloadForSigning() ([]byte, error) {
return rlp.EncodeToBytes(
[]interface{}{
p.Code(),
[]interface{}{p.Sequence, p.Round, p.Digest},
})
}
func (p *Prepare) EncodeRLP(w io.Writer) error {
return rlp.Encode(
w,
[]interface{}{
[]interface{}{
p.Sequence,
p.Round,
p.Digest},
p.signature,
})
}
func (p *Prepare) DecodeRLP(stream *rlp.Stream) error {
var message struct {
Payload struct {
Sequence *big.Int
Round *big.Int
Digest common.Hash
}
Signature []byte
}
if err := stream.Decode(&message); err != nil {
return err
}
p.code = PrepareCode
p.Sequence = message.Payload.Sequence
p.Round = message.Payload.Round
p.Digest = message.Payload.Digest
p.signature = message.Signature
return nil
}