remove privkey from mock withdrawal credentials (#2936)

In tests, the private key was put into the validator deposit's withdraw
credentials so that it can be recovered later. This leads to problems
when creating the validators through other means that do not put the key
there. In general, mock private keys only depend on the validator index,
though, and because it is clear what the index of a validator is, it is
not actually needed to put the key into the credentials.
This commit is contained in:
Etan Kissling 2021-10-01 13:35:16 +02:00 committed by GitHub
parent 744c0d2def
commit f8e9b1ff9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 37 deletions

View File

@ -128,8 +128,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
sig =
get_attestation_signature(getStateField(stateData.data, fork),
getStateField(stateData.data, genesis_validators_root),
data, hackPrivKey(
getStateField(stateData.data, validators)[validatorIdx]))
data, MockPrivKeys[validatorIdx])
var aggregation_bits = CommitteeValidatorsBits.init(committee.len)
aggregation_bits.setBit index_in_committee
@ -165,7 +164,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
let
validatorIdx = validatorKeyToIndex[valKey]
validarorPrivKey = makeFakeValidatorPrivKey(validatorIdx)
validarorPrivKey = MockPrivKeys[validatorIdx.ValidatorIndex]
signature = blsSign(validarorPrivKey, signingRoot.data)
msg = SyncCommitteeMessage(
slot: slot,
@ -210,7 +209,8 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
signingRoot = contribution_and_proof_signing_root(
fork, genesisValidatorsRoot, contributionAndProof)
validarorPrivKey = makeFakeValidatorPrivKey(aggregator.validatorIdx)
validarorPrivKey =
MockPrivKeys[aggregator.validatorIdx.ValidatorIndex]
signedContributionAndProof = SignedContributionAndProof(
message: contributionAndProof,
@ -230,8 +230,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
finalizedEpochRef = dag.getFinalizedEpochRef()
proposerIdx = get_beacon_proposer_index(
stateData.data, cache, getStateField(stateData.data, slot)).get()
privKey = hackPrivKey(
getStateField(stateData.data, validators)[proposerIdx])
privKey = MockPrivKeys[proposerIdx]
eth1ProposalData = eth1Chain.getBlockProposalData(
stateData.data,
finalizedEpochRef.eth1_data,

View File

@ -218,7 +218,7 @@ suite "Gossip validation - Extra": # Not based on preset config
state[].data.validators.mapIt(it.pubkey).find(pubKey))
validator = AttachedValidator(
pubKey: pubkey,
kind: inProcess, privKey: hackPrivKey(state[].data.validators[index]),
kind: inProcess, privKey: MockPrivKeys[index],
index: some(index))
msg = waitFor signSyncCommitteeMessage(
validator, state[].data.slot,

View File

@ -47,10 +47,10 @@ suite "Sync committee pool":
fork = altairFork(defaultRuntimeConfig)
genesisValidatorsRoot = eth2digest(@[5.byte, 6, 7])
privkey1 = makeFakeValidatorPrivKey(1)
privkey2 = makeFakeValidatorPrivKey(2)
privkey3 = makeFakeValidatorPrivKey(3)
privkey4 = makeFakeValidatorPrivKey(4)
privkey1 = MockPrivKeys[1.ValidatorIndex]
privkey2 = MockPrivKeys[2.ValidatorIndex]
privkey3 = MockPrivKeys[3.ValidatorIndex]
privkey4 = MockPrivKeys[4.ValidatorIndex]
root1 = eth2digest(@[1.byte])
root2 = eth2digest(@[1.byte, 2])

View File

@ -10,7 +10,7 @@ import
options, stew/endians2,
../beacon_chain/validators/validator_pool,
../beacon_chain/spec/datatypes/merge,
../beacon_chain/spec/[helpers, signatures, state_transition, forks]
../beacon_chain/spec/[helpers, keystore, signatures, state_transition, forks]
type
MockPrivKeysT = object
@ -30,35 +30,22 @@ func `[]`*(_: MockPrivKeysT, index: ValidatorIndex): ValidatorPrivKey =
func `[]`*(_: MockPubKeysT, index: ValidatorIndex): ValidatorPubKey =
MockPrivKeys[index].toPubKey().toPubKey()
func makeFakeValidatorPrivKey*(i: int): ValidatorPrivKey =
MockPrivKeys[i.ValidatorIndex]
func makeFakeHash*(i: int): Eth2Digest =
var bytes = uint64(i).toBytesLE()
static: doAssert sizeof(bytes) <= sizeof(result.data)
copyMem(addr result.data[0], addr bytes[0], sizeof(bytes))
func hackPrivKey*(v: Validator): ValidatorPrivKey =
## Extract private key, per above hack
var bytes: array[8, byte]
static: doAssert sizeof(bytes) <= sizeof(v.withdrawal_credentials.data)
copyMem(
addr bytes, unsafeAddr v.withdrawal_credentials.data[0], sizeof(bytes))
let i = int(uint64.fromBytesLE(bytes))
makeFakeValidatorPrivKey(i)
func makeDeposit*(i: int, flags: UpdateFlags = {}, cfg = defaultRuntimeConfig): DepositData =
## Ugly hack for now: we stick the private key in withdrawal_credentials
## which means we can repro private key and randao reveal from this data,
## for testing :)
func makeDeposit*(
i: int,
flags: UpdateFlags = {},
cfg = defaultRuntimeConfig): DepositData =
let
privkey = makeFakeValidatorPrivKey(i)
pubkey = privkey.toPubKey()
withdrawal_credentials = makeFakeHash(i)
privkey = MockPrivKeys[i.ValidatorIndex]
pubkey = MockPubKeys[i.ValidatorIndex]
withdrawal_credentials = makeWithdrawalCredentials(pubkey)
result = DepositData(
pubkey: pubkey.toPubKey(),
pubkey: pubkey,
withdrawal_credentials: withdrawal_credentials,
amount: MAX_EFFECTIVE_BALANCE)
@ -104,7 +91,7 @@ proc addTestBlock*(
let
proposer_index = get_beacon_proposer_index(
state, cache, getStateField(state, slot))
privKey = hackPrivKey(getStateField(state, validators)[proposer_index.get])
privKey = MockPrivKeys[proposer_index.get]
randao_reveal =
if skipBlsValidation notin flags:
privKey.genRandaoReveal(
@ -221,7 +208,7 @@ func makeAttestation*(
get_attestation_signature(
getStateField(state, fork),
getStateField(state, genesis_validators_root),
data, hackPrivKey(validator)).toValidatorSig()
data, MockPrivKeys[validator_index]).toValidatorSig()
else:
ValidatorSig()
@ -280,7 +267,7 @@ func makeFullAttestations*(
agg.init(get_attestation_signature(
getStateField(state, fork),
getStateField(state, genesis_validators_root), data,
hackPrivKey(getStateField(state, validators)[committee[0]])))
MockPrivKeys[committee[0]]))
# Aggregate the remainder
attestation.aggregation_bits.setBit 0
@ -290,7 +277,7 @@ func makeFullAttestations*(
agg.aggregate(get_attestation_signature(
getStateField(state, fork),
getStateField(state, genesis_validators_root), data,
hackPrivKey(getStateField(state, validators)[committee[j]])
MockPrivKeys[committee[j]]
))
attestation.signature = agg.finish().toValidatorSig()