2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2021-06-21 08:35:24 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
# Mocking validator public and private keys
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
import
|
2020-07-07 15:51:02 +00:00
|
|
|
bearssl, eth/keys,
|
2020-08-15 17:33:58 +00:00
|
|
|
blscurve,
|
2021-06-21 08:35:24 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func newKeyPair(rng: var BrHmacDrbgContext): BlsResult[tuple[pub: ValidatorPubKey, priv: ValidatorPrivKey]] =
|
2020-07-07 15:51:02 +00:00
|
|
|
## Generates a new public-private keypair
|
|
|
|
## This requires entropy on the system
|
|
|
|
# The input-keying-material requires 32 bytes at least for security
|
|
|
|
# The generation is deterministic and the input-keying-material
|
|
|
|
# must be protected against side-channel attacks
|
|
|
|
|
|
|
|
var ikm: array[32, byte]
|
|
|
|
brHmacDrbgGenerate(rng, ikm)
|
2020-08-04 17:15:13 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
sk: SecretKey
|
2020-08-15 17:33:58 +00:00
|
|
|
pk: blscurve.PublicKey
|
2020-08-04 17:15:13 +00:00
|
|
|
if keyGen(ikm, pk, sk):
|
performance fixes (#2259)
* performance fixes
* don't mark tree cache as dirty on read-only List accesses
* store only blob in memory for keys and signatures, parse blob lazily
* compare public keys by blob instead of parsing / converting to raw
* compare Eth2Digest using non-constant-time comparison
* avoid some unnecessary validator copying
This branch will in particular speed up deposit processing which has
been slowing down block replay.
Pre (mainnet, 1600 blocks):
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3450.269, 0.000, 3450.269, 3450.269, 1, Initialize DB
0.417, 0.822, 0.036, 21.098, 1400, Load block from database
16.521, 0.000, 16.521, 16.521, 1, Load state from database
27.906, 50.846, 8.104, 1507.633, 1350, Apply block
52.617, 37.029, 20.640, 135.938, 50, Apply epoch block
```
Post:
```
3502.715, 0.000, 3502.715, 3502.715, 1, Initialize DB
0.080, 0.560, 0.035, 21.015, 1400, Load block from database
17.595, 0.000, 17.595, 17.595, 1, Load state from database
15.706, 11.028, 8.300, 107.537, 1350, Apply block
33.217, 12.622, 17.331, 60.580, 50, Apply epoch block
```
* more perf fixes
* load EpochRef cache into StateCache more aggressively
* point out security concern with public key cache
* reuse proposer index from state when processing block
* avoid genericAssign in a few more places
* don't parse key when signature is unparseable
* fix `==` overload for Eth2Digest
* preallocate validator list when getting active validators
* speed up proposer index calculation a little bit
* reuse cache when replaying blocks in ncli_db
* avoid a few more copying loops
```
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3279.158, 0.000, 3279.158, 3279.158, 1, Initialize DB
0.072, 0.357, 0.035, 13.400, 1400, Load block from database
17.295, 0.000, 17.295, 17.295, 1, Load state from database
5.918, 9.896, 0.198, 98.028, 1350, Apply block
15.888, 10.951, 7.902, 39.535, 50, Apply epoch block
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
* clear full balance cache before processing rewards and penalties
```
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
3947.901, 0.000, 3947.901, 3947.901, 1, Initialize DB
0.124, 0.506, 0.026, 202.370, 363345, Load block from database
97.614, 0.000, 97.614, 97.614, 1, Load state from database
0.186, 0.188, 0.012, 99.561, 357262, Advance slot, non-epoch
14.161, 5.966, 1.099, 395.511, 11524, Advance slot, epoch
1.372, 4.170, 0.017, 276.401, 363345, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database block store
```
2021-01-25 12:04:18 +00:00
|
|
|
ok((ValidatorPubKey(blob: pk.exportRaw()), ValidatorPrivKey(sk)))
|
2020-08-04 17:15:13 +00:00
|
|
|
else:
|
|
|
|
err "bls: cannot generate keypair"
|
2020-07-07 15:51:02 +00:00
|
|
|
|
2019-11-29 09:17:18 +00:00
|
|
|
# this is being indexed inside "mock_deposits.nim" by a value up to `validatorCount`
|
|
|
|
# which is `num_validators` which is `MIN_GENESIS_ACTIVE_VALIDATOR_COUNT`
|
2020-10-28 18:35:31 +00:00
|
|
|
proc genMockPrivKeys(privkeys: var openArray[ValidatorPrivKey]) =
|
2020-07-07 15:51:02 +00:00
|
|
|
let rng = newRng()
|
2020-03-11 13:50:08 +00:00
|
|
|
for i in 0 ..< privkeys.len:
|
2020-07-07 15:51:02 +00:00
|
|
|
let pair = newKeyPair(rng[])[]
|
2020-03-11 13:50:08 +00:00
|
|
|
privkeys[i] = pair.priv
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func genMockPubKeys(pubkeys: var openArray[ValidatorPubKey],
|
|
|
|
privkeys: openArray[ValidatorPrivKey]) =
|
2020-03-11 13:50:08 +00:00
|
|
|
for i in 0 ..< privkeys.len:
|
2021-06-01 11:13:40 +00:00
|
|
|
pubkeys[i] = toPubKey(privkeys[i]).toPubKey()
|
2020-03-11 13:50:08 +00:00
|
|
|
|
|
|
|
# Ref array necessary to limit stack usage / binary size
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
var MockPrivKeys* = newSeq[ValidatorPrivKey](defaultRuntimeConfig.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT)
|
2020-07-07 23:02:14 +00:00
|
|
|
genMockPrivKeys(MockPrivKeys)
|
2020-03-11 13:50:08 +00:00
|
|
|
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
var MockPubKeys* = newSeq[ValidatorPubKey](defaultRuntimeConfig.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT)
|
2020-07-07 23:02:14 +00:00
|
|
|
genMockPubKeys(MockPubKeys, MockPrivKeys)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
type MockKey = ValidatorPrivKey or ValidatorPubKey
|
|
|
|
|
|
|
|
template `[]`*[N: static int](a: array[N, MockKey], idx: ValidatorIndex): MockKey =
|
|
|
|
a[idx.int]
|
2020-03-11 13:50:08 +00:00
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
echo "========================================"
|
|
|
|
echo "Mock keys"
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
for i in 0 ..< defaultRuntimeConfig.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT:
|
2020-03-11 13:50:08 +00:00
|
|
|
echo " validator ", i
|
|
|
|
echo " seckey: ", MockPrivKeys[i].toHex()
|
|
|
|
echo " pubkey: ", MockPubKeys[i]
|