Skip to content

Commit

Permalink
[Tower] - Tower backlog, handle tower running over the limit (0LNetwo…
Browse files Browse the repository at this point in the history
…rkCommunity#97)

Co-authored-by: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com>
  • Loading branch information
hemulin and 0o-de-lally committed Aug 16, 2024
1 parent 0933151 commit d8d46ae
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 49 deletions.
16 changes: 15 additions & 1 deletion tools/query/src/chain_queries.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
//! chain queries
use crate::query_view;
use crate::query_view::{self, get_view};

use anyhow::Context;
use diem_sdk::rest_client::{diem_api_types::ViewRequest, Client};
use libra_types::type_extensions::client_ext::entry_function_id;

pub async fn get_epoch(client: &Client) -> anyhow::Result<u64> {
let res = get_view(
client,
"0x1::reconfiguration::get_current_epoch",
None,
None,
)
.await?;

let value: Vec<String> = serde_json::from_value(res)?;
let num = value.first().unwrap().parse::<u64>()?;

Ok(num)
}
/// helper to get libra balance at a SlowWalletBalance type which shows
/// total balance and the unlocked balance.
pub async fn get_tower_difficulty(client: &Client) -> anyhow::Result<(u64, u64)> {
Expand Down
13 changes: 3 additions & 10 deletions tools/query/src/query_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
account_queries::{get_account_balance_libra, get_tower_state, get_val_config},
chain_queries::get_epoch,
query_view::get_view,
};
use anyhow::{bail, Context, Result};
Expand Down Expand Up @@ -141,17 +142,9 @@ impl QueryType {
Ok(json)
}
QueryType::Epoch => {
let res = get_view(
&client,
"0x1::reconfiguration::get_current_epoch",
None,
None,
)
.await?;

let num: Vec<String> = serde_json::from_value(res)?;
let num = get_epoch(&client).await?;
let json = json!({
"epoch": num.first().unwrap().parse::<u64>()?,
"epoch": num,
});
Ok(json)
}
Expand Down
20 changes: 12 additions & 8 deletions tools/tower/src/core/backlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::core::{garbage_collection::gc_failed_proof, tower_error};

use anyhow::{anyhow, bail, Error, Result};
use anyhow::{bail, Error, Result};
use std::path::PathBuf;

use libra_query::account_queries;
Expand All @@ -18,9 +18,14 @@ use libra_types::{
};

const EPOCH_MINING_THRES_UPPER: u64 = 72;

pub enum BacklogResult {
Success,
AboveLimit,
}
/// Submit a backlog of blocks that may have been mined while network is offline.
/// Likely not more than 1.
pub async fn process_backlog(config: &AppCfg) -> anyhow::Result<()> {
pub async fn process_backlog(config: &AppCfg) -> anyhow::Result<BacklogResult> {
// Getting local state height
let mut blocks_dir = config.workspace.node_home.clone();
blocks_dir.push(&config.get_block_dir(None)?);
Expand All @@ -32,7 +37,8 @@ pub async fn process_backlog(config: &AppCfg) -> anyhow::Result<()> {
println!("Local tower height: {:?}", current_proof_number);
if current_proof_number == 0 {
// if we are at genesis
return submit_or_delete(config, current_local_proof, current_block_path).await;
submit_or_delete(config, current_local_proof, current_block_path).await?;
return Ok(BacklogResult::Success);
}

let mut i = 0;
Expand All @@ -50,12 +56,10 @@ pub async fn process_backlog(config: &AppCfg) -> anyhow::Result<()> {
// use i64 for safety
if proofs_in_epoch >= EPOCH_MINING_THRES_UPPER {
println!(
"Backlog: Maximum number of proofs sent this epoch {}, exiting.",
"Backlog: Maximum number of proofs already sent this epoch {}",
EPOCH_MINING_THRES_UPPER
);
return Err(anyhow!(
"cannot submit more proofs than allowed in epoch, aborting backlog."
));
return Ok(BacklogResult::AboveLimit);
}

if proofs_in_epoch > 0 {
Expand All @@ -78,7 +82,7 @@ pub async fn process_backlog(config: &AppCfg) -> anyhow::Result<()> {
i += 1;
submitted_now += 1;
}
Ok(())
Ok(BacklogResult::Success)
}

pub async fn submit_or_delete(config: &AppCfg, block: VDFProof, path: PathBuf) -> Result<()> {
Expand Down
28 changes: 0 additions & 28 deletions tools/tower/src/core/next_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,32 +96,4 @@ pub async fn get_next_proof_from_chain(
};

Ok(p)

// n.refresh_onchain_state();
// // TODO: we are picking Client twice
// let diff = get_difficulty_from_chain(&n)?;

// // get the user's tower state from chain.
// let ts = n.client
// .get_account_state(config.profile.account)?
// .get_miner_state()?;

// if let Some(t) = tower_view {
// Ok()
// } else {
// bail!("cannot get tower resource for account")
// }
}

// /// Get the VDF difficulty from chain.
// pub fn get_difficulty_from_chain(n: &Node) -> anyhow::Result<VDFDifficulty> {

// if let Some(a) = &n.chain_state {

// if let Some(diff) = a.get_tower_params()? {
// return Ok(diff);
// }
// bail!("could not get this epoch's VDF params from chain.")
// }
// bail!("could not get account state for 0x0")
// }
32 changes: 30 additions & 2 deletions tools/tower/src/core/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::core::{

use anyhow::Error;
use indicatif::ProgressBar;
use libra_query::chain_queries;
use libra_types::{
exports::Client, legacy_types::app_cfg::AppCfg, type_extensions::client_ext::ClientExt,
};
Expand All @@ -20,7 +21,7 @@ use libra_types::{
use std::{
fs,
path::{Path, PathBuf},
time::Instant,
time::{Duration, Instant},
};

// writes a JSON file with the first vdf proof
Expand Down Expand Up @@ -111,7 +112,32 @@ pub async fn mine_and_submit(
get_next_and_mine(config, &client, local_mode).await?;
// submits backlog to client
match backlog::process_backlog(config).await {
Ok(()) => println!("Success: Proof committed to chain"),
Ok(BacklogResult::Success) => {
println!("Success: Proof committed to chain");
}
Ok(BacklogResult::AboveLimit) => {
// Speed demon, you're the very same one
// Who said the future's in your hands
// The life you save could be your own
// You're preachin' 'bout my life like you're the law
// Gonna live each day and hour like
// For me there's no tomorrow
println!(
"Speed demon, let's hang out here until the next epoch. Exit with ctrl+c."
);

// check the current epoch every 5 minutes.
// When we get to next epoch
let full_epoch = chain_queries::get_epoch(&client).await?;
let mut next_epoch = full_epoch;
while full_epoch >= next_epoch {
// in case next epoch returns 0
let lets_wait = Duration::from_secs(5 * 60); // 5 mins
std::thread::sleep(lets_wait);
next_epoch = chain_queries::get_epoch(&client).await.unwrap_or(0);
// don't fail on intermittent API
}
}
Err(e) => {
// don't stop on tx errors
println!("ERROR: Failed processing backlog, message: {:?}", e);
Expand Down Expand Up @@ -221,6 +247,8 @@ use diem_temppath::TempPath;
#[cfg(test)]
use libra_types::legacy_types::vdf_difficulty::VDFDifficulty;

use super::backlog::BacklogResult;

#[test]
fn test_mine_once() {
// if no file is found, the block height is 0
Expand Down

0 comments on commit d8d46ae

Please sign in to comment.