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

Introduce proptest in chainstate #294

Merged
merged 12 commits into from
Jul 26, 2022
55 changes: 46 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ members = [
"utils", # various utilities
"utxo", # utxo and related utilities (cache, undo, etc.)
"test", # integration tests
"test-utils", # various utilities for tests
]

default-members = [
Expand Down
2 changes: 2 additions & 0 deletions chainstate-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ crypto = { path = '../crypto' }
itertools = "0.10"
mockall = "0.11"
num-traits = "0.2"
rstest = "0.15"
test-utils = {path = '../test-utils'}

[features]
mock = [ 'mockall' ]
6 changes: 3 additions & 3 deletions chainstate-storage/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! A mock version of the blockchian storage.
//! A mock version of the blockchain storage.
azarovh marked this conversation as resolved.
Show resolved Hide resolved

use chainstate_types::block_index::BlockIndex;
use common::chain::transaction::{
Expand Down Expand Up @@ -83,7 +83,7 @@ mockall::mock! {
}

mockall::mock! {
/// A mock object for blockcain storage transaction
/// A mock object for blockchain storage transaction
pub StoreTxRo {}

impl crate::BlockchainStorageRead for StoreTxRo {
Expand Down Expand Up @@ -115,7 +115,7 @@ mockall::mock! {
}

mockall::mock! {
/// A mock object for blockcain storage transaction
/// A mock object for blockchain storage transaction
pub StoreTxRw {}

impl crate::BlockchainStorageRead for StoreTxRw {
Expand Down
26 changes: 16 additions & 10 deletions chainstate-storage/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ pub(crate) mod test {
use common::chain::{Destination, OutputPurpose, TxOutput};
use common::primitives::{Amount, H256};
use crypto::key::{KeyKind, PrivateKey};
use crypto::random::{make_pseudo_rng, Rng};
use crypto::random::Rng;
use rstest::rstest;
use test_utils::random::{make_seedable_rng, Seed};
use utxo::{BlockUndo, TxUndo};

#[test]
Expand Down Expand Up @@ -643,9 +645,9 @@ pub(crate) mod test {
}

/// returns a tuple of utxo and outpoint, for testing.
fn create_rand_utxo(block_height: u64) -> Utxo {
fn create_rand_utxo(rng: &mut impl Rng, block_height: u64) -> Utxo {
// just a random value generated, and also a random `is_block_reward` value.
let random_value = make_pseudo_rng().gen_range(0..(u128::MAX - 1));
let random_value = rng.gen_range(0..(u128::MAX - 1));
let (_, pub_key) = PrivateKey::new(KeyKind::RistrettoSchnorr);
let output = TxOutput::new(
Amount::from_atoms(random_value),
Expand All @@ -663,6 +665,7 @@ pub(crate) mod test {
/// `max_lim_of_utxos` - sets the maximum limit of utxos of a random TxUndo.
/// `max_lim_of_tx_undos` - the maximum limit of TxUndos in the BlockUndo.
pub fn create_rand_block_undo(
rng: &mut impl Rng,
max_lim_of_utxos: u8,
max_lim_of_tx_undos: u8,
block_height: BlockHeight,
Expand All @@ -671,15 +674,15 @@ pub(crate) mod test {

let mut block_undo: Vec<TxUndo> = vec![];

let undo_rng = make_pseudo_rng().gen_range(1..max_lim_of_tx_undos);
let undo_rng = rng.gen_range(1..max_lim_of_tx_undos);
for _ in 0..undo_rng {
let mut tx_undo = vec![];

let utxo_rng = make_pseudo_rng().gen_range(1..max_lim_of_utxos);
let utxo_rng = rng.gen_range(1..max_lim_of_utxos);
for i in 0..utxo_rng {
counter += u64::from(i);

tx_undo.push(create_rand_utxo(counter));
tx_undo.push(create_rand_utxo(rng, counter));
}

block_undo.push(TxUndo::new(tx_undo));
Expand All @@ -689,9 +692,12 @@ pub(crate) mod test {
}

#[cfg(not(loom))]
#[test]
fn undo_test() {
let block_undo0 = create_rand_block_undo(10, 5, BlockHeight::new(1));
#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn undo_test(#[case] seed: Seed) {
let mut rng = make_seedable_rng(seed);
let block_undo0 = create_rand_block_undo(&mut rng, 10, 5, BlockHeight::new(1));
// create id:
let id0: Id<Block> = Id::new(H256::random());

Expand All @@ -710,7 +716,7 @@ pub(crate) mod test {

// insert, remove, and reinsert the next block_undo

let block_undo1 = create_rand_block_undo(5, 10, BlockHeight::new(2));
let block_undo1 = create_rand_block_undo(&mut rng, 5, 10, BlockHeight::new(2));
// create id:
let id1: Id<Block> = Id::new(H256::random());

Expand Down
20 changes: 12 additions & 8 deletions chainstate-storage/src/utxo_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ mod test {
use common::chain::{Destination, OutPoint, OutPointSourceId, OutputPurpose, TxOutput};
use common::primitives::{Amount, BlockHeight, H256};
use crypto::key::{KeyKind, PrivateKey};
use crypto::random::{make_pseudo_rng, Rng};
use crypto::random::Rng;
use rstest::rstest;
use test_utils::random::{make_seedable_rng, Seed};

fn create_utxo(block_height: u64) -> (Utxo, OutPoint) {
fn create_utxo(block_height: u64, output_value: u128) -> (Utxo, OutPoint) {
// just a random value generated, and also a random `is_block_reward` value.
let random_value = make_pseudo_rng().gen_range(0..u128::MAX);
let (_, pub_key) = PrivateKey::new(KeyKind::RistrettoSchnorr);
let output = TxOutput::new(
Amount::from_atoms(random_value),
Amount::from_atoms(output_value),
OutputPurpose::Transfer(Destination::PublicKey(pub_key)),
);
let utxo = Utxo::new(output, true, BlockHeight::new(block_height));
Expand All @@ -95,13 +96,16 @@ mod test {
}

#[cfg(not(loom))]
#[test]
fn db_impl_test() {
#[rstest]
#[trace]
#[case(Seed::from_entropy())]
fn db_impl_test(#[case] seed: Seed) {
let mut rng = make_seedable_rng(seed);
let store = Store::new_empty().expect("should create a store");
let mut db_interface = UtxoDBImpl::new(store);

// utxo checking
let (utxo, outpoint) = create_utxo(1);
let (utxo, outpoint) = create_utxo(1, rng.gen_range(0..u128::MAX));
assert!(db_interface.set_utxo(&outpoint, utxo.clone()).is_ok());
assert_eq!(db_interface.get_utxo(&outpoint), Ok(Some(utxo)));
assert!(db_interface.del_utxo(&outpoint).is_ok());
Expand All @@ -120,7 +124,7 @@ mod test {
);

// undo checking
let undo = create_rand_block_undo(10, 10, BlockHeight::new(10));
let undo = create_rand_block_undo(&mut rng, 10, 10, BlockHeight::new(10));

assert!(db_interface.set_undo_data(block_id, &undo).is_ok());
assert_eq!(db_interface.get_undo_data(block_id), Ok(Some(undo)));
Expand Down
2 changes: 2 additions & 0 deletions chainstate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ serde = { version = "1", features = ["derive"] }

[dev-dependencies]
mockall = "0.11"
rstest = "0.15"
serde_json = "1.0"
static_assertions = "1.1"
test-utils = {path = '../test-utils'}
tokio = "1.19"
2 changes: 2 additions & 0 deletions chainstate/src/detail/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub enum BlockSource {
}

impl Chainstate {
#[allow(dead_code)]
azarovh marked this conversation as resolved.
Show resolved Hide resolved
iljakuklic marked this conversation as resolved.
Show resolved Hide resolved
pub fn wait_for_all_events(&self) {
self.events_controller.wait_for_all_events();
}
Expand Down Expand Up @@ -294,6 +295,7 @@ impl Chainstate {
self.make_db_tx_ro().get_best_block_id()
}

#[allow(dead_code)]
pub fn get_header_from_height(
&self,
height: &BlockHeight,
Expand Down
Loading