-
Notifications
You must be signed in to change notification settings - Fork 0
/
hookregister.go
102 lines (83 loc) · 2.7 KB
/
hookregister.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
package fork
import (
"github.com/coregenesis/coregenesis/consensus/ibft/hook"
)
// PoAHookRegisterer that registers hooks for PoA mode
type PoAHookRegister struct {
getValidatorsStore func(*IBFTFork) ValidatorStore
poaForks IBFTForks
updateValidatorsForks map[uint64]*IBFTFork
}
// NewPoAHookRegisterer is a constructor of PoAHookRegister
func NewPoAHookRegisterer(
getValidatorsStore func(*IBFTFork) ValidatorStore,
forks IBFTForks,
) *PoAHookRegister {
poaForks := forks.filterByType(PoA)
updateValidatorsForks := make(map[uint64]*IBFTFork)
for _, fork := range poaForks {
if fork.Validators == nil {
continue
}
updateValidatorsForks[fork.From.Value] = fork
}
return &PoAHookRegister{
getValidatorsStore: getValidatorsStore,
poaForks: poaForks,
updateValidatorsForks: updateValidatorsForks,
}
}
// RegisterHooks registers hooks of PoA for voting and validators updating
func (r *PoAHookRegister) RegisterHooks(hooks *hook.Hooks, height uint64) {
if currentFork := r.poaForks.getFork(height); currentFork != nil {
// in PoA mode currently
validatorStore := r.getValidatorsStore(currentFork)
registerHeaderModifierHooks(hooks, validatorStore)
}
// update validators in the end of the last block
if updateValidatorsFork, ok := r.updateValidatorsForks[height+1]; ok {
validatorStore := r.getValidatorsStore(updateValidatorsFork)
registerUpdateValidatorsHooks(
hooks,
validatorStore,
updateValidatorsFork.Validators,
updateValidatorsFork.From.Value,
)
}
}
// PoAHookRegisterer that registers hooks for PoS mode
type PoSHookRegister struct {
posForks IBFTForks
epochSize uint64
deployContractForks map[uint64]*IBFTFork
}
// NewPoSHookRegister is a constructor of PoSHookRegister
func NewPoSHookRegister(
forks IBFTForks,
epochSize uint64,
) *PoSHookRegister {
posForks := forks.filterByType(PoS)
deployContractForks := make(map[uint64]*IBFTFork)
for _, fork := range posForks {
if fork.Deployment == nil {
continue
}
deployContractForks[fork.Deployment.Value] = fork
}
return &PoSHookRegister{
posForks: posForks,
epochSize: epochSize,
deployContractForks: deployContractForks,
}
}
// RegisterHooks registers hooks of PoA for additional block verification and contract deployment
func (r *PoSHookRegister) RegisterHooks(hooks *hook.Hooks, height uint64) {
if currentFork := r.posForks.getFork(height); currentFork != nil {
// in PoS mode currently
registerTxInclusionGuardHooks(hooks, r.epochSize)
}
if deploymentFork, ok := r.deployContractForks[height]; ok {
// deploy or update staking contract in deployment height
registerStakingContractDeploymentHooks(hooks, deploymentFork)
}
}