-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
105 lines (88 loc) · 2.39 KB
/
messages.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
package ibft
import (
"google.golang.org/protobuf/proto"
protoIBFT "github.com/0xDaoChain/go-ibft/messages/proto"
"github.com/0xDaoChain/dao-chain/types"
)
func (i *backendIBFT) signMessage(msg *protoIBFT.Message) *protoIBFT.Message {
raw, err := proto.Marshal(msg)
if err != nil {
return nil
}
if msg.Signature, err = i.currentSigner.SignIBFTMessage(raw); err != nil {
return nil
}
return msg
}
func (i *backendIBFT) BuildPrePrepareMessage(
proposal []byte,
certificate *protoIBFT.RoundChangeCertificate,
view *protoIBFT.View,
) *protoIBFT.Message {
block := &types.Block{}
if err := block.UnmarshalRLP(proposal); err != nil {
return nil
}
proposalHash := block.Hash().Bytes()
msg := &protoIBFT.Message{
View: view,
From: i.ID(),
Type: protoIBFT.MessageType_PREPREPARE,
Payload: &protoIBFT.Message_PreprepareData{
PreprepareData: &protoIBFT.PrePrepareMessage{
Proposal: proposal,
ProposalHash: proposalHash,
Certificate: certificate,
},
},
}
return i.signMessage(msg)
}
func (i *backendIBFT) BuildPrepareMessage(proposalHash []byte, view *protoIBFT.View) *protoIBFT.Message {
msg := &protoIBFT.Message{
View: view,
From: i.ID(),
Type: protoIBFT.MessageType_PREPARE,
Payload: &protoIBFT.Message_PrepareData{
PrepareData: &protoIBFT.PrepareMessage{
ProposalHash: proposalHash,
},
},
}
return i.signMessage(msg)
}
func (i *backendIBFT) BuildCommitMessage(proposalHash []byte, view *protoIBFT.View) *protoIBFT.Message {
committedSeal, err := i.currentSigner.CreateCommittedSeal(proposalHash)
if err != nil {
i.logger.Error("Unable to build commit message, %v", err)
return nil
}
msg := &protoIBFT.Message{
View: view,
From: i.ID(),
Type: protoIBFT.MessageType_COMMIT,
Payload: &protoIBFT.Message_CommitData{
CommitData: &protoIBFT.CommitMessage{
ProposalHash: proposalHash,
CommittedSeal: committedSeal,
},
},
}
return i.signMessage(msg)
}
func (i *backendIBFT) BuildRoundChangeMessage(
proposal []byte,
certificate *protoIBFT.PreparedCertificate,
view *protoIBFT.View,
) *protoIBFT.Message {
msg := &protoIBFT.Message{
View: view,
From: i.ID(),
Type: protoIBFT.MessageType_ROUND_CHANGE,
Payload: &protoIBFT.Message_RoundChangeData{RoundChangeData: &protoIBFT.RoundChangeMessage{
LastPreparedProposedBlock: proposal,
LatestPreparedCertificate: certificate,
}},
}
return i.signMessage(msg)
}