Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: Validator eth secp keys #371

Closed
wants to merge 57 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5763877
shared/key: add arb_common_keypair testing strategy
tzemanovic May 6, 2022
275a636
shared/token: add arb_amount testing strategy
tzemanovic May 6, 2022
07a6821
wasm: add tx_bond tests
tzemanovic May 6, 2022
9ccbece
shared/token: add arb_amount_ceiled testing strategy
tzemanovic May 6, 2022
d1887fd
tests: expose native_vp test helpers
tzemanovic May 11, 2022
0b278a6
PoS: add PartialOrd, Ord, PartialEq and Eq for Epoched an EpochedDelta
tzemanovic May 11, 2022
ba9983e
tests: add re-usable PoS initialization helper
tzemanovic May 11, 2022
cfe2971
tests: make native pos vp module public
junkicide May 13, 2022
72aaf3a
tests: reuse init_pos for native pos vp test
junkicide May 13, 2022
5baeda5
PoS: fix type s/OffsetUnboundingLen/OffsetUnbondingLen
tzemanovic Jun 2, 2022
f9fd2d0
PoS: fix Bonds data type and application of unbonding on it
tzemanovic Jun 2, 2022
80f9191
wasm: tx_unbond tests
junkicide May 30, 2022
ef97a46
wasm: test tx_withdraw
tzemanovic Jun 8, 2022
553fc54
Changelog: add #462
tzemanovic Jun 8, 2022
65bdbfd
tests/pos: add proptest-regressions file
tzemanovic Jul 15, 2022
36e9c58
clean up documentation
brentstone Jul 14, 2022
4bc4b59
removed a deprecated function, spelling fixes
brentstone Jul 15, 2022
4fa9eff
quick doc fix
brentstone Jul 21, 2022
117959d
Update comments
brentstone Jul 21, 2022
4b43766
doc fixed in proof_of_stake/
brentstone Jul 21, 2022
c4a61c4
pos/vp: remove redundant validity predicate storage key check
tzemanovic Jul 29, 2022
74cce90
[ci skip] wasm checksums update
github-actions[bot] Aug 2, 2022
d5ccb62
test: add seed for failed PoS VP test
tzemanovic Aug 12, 2022
08ae9ca
pos/validation: refactor accumulation of changes
tzemanovic Jul 6, 2022
f716311
pos: fix bond zero amount error msg
tzemanovic Jul 6, 2022
2a53746
changelog: add #318
tzemanovic Aug 21, 2022
46cc2dc
Merge branch 'junkicide+tomas/pos-tx-tests' (#254)
tzemanovic Aug 21, 2022
f439b39
Merge branch 'tomas/refactor-pos-vp' (#318)
tzemanovic Aug 21, 2022
3978dff
pos: add validator eth cold and hot keys
tzemanovic Aug 21, 2022
de292a2
fixup! pos: add validator eth cold and hot keys
tzemanovic Aug 21, 2022
d76917b
shared: add eth keys for validators
tzemanovic Aug 21, 2022
a3dde58
client: add eth keys for validators
tzemanovic Aug 21, 2022
d681d7e
TODO: update genesis
tzemanovic Aug 21, 2022
b731b0e
config: add eth keys to genesis
brentstone Aug 21, 2022
5a8c895
client: add eth keys to pre_genesis
brentstone Aug 21, 2022
e179748
wallet: add eth keys to ValidatorWallet
brentstone Aug 21, 2022
4073a6f
proper hard-coded secp keys, remove outdated comments
brentstone Aug 24, 2022
75de2b2
shared: add conversion from secp256k1 pk to eth address
tzemanovic Sep 7, 2022
7fbeb28
WIP: add maps for eth address reverse lookup to native address
tzemanovic Sep 7, 2022
cd1f091
TODO: add eth hot key to validator keys in the wallet store
tzemanovic Sep 7, 2022
09b1aee
WIP: Merge eth hot and cold keys into single storage sub-key space
sug0 Sep 8, 2022
9a18ba1
add eth keys to gen_genesis_validator test
brentstone Sep 9, 2022
061cfc8
WIP: adding secp key as a ValidatorKeys field
sug0 Sep 9, 2022
cf92b69
WIP: add missing ValidatorKeys field
sug0 Sep 9, 2022
318e586
WIP: gen eth keys
sug0 Sep 9, 2022
72d5204
fixup! WIP: gen eth keys
tzemanovic Sep 9, 2022
9ef74bf
fixup! WIP: add maps for eth address reverse lookup to native address
tzemanovic Sep 9, 2022
e636402
write eth keys and addresses for genesis and on-chain created validators
tzemanovic Sep 9, 2022
75489df
fixup! WIP: gen eth keys
tzemanovic Sep 12, 2022
40311d0
fixup! WIP: gen eth keys
tzemanovic Sep 12, 2022
fdbcdc0
fix cli
tzemanovic Sep 12, 2022
e62a1c7
Fix wasm tests
sug0 Sep 12, 2022
86ef7a6
Add missing read_eth_key_addresses() to PosReadOnly
sug0 Sep 15, 2022
f263839
Remove unused eth key addresses pos methods
sug0 Sep 15, 2022
0ef4072
Fixed signature of PoS eth key read methods
sug0 Sep 16, 2022
7cd4852
Fix typo in secp key code
sug0 Sep 16, 2022
19bf28b
Load the right keys from ValidatorConfig
sug0 Sep 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! WIP: add maps for eth address reverse lookup to native address
  • Loading branch information
tzemanovic committed Sep 9, 2022
commit 9ef74bf9c2c248b1dbbd5a749f823a67c7dfaf2e
97 changes: 64 additions & 33 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod validation;

use core::fmt::Debug;
use std::collections::{BTreeSet, HashMap};
use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
use std::hash::Hash;
use std::num::TryFromIntError;
Expand Down Expand Up @@ -91,7 +91,6 @@ pub trait PosReadOnly {
/// Cryptographic public key type
type PublicKey: Debug
+ Clone
+ TryInto<EthAddress>
+ BorshDeserialize
+ BorshSerialize
+ BorshSchema;
Expand Down Expand Up @@ -274,7 +273,10 @@ pub trait PosActions: PosReadOnly {
eth_cold_key: &Self::PublicKey,
eth_hot_key: &Self::PublicKey,
current_epoch: impl Into<Epoch>,
) -> Result<(), BecomeValidatorError<Self::Address>> {
) -> Result<(), BecomeValidatorError<Self::Address>>
where
for<'a> &'a Self::PublicKey: TryInto<EthAddress>,
{
let current_epoch = current_epoch.into();
let params = self.read_pos_params();
let mut validator_set = self.read_validator_set();
Expand All @@ -290,22 +292,13 @@ pub trait PosActions: PosReadOnly {
),
);
}
let convert_key_to_addr = |k| {
k.try_into()
.map_err(|_| BecomeValidatorError::SecpKeyConversion)
};
let eth_addresses_map = self.read_eth_key_addresses();
let have_duped_key = eth_key_reverse_map
.contains_key(&convert_key_to_addr(eth_cold_key))
|| eth_key_reverse_map
.contains_key(&convert_key_to_addr(eth_hot_key));
if have_duped_key {
return Err(BecomeValidatorError::DupedEthKeyFound);
}
let BecomeValidatorData {
consensus_key,
eth_cold_key,
eth_hot_key,
eth_cold_key_addr,
eth_hot_key_addr,

state,
total_deltas,
voting_power,
Expand All @@ -317,14 +310,28 @@ pub trait PosActions: PosReadOnly {
eth_hot_key,
&mut validator_set,
current_epoch,
);
)?;
let mut eth_addresses_map = self.read_eth_key_addresses();
if eth_addresses_map
.insert(eth_cold_key_addr, address.clone())
.is_some()
{
return Err(BecomeValidatorError::DupedEthKeyFound);
}
if eth_addresses_map
.insert(eth_hot_key_addr, address.clone())
.is_some()
{
return Err(BecomeValidatorError::DupedEthKeyFound);
}
self.write_validator_staking_reward_address(
address,
staking_reward_address.clone(),
);
self.write_validator_consensus_key(address, consensus_key);
self.write_validator_eth_cold_key(address, eth_cold_key);
self.write_validator_eth_hot_key(address, eth_hot_key);
self.write_eth_key_addresses(eth_addresses_map);
self.write_validator_state(address, state);
self.write_validator_set(validator_set);
self.write_validator_address_raw_hash(address);
Expand Down Expand Up @@ -589,7 +596,6 @@ pub trait PosBase {
type PublicKey: 'static
+ Debug
+ Clone
+ TryInto<EthAddress>
+ BorshDeserialize
+ BorshSerialize
+ BorshSchema;
Expand Down Expand Up @@ -758,7 +764,10 @@ pub trait PosBase {
> + Clone
+ 'a,
current_epoch: impl Into<Epoch>,
) -> Result<(), GenesisError> {
) -> Result<(), GenesisError>
where
&'a Self::PublicKey: TryInto<EthAddress>,
{
let current_epoch = current_epoch.into();
self.write_pos_params(params);

Expand All @@ -769,7 +778,7 @@ pub trait PosBase {
total_bonded_balance,
} = init_genesis(params, validators, current_epoch)?;

let mut eth_key_reverse_map = HashMap::default();
let mut eth_addresses_map = HashMap::default();

for res in validators {
let GenesisValidatorData {
Expand All @@ -783,6 +792,8 @@ pub trait PosBase {
bond: (bond_id, bond),
eth_cold_key,
eth_hot_key,
eth_cold_key_addr,
eth_hot_key_addr,
} = res?;
self.write_validator_address_raw_hash(address);
self.write_validator_staking_reward_address(
Expand All @@ -800,22 +811,20 @@ pub trait PosBase {
&staking_reward_address,
&staking_reward_key,
);
let convert_key_to_addr =
|k| k.try_into().map_err(|_| GenesisError::SecpKeyConversion);
if eth_key_reverse_map
.insert(convert_key_to_addr(&eth_cold_key)?, address)
if eth_addresses_map
.insert(eth_cold_key_addr, address.clone())
.is_some()
{
return Err(GenesisError::DupedEthKeyFound);
}
if eth_key_reverse_map
.insert(convert_key_to_addr(&eth_hot_key)?, address)
if eth_addresses_map
.insert(eth_hot_key_addr, address.clone())
.is_some()
{
return Err(GenesisError::DupedEthKeyFound);
}
}
self.write_eth_key_addresses(eth_key_reverse_map);
self.write_eth_key_addresses(&eth_addresses_map);
self.write_validator_set(&validator_set);
self.write_total_voting_power(&total_voting_power);
// Credit the bonded tokens to the PoS account
Expand Down Expand Up @@ -1182,6 +1191,8 @@ where
bond: (BondId<Address>, Bonds<TokenAmount>),
eth_cold_key: ValidatorEthKey<PK>,
eth_hot_key: ValidatorEthKey<PK>,
eth_cold_key_addr: EthAddress,
eth_hot_key_addr: EthAddress,
}

/// A function that returns genesis data created from the initial validator set.
Expand Down Expand Up @@ -1236,6 +1247,7 @@ where
+ BorshSerialize
+ BorshSchema,
PK: 'a + Debug + Clone + BorshDeserialize + BorshSerialize + BorshSchema,
&'a PK: TryInto<EthAddress>,
{
// Accumulate the validator set and total voting power
let mut active: BTreeSet<WeightedValidator<Address>> = BTreeSet::default();
Expand Down Expand Up @@ -1284,6 +1296,11 @@ where
eth_cold_key,
eth_hot_key,
}| {
let convert_key_to_addr = |k: &'a PK| {
k.try_into().map_err(|_| GenesisError::SecpKeyConversion)
};
let eth_cold_key_addr = convert_key_to_addr(&eth_cold_key)?;
let eth_hot_key_addr = convert_key_to_addr(&eth_hot_key)?;
let consensus_key =
Epoched::init_at_genesis(consensus_key.clone(), current_epoch);
let eth_cold_key =
Expand Down Expand Up @@ -1326,6 +1343,8 @@ where
bond: (bond_id, bond),
eth_cold_key,
eth_hot_key,
eth_cold_key_addr,
eth_hot_key_addr,
})
},
);
Expand Down Expand Up @@ -1438,30 +1457,34 @@ where
consensus_key: ValidatorConsensusKeys<PK>,
eth_cold_key: ValidatorEthKey<PK>,
eth_hot_key: ValidatorEthKey<PK>,
eth_cold_key_addr: EthAddress,
eth_hot_key_addr: EthAddress,
state: ValidatorStates,
total_deltas: ValidatorTotalDeltas<TokenChange>,
voting_power: ValidatorVotingPowers,
}

