-
Notifications
You must be signed in to change notification settings - Fork 199
/
blsWorker.go
136 lines (111 loc) · 5.27 KB
/
blsWorker.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
package bls
import (
"github.com/ElrondNetwork/elrond-go/consensus"
"github.com/ElrondNetwork/elrond-go/consensus/spos"
)
// peerMaxMessagesPerSec defines how many messages can be propagated by a pid in a round. The value was chosen by
// following the next premises:
// 1. a leader can propagate as maximum as 3 messages per round: proposed header block + proposed body + final info;
// 2. due to the fact that a delayed signature of the proposer (from previous round) can be received in the current round
// adds an extra 1 to the total value, reaching value 4;
// 3. Because the leader might be selected in the next round and might have an empty data pool, it can send the newly
// empty proposed block at the very beginning of the next round. One extra message here, yielding to a total of 5.
// 4. If we consider the forks that can appear on the system wee need to add one more to the value.
// Validators only send one signature message in a round, treating the edge case of a delayed message, will need at most
// 2 messages per round (which is ok as it is below the set value of 5)
const peerMaxMessagesPerSec = uint32(6)
// worker defines the data needed by spos to communicate between nodes which are in the validators group
type worker struct {
}
// NewConsensusService creates a new worker object
func NewConsensusService() (*worker, error) {
wrk := worker{}
return &wrk, nil
}
//InitReceivedMessages initializes the MessagesType map for all messages for the current ConsensusService
func (wrk *worker) InitReceivedMessages() map[consensus.MessageType][]*consensus.Message {
receivedMessages := make(map[consensus.MessageType][]*consensus.Message)
receivedMessages[MtBlockBodyAndHeader] = make([]*consensus.Message, 0)
receivedMessages[MtBlockBody] = make([]*consensus.Message, 0)
receivedMessages[MtBlockHeader] = make([]*consensus.Message, 0)
receivedMessages[MtSignature] = make([]*consensus.Message, 0)
receivedMessages[MtBlockHeaderFinalInfo] = make([]*consensus.Message, 0)
return receivedMessages
}
// GetMaxMessagesInARoundPerPeer returns the maximum number of messages a peer can send per round for BLS
func (wrk *worker) GetMaxMessagesInARoundPerPeer() uint32 {
return peerMaxMessagesPerSec
}
//GetStringValue gets the name of the messageType
func (wrk *worker) GetStringValue(messageType consensus.MessageType) string {
return getStringValue(messageType)
}
//GetSubroundName gets the subround name for the subround id provided
func (wrk *worker) GetSubroundName(subroundId int) string {
return getSubroundName(subroundId)
}
//IsMessageWithBlockBodyAndHeader returns if the current messageType is about block body and header
func (wrk *worker) IsMessageWithBlockBodyAndHeader(msgType consensus.MessageType) bool {
return msgType == MtBlockBodyAndHeader
}
//IsMessageWithBlockBody returns if the current messageType is about block body
func (wrk *worker) IsMessageWithBlockBody(msgType consensus.MessageType) bool {
return msgType == MtBlockBody
}
//IsMessageWithBlockHeader returns if the current messageType is about block header
func (wrk *worker) IsMessageWithBlockHeader(msgType consensus.MessageType) bool {
return msgType == MtBlockHeader
}
//IsMessageWithSignature returns if the current messageType is about signature
func (wrk *worker) IsMessageWithSignature(msgType consensus.MessageType) bool {
return msgType == MtSignature
}
//IsMessageWithFinalInfo returns if the current messageType is about header final info
func (wrk *worker) IsMessageWithFinalInfo(msgType consensus.MessageType) bool {
return msgType == MtBlockHeaderFinalInfo
}
//IsMessageTypeValid returns if the current messageType is valid
func (wrk *worker) IsMessageTypeValid(msgType consensus.MessageType) bool {
isMessageTypeValid := msgType == MtBlockBodyAndHeader ||
msgType == MtBlockBody ||
msgType == MtBlockHeader ||
msgType == MtSignature ||
msgType == MtBlockHeaderFinalInfo
return isMessageTypeValid
}
//IsSubroundSignature returns if the current subround is about signature
func (wrk *worker) IsSubroundSignature(subroundId int) bool {
return subroundId == SrSignature
}
//IsSubroundStartRound returns if the current subround is about start round
func (wrk *worker) IsSubroundStartRound(subroundId int) bool {
return subroundId == SrStartRound
}
//GetMessageRange provides the MessageType range used in checks by the consensus
func (wrk *worker) GetMessageRange() []consensus.MessageType {
var v []consensus.MessageType
for i := MtBlockBodyAndHeader; i <= MtBlockHeaderFinalInfo; i++ {
v = append(v, i)
}
return v
}
//CanProceed returns if the current messageType can proceed further if previous subrounds finished
func (wrk *worker) CanProceed(consensusState *spos.ConsensusState, msgType consensus.MessageType) bool {
switch msgType {
case MtBlockBodyAndHeader:
return consensusState.Status(SrStartRound) == spos.SsFinished
case MtBlockBody:
return consensusState.Status(SrStartRound) == spos.SsFinished
case MtBlockHeader:
return consensusState.Status(SrStartRound) == spos.SsFinished
case MtSignature:
return consensusState.Status(SrBlock) == spos.SsFinished
case MtBlockHeaderFinalInfo:
return consensusState.Status(SrSignature) == spos.SsFinished
}
return false
}
// IsInterfaceNil returns true if there is no value under the interface
func (wrk *worker) IsInterfaceNil() bool {
return wrk == nil
}