Skip to content

Commit e63c899

Browse files
authored
Merge pull request #107 from heliaxdev/yuji/fix-vote-reactivate
Fix vote and reactivate validator
2 parents 08625f0 + fe01d2e commit e63c899

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

workload/src/executor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::step::{StepContext, StepType};
1212
use crate::task::{Task, TaskContext};
1313
use crate::types::{Alias, Epoch, Fee, Height};
1414
use crate::utils::{
15-
execute_reveal_pk, get_block_height, is_pk_revealed, retry_config, wait_block_settlement,
15+
execute_reveal_pk, get_block_height, get_proposals, is_pk_revealed, retry_config,
16+
wait_block_settlement,
1617
};
1718

1819
pub struct WorkloadExecutor {
@@ -242,6 +243,11 @@ impl WorkloadExecutor {
242243
.save()
243244
.map_err(|e| TaskError::Wallet(e.to_string()))?;
244245
}
246+
Task::DefaultProposal(_) | Task::Vote(_) => {
247+
let last_proposal_id = self.state.proposals.keys().max().cloned();
248+
let new_proposals = get_proposals(&self.sdk, last_proposal_id).await?;
249+
self.state.add_proposals(new_proposals);
250+
}
245251
_ => {}
246252
}
247253
}

workload/src/state.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
99
use thiserror::Error;
1010

1111
use crate::constants::{MIN_TRANSFER_BALANCE, PIPELINE_LEN};
12-
use crate::types::{Alias, Epoch};
12+
use crate::types::{Alias, Epoch, ProposalId};
1313

1414
#[derive(Error, Debug)]
1515
pub enum StateError {
@@ -544,14 +544,13 @@ impl State {
544544
.insert(alias.clone(), (account, epoch));
545545
}
546546

547-
pub fn remove_deactivate_validator(&mut self, alias: &Alias) {
548-
self.deactivated_validators.remove(alias).unwrap();
547+
pub fn reactivate_validator(&mut self, alias: &Alias) {
548+
let (account, _) = self.deactivated_validators.remove(alias).unwrap();
549+
self.validators.insert(alias.clone(), account);
549550
}
550551

551-
pub fn add_proposal(&mut self, start_epoch: u64, end_epoch: u64) {
552-
let latest_proposal_id = self.proposals.keys().max().unwrap_or(&0).to_owned();
553-
self.proposals
554-
.insert(latest_proposal_id, (start_epoch, end_epoch));
552+
pub fn add_proposals(&mut self, new_proposals: HashMap<ProposalId, (Epoch, Epoch)>) {
553+
self.proposals.extend(new_proposals);
555554
}
556555

557556
pub fn set_claimed_epoch(&mut self, source: &Alias, epoch: Epoch) {

workload/src/task/default_proposal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,6 @@ impl TaskContext for DefaultProposal {
112112

113113
fn update_state(&self, state: &mut State) {
114114
state.decrease_balance(&self.source, PROPOSAL_DEPOSIT);
115-
state.add_proposal(self.start_epoch, self.end_epoch);
115+
// proposal will be added later
116116
}
117117
}

workload/src/task/reactivate_validator.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ impl TaskContext for ReactivateValidator {
8686
}
8787

8888
fn update_state(&self, state: &mut State) {
89-
state.remove_deactivate_validator(&self.target);
90-
state.set_established_as_validator(&self.target);
89+
state.reactivate_validator(&self.target);
9190
}
9291
}

workload/src/utils/query.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::str::FromStr;
23
use std::time::{self, Instant};
34

@@ -539,6 +540,27 @@ async fn shielded_sync(
539540
res
540541
}
541542

543+
pub async fn get_proposals(
544+
sdk: &Sdk,
545+
last_proposal_id: Option<ProposalId>,
546+
) -> Result<HashMap<ProposalId, (Epoch, Epoch)>, QueryError> {
547+
let mut proposals = HashMap::new();
548+
let mut proposal_id = last_proposal_id.map(|id| id + 1).unwrap_or_default();
549+
while let Some(proposal) = rpc::query_proposal_by_id(&sdk.namada.client, proposal_id)
550+
.await
551+
.map_err(QueryError::Rpc)?
552+
{
553+
proposals.insert(
554+
proposal_id,
555+
(proposal.voting_start_epoch.0, proposal.voting_end_epoch.0),
556+
);
557+
558+
proposal_id += 1;
559+
}
560+
561+
Ok(proposals)
562+
}
563+
542564
pub async fn get_vote_results(
543565
sdk: &Sdk,
544566
target: &Alias,

0 commit comments

Comments
 (0)