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 = sig =
get_attestation_signature(getStateField(stateData.data, fork), get_attestation_signature(getStateField(stateData.data, fork),
getStateField(stateData.data, genesis_validators_root), getStateField(stateData.data, genesis_validators_root),
data, hackPrivKey( data, MockPrivKeys[validatorIdx])
getStateField(stateData.data, validators)[validatorIdx]))
var aggregation_bits = CommitteeValidatorsBits.init(committee.len) var aggregation_bits = CommitteeValidatorsBits.init(committee.len)
aggregation_bits.setBit index_in_committee aggregation_bits.setBit index_in_committee
@ -165,7 +164,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
let let
validatorIdx = validatorKeyToIndex[valKey] validatorIdx = validatorKeyToIndex[valKey]
validarorPrivKey = makeFakeValidatorPrivKey(validatorIdx) validarorPrivKey = MockPrivKeys[validatorIdx.ValidatorIndex]
signature = blsSign(validarorPrivKey, signingRoot.data) signature = blsSign(validarorPrivKey, signingRoot.data)
msg = SyncCommitteeMessage( msg = SyncCommitteeMessage(
slot: slot, slot: slot,
@ -210,7 +209,8 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
signingRoot = contribution_and_proof_signing_root( signingRoot = contribution_and_proof_signing_root(
fork, genesisValidatorsRoot, contributionAndProof) fork, genesisValidatorsRoot, contributionAndProof)
validarorPrivKey = makeFakeValidatorPrivKey(aggregator.validatorIdx) validarorPrivKey =
MockPrivKeys[aggregator.validatorIdx.ValidatorIndex]
signedContributionAndProof = SignedContributionAndProof( signedContributionAndProof = SignedContributionAndProof(
message: contributionAndProof, message: contributionAndProof,
@ -230,8 +230,7 @@ cli do(slots = SLOTS_PER_EPOCH * 6,
finalizedEpochRef = dag.getFinalizedEpochRef() finalizedEpochRef = dag.getFinalizedEpochRef()
proposerIdx = get_beacon_proposer_index( proposerIdx = get_beacon_proposer_index(
stateData.data, cache, getStateField(stateData.data, slot)).get() stateData.data, cache, getStateField(stateData.data, slot)).get()
privKey = hackPrivKey( privKey = MockPrivKeys[proposerIdx]
getStateField(stateData.data, validators)[proposerIdx])
eth1ProposalData = eth1Chain.getBlockProposalData( eth1ProposalData = eth1Chain.getBlockProposalData(
stateData.data, stateData.data,
finalizedEpochRef.eth1_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)) state[].data.validators.mapIt(it.pubkey).find(pubKey))
validator = AttachedValidator( validator = AttachedValidator(
pubKey: pubkey, pubKey: pubkey,
kind: inProcess, privKey: hackPrivKey(state[].data.validators[index]), kind: inProcess, privKey: MockPrivKeys[index],
index: some(index)) index: some(index))
msg = waitFor signSyncCommitteeMessage( msg = waitFor signSyncCommitteeMessage(
validator, state[].data.slot, validator, state[].data.slot,

View File

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

View File

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