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

Stake contract migration #1464

Merged
merged 18 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions consensus/src/user/provisioners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub struct Provisioners {
members: BTreeMap<PublicKey, Stake>,
}

impl Provisioners {
pub fn iter(&self) -> impl Iterator<Item = (&PublicKey, &Stake)> {
self.members.iter()
}
}

#[derive(Clone, Debug)]
pub struct ContextProvisioners {
current: Provisioners,
Expand Down
15 changes: 11 additions & 4 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use node_data::message::Payload;

use node_data::{Serializable, StepName};
use stake_contract_types::Unstake;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use std::time::Duration;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
Expand Down Expand Up @@ -101,6 +101,12 @@ impl ProvisionerChange {
}
}

pub static DUSK_KEY: LazyLock<PublicKey> = LazyLock::new(|| {
ureeves marked this conversation as resolved.
Show resolved Hide resolved
let dusk_cpk_bytes = include_bytes!("../../../rusk/src/assets/dusk.cpk");
PublicKey::try_from(*dusk_cpk_bytes)
.expect("Dusk consensus public key to be valid")
});

impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
/// Initializes a new `Acceptor` struct,
///
Expand Down Expand Up @@ -269,7 +275,8 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
.try_into()
.map_err(|e| anyhow::anyhow!("Cannot deserialize bytes {e:?}"))?;
let reward = ProvisionerChange::Reward(generator);
let mut changed_provisioners = vec![reward];
let dusk_reward = ProvisionerChange::Reward(DUSK_KEY.clone());
let mut changed_provisioners = vec![reward, dusk_reward];

// Update provisioners if a slash has been applied
for bytes in blk.header().failed_iterations.to_missed_generators_bytes()
Expand Down Expand Up @@ -463,9 +470,9 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
let vm = self.vm.write().await;
let txs = self.db.read().await.update(|t| {
let (txs, verification_output) = if blk.is_final() {
vm.finalize(blk.inner())?
vm.finalize(blk.inner(), provisioners_list.current())?
} else {
vm.accept(blk.inner())?
vm.accept(blk.inner(), provisioners_list.current())?
};

assert_eq!(header.state_hash, verification_output.state_root);
Expand Down
22 changes: 15 additions & 7 deletions node/src/chain/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,12 @@ impl<DB: database::DB, VM: vm::VMExecution> Operations for Executor<DB, VM> {

let vm = self.vm.read().await;

Ok(vm.verify_state_transition(blk).map_err(|err| {
error!("failed to call VST {}", err);
Error::Failed
})?)
Ok(vm
.verify_state_transition(blk, self.provisioners.current())
.map_err(|err| {
error!("failed to call VST {}", err);
Error::Failed
})?)
}

async fn execute_state_transition(
Expand All @@ -340,9 +342,15 @@ impl<DB: database::DB, VM: vm::VMExecution> Operations for Executor<DB, VM> {
let txs = view.get_txs_sorted_by_fee().map_err(|err| {
anyhow::anyhow!("failed to get mempool txs: {}", err)
})?;
let ret = vm.execute_state_transition(&params, txs).map_err(
|err| anyhow::anyhow!("failed to call EST {}", err),
)?;
let ret = vm
.execute_state_transition(
&params,
txs,
self.provisioners.current(),
)
.map_err(|err| {
anyhow::anyhow!("failed to call EST {}", err)
})?;
Ok(ret)
})
.map_err(|err: anyhow::Error| {
Expand Down
2 changes: 2 additions & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

#![feature(lazy_cell)]

pub mod chain;
pub mod database;
pub mod databroker;
Expand Down
4 changes: 4 additions & 0 deletions node/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub trait VMExecution: Send + Sync + 'static {
&self,
params: &CallParams,
txs: I,
provisioners: &Provisioners,
) -> anyhow::Result<(
Vec<SpentTransaction>,
Vec<Transaction>,
Expand All @@ -28,16 +29,19 @@ pub trait VMExecution: Send + Sync + 'static {
fn verify_state_transition(
&self,
blk: &Block,
provisioners: &Provisioners,
) -> anyhow::Result<VerificationOutput>;

fn accept(
&self,
blk: &Block,
provisioners: &Provisioners,
) -> anyhow::Result<(Vec<SpentTransaction>, VerificationOutput)>;

fn finalize(
&self,
blk: &Block,
provisioners: &Provisioners,
) -> anyhow::Result<(Vec<SpentTransaction>, VerificationOutput)>;

fn preverify(&self, tx: &Transaction) -> anyhow::Result<()>;
Expand Down
2 changes: 2 additions & 0 deletions rusk/benches/block_ingestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use rand::SeedableRng;
use tempfile::tempdir;

use common::state::new_state;
use dusk_consensus::user::provisioners::Provisioners;

fn load_txs() -> Vec<Transaction> {
const TXS_BYTES: &[u8] = include_bytes!("block");
Expand Down Expand Up @@ -97,6 +98,7 @@ pub fn accept_benchmark(c: &mut Criterion) {
txs,
None,
&[],
&Provisioners::empty(),
)
.expect("Accepting transactions should succeed");

Expand Down
Binary file added rusk/src/assets/stake_contract.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions rusk/src/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pub struct Args {
/// path to encrypted BLS keys
pub consensus_keys_path: Option<PathBuf>,

#[clap(long)]
/// height at which migration will be performed
pub migration_height: Option<u64>,
ureeves marked this conversation as resolved.
Show resolved Hide resolved

#[clap(long)]
/// Delay in milliseconds to mitigate UDP drops for DataBroker service in
/// localnet
Expand Down
10 changes: 10 additions & 0 deletions rusk/src/bin/config/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::args::Args;
pub(crate) struct ChainConfig {
db_path: Option<PathBuf>,
consensus_keys_path: Option<PathBuf>,
migration_height: Option<u64>,
}

impl ChainConfig {
Expand All @@ -27,6 +28,11 @@ impl ChainConfig {
if let Some(db_path) = args.db_path.clone() {
self.db_path = Some(db_path);
}

// Override config migration_height
if let Some(migration_height) = args.migration_height {
self.migration_height = Some(migration_height)
}
}

pub(crate) fn db_path(&self) -> PathBuf {
Expand All @@ -52,4 +58,8 @@ impl ChainConfig {
.display()
.to_string()
}

pub(crate) fn migration_height(&self) -> Option<u64> {
self.migration_height
}
}
2 changes: 1 addition & 1 deletion rusk/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (rusk, node, mut service_list) = {
let state_dir = rusk_profile::get_rusk_state_dir()?;
info!("Using state from {state_dir:?}");
let rusk = Rusk::new(state_dir)?;
let rusk = Rusk::new(state_dir, config.chain.migration_height())?;

info!("Rusk VM loaded");

Expand Down
1 change: 1 addition & 0 deletions rusk/src/lib/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct Rusk {
pub(crate) tip: Arc<RwLock<RuskTip>>,
pub(crate) vm: Arc<VM>,
dir: PathBuf,
migration_height: Option<u64>,
}

#[derive(Clone)]
Expand Down
Loading