Skip to content

Commit

Permalink
[move/rust] libra-framework patch 7.0.3 & AppCfg private key for test…
Browse files Browse the repository at this point in the history
…suite (#313)

Co-authored-by: xyz <xyz>
  • Loading branch information
sirouk authored Sep 2, 2024
1 parent b7aaed3 commit d6ada0f
Show file tree
Hide file tree
Showing 26 changed files with 280 additions and 99 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(clippy::too_many_arguments)]

use diem_types::{
account_address::AccountAddress,
transaction::{EntryFunction, TransactionPayload},
Expand Down
2 changes: 1 addition & 1 deletion framework/libra-framework/sources/diem_governance.move
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ module diem_framework::diem_governance {
}

#[view]
// is the proposal complete and executed?
// how many votes on the proposal
public fun get_votes(proposal_id: u64): (u128, u128) {
voting::get_votes<GovernanceProposal>(@diem_framework, proposal_id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ module ol_framework::address_utils {

// Shuffle addresses with the same values to ensure randomness position
public fun shuffle_duplicates(addresses: &mut vector<address>, values: &vector<u64>) {
// belt and suspenders, if migration didn't happen.
// assert!(randomness::is_init(), error::invalid_state(ERANDOM_INIT_ERROR));

assert!(vector::length(addresses) == vector::length(values), error::invalid_argument(EDIFFERENT_LENGTH));
let len = vector::length(values);
let i = 0;
Expand Down
14 changes: 8 additions & 6 deletions framework/libra-framework/sources/ol_sources/epoch_boundary.move
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module diem_framework::epoch_boundary {
const ETRIGGER_EPOCH_UNAUTHORIZED: u64 = 1;
/// Epoch is not ready for reconfiguration
const ETRIGGER_NOT_READY: u64 = 2;
/// Epoch number mismat
/// Epoch number mismatch
const ENOT_SAME_EPOCH: u64 = 3;

/////// Constants ////////
Expand Down Expand Up @@ -254,20 +254,22 @@ module diem_framework::epoch_boundary {
}

#[view]
/// check to see if the epoch Boundary Bit is true
/// check to see if the epoch BoundaryBit is true
public fun can_trigger(): bool acquires BoundaryBit {
let state = borrow_global_mut<BoundaryBit>(@ol_framework);
assert!(state.ready, ETRIGGER_NOT_READY);
assert!(state.closing_epoch == reconfiguration::get_current_epoch(),
// greater than, in case there is an epoch change due to an epoch bump in
// testnet Twin tools, or a rescue operation.
assert!(state.closing_epoch <= reconfiguration::get_current_epoch(),
ENOT_SAME_EPOCH);
true
}

// This function handles the necessary migrations that occur at the epoch boundary
// when new modules or structures are added by chain upgrades.
fun migrate_data(root: &signer) {
randomness::initialize(root);
migrations::execute(root);
fun migrate_data(framework: &signer) {
randomness::initialize(framework);
migrations::execute(framework);
}

// Contains all of 0L's business logic for end of epoch.
Expand Down
2 changes: 1 addition & 1 deletion framework/libra-framework/sources/ol_sources/vouch.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module ol_framework::vouch {
use diem_framework::system_addresses;
use diem_framework::transaction_fee;

use diem_std::debug::print;
use diem_std::debug::print;

friend diem_framework::genesis;
friend ol_framework::proof_of_fee;
Expand Down
21 changes: 16 additions & 5 deletions framework/libra-framework/sources/randomness.move
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module diem_framework::randomness {
friend diem_framework::block;
friend ol_framework::musical_chairs;

const DST: vector<u8> = b"ALL_YOUR_BASE";
const INIT_SEED: vector<u8> = b"all your base are belong to us";


const MAX_U256: u256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
Expand Down Expand Up @@ -68,12 +68,17 @@ module diem_framework::randomness {
move_to(framework, PerBlockRandomness {
epoch: 0,
round: 0,
seed: option::none(),
seed: option::some(INIT_SEED),
seq: 0,
});
}
}

#[view]
public fun is_init(): bool {
exists<PerBlockRandomness>(@diem_framework)
}

#[test_only]
public fun initialize_for_testing(framework: &signer) acquires PerBlockRandomness {
initialize(framework);
Expand All @@ -98,18 +103,24 @@ module diem_framework::randomness {
// public facing API.
// assert!(is_unbiasable(), E_API_USE_IS_BIASIBLE);

let input = DST;
let randomness = borrow_global_mut<PerBlockRandomness>(@diem_framework);
let seed = *option::borrow(&randomness.seed);

vector::append(&mut input, seed);
// belt and suspenders if something didn't initialize
let input = if (option::is_some(&randomness.seed)) {
*option::borrow(&randomness.seed)
} else {
INIT_SEED
};


// 0L NOTE: these native APIs dont exist in 0L V7.
// get_transaction_hash() doesnt exist. So different than vendor,
// we will always increment a seed based on the block hash.
// Note: will then be the previousl block's hash for the next
// transaction.
// we will add the script hash of the entry function as a placeholder
// though this will likely not be adding much entropy.

vector::append(&mut input,
transaction_context::get_script_hash());

Expand Down
Binary file modified framework/releases/head.mrb
Binary file not shown.
56 changes: 36 additions & 20 deletions smoke-tests/src/configure_validator.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
use diem_forge::{LocalSwarm, Swarm};
use diem_forge::{LocalSwarm, Node};
use diem_sdk::crypto::PrivateKey;
use diem_sdk::types::LocalAccount;
use diem_types::chain_id::NamedChain;
use libra_types::core_types::network_playlist::NetworkPlaylist;
use libra_types::{core_types::app_cfg::AppCfg, exports::AuthenticationKey};
use std::path::PathBuf;
use url::Url;

/// Set up the 0L local files, and get an AppCfg back after initializing in a temp dir, that will drop at the end of the test.
pub async fn init_val_config_files(
pub fn init_val_config_files(
swarm: &mut LocalSwarm,
nth: usize,
dir: PathBuf,
dir_opt: Option<PathBuf>,
) -> anyhow::Result<(LocalAccount, AppCfg)> {
let info = swarm.diem_public_info_for_node(nth);
let url: Url = info.url().parse().unwrap();
// TODO: unclear why public info needs to be a mutable borrow
let node = swarm
.validators()
.nth(nth)
.expect("could not get nth validator");
let url = node.rest_api_endpoint();

let node = swarm.validators().next().unwrap();
let np = NetworkPlaylist::new(Some(url), Some(diem_types::chain_id::NamedChain::TESTING));
let dir = dir_opt.unwrap_or(node.config_path().parent().unwrap().to_owned());

let chain_name = NamedChain::from_chain_id(&swarm.chain_id()).ok();
let np = NetworkPlaylist::new(Some(url), chain_name);
let cfg_key = node.account_private_key().as_ref().unwrap();
let prikey = cfg_key.private_key();
let pubkey = prikey.public_key();
let mut app_cfg = AppCfg::init_app_configs(
AuthenticationKey::ed25519(&node.account_private_key().as_ref().unwrap().public_key()),
AuthenticationKey::ed25519(&pubkey),
node.peer_id(),
Some(dir),
Some(np.chain_name),
Some(np),
)
.unwrap();

let pri_key = node
.account_private_key()
.as_ref()
.expect("could not get pri_key")
.private_key();
let auth = AuthenticationKey::ed25519(&pri_key.public_key());
)?;

let profile = app_cfg
.get_profile_mut(None)
.expect("could not get profile");
profile.set_private_key(&pri_key);

let local_account = LocalAccount::new(auth.derived_address(), pri_key, 0);
profile.set_private_key(&prikey);

let local_account = LocalAccount::new(profile.account, prikey, 0);

Ok((local_account, app_cfg))
}

/// helper to save libra-cli config files for each of the validators in
/// their local temp folder (alongside validator.yaml)
pub fn save_cli_config_all(swarm: &mut LocalSwarm) -> anyhow::Result<()> {
let len = swarm.validators().count();
for i in 0..len {
// a libra-cli-config file will be created at the temp swarm
// directory of the node
let (_, app_cfg) = init_val_config_files(swarm, i, None)?;
let _file = app_cfg.save_file()?;
}
Ok(())
}
122 changes: 122 additions & 0 deletions testsuites/twin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# NOTE: you'll need to have a db of a fullnode already synced
# twin tool will make a copy of this
# THE DB WILL NOT BE WRITTEN TO

# TMP_DIR = /tmp/.tmpCu3Rxh/

ifndef DB_DIR
DB_DIR=$$HOME/.libra/data/db
endif

ifndef UPGRADE_SCRIPT_PATH
UPGRADE_SCRIPT_PATH = $$HOME/upgrade-six/
endif

ifndef FRAMEWORK_SOURCE_PATH
FRAMEWORK_SOURCE_PATH = $$HOME/libra-framework/framework
endif

ifndef DIEM_FORGE_NODE_BIN_PATH
DIEM_FORGE_NODE_BIN_PATH = $$HOME/.cargo/bin/libra
endif

PROPOSAL_ID = 6

##### INSTRUCTIONS

# Grab the essentials:
# sudo apt update
# sudo apt install -y git build-essential cmake clang llvm libgmp-dev pkg-config libssl-dev lld libpq-dev
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 1. use an up to date libra-cli binary
# > cargo build --release -p libra --locked
# > cp target/release/libra ~/.cargo/bin/

# 2. Make sure you have a fullnode that is already syced, you'll need the DB_DIR of it
# if you are starting fresh use:
# > libra config fullnode-init
# > libra node
# check height while syncing
# > watch -n 5 'curl -s 127.0.0.1:9101/metrics | grep diem_state_sync_version'

# 2. compile the new libra-framework MOVE code with:
# > make upgrade-script
# note the defaults for input: FRAMEWORK_SOURCE_PATH and and output: UPGRADE_SCRIPT_PATH

# 3. start a Twin swarm locally
# > make start-twin
# NOTE: the output `temp files found at: /tmp/<......> `
# A local Twin of mainnet is now running on your machine.

# 4. export that temp dir to your path
# > export TMP_DIR=/tmp/<......>

# Now your can do transactions as the new random validators
# 5. check validator set
# > make view-vals

# 6. try to tigger epoch
# > make tx-epoch
# NOTE: this usually should fail unless enough time has passed.

# 7. Send the full upgrade e2e
# > make upgrade-ceremony

# 8. check the state of the proposal
# > make view-state

# start twin with three validators
start-twin:
cargo run -p libra-twin-tests -- -d ${DB_DIR} -c 3


######### UPGRADE SCRIPT GENERATION
upgrade-script: move-build-framework move-build-script

move-build-framework:
cd ${FRAMEWORK_SOURCE_PATH} && libra move framework release

move-build-script:
libra move framework upgrade --core-modules libra-framework --output-dir ${UPGRADE_SCRIPT_PATH} --framework-local-dir ${FRAMEWORK_SOURCE_PATH}

######## EPOCH TRIGGER
tx-epoch:
libra txs -c ${TMP_DIR}/0/libra-cli-config.yaml governance epoch-boundary


######## UPGRADE TRANSACTIONS
upgrade-ceremony: tx-propose tx-vote tx-resolve

tx-propose:
libra txs -c ${TMP_DIR}/0/libra-cli-config.yaml governance propose -d ${UPGRADE_SCRIPT_PATH}/1-libra-framework -m https://tbd.com

tx-vote:
libra txs -c ${TMP_DIR}/0/libra-cli-config.yaml governance vote -i ${PROPOSAL_ID}
libra txs -c ${TMP_DIR}/1/libra-cli-config.yaml governance vote -i ${PROPOSAL_ID}
libra txs -c ${TMP_DIR}/2/libra-cli-config.yaml governance vote -i ${PROPOSAL_ID}

tx-resolve:
libra txs -c ${TMP_DIR}/0/libra-cli-config.yaml --tx-profile critical governance resolve -i ${PROPOSAL_ID} -d ${UPGRADE_SCRIPT_PATH}/1-libra-framework

#### VIEW STATE OF UPGRADE PROPOSALS
view-state:
libra query -c ${TMP_DIR}/0/libra-cli-config.yaml view -f 0x1::diem_governance::get_proposal_state -a ${PROPOSAL_ID}

view-resolve:
libra query -c ${TMP_DIR}/0/libra-cli-config.yaml view -f 0x1::diem_governance::get_can_resolve -a ${PROPOSAL_ID}

view-vals:
libra query -c ${TMP_DIR}/0/libra-cli-config.yaml view -f 0x1::stake::get_current_validators


######## OTHER
debug-keys:
cat ${TMP_DIR}/0/private-identity.yaml
cat ${TMP_DIR}/1/private-identity.yaml
cat ${TMP_DIR}/2/private-identity.yaml

help-tx-bid-shuffle:
libra txs -c ${TMP_DIR}/0/libra-cli-config.yaml validator pof -b 0.3 -e 1000
libra txs -c ${TMP_DIR}/1/libra-cli-config.yaml validator pof -b 0.4 -e 1000
libra txs -c ${TMP_DIR}/2/libra-cli-config.yaml validator pof -b 0.5 -e 1000
4 changes: 3 additions & 1 deletion testsuites/twin/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::{self, Parser};
use libra_smoke_tests::libra_smoke::LibraSmoke;
use std::{fs, path::PathBuf};

/// Twin of the network
#[derive(Parser)]

Expand Down Expand Up @@ -29,6 +28,9 @@ impl Twin {
let num_validators = self.count_vals.unwrap_or(1);

let mut smoke = LibraSmoke::new(Some(num_validators), None).await?;
// save_cli_config_all(&mut smoke.swarm)?;

// thread::sleep(Duration::from_secs(60));
Twin::make_twin_swarm(&mut smoke, Some(db_path), true).await?;

Ok(())
Expand Down
Loading

0 comments on commit d6ada0f

Please sign in to comment.