Skip to content

Commit

Permalink
Merge pull request #3382 from dusk-network/recovery-embed-contracts
Browse files Browse the repository at this point in the history
rusk-recovery: embed mainnet contracts
  • Loading branch information
herr-seppia authored Jan 18, 2025
2 parents 7e84c13 + 9d8bb55 commit 527e759
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 41 deletions.
8 changes: 3 additions & 5 deletions rusk-recovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ rusk-profile = { workspace = true }

# stake and keys feature dependency
dusk-core = { workspace = true, features = ["zk", "std"], optional = true }
once_cell = { workspace = true, optional = true }
tracing = { workspace = true, features = ["log"], optional = true }

# state feature dependencies
Expand All @@ -37,14 +36,15 @@ zip = { workspace = true, optional = true }
# keys feature dependencies
cargo_toml = { workspace = true, optional = true }
dusk-plonk = { workspace = true, features = ["rkyv-impl"], optional = true }
lazy_static = { workspace = true, optional = true }
reqwest = { workspace = true, optional = true }
tokio = { workspace = true, features = ["full"], optional = true }

[dev-dependencies]
tempdir = { workspace = true }

[features]
state = [
"dusk-core",
"once_cell",
"tracing",
"serde_derive",
"serde",
Expand All @@ -63,11 +63,9 @@ state = [
]
keys = [
"dusk-core",
"once_cell",
"tracing",
"cargo_toml",
"dusk-plonk",
"lazy_static",
"reqwest",
"tokio",
]
Binary file added rusk-recovery/assets/stake_contract.wasm
Binary file not shown.
Binary file added rusk-recovery/assets/transfer_contract.wasm
Binary file not shown.
15 changes: 6 additions & 9 deletions rusk-recovery/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::sync::{mpsc, Mutex};
use std::sync::{mpsc, LazyLock, Mutex};
use std::{io, thread};

use dusk_core::transfer::phoenix::TRANSCRIPT_LABEL;
use dusk_plonk::prelude::{Compiler, PublicParameters};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use rusk_profile::Circuit as CircuitProfile;
use tracing::{info, warn};

use crate::Theme;

mod circuits;

lazy_static! {
static ref CRS_URL: Mutex<String> = Mutex::new(String::default());
}
static CRS_URL: LazyLock<Mutex<String>> =
LazyLock::new(|| Mutex::new(String::new()));

static PUB_PARAMS: Lazy<PublicParameters> = Lazy::new(|| {
static PUB_PARAMS: LazyLock<PublicParameters> = LazyLock::new(|| {
let theme = Theme::default();
info!("{} CRS from cache", theme.action("Fetching"));
match rusk_profile::get_common_reference_string() {
Expand Down Expand Up @@ -148,7 +145,7 @@ pub fn exec(

// This force init is needed to check CRS and create it (if not available)
// See also: https://github.com/dusk-network/rusk/issues/767
Lazy::force(&PUB_PARAMS);
LazyLock::force(&PUB_PARAMS);

// cache all circuit descriptions, check if they changed
circuits::cache_all()?;
Expand All @@ -174,6 +171,6 @@ mod tests {
use super::*;
#[test]
fn test_crs() {
Lazy::force(&PUB_PARAMS);
LazyLock::force(&PUB_PARAMS);
}
}
1 change: 1 addition & 0 deletions rusk-recovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#![deny(unused_crate_dependencies)]
#![deny(unused_extern_crates)]
#![feature(lazy_cell)]

#[cfg(feature = "keys")]
pub mod keys;
Expand Down
63 changes: 40 additions & 23 deletions rusk-recovery/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ use std::error::Error;
use std::fs;
use std::path::Path;

use dusk_bytes::DeserializableSlice;
use dusk_core::abi::ContractId;
use dusk_core::signatures::bls::PublicKey as AccountPublicKey;
use dusk_core::stake::{StakeAmount, StakeData, StakeKeys, STAKE_CONTRACT};
use dusk_core::transfer::phoenix::{Note, PublicKey, Sender};
use dusk_core::transfer::phoenix::{Note, Sender};
use dusk_core::transfer::TRANSFER_CONTRACT;
use dusk_core::JubJubScalar;
use dusk_vm::{ContractData, Session, VM};
use ff::Field;
use once_cell::sync::Lazy;
use rand::rngs::StdRng;
use rand::SeedableRng;

Expand All @@ -40,19 +38,6 @@ pub const DEFAULT_SNAPSHOT: &str =
const GENESIS_BLOCK_HEIGHT: u64 = 0;
const GENESIS_CHAIN_ID: u8 = 0xFA;

pub static FAUCET_PHOENIX_KEY: Lazy<PublicKey> = Lazy::new(|| {
let addr = include_str!("../assets/faucet.address");
let bytes = bs58::decode(addr).into_vec().expect("valid bs58");
PublicKey::from_slice(&bytes).expect("faucet should have a valid key")
});

pub static FAUCET_MOONLIGHT_KEY: Lazy<AccountPublicKey> = Lazy::new(|| {
let addr = include_str!("../assets/faucet.moonlight.address");
let bytes = bs58::decode(addr).into_vec().expect("valid bs58");
AccountPublicKey::from_slice(&bytes)
.expect("faucet should have a valid key")
});

fn generate_transfer_state(
session: &mut Session,
snapshot: &Snapshot,
Expand Down Expand Up @@ -174,13 +159,8 @@ fn generate_empty_state<P: AsRef<Path>>(
let vm = VM::new(state_dir)?;
let mut session = vm.genesis_session(GENESIS_CHAIN_ID);

let transfer_code = include_bytes!(
"../../target/dusk/wasm64-unknown-unknown/release/transfer_contract.wasm"
);

let stake_code = include_bytes!(
"../../target/dusk/wasm32-unknown-unknown/release/stake_contract.wasm"
);
let transfer_code = include_bytes!("../assets/transfer_contract.wasm");
let stake_code = include_bytes!("../assets/stake_contract.wasm");

let owner = snapshot.owner_or(dusk_key);

Expand Down Expand Up @@ -341,3 +321,40 @@ fn load_state<P: AsRef<Path>>(

Ok((vm, commit))
}

#[cfg(test)]
mod tests {

use std::error::Error;

use dusk_bytes::DeserializableSlice;

use super::*;

pub(crate) fn mainnet_from_file() -> Result<Snapshot, Box<dyn Error>> {
let toml = include_str!("../config/mainnet.toml");
let snapshot = toml::from_str(toml)?;
Ok(snapshot)
}

fn dusk_mainnet_key() -> AccountPublicKey {
let bytes = include_bytes!("../../rusk/src/assets/dusk.cpk");
AccountPublicKey::from_slice(&bytes[..])
.expect("faucet should have a valid key")
}

#[test]
fn mainnet_genesis() -> Result<(), Box<dyn Error>> {
let mainnet = mainnet_from_file()?;
let tmp = tempdir::TempDir::new("genesis")
.expect("Should be able to create temporary directory");
let (_, root) =
deploy(tmp.path(), &mainnet, dusk_mainnet_key(), |_| {})?;
let root = hex::encode(root);
let mainnet_root =
"d90d03cf808252037ac2fdd8677868e1ac419caab09ec4cf0e87eafa86b8a612";
assert_eq!(root, mainnet_root);

Ok(())
}
}
23 changes: 19 additions & 4 deletions rusk-recovery/src/state/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,27 @@ mod tests {

use std::error::Error;

use dusk_bytes::DeserializableSlice;
use dusk_core::signatures::bls::PublicKey as AccountPublicKey;
use dusk_core::stake::DEFAULT_MINIMUM_STAKE;
use dusk_core::transfer::phoenix::PublicKey;

use super::*;
use crate::state;

pub(crate) fn testnet_from_file() -> Result<Snapshot, Box<dyn Error>> {
fn testnet_faucet_phoenix() -> PublicKey {
let addr = include_str!("../../assets/faucet.address");
let bytes = bs58::decode(addr).into_vec().expect("valid bs58");
PublicKey::from_slice(&bytes).expect("faucet should have a valid key")
}

fn testnet_faucet_moonlight() -> AccountPublicKey {
let addr = include_str!("../../assets/faucet.moonlight.address");
let bytes = bs58::decode(addr).into_vec().expect("valid bs58");
AccountPublicKey::from_slice(&bytes)
.expect("faucet should have a valid key")
}

fn testnet_from_file() -> Result<Snapshot, Box<dyn Error>> {
let toml = include_str!("../../config/testnet.toml");
let snapshot = toml::from_str(toml)?;
Ok(snapshot)
Expand All @@ -122,12 +137,12 @@ mod tests {
let faucet_phoenix = testnet
.phoenix_balance
.iter()
.any(|b| b.address().eq(&*state::FAUCET_PHOENIX_KEY));
.any(|b| b.address().eq(&testnet_faucet_phoenix()));

let faucet_moonlight = testnet
.moonlight_account
.iter()
.any(|b| b.address().eq(&*state::FAUCET_MOONLIGHT_KEY));
.any(|b| b.address().eq(&testnet_faucet_moonlight()));

if !faucet_phoenix && !faucet_moonlight {
panic!("Testnet must have faucet configured");
Expand Down

0 comments on commit 527e759

Please sign in to comment.