forked from pokt-network/pocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.go
More file actions
304 lines (243 loc) · 9.05 KB
/
Copy pathmodule.go
File metadata and controls
304 lines (243 loc) · 9.05 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package consensus
import (
"fmt"
"log"
"sort"
"sync"
"github.com/pokt-network/pocket/consensus/leader_election"
typesCons "github.com/pokt-network/pocket/consensus/types"
"github.com/pokt-network/pocket/shared/codec"
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
"google.golang.org/protobuf/types/known/anypb"
consensusTelemetry "github.com/pokt-network/pocket/consensus/telemetry"
"github.com/pokt-network/pocket/shared/modules"
)
const (
DefaultLogPrefix = "NODE" // TODO(#164): Make implicit when logging is standardized
consensusModuleName = "consensus"
)
var (
_ modules.ConsensusModule = &consensusModule{}
_ modules.ConsensusConfig = &typesCons.ConsensusConfig{}
_ modules.ConsensusGenesisState = &typesCons.ConsensusGenesisState{}
)
// TODO(#256): Do not export the `ConsensusModule` struct or the fields inside of it.
type consensusModule struct {
bus modules.Bus
privateKey cryptoPocket.Ed25519PrivateKey
consCfg modules.ConsensusConfig
consGenesis modules.ConsensusGenesisState
// m is a mutex used to control synchronization when multiple goroutines are accessing the struct and its fields / properties.
//
// The idea is that you want to acquire a Lock when you are writing values and a RLock when you want to make sure that no other goroutine is changing the values you are trying to read concurrently.
//
// Locking context should be the smallest possible but not smaller than a single "unit of work".
m sync.RWMutex
// Hotstuff
Height uint64
Round uint64
Step typesCons.HotstuffStep
Block *typesCons.Block // The current block being proposed / voted on; it has not been committed to finality
// TODO(#315): Move the statefulness of `TxResult` to the persistence module
TxResults []modules.TxResult // The current block applied transaction results / voted on; it has not been committed to finality
highPrepareQC *typesCons.QuorumCertificate // Highest QC for which replica voted PRECOMMIT
lockedQC *typesCons.QuorumCertificate // Highest QC for which replica voted COMMIT
// Leader Election
LeaderId *typesCons.NodeId
nodeId typesCons.NodeId
valAddrToIdMap typesCons.ValAddrToIdMap // TODO: This needs to be updated every time the ValMap is modified
idToValAddrMap typesCons.IdToValAddrMap // TODO: This needs to be updated every time the ValMap is modified
// Consensus State
validatorMap typesCons.ValidatorMap
// Module Dependencies
// TODO(#283): Improve how `utilityContext` is managed
utilityContext modules.UtilityContext
paceMaker Pacemaker
leaderElectionMod leader_election.LeaderElectionModule
// DEPRECATE: Remove later when we build a shared/proper/injected logger
logPrefix string
// TECHDEBT: Move this over to use the txIndexer
messagePool map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage
}
func Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) {
return new(consensusModule).Create(runtimeMgr)
}
func (*consensusModule) Create(runtimeMgr modules.RuntimeMgr) (modules.Module, error) {
var m *consensusModule
cfg := runtimeMgr.GetConfig()
if err := m.ValidateConfig(cfg); err != nil {
return nil, fmt.Errorf("config validation failed: %w", err)
}
consensusCfg := cfg.GetConsensusConfig()
genesis := runtimeMgr.GetGenesis()
if err := m.ValidateGenesis(genesis); err != nil {
return nil, fmt.Errorf("genesis validation failed: %w", err)
}
consensusGenesis := genesis.GetConsensusGenesisState()
leaderElectionMod, err := leader_election.Create(runtimeMgr)
if err != nil {
return nil, err
}
// TODO(olshansky): Can we make this a submodule?
paceMakerMod, err := CreatePacemaker(runtimeMgr)
if err != nil {
return nil, err
}
valMap := typesCons.ActorListToValidatorMap(consensusGenesis.GetVals())
privateKey, err := cryptoPocket.NewPrivateKey(consensusCfg.GetPrivateKey())
if err != nil {
return nil, err
}
address := privateKey.Address().String()
valIdMap, idValMap := typesCons.GetValAddrToIdMap(valMap)
paceMaker := paceMakerMod.(Pacemaker)
m = &consensusModule{
bus: nil,
privateKey: privateKey.(cryptoPocket.Ed25519PrivateKey),
consCfg: cfg.GetConsensusConfig(),
consGenesis: genesis.GetConsensusGenesisState(),
Height: 0,
Round: 0,
Step: NewRound,
Block: nil,
highPrepareQC: nil,
lockedQC: nil,
nodeId: valIdMap[address],
LeaderId: nil,
valAddrToIdMap: valIdMap,
idToValAddrMap: idValMap,
validatorMap: valMap,
utilityContext: nil,
paceMaker: paceMaker,
leaderElectionMod: leaderElectionMod.(leader_election.LeaderElectionModule),
logPrefix: DefaultLogPrefix,
messagePool: make(map[typesCons.HotstuffStep][]*typesCons.HotstuffMessage),
}
// TODO(olshansky): Look for a way to avoid doing this.
paceMaker.SetConsensusModule(m)
return m, nil
}
func (m *consensusModule) Start() error {
m.GetBus().
GetTelemetryModule().
GetTimeSeriesAgent().
CounterRegister(
consensusTelemetry.CONSENSUS_BLOCKCHAIN_HEIGHT_COUNTER_NAME,
consensusTelemetry.CONSENSUS_BLOCKCHAIN_HEIGHT_COUNTER_DESCRIPTION,
)
if err := m.loadPersistedState(); err != nil {
return err
}
if err := m.paceMaker.Start(); err != nil {
return err
}
if err := m.leaderElectionMod.Start(); err != nil {
return err
}
return nil
}
func (m *consensusModule) Stop() error {
return nil
}
func (m *consensusModule) GetModuleName() string {
return consensusModuleName
}
func (m *consensusModule) GetBus() modules.Bus {
if m.bus == nil {
log.Fatalf("PocketBus is not initialized")
}
return m.bus
}
func (m *consensusModule) SetBus(pocketBus modules.Bus) {
m.bus = pocketBus
m.paceMaker.SetBus(pocketBus)
m.leaderElectionMod.SetBus(pocketBus)
}
func (*consensusModule) ValidateConfig(cfg modules.Config) error {
// TODO (#334): implement this
return nil
}
func (*consensusModule) ValidateGenesis(genesis modules.GenesisState) error {
// Sort the validators by their generic param (i.e. service URL)
vals := genesis.GetConsensusGenesisState().GetVals()
sort.Slice(vals, func(i, j int) bool {
return vals[i].GetGenericParam() < vals[j].GetGenericParam()
})
// Sort the validators by their address
vals2 := vals[:]
sort.Slice(vals, func(i, j int) bool {
return vals[i].GetAddress() < vals[j].GetAddress()
})
for i := 0; i < len(vals); i++ {
if vals[i].GetAddress() != vals2[i].GetAddress() {
// There is an implicit dependency because of how RainTree works and how the validator map
// is currently managed to make sure that the ordering of the address and the service URL
// are the same. This will be addressed once the # of validators will scale.
panic("HACK(olshansky): service url and address must be sorted the same way")
}
}
return nil
}
func (m *consensusModule) GetPrivateKey() (cryptoPocket.PrivateKey, error) {
return cryptoPocket.NewPrivateKey(m.consCfg.GetPrivateKey())
}
func (m *consensusModule) HandleMessage(message *anypb.Any) error {
m.m.Lock()
defer m.m.Unlock()
switch message.MessageName() {
case HotstuffMessage:
msg, err := codec.GetCodec().FromAny(message)
if err != nil {
return err
}
hotstuffMessage, ok := msg.(*typesCons.HotstuffMessage)
if !ok {
return fmt.Errorf("failed to cast message to HotstuffMessage")
}
if err := m.handleHotstuffMessage(hotstuffMessage); err != nil {
return err
}
case UtilityMessage:
panic("[WARN] UtilityMessage handling is not implemented by consensus yet...")
default:
return typesCons.ErrUnknownConsensusMessageType(message.MessageName())
}
return nil
}
func (m *consensusModule) CurrentHeight() uint64 {
return m.Height
}
func (m *consensusModule) CurrentRound() uint64 {
return m.Round
}
func (m *consensusModule) CurrentStep() uint64 {
return uint64(m.Step)
}
func (m *consensusModule) ValidatorMap() modules.ValidatorMap { // TODO: This needs to be dynamically updated during various operations and network changes.
return typesCons.ValidatorMapToModulesValidatorMap(m.validatorMap)
}
// TODO(#256): Currently only used for testing purposes
func (m *consensusModule) SetUtilityContext(utilityContext modules.UtilityContext) {
m.utilityContext = utilityContext
}
// TODO: Populate the entire state from the persistence module: validator set, quorum cert, last block hash, etc...
func (m *consensusModule) loadPersistedState() error {
persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(-1) // Unknown height
if err != nil {
return nil
}
defer persistenceContext.Close()
latestHeight, err := persistenceContext.GetLatestBlockHeight()
if err != nil || latestHeight == 0 {
m.nodeLog("TODO: State sync not implemented yet")
return nil
}
m.Height = uint64(latestHeight) + 1 // +1 because the height of the consensus module is where it is actively participating in consensus
m.nodeLog(fmt.Sprintf("Starting node at height %d", latestHeight))
return nil
}
// HasPacemakerConfig is used to determine if a ConsensusConfig includes a PacemakerConfig without having to cast to the struct
// (which would break mocks and/or pollute the codebase with mock types casts and checks)
type HasPacemakerConfig interface {
GetPacemakerConfig() *typesCons.PacemakerConfig
}