-
Notifications
You must be signed in to change notification settings - Fork 110
/
genesis.go
183 lines (163 loc) · 5.88 KB
/
genesis.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
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
package scheduler
import (
"context"
"encoding/hex"
"fmt"
"github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/node"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
schedulerState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/scheduler/state"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
genesis "github.com/oasisprotocol/oasis-core/go/genesis/api"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)
func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.RequestInitChain, doc *genesis.Document) error {
var err error
state := schedulerState.NewMutableState(ctx.State())
if err = state.SetConsensusParameters(ctx, &doc.Scheduler.Parameters); err != nil {
return fmt.Errorf("failed to set consensus parameters: %w", err)
}
if doc.Scheduler.Parameters.MinValidators <= 0 {
return fmt.Errorf("tendermint/scheduler: minimum number of validators not configured")
}
if doc.Scheduler.Parameters.MaxValidators <= 0 {
return fmt.Errorf("tendermint/scheduler: maximum number of validators not configured")
}
if doc.Scheduler.Parameters.MaxValidatorsPerEntity <= 0 {
return fmt.Errorf("tendermint/scheduler: maximum number of validators per entity not configured")
}
if doc.Scheduler.Parameters.MaxValidatorsPerEntity > 1 {
// This should only ever be true for test deployments.
ctx.Logger().Warn("maximum number of validators is non-standard, fairness not guaranteed",
"max_valiators_per_entity", doc.Scheduler.Parameters.MaxValidatorsPerEntity,
)
}
regState := registryState.NewMutableState(ctx.State())
nodes, err := regState.Nodes(ctx)
if err != nil {
return fmt.Errorf("tendermint/scheduler: couldn't get nodes: %w", err)
}
registeredValidators := make(map[signature.PublicKey]*node.Node)
for _, v := range nodes {
if v.HasRoles(node.RoleValidator) {
registeredValidators[v.Consensus.ID] = v
}
}
stakeState := stakingState.NewMutableState(ctx.State())
// Assemble the list of the tendermint genesis validators, and do some
// sanity checking.
currentValidators := make(map[signature.PublicKey]int64)
for _, v := range req.Validators {
tmPk := v.GetPubKey()
pk := tmPk.GetEd25519()
if pk == nil {
ctx.Logger().Error("invalid genesis validator public key type",
"type", v.GetPubKey(),
)
return fmt.Errorf("scheduler: invalid genesis validator public key type: '%v'", v.GetPubKey())
}
var id signature.PublicKey
if err = id.UnmarshalBinary(pk); err != nil {
ctx.Logger().Error("invalid genesis validator public key",
"err", err,
"public_key", hex.EncodeToString(pk),
)
return fmt.Errorf("scheduler: invalid genesis validator public key: %w", err)
}
n := registeredValidators[id]
if n == nil {
ctx.Logger().Error("genesis validator not in registry",
"id", id,
)
return fmt.Errorf("scheduler: genesis validator not in registry")
}
var expectedPower int64
if doc.Scheduler.Parameters.DebugBypassStake {
expectedPower = 1
} else {
var account *staking.Account
acctAddr := staking.NewAddress(n.EntityID)
account, err = stakeState.Account(ctx, acctAddr)
if err != nil {
ctx.Logger().Error("couldn't get account for genesis validator entity",
"err", err,
"node", n.ID,
"entity", n.EntityID,
"accont", acctAddr,
)
return fmt.Errorf("scheduler: getting account %s for genesis validator %s of entity %s: %w",
acctAddr,
n.ID,
n.EntityID,
err,
)
}
expectedPower, err = scheduler.VotingPowerFromStake(&account.Escrow.Active.Balance)
if err != nil {
ctx.Logger().Error("computing voting power from stake failed",
"err", err,
"node", n.ID,
"entity", n.EntityID,
"account", acctAddr,
"stake", &account.Escrow.Active.Balance,
)
return fmt.Errorf(
"scheduler: computing voting power from stake (node %s entity %s account %s stake %v): %w",
n.ID,
n.EntityID,
acctAddr,
&account.Escrow.Active.Balance,
err,
)
}
}
if v.Power != expectedPower {
ctx.Logger().Error("validator power is wrong",
"node_id", n.ID,
"expected_power", expectedPower,
"validator_power", v.Power,
)
return fmt.Errorf("scheduler: genesis validator node %s has wrong power %d, expected %d",
n.ID,
v.Power,
expectedPower,
)
}
ctx.Logger().Debug("adding validator to current validator set",
"id", id,
)
currentValidators[n.Consensus.ID] = v.Power
}
// TODO/security: Enforce genesis validator staking thresholds.
// Add the current validator set to ABCI, so that we can alter it later.
//
// Sort of stupid it needs to be done this way, but tendermint doesn't
// appear to pass ABCI the validator set anywhere other than InitChain.
if err = state.PutCurrentValidators(ctx, currentValidators); err != nil {
return fmt.Errorf("failed to set validator set: %w", err)
}
if !doc.Scheduler.Parameters.DebugBypassStake {
supplyPower, err := scheduler.VotingPowerFromStake(&doc.Staking.TotalSupply)
if err != nil {
return fmt.Errorf("init chain: total supply would break voting power computation: %w", err)
}
if supplyPower > tmtypes.MaxTotalVotingPower {
return fmt.Errorf("init chain: total supply power %d exceeds Tendermint voting power limit %d", supplyPower, tmtypes.MaxTotalVotingPower)
}
}
return nil
}
func (sq *schedulerQuerier) Genesis(ctx context.Context) (*scheduler.Genesis, error) {
params, err := sq.state.ConsensusParameters(ctx)
if err != nil {
return nil, err
}
genesis := &scheduler.Genesis{
Parameters: *params,
}
return genesis, nil
}