/
default.go
331 lines (299 loc) · 11.7 KB
/
default.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package fixtures
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"time"
"github.com/spf13/viper"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/common/sgx"
"github.com/oasisprotocol/oasis-core/go/common/version"
consensusGenesis "github.com/oasisprotocol/oasis-core/go/consensus/genesis"
cmdCommon "github.com/oasisprotocol/oasis-core/go/oasis-node/cmd/common"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
storage "github.com/oasisprotocol/oasis-core/go/storage/api"
"github.com/oasisprotocol/oasis-core/go/storage/database"
mkvsAPI "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/badger"
)
const (
cfgDeterministicIdentities = "fixture.default.deterministic_entities"
cfgFundEntities = "fixture.default.fund_entities"
cfgEpochtimeMock = "fixture.default.epochtime_mock"
cfgHaltEpoch = "fixture.default.halt_epoch"
cfgKeymanagerBinary = "fixture.default.keymanager.binary"
cfgNodeBinary = "fixture.default.node.binary"
cfgNumEntities = "fixture.default.num_entities"
cfgRuntimeID = "fixture.default.runtime.id"
cfgRuntimeBinary = "fixture.default.runtime.binary"
cfgRuntimeVersion = "fixture.default.runtime.version"
cfgRuntimeStatePath = "fixture.default.runtime.state_path"
cfgRuntimeProvisioner = "fixture.default.runtime.provisioner"
cfgRuntimeLoader = "fixture.default.runtime.loader"
cfgSetupRuntimes = "fixture.default.setup_runtimes"
cfgTEEHardware = "fixture.default.tee_hardware"
cfgInitialHeight = "fixture.default.initial_height"
cfgStakingGenesis = "fixture.default.staking_genesis"
)
var keymanagerID common.Namespace
// newDefaultFixture returns a default network fixture.
func newDefaultFixture() (*oasis.NetworkFixture, error) {
var tee node.TEEHardware
err := tee.FromString(viper.GetString(cfgTEEHardware))
if err != nil {
return nil, err
}
var mrSigner *sgx.MrSigner
if tee == node.TEEHardwareIntelSGX {
mrSigner = &sgx.FortanixDummyMrSigner
}
// Default staking genesis enables 16 allowances per account which matches the current
// mainnet and testnet setting.
stakingGenesis := staking.Genesis{
Parameters: staking.ConsensusParameters{
MaxAllowances: 16,
},
}
if genesis := viper.GetString(cfgStakingGenesis); genesis != "" {
var raw []byte
raw, err = ioutil.ReadFile(genesis)
if err != nil {
return nil, fmt.Errorf("loading staking genesis file: %w", err)
}
if err = json.Unmarshal(raw, &stakingGenesis); err != nil {
return nil, fmt.Errorf("loading staking genesis: %w", err)
}
}
fixture := &oasis.NetworkFixture{
TEE: oasis.TEEFixture{
Hardware: tee,
MrSigner: mrSigner,
},
Network: oasis.NetworkCfg{
NodeBinary: viper.GetString(cfgNodeBinary),
RuntimeSGXLoaderBinary: viper.GetString(cfgRuntimeLoader),
Consensus: consensusGenesis.Genesis{
Parameters: consensusGenesis.Parameters{
TimeoutCommit: 1 * time.Second,
},
},
Beacon: beacon.ConsensusParameters{
Backend: beacon.BackendInsecure,
},
InitialHeight: viper.GetInt64(cfgInitialHeight),
HaltEpoch: viper.GetUint64(cfgHaltEpoch),
IAS: oasis.IASCfg{
Mock: true,
},
DeterministicIdentities: viper.GetBool(cfgDeterministicIdentities),
FundEntities: viper.GetBool(cfgFundEntities),
StakingGenesis: &stakingGenesis,
},
Entities: []oasis.EntityCfg{
{IsDebugTestEntity: true},
},
Validators: []oasis.ValidatorFixture{
{Entity: 1, Consensus: oasis.ConsensusFixture{SupplementarySanityInterval: 1}},
},
Seeds: []oasis.SeedFixture{{}},
}
if viper.GetBool(cfgEpochtimeMock) {
fixture.Network.SetMockEpoch()
}
for i := 0; i < viper.GetInt(cfgNumEntities); i++ {
fixture.Entities = append(fixture.Entities, oasis.EntityCfg{})
}
runtimeProvisioner := viper.GetString(cfgRuntimeProvisioner)
// Always run a client node.
fixture.Clients = []oasis.ClientFixture{{
RuntimeProvisioner: runtimeProvisioner,
}}
usingKeymanager := len(viper.GetString(cfgKeymanagerBinary)) > 0
if viper.GetBool(cfgSetupRuntimes) {
if usingKeymanager {
// Key manager runtime.
fixture.Runtimes = append(fixture.Runtimes, oasis.RuntimeFixture{
ID: keymanagerID,
Kind: registry.KindKeyManager,
Entity: 0,
Keymanager: -1,
AdmissionPolicy: registry.RuntimeAdmissionPolicy{
AnyNode: ®istry.AnyNodeRuntimeAdmissionPolicy{},
},
GovernanceModel: registry.GovernanceEntity,
Deployments: []oasis.DeploymentCfg{
{
Binaries: map[node.TEEHardware]string{
tee: viper.GetString(cfgKeymanagerBinary),
},
},
},
})
fixture.KeymanagerPolicies = []oasis.KeymanagerPolicyFixture{
{Runtime: 0, Serial: 1},
}
fixture.Keymanagers = []oasis.KeymanagerFixture{
{Runtime: 0, Entity: 1, RuntimeProvisioner: runtimeProvisioner},
}
}
fixture.ComputeWorkers = []oasis.ComputeWorkerFixture{
{Entity: 1, Runtimes: []int{}, RuntimeProvisioner: runtimeProvisioner, RuntimeStatePaths: make(map[int]string)},
{Entity: 1, Runtimes: []int{}, RuntimeProvisioner: runtimeProvisioner, RuntimeStatePaths: make(map[int]string)},
{Entity: 1, Runtimes: []int{}, RuntimeProvisioner: runtimeProvisioner, RuntimeStatePaths: make(map[int]string)},
}
var runtimeIDs []common.Namespace
for _, rtID := range viper.GetStringSlice(cfgRuntimeID) {
var rt common.Namespace
if err = rt.UnmarshalHex(rtID); err != nil {
cmdCommon.EarlyLogAndExit(fmt.Errorf("invalid runtime ID: %s: %w", rtID, err))
}
runtimeIDs = append(runtimeIDs, rt)
}
runtimes := viper.GetStringSlice(cfgRuntimeBinary)
if l1, l2 := len(runtimeIDs), len(runtimes); l1 < l2 {
cmdCommon.EarlyLogAndExit(fmt.Errorf("missing runtime IDs, required: %d, provided: %d", l1, l2))
}
// Runtime versions should be ignored or must be one-to-one mapped to runtimes.
runtimeVersions := viper.GetStringSlice(cfgRuntimeVersion)
if l1, l2 := len(runtimeIDs), len(runtimeVersions); l2 != 0 && l1 != l2 {
cmdCommon.EarlyLogAndExit(fmt.Errorf("runtime versions number mismatch, required: %d, provided: %d", l1, l2))
}
keymanagerIdx := -1
if usingKeymanager {
keymanagerIdx = 0
}
runtimeStatePaths := viper.GetStringSlice(cfgRuntimeStatePath)
if l1, l2 := len(runtimeStatePaths), len(runtimeIDs); l1 > 0 && l1 != l2 {
cmdCommon.EarlyLogAndExit(fmt.Errorf("missing runtime state paths: number of runtimes: %d, provided state paths: %d", l2, l1))
}
for i, rt := range runtimes {
// Compute runtime.
rtVersion := version.Version{}
if len(runtimeVersions) > i {
rtVersion, err = version.FromString(runtimeVersions[i])
if err != nil {
return nil, fmt.Errorf("parsing runtime version: %w", err)
}
}
fixture.Runtimes = append(fixture.Runtimes, oasis.RuntimeFixture{
ID: runtimeIDs[i],
Kind: registry.KindCompute,
Entity: 0,
Keymanager: keymanagerIdx,
Executor: registry.ExecutorParameters{
GroupSize: 2,
GroupBackupSize: 1,
RoundTimeout: 20,
MaxMessages: 128,
},
TxnScheduler: registry.TxnSchedulerParameters{
MaxBatchSize: 1000,
MaxBatchSizeBytes: 16 * 1024 * 1024, // 16 MiB
BatchFlushTimeout: 1 * time.Second,
ProposerTimeout: 20,
},
AdmissionPolicy: registry.RuntimeAdmissionPolicy{
AnyNode: ®istry.AnyNodeRuntimeAdmissionPolicy{},
},
GenesisRound: 0,
GovernanceModel: registry.GovernanceEntity,
Deployments: []oasis.DeploymentCfg{
{
Version: rtVersion,
ValidFrom: 0,
Binaries: map[node.TEEHardware]string{
tee: rt,
},
},
},
})
rtIndex := len(fixture.Runtimes) - 1
for j := range fixture.ComputeWorkers {
fixture.ComputeWorkers[j].Runtimes = append(fixture.ComputeWorkers[j].Runtimes, rtIndex)
}
fixture.Clients[0].Runtimes = append(fixture.Clients[0].Runtimes, rtIndex)
// Runtime state paths to use to initialize the runtime with.
if len(runtimeStatePaths) <= i {
continue
}
runtimeStatePath := runtimeStatePaths[i]
if runtimeStatePath == "" {
continue
}
// Set workers runtime state.
for j := range fixture.ComputeWorkers {
fixture.ComputeWorkers[j].RuntimeStatePaths[i] = runtimeStatePath
}
dbPath := filepath.Join(runtimeStatePath, database.DBFileBadgerDB)
_, err := os.Stat(dbPath)
if err != nil {
return nil, fmt.Errorf("runtime state path: %w", err)
}
db, err := badger.New(&mkvsAPI.Config{
DB: dbPath,
Namespace: runtimeIDs[i],
})
if err != nil {
return nil, fmt.Errorf("opening state path: %w", err)
}
version, stateRoot, err := getLatestVersionAndStateRoot(db)
if err != nil {
return nil, fmt.Errorf("loading version and state root: %w", err)
}
// Set runtime genesis state.
fixture.Runtimes[i].GenesisRound = version
fixture.Runtimes[i].GenesisStateRoot = stateRoot
}
}
return fixture, nil
}
// Loads latest runtime version and state root from the NodeDB.
func getLatestVersionAndStateRoot(db mkvsAPI.NodeDB) (uint64, *hash.Hash, error) {
// Get latest version.
dbVersion, exists := db.GetLatestVersion()
if !exists {
return 0, nil, fmt.Errorf("no version found in runtime state db: %v", dbVersion)
}
rts, err := db.GetRootsForVersion(context.Background(), dbVersion)
if err != nil {
return 0, nil, err
}
// Get latest state root.
var stateRootHash hash.Hash
for _, r := range rts {
if r.Type == storage.RootTypeState {
stateRootHash = r.Hash
}
}
return dbVersion, &stateRootHash, nil
}
func init() {
DefaultFixtureFlags.Bool(cfgDeterministicIdentities, false, "generate nodes with deterministic identities")
DefaultFixtureFlags.Bool(cfgFundEntities, false, "fund all entities in genesis")
DefaultFixtureFlags.Bool(cfgEpochtimeMock, false, "use mock epochtime")
DefaultFixtureFlags.Bool(cfgSetupRuntimes, true, "initialize the network with runtimes and runtime nodes")
DefaultFixtureFlags.Int(cfgNumEntities, 1, "number of (non debug) entities in genesis")
DefaultFixtureFlags.String(cfgKeymanagerBinary, "simple-keymanager", "path to the keymanager runtime")
DefaultFixtureFlags.String(cfgNodeBinary, "oasis-node", "path to the oasis-node binary")
DefaultFixtureFlags.StringSlice(cfgRuntimeID, []string{"8000000000000000000000000000000000000000000000000000000000000000"}, "runtime ID")
DefaultFixtureFlags.StringSlice(cfgRuntimeBinary, []string{"simple-keyvalue"}, "path to the runtime binary")
DefaultFixtureFlags.StringSlice(cfgRuntimeVersion, []string{"0.1.0"}, "runtime version to register")
DefaultFixtureFlags.StringSlice(cfgRuntimeStatePath, []string{""}, "runtime state path to initialize the runtime (and nodes) with")
DefaultFixtureFlags.String(cfgRuntimeProvisioner, "sandboxed", "the runtime provisioner: mock, unconfined, or sandboxed")
DefaultFixtureFlags.String(cfgRuntimeLoader, "oasis-core-runtime-loader", "path to the runtime loader")
DefaultFixtureFlags.String(cfgTEEHardware, "", "TEE hardware to use")
DefaultFixtureFlags.Uint64(cfgHaltEpoch, math.MaxUint64, "halt epoch height")
DefaultFixtureFlags.Int64(cfgInitialHeight, 1, "initial block height")
DefaultFixtureFlags.String(cfgStakingGenesis, "", "path to the staking genesis to use")
_ = viper.BindPFlags(DefaultFixtureFlags)
_ = keymanagerID.UnmarshalHex("c000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff")
}