Skip to content

Commit

Permalink
Support validator score parameters sequence number.
Browse files Browse the repository at this point in the history
  • Loading branch information
td202 committed Jan 16, 2025
1 parent 61678e6 commit e949ffa
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
suspended.
- New `UpdatePayload` type `ValidatorScoreParametersCPV3`, which updates the maximum number of
consecutive failures a validator can have before it faces suspension.
- `NextUpdateSequenceNumbers`: add `validator_score_parameters`.
- `ContractInitializedEvent` adds the `parameter` used to initialize the contract (supported from
node version >= 8).
- New functionality for querying which accounts have scheduled releases or cooldowns (supported
Expand Down
123 changes: 123 additions & 0 deletions examples/update-validator-score-param.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//! An example showing how to do a simple update of the validator score
//! parameters.
use anyhow::Context;
use clap::AppSettings;
use concordium_base::updates::ValidatorScoreParameters;
use concordium_rust_sdk::{
common::types::TransactionTime,
types::{
transactions::{update, BlockItem, Payload},
TransactionStatus, UpdateKeyPair, UpdatePayload,
},
v2::{self, BlockIdentifier, ChainParameters},
};
use std::path::PathBuf;
use structopt::StructOpt;
use tokio_stream::StreamExt;

#[derive(StructOpt)]
struct App {
#[structopt(
long = "node",
help = "GRPC interface of the node.",
default_value = "http://localhost:20000"
)]
endpoint: v2::Endpoint,
#[structopt(long = "key", help = "Path to update keys to use.")]
keys: Vec<PathBuf>,
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> anyhow::Result<()> {
let app = {
let app = App::clap().global_setting(AppSettings::ColoredHelp);
let matches = app.get_matches();
App::from_clap(&matches)
};

let kps: Vec<UpdateKeyPair> = app
.keys
.iter()
.map(|p| {
serde_json::from_reader(std::fs::File::open(p).context("Could not open file.")?)
.context("Could not read keys from file.")
})
.collect::<anyhow::Result<_>>()?;

let mut client = v2::Client::new(app.endpoint).await?;

// Get the key indices, as well as the next sequence number from the last
// finalized block.
let summary: ChainParameters = client
.get_block_chain_parameters(BlockIdentifier::LastFinal)
.await
.context("Could not obtain last finalized block's chain parameters")?
.response;

// find the key indices to sign with
let signer = summary
.common_update_keys()
.construct_update_signer(&summary.common_update_keys().micro_gtu_per_euro, kps)
.context("Invalid keys supplied.")?;

let seq_number = client
.get_next_update_sequence_numbers(BlockIdentifier::LastFinal)
.await?
.response;
let seq_number = seq_number.validator_score_parameters;

let now = chrono::offset::Utc::now().timestamp() as u64;
let effective_time = TransactionTime::from_seconds(now + 300); // effective in 5min
let timeout = TransactionTime::from_seconds(now + 60); // 1min expiry.
let payload = UpdatePayload::ValidatorScoreParametersCPV3(ValidatorScoreParameters {
max_missed_rounds: 10,
});
let block_item: BlockItem<Payload> =
update::update(&signer, seq_number, effective_time, timeout, payload).into();

let submission_id = client
.send_block_item(&block_item)
.await
.context("Could not send the update instruction.")?;

println!("Submitted update with hash {}", submission_id);

// wait until it's finalized.
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));
loop {
interval.tick().await;
match client
.get_block_item_status(&submission_id)
.await
.context("Could not query submission status.")?
{
TransactionStatus::Finalized(blocks) => {
println!(
"Submission is finalized in blocks {:?}",
blocks.keys().collect::<Vec<_>>()
);
break;
}
TransactionStatus::Committed(blocks) => {
println!(
"Submission is committed to blocks {:?}",
blocks.keys().collect::<Vec<_>>()
);
}
TransactionStatus::Received => {
println!("Submission is received.")
}
}
}
let mut pending_updates = client
.get_block_pending_updates(BlockIdentifier::LastFinal)
.await?
.response;
while let Some(update) = pending_updates.next().await.transpose()? {
// Display the update with the serde JSON serialization.
let update = serde_json::to_string_pretty(&update)?;
println!("Pending update: {}", update);
}

Ok(())
}
3 changes: 3 additions & 0 deletions src/types/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,7 @@ pub struct NextUpdateSequenceNumbers {
pub block_energy_limit: UpdateSequenceNumber,
/// Updates to the consensus version 2 finalization committee parameters
pub finalization_committee_parameters: UpdateSequenceNumber,
/// Updates to the validator score parameters for chain parameters version 3
/// onwards.
pub validator_score_parameters: UpdateSequenceNumber,
}
4 changes: 4 additions & 0 deletions src/v2/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3332,6 +3332,10 @@ impl TryFrom<NextUpdateSequenceNumbers> for super::types::queries::NextUpdateSeq
.finalization_committee_parameters
.require()?
.into(),
validator_score_parameters: message
.validator_score_parameters
.map(Into::into)
.unwrap_or_default(),
})
}
}
Expand Down

0 comments on commit e949ffa

Please sign in to comment.