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

fix: change default rows_to_discard for oct trees #1165

Merged
merged 3 commits into from
Jun 15, 2020
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
3 changes: 2 additions & 1 deletion fil-proofs-tooling/src/bin/benchy/stacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use storage_proofs::porep::stacked::{
use storage_proofs::porep::PoRep;
use storage_proofs::proof::ProofScheme;
use storage_proofs::test_helper::setup_replica;
use storage_proofs::util::default_rows_to_discard;
use tempfile::TempDir;

fn dump_proof_bytes<Tree: MerkleTreeTrait>(
Expand Down Expand Up @@ -116,7 +117,7 @@ where
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

let replica_id = H::Domain::random(rng);
Expand Down
3 changes: 2 additions & 1 deletion filecoin-proofs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use storage_proofs::porep::stacked::{
};
use storage_proofs::porep::PoRep;
use storage_proofs::sector::SectorId;
use storage_proofs::util::default_rows_to_discard;
use typenum::Unsigned;

use crate::api::util::{as_safe_commitment, get_base_tree_leafs, get_base_tree_size};
Expand Down Expand Up @@ -149,7 +150,7 @@ where
let config = StoreConfig::new(
cache_path.as_ref(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(
default_rows_to_discard(
base_tree_leafs,
<DefaultBinaryTree as MerkleTreeTrait>::Arity::to_usize(),
),
Expand Down
5 changes: 3 additions & 2 deletions filecoin-proofs/src/api/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use storage_proofs::merkle::{
use storage_proofs::multi_proof::MultiProof;
use storage_proofs::post::fallback;
use storage_proofs::sector::*;
use storage_proofs::util::default_rows_to_discard;

use crate::api::util::{as_safe_commitment, get_base_tree_leafs, get_base_tree_size};
use crate::caches::{get_post_params, get_post_verifying_key};
Expand Down Expand Up @@ -149,7 +150,7 @@ impl<Tree: 'static + MerkleTreeTrait> PrivateReplicaInfo<Tree> {
"post: base tree size {}, base tree leafs {}, rows_to_discard {}, arities [{}, {}, {}]",
base_tree_size,
base_tree_leafs,
StoreConfig::default_rows_to_discard(base_tree_leafs, Tree::Arity::to_usize()),
default_rows_to_discard(base_tree_leafs, Tree::Arity::to_usize()),
Tree::Arity::to_usize(),
Tree::SubTreeArity::to_usize(),
Tree::TopTreeArity::to_usize(),
Expand All @@ -158,7 +159,7 @@ impl<Tree: 'static + MerkleTreeTrait> PrivateReplicaInfo<Tree> {
let mut config = StoreConfig::new(
self.cache_dir_path(),
CacheKey::CommRLastTree.to_string(),
StoreConfig::default_rows_to_discard(base_tree_leafs, Tree::Arity::to_usize()),
default_rows_to_discard(base_tree_leafs, Tree::Arity::to_usize()),
);
config.size = Some(base_tree_size);

Expand Down
11 changes: 5 additions & 6 deletions filecoin-proofs/src/api/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use storage_proofs::porep::stacked::{
};
use storage_proofs::proof::ProofScheme;
use storage_proofs::sector::SectorId;
use storage_proofs::util::default_rows_to_discard;

use crate::api::util::{
as_safe_commitment, commitment_from_fr, get_base_tree_leafs, get_base_tree_size,
Expand Down Expand Up @@ -127,19 +128,18 @@ where
);

trace!(
"seal phase 1: sector_size {}, base tree size {}, base tree leafs {}, rows to discard {}",
"seal phase 1: sector_size {}, base tree size {}, base tree leafs {}",
u64::from(porep_config.sector_size),
base_tree_size,
base_tree_leafs,
StoreConfig::default_rows_to_discard(base_tree_leafs, BINARY_ARITY)
);

// MT for original data is always named tree-d, and it will be
// referenced later in the process as such.
let mut config = StoreConfig::new(
cache_path.as_ref(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(base_tree_leafs, BINARY_ARITY),
default_rows_to_discard(base_tree_leafs, BINARY_ARITY),
);
let data_tree = create_base_merkle_tree::<BinaryMerkleTree<DefaultPieceHasher>>(
Some(config.clone()),
Expand Down Expand Up @@ -247,11 +247,10 @@ where
"seal phase 2: base tree size {}, base tree leafs {}, rows to discard {}",
base_tree_size,
base_tree_leafs,
StoreConfig::default_rows_to_discard(base_tree_leafs, BINARY_ARITY)
default_rows_to_discard(base_tree_leafs, BINARY_ARITY)
);
ensure!(
config.rows_to_discard
== StoreConfig::default_rows_to_discard(base_tree_leafs, BINARY_ARITY),
config.rows_to_discard == default_rows_to_discard(base_tree_leafs, BINARY_ARITY),
"Invalid cache size specified"
);

Expand Down
4 changes: 2 additions & 2 deletions storage-proofs/core/src/merkle/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rayon::prelude::*;

use crate::error::*;
use crate::hasher::{Domain, Hasher, PoseidonArity};
use crate::util::{data_at_node, NODE_SIZE};
use crate::util::{data_at_node, default_rows_to_discard, NODE_SIZE};

use super::*;

Expand Down Expand Up @@ -409,7 +409,7 @@ where
let config = StoreConfig::new(
&temp_path,
format!("test-lc-tree-{}", id),
StoreConfig::default_rows_to_discard(nodes, Tree::Arity::to_usize()),
default_rows_to_discard(nodes, Tree::Arity::to_usize()),
);

let mut tree =
Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Default for Settings {
column_write_batch_size: 262_144,
use_gpu_tree_builder: false,
max_gpu_tree_batch_size: 700_000,
rows_to_discard: 0,
rows_to_discard: 2,
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions storage-proofs/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ use crate::error;
use anyhow::ensure;
use bellperson::gadgets::boolean::{self, AllocatedBit, Boolean};
use bellperson::{ConstraintSystem, SynthesisError};
use merkletree::merkle::get_merkle_tree_row_count;
use paired::Engine;

use super::settings;

pub const NODE_SIZE: usize = 32;

/// Returns the start position of the data, 0-indexed.
Expand Down Expand Up @@ -142,6 +145,36 @@ pub fn reverse_bit_numbering(bits: Vec<boolean::Boolean>) -> Vec<boolean::Boolea
.collect()
}

// If the tree is large enough to use the default value (per-arity), use it. If it's too small to cache anything (i.e. not enough rows), don't discard any.
pub fn default_rows_to_discard(leafs: usize, arity: usize) -> usize {
let row_count = get_merkle_tree_row_count(leafs, arity);
if row_count <= 2 {
// If a tree only has a root row and/or base, there is
// nothing to discard.
return 0;
} else if row_count == 3 {
// If a tree only has 1 row between the base and root,
// it's all that can be discarded.
return 1;
}

// row_count - 2 discounts the base layer (1) and root (1)
let max_rows_to_discard = row_count - 2;

// This configurable setting is for a default oct-tree
// rows_to_discard value, which defaults to 2.
let rows_to_discard = settings::SETTINGS.lock().unwrap().rows_to_discard as usize;

// Discard at most 'constant value' rows (coded below,
// differing by arity) while respecting the max number that
// the tree can support discarding.
match arity {
2 => std::cmp::min(max_rows_to_discard, 7),
4 => std::cmp::min(max_rows_to_discard, 5),
_ => std::cmp::min(max_rows_to_discard, rows_to_discard),
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Along with a single rows_to_discard default, we could probably get rid of this match — unless it simplifies testing.

}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 3 additions & 6 deletions storage-proofs/porep/src/drg/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ mod tests {
merkle::MerkleProofTrait,
proof::ProofScheme,
test_helper::setup_replica,
util::data_at_node,
util::{data_at_node, default_rows_to_discard},
};

use super::super::compound::DrgPoRepCompound;
Expand Down Expand Up @@ -349,7 +349,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -395,10 +395,7 @@ mod tests {
let priv_inputs = drg::PrivateInputs::<PedersenHasher> {
tree_d: &aux.tree_d,
tree_r: &aux.tree_r,
tree_r_config_rows_to_discard: StoreConfig::default_rows_to_discard(
nodes,
BINARY_ARITY,
),
tree_r_config_rows_to_discard: default_rows_to_discard(nodes, BINARY_ARITY),
};

let proof_nc = drg::DrgPoRep::<PedersenHasher, _>::prove(&pp, &pub_inputs, &priv_inputs)
Expand Down
8 changes: 3 additions & 5 deletions storage-proofs/porep/src/drg/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ mod tests {
merkle::{BinaryMerkleTree, MerkleTreeTrait},
proof::NoRequirements,
test_helper::setup_replica,
util::default_rows_to_discard,
};

use crate::stacked::BINARY_ARITY;
Expand Down Expand Up @@ -335,7 +336,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -380,10 +381,7 @@ mod tests {
let private_inputs = drg::PrivateInputs {
tree_d: &aux.tree_d,
tree_r: &aux.tree_r,
tree_r_config_rows_to_discard: StoreConfig::default_rows_to_discard(
nodes,
BINARY_ARITY,
),
tree_r_config_rows_to_discard: default_rows_to_discard(nodes, BINARY_ARITY),
};

// This duplication is necessary so public_params don't outlive public_inputs and private_inputs.
Expand Down
13 changes: 5 additions & 8 deletions storage-proofs/porep/src/drg/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ mod tests {
merkle::{BinaryMerkleTree, MerkleTreeTrait},
table_tests,
test_helper::setup_replica,
util::data_at_node,
util::{data_at_node, default_rows_to_discard},
};
use tempfile;

Expand All @@ -639,7 +639,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -718,7 +718,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -809,7 +809,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -854,10 +854,7 @@ mod tests {
let priv_inputs = PrivateInputs::<Tree::Hasher> {
tree_d: &aux.tree_d,
tree_r: &aux.tree_r,
tree_r_config_rows_to_discard: StoreConfig::default_rows_to_discard(
nodes,
BINARY_ARITY,
),
tree_r_config_rows_to_discard: default_rows_to_discard(nodes, BINARY_ARITY),
};

let real_proof = DrgPoRep::<Tree::Hasher, _>::prove(&pp, &pub_inputs, &priv_inputs)
Expand Down
5 changes: 3 additions & 2 deletions storage-proofs/porep/src/stacked/circuit/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ mod tests {
merkle::{get_base_tree_count, DiskTree, MerkleTreeTrait},
proof::ProofScheme,
test_helper::setup_replica,
util::default_rows_to_discard,
};

use crate::stacked::{
Expand Down Expand Up @@ -412,7 +413,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -608,7 +609,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down
29 changes: 13 additions & 16 deletions storage-proofs/porep/src/stacked/vanilla/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use storage_proofs_core::{
},
merkle::*,
settings,
util::NODE_SIZE,
util::{default_rows_to_discard, NODE_SIZE},
};
use typenum::{U11, U2, U8};

Expand Down Expand Up @@ -973,34 +973,31 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher> StackedDrg<'a, Tr
CacheKey::CommDTree.to_string(),
Some(get_merkle_tree_len(nodes_count, BINARY_ARITY)?),
);
tree_d_config.rows_to_discard =
StoreConfig::default_rows_to_discard(nodes_count, BINARY_ARITY);
tree_d_config.rows_to_discard = default_rows_to_discard(nodes_count, BINARY_ARITY);

let mut tree_r_last_config = StoreConfig::from_config(
&config,
CacheKey::CommRLastTree.to_string(),
Some(get_merkle_tree_len(nodes_count, Tree::Arity::to_usize())?),
);

// A default 'rows_to_discard' value will be chosen for
// tree_r_last, unless the user overrides this value via the
// environment setting (FIL_PROOFS_ROWS_TO_DISCARD). If this
// value is specified, no checking is done on it and it may
// A default 'rows_to_discard' value will be chosen for tree_r_last, unless the user overrides this value via the
// environment setting (FIL_PROOFS_ROWS_TO_DISCARD). If this value is specified, no checking is done on it and it may
// result in a broken configuration. Use with caution.
let env_rows_to_discard = settings::SETTINGS.lock().unwrap().rows_to_discard as usize;
tree_r_last_config.rows_to_discard = if env_rows_to_discard != 0 {
env_rows_to_discard
} else {
StoreConfig::default_rows_to_discard(nodes_count, Tree::Arity::to_usize())
};
tree_r_last_config.rows_to_discard =
default_rows_to_discard(nodes_count, Tree::Arity::to_usize());
trace!(
"tree_r_last using rows_to_discard={}",
tree_r_last_config.rows_to_discard
);

let mut tree_c_config = StoreConfig::from_config(
&config,
CacheKey::CommCTree.to_string(),
Some(get_merkle_tree_len(nodes_count, Tree::Arity::to_usize())?),
);
tree_c_config.rows_to_discard =
StoreConfig::default_rows_to_discard(nodes_count, Tree::Arity::to_usize());
default_rows_to_discard(nodes_count, Tree::Arity::to_usize());

let labels = LabelsCache::<Tree>::new(&label_configs)?;
let configs = split_config(tree_c_config.clone(), tree_count)?;
Expand Down Expand Up @@ -1264,7 +1261,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down Expand Up @@ -1441,7 +1438,7 @@ mod tests {
let config = StoreConfig::new(
cache_dir.path(),
CacheKey::CommDTree.to_string(),
StoreConfig::default_rows_to_discard(nodes, BINARY_ARITY),
default_rows_to_discard(nodes, BINARY_ARITY),
);

// Generate a replica path.
Expand Down