/// A function that initialized data for a new validator.
fn become_validator<Address, PK, TokenChange>(
fn become_validator<'a, Address, PK, TokenChange>(
params: &PosParams,
address: &Address,
consensus_key: &PK,
eth_cold_key: &PK,
eth_hot_key: &PK,
eth_cold_key: &'a PK,
eth_hot_key: &'a PK,
validator_set: &mut ValidatorSets<Address>,
current_epoch: Epoch,
) -> BecomeValidatorData<PK, TokenChange>
) -> Result<BecomeValidatorData<PK, TokenChange>, BecomeValidatorError<Address>>
where
Address: Debug
Address: Display
+ Debug
+ Clone
+ Ord
+ Hash
+ BorshDeserialize
+ BorshSerialize
+ BorshSchema,
PK: Debug + Clone + BorshDeserialize + BorshSerialize + BorshSchema,
&'a PK: TryInto<EthAddress>,
TokenChange: Default
+ Debug
+ Clone
Expand All @@ -1471,6 +1494,12 @@ where
+ BorshSerialize
+ BorshSchema,
{
let convert_key_to_addr = |k: &'a PK| {
k.try_into()
.map_err(|_| BecomeValidatorError::SecpKeyConversion)
};
let eth_cold_key_addr = convert_key_to_addr(&eth_cold_key)?;
let eth_hot_key_addr = convert_key_to_addr(&eth_hot_key)?;
let consensus_key =
Epoched::init(consensus_key.clone(), current_epoch, params);
let eth_cold_key =
Expand Down Expand Up @@ -1512,14 +1541,16 @@ where
params,
);

BecomeValidatorData {
Ok(BecomeValidatorData {
consensus_key,
state,
total_deltas,
voting_power,
eth_cold_key,
eth_hot_key,
}
eth_cold_key_addr,
eth_hot_key_addr,
})
}

struct BondData<TokenAmount, TokenChange>
Expand Down