Skip to content

Commit

Permalink
Merge branch 'brent/delegation-validators' (#3043)
Browse files Browse the repository at this point in the history
* origin/brent/delegation-validators:
  better comment
  use unslashed bond amounts in `find_delegations`
  fix from comments
  changelog: add #3043
  prune old `prev_ranges` data
  fix edge case (fully unbond then rebond)
  fixes from review comments
  fix and finish test
  new approach
  don't merklize delegation targets
  improve nomenclature and comments
  rename and fix rpc fns to get delegator's target validators
  track all validators to which an account has bonded tokens
  • Loading branch information
brentstone committed Apr 26, 2024
2 parents d6b85a1 + 4336b33 commit 9ee11a9
Show file tree
Hide file tree
Showing 27 changed files with 880 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Optimize the finding of validators to which a delegator has bonds at a
given epoch. Now keeps data in storage rather than iterating over all bonds.
([\#3043](https://github.com/anoma/namada/pull/3043))
21 changes: 14 additions & 7 deletions crates/apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2412,13 +2412,19 @@ pub async fn query_delegations<N: Namada>(
let delegations: HashSet<Address> = unwrap_client_response::<N::Client, _>(
RPC.vp()
.pos()
.delegation_validators(context.client(), &owner)
.delegation_validators(context.client(), &owner, &None)
.await,
);
if delegations.is_empty() {
display_line!(context.io(), "No delegations found");
display_line!(
context.io(),
"No delegations found active in the current epoch"
);
} else {
display_line!(context.io(), "Found delegations to:");
display_line!(
context.io(),
"Found delegations in the current epoch to:"
);
for delegation in delegations {
display_line!(context.io(), " {delegation}");
}
Expand Down Expand Up @@ -2823,25 +2829,26 @@ async fn get_validator_stake<C: namada::ledger::queries::Client + Sync>(
)
}

pub async fn get_delegators_delegation<
pub async fn get_delegation_validators<
C: namada::ledger::queries::Client + Sync,
>(
client: &C,
address: &Address,
) -> HashSet<Address> {
namada_sdk::rpc::get_delegators_delegation(client, address)
let epoch = namada_sdk::rpc::query_epoch(client).await.unwrap();
namada_sdk::rpc::get_delegation_validators(client, address, epoch)
.await
.unwrap()
}

pub async fn get_delegators_delegation_at<
pub async fn get_delegations_of_delegator_at<
C: namada::ledger::queries::Client + Sync,
>(
client: &C,
address: &Address,
epoch: Epoch,
) -> HashMap<Address, token::Amount> {
namada_sdk::rpc::get_delegators_delegation_at(client, address, epoch)
namada_sdk::rpc::get_delegations_of_delegator_at(client, address, epoch)
.await
.unwrap()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ mod test_finalize_block {
id: proposal_id,
vote,
voter: validator,
delegations: vec![],
delegation_validators: vec![],
};
// Vote to accept the proposal (there's only one validator, so its
// vote decides)
Expand Down
3 changes: 2 additions & 1 deletion crates/apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ pub fn is_merklized_storage_key(key: &namada_sdk::storage::Key) -> bool {
&& *key != token::storage_key::masp_convert_anchor_key()
&& *key != token::storage_key::masp_token_map_key()
&& *key != token::storage_key::masp_assets_hash_key()
|| namada::ibc::storage::is_ibc_counter_key(key))
|| namada::ibc::storage::is_ibc_counter_key(key)
|| namada::proof_of_stake::storage_key::is_delegation_targets_key(key))
}

/// Channels for communicating with an Ethereum oracle.
Expand Down
6 changes: 4 additions & 2 deletions crates/benches/native_vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ fn governance(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
delegation_validators: vec![
defaults::validator_address(),
],
},
None,
None,
Expand All @@ -105,7 +107,7 @@ fn governance(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Nay,
voter: defaults::validator_address(),
delegations: vec![],
delegation_validators: vec![],
},
None,
None,
Expand Down
4 changes: 2 additions & 2 deletions crates/benches/txs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ fn vote_proposal(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Yay,
voter: defaults::albert_address(),
delegations: vec![defaults::validator_address()],
delegation_validators: vec![defaults::validator_address()],
},
None,
None,
Expand All @@ -560,7 +560,7 @@ fn vote_proposal(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Nay,
voter: defaults::validator_address(),
delegations: vec![],
delegation_validators: vec![],
},
None,
None,
Expand Down
7 changes: 4 additions & 3 deletions crates/benches/vps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ fn vp_implicit(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Yay,
voter: Address::from(&implicit_account.to_public()),
delegations: vec![], /* NOTE: no need to bond tokens because the
* implicit vp doesn't check that */
delegation_validators: vec![], /* NOTE: no need to bond tokens
* because the
* implicit vp doesn't check that */
},
None,
None,
Expand Down Expand Up @@ -251,7 +252,7 @@ fn vp_user(c: &mut Criterion) {
id: 0,
vote: ProposalVote::Yay,
voter: defaults::validator_address(),
delegations: vec![],
delegation_validators: vec![],
},
None,
None,
Expand Down
4 changes: 2 additions & 2 deletions crates/governance/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ pub fn get_proposal_vote_prefix_key(id: u64) -> Key {
pub fn get_vote_proposal_key(
id: u64,
voter_address: Address,
delegation_address: Address,
validator_address: Address,
) -> Key {
get_proposal_vote_prefix_key(id)
.push(&delegation_address)
.push(&validator_address)
.expect("Cannot obtain a storage key")
.push(&voter_address)
.expect("Cannot obtain a storage key")
Expand Down
4 changes: 2 additions & 2 deletions crates/governance/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ pub fn vote_proposal<S>(storage: &mut S, data: VoteProposalData) -> Result<()>
where
S: StorageRead + StorageWrite,
{
for delegation in data.delegations {
for validator in data.delegation_validators {
let vote_key = governance_keys::get_vote_proposal_key(
data.id,
data.voter.clone(),
delegation,
validator,
);
storage.write(&vote_key, data.vote.clone())?;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/governance/src/storage/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct VoteProposalData {
/// The proposal voter address
pub voter: Address,
/// Validators to who the voter has delegations to
pub delegations: Vec<Address>,
pub delegation_validators: Vec<Address>,
}

impl TryFrom<DefaultProposal> for InitProposalData {
Expand Down Expand Up @@ -770,13 +770,13 @@ pub mod testing {
id: u64,
vote in arb_proposal_vote(),
voter in arb_non_internal_address(),
delegations in collection::vec(arb_non_internal_address(), 0..10),
delegation_validators in collection::vec(arb_non_internal_address(), 0..10),
) -> VoteProposalData {
VoteProposalData {
id,
vote,
voter,
delegations,
delegation_validators,
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/light_sdk/src/reading/asynchronous/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub async fn get_validator_state(
}

/// Get the delegator's delegation
pub async fn get_delegators_delegation(
pub async fn get_delegation_validators(
tendermint_addr: &str,
address: &Address,
) -> Result<HashSet<Address>, Error> {
Expand All @@ -163,11 +163,12 @@ pub async fn get_delegators_delegation(
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_delegators_delegation(&client, address).await
let epoch = rpc::query_epoch(&client).await?;
rpc::get_delegation_validators(&client, address, epoch).await
}

/// Get the delegator's delegation at some epoh
pub async fn get_delegators_delegation_at(
pub async fn get_delegations_of_delegator_at(
tendermint_addr: &str,
address: &Address,
epoch: Epoch,
Expand All @@ -177,7 +178,7 @@ pub async fn get_delegators_delegation_at(
.map_err(|e| Error::Other(e.to_string()))?,
)
.map_err(|e| Error::Other(e.to_string()))?;
rpc::get_delegators_delegation_at(&client, address, epoch).await
rpc::get_delegations_of_delegator_at(&client, address, epoch).await
}

/// Query and return validator's commission rate and max commission rate
Expand Down
11 changes: 7 additions & 4 deletions crates/light_sdk/src/reading/blocking/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn get_validator_state(
}

/// Get the delegator's delegation
pub fn get_delegators_delegation(
pub fn get_delegation_validators(
tendermint_addr: &str,
address: &Address,
) -> Result<HashSet<Address>, Error> {
Expand All @@ -175,11 +175,12 @@ pub fn get_delegators_delegation(
)
.map_err(|e| Error::Other(e.to_string()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::get_delegators_delegation(&client, address))
let epoch = rpc::query_epoch(&client).await?;
rt.block_on(rpc::get_delegation_validators(&client, address, epoch))
}

/// Get the delegator's delegation at some epoh
pub fn get_delegators_delegation_at(
pub fn get_delegations_of_delegator_at(
tendermint_addr: &str,
address: &Address,
epoch: Epoch,
Expand All @@ -190,7 +191,9 @@ pub fn get_delegators_delegation_at(
)
.map_err(|e| Error::Other(e.to_string()))?;
let rt = Runtime::new().unwrap();
rt.block_on(rpc::get_delegators_delegation_at(&client, address, epoch))
rt.block_on(rpc::get_delegations_of_delegator_at(
&client, address, epoch,
))
}

/// Query and return validator's commission rate and max commission rate
Expand Down
2 changes: 1 addition & 1 deletion crates/light_sdk/src/transaction/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl VoteProposal {
id,
vote,
voter,
delegations,
delegation_validators: delegations,
};

Self(transaction::build_tx(
Expand Down
Loading

0 comments on commit 9ee11a9

Please sign in to comment.