Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
tower: delete a vdf proof that cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Jul 25, 2022
1 parent ef06ad2 commit c833393
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ol/cli/src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn find_a_remote_jsonrpc(config: &AppCfg, waypoint: Waypoint) -> Result<Diem
return make_client(Some(url_clean.to_owned()), waypoint);
};
Err(Error::msg(
"Cannot connect to any JSON RPC peers in the list of upstream_nodes in 0L.toml",
format!("Cannot connect to any JSON RPC peers in the list of upstream_nodes in 0L.toml {:?}", list)
))
}

Expand Down
1 change: 1 addition & 0 deletions ol/integration-tests/test-mining-epochs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ echo:

init:
cd ${SOURCE_PATH} && cargo r -p ol -- --swarm-path ${SWARM_TEMP} --swarm-persona ${PERSONA} init --source-path ${SOURCE_PATH} --chain-id TESTING
cp ${SWARM_TEMP}/0/0L.toml ${HOME}/.0L/0L.toml

mine:
cd ${SOURCE_PATH} && cargo r -p tower -- --swarm-path ${SWARM_TEMP} --swarm-persona ${PERSONA} start
Expand Down
2 changes: 1 addition & 1 deletion ol/tower/src/garbage_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn gc_failed_proof(cfg: &AppCfg, bad_proof_path: PathBuf) -> anyhow::Result<

/// collect all the proofs after a given height, inclusive of the given height
pub fn collect_subsequent_proofs(bad_proof_path: PathBuf, block_dir: PathBuf) -> anyhow::Result<Option<Vec<PathBuf>>> {
let bad_proof = proof::parse_block_file(&bad_proof_path)?;
let bad_proof = proof::parse_block_file(&bad_proof_path, true)?;

let highest_local = proof::get_highest_block(&block_dir)?.0.height;

Expand Down
34 changes: 23 additions & 11 deletions ol/tower/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ pub fn mine_and_submit(
let next = match local_mode {
true => next_proof::get_next_proof_params_from_local(config)?,
false => {
let client = client::find_a_remote_jsonrpc(&config, config.get_waypoint(swarm_path.clone())?)?;
let client = client::find_a_remote_jsonrpc(
&config,
config.get_waypoint(swarm_path.clone())?,
)?;
match next_proof::get_next_proof_from_chain(config, client, swarm_path.clone()) {
Ok(n) => n,
// failover to local mode, if no onchain data can be found.
Expand Down Expand Up @@ -152,7 +155,13 @@ pub fn get_highest_block(blocks_dir: &PathBuf) -> Result<(VDFProof, PathBuf), Er
if let Ok(entry) = entry {
// let file = fs::File::open(&entry).expect("Could not open block file");
// let reader = BufReader::new(file);
let block: VDFProof = parse_block_file(&entry)?;
let block = match parse_block_file(&entry, false) {
Ok(v) => v,
Err(e) => {
println!("could not parse the proof file: {}, skipping. Manually delete if this proof is not readable.", e.to_string());
continue
},
};

let blocknumber = block.height;

Expand All @@ -179,16 +188,19 @@ pub fn get_highest_block(blocks_dir: &PathBuf) -> Result<(VDFProof, PathBuf), Er
}

/// Parse a proof_x.json file and return a VDFProof
pub fn parse_block_file(path: &PathBuf) -> Result<VDFProof, Error> {
pub fn parse_block_file(path: &PathBuf, purge_if_bad: bool) -> Result<VDFProof, Error> {
let block_file = fs::read_to_string(path)?;

match serde_json::from_str(&block_file) {
Ok(v) => Ok(v),
Err(e) => bail!(
"Could not read latest block file in path {:?}, message: {:?}",
&path,
e
),
Err(e) => {
if purge_if_bad { fs::remove_file(&block_file)? }
bail!(
"Could not read latest block file in path {:?}, message: {:?}",
&path,
e
)
}
}
}

Expand All @@ -200,7 +212,7 @@ pub fn find_proof_number(num: u64, blocks_dir: &PathBuf) -> Result<(VDFProof, Pa
FILENAME,
num.to_string()
));
match parse_block_file(&file) {
match parse_block_file(&file, false) {
Ok(p) => {
if p.height == num {
return Ok((p, file));
Expand All @@ -218,10 +230,10 @@ pub fn find_proof_number(num: u64, blocks_dir: &PathBuf) -> Result<(VDFProof, Pa
}

/// find the most recent proof on disk
pub fn get_latest_proof(config: &AppCfg) -> Result<VDFProof, Error> {
pub fn get_latest_proof(config: &AppCfg, purge_if_bad: bool) -> Result<VDFProof, Error> {
let (_current_block_number, current_block_path) = get_highest_block(&config.get_block_dir())?;

parse_block_file(&current_block_path)
parse_block_file(&current_block_path, purge_if_bad)
}

/* ////////////// */
Expand Down

0 comments on commit c833393

Please sign in to comment.