Skip to content

Commit

Permalink
Fix tests, v0.19.11
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Jun 7, 2023
1 parent 26d96c3 commit a56757e
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bazuka"
version = "0.19.10"
version = "0.19.11"
authors = ["El Geuse <[email protected]>"]
edition = "2021"

Expand Down
32 changes: 22 additions & 10 deletions src/blockchain/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ mod tests {

for i in 0..5 {
let mut mempool = Mempool::new(Amount(1));
mempool.add_tx(&chain, dummy_tx(&abc, i), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, i), false, 0, None)
.unwrap();

let snapshot = mempool.all().collect::<Vec<_>>();
// Tx is only added if nonce is correct based on its account on the blockchain
Expand All @@ -398,31 +400,41 @@ mod tests {
let other = TxBuilder::new(&Vec::from("DELEGATOR"));
let mut mempool = Mempool::new(Amount(1));

mempool.add_tx(&chain, dummy_tx(&abc, 1), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, 1), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 1);
mempool.add_tx(&chain, dummy_tx(&abc, 2), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, 2), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 2);
mempool.add_tx(&chain, dummy_tx(&abc, 4), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, 4), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 2);
mempool.add_tx(&chain, dummy_tx(&abc, 3), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, 3), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 3);
mempool.add_tx(&chain, dummy_tx(&abc, 4), false, 0).unwrap();
mempool
.add_tx(&chain, dummy_tx(&abc, 4), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 4);

mempool
.add_tx(&chain, dummy_tx(&other, 10), false, 0)
.add_tx(&chain, dummy_tx(&other, 10), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 4);
mempool
.add_tx(&chain, dummy_tx(&other, 1), false, 0)
.add_tx(&chain, dummy_tx(&other, 1), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 5);
mempool
.add_tx(&chain, dummy_tx(&other, 3), false, 0)
.add_tx(&chain, dummy_tx(&other, 3), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 5);
mempool
.add_tx(&chain, dummy_tx(&other, 2), false, 0)
.add_tx(&chain, dummy_tx(&other, 2), false, 0, None)
.unwrap();
assert_eq!(mempool.all().collect::<Vec<_>>().len(), 6);
}
Expand Down
14 changes: 7 additions & 7 deletions src/blockchain/ops/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub fn apply_block<K: KvStore>(
} else {
return Err(BlockchainError::ValidatorProofNotGiven);
}
} else {
// NOTE: THIS ONLY HAPPENS IN TESTS!
curr_pow += 1.;
}
// WARN: Sum will be invalid if fees are not in Ziesha
let fee_sum = Amount(
Expand Down Expand Up @@ -111,17 +114,14 @@ pub fn apply_block<K: KvStore>(
let block_epoch = chain.epoch_slot(block.header.proof_of_stake.timestamp).0;
if block_epoch > tip_epoch {
// New randomness = H(H(tip) | VRF_out)
let new_randomness = if let Some(proof) = block.header.proof_of_stake.proof.clone()
{
let mut preimage: Vec<u8> = chain.get_tip()?.hash().to_vec();
if let Some(proof) = block.header.proof_of_stake.proof.clone() {
if proof.attempt != 0 {
return Err(BlockchainError::RandomnessChangeNotPermitted);
}
let mut preimage: Vec<u8> = chain.get_tip()?.hash().to_vec();
preimage.extend(Into::<Vec<u8>>::into(proof.vrf_output.clone()));
Hasher::hash(&preimage)
} else {
Default::default()
};
}
let new_randomness = Hasher::hash(&preimage);

chain
.database
Expand Down
24 changes: 8 additions & 16 deletions src/blockchain/test/vrf_randomness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ fn test_vrf_randomness_changes() {
)
.unwrap();

let expected = [
"0000000000000000000000000000000000000000000000000000000000000000",
"1fb667e616e3e0778cf6f8f787761c6eb810893dc7e9906ab26c519e688e6b68",
"68f18aa94f2abeed4507b775aa2e5b503dd5b86ddfa842a7fe31343441cc9afa",
"0b6fac0de7b1c6a0f9f9be1775245bebbbc8474bca4cc65f750e8f31855824cf",
"8e5710de01051390bdb7f1bb1c43b54424401fafe80b048a11792ef98c5f85e6",
"95cbc4b5c1f35aaa99ffe166d05445472bf5a3b7643259f2927e1e3329160ee2",
"119b969f72fd06a3d8fec4861bad921089903349ab9ef4e2ff6b0319f9e59390",
"53d1df368d9ad9e7a025d1e6cef49b95747ba2c8ac908b102e6ed9bc29b8035a",
"a69225ad97db281cad80595ec54076c4d752432ca0c8c689e578dfa636fc78b7",
"5262d9d9ec7a04702ea20dcfec40034822953d966ff7654514c293d3fee4d633",
];
for i in 0..100 {
let prev_rand = chain.epoch_randomness().unwrap();
let draft = chain
.draft_block(1700000000 + i * 5, &[], &validator, true)
.unwrap()
.unwrap();
chain.apply_block(&draft).unwrap();
assert_eq!(
hex::encode(chain.epoch_randomness().unwrap()),
expected[(i / 10) as usize]
);
let post_rand = chain.epoch_randomness().unwrap();

if i % 10 == 0 {
assert!(prev_rand != post_rand);
} else {
assert!(prev_rand == post_rand);
}
}
}
Empty file added src/cmp
Empty file.
1 change: 1 addition & 0 deletions src/config/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ pub fn get_test_blockchain_config() -> BlockchainConfig {
conf.chain_start_timestamp = 0;
conf.check_validator = false;
conf.slot_duration = 5;
conf.reward_ratio = 100_000;

conf.genesis.body[1] = get_test_mpn_contract().tx;
conf.genesis.body.drain(2..);
Expand Down
43 changes: 23 additions & 20 deletions src/mpn/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
db: &mut B,
txs: &[MpnDeposit],
new_account_indices: &mut HashMap<MpnAddress, u64>,
check_balance: bool,
) -> Result<(ZkCompressedState, ZkPublicInputs, Vec<DepositTransition>), BlockchainError> {
let mut mirror = db.database().mirror();

Expand Down Expand Up @@ -81,27 +82,29 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
rejected_pub_keys.insert(src_pub);
continue;
} else {
// Check src have enough balance
let src_amount_bal = db.get_balance(src_pub.clone(), tx.payment.amount.token_id)?;
if src_amount_bal < tx.payment.amount.amount {
rejected.push(tx.clone());
rejected_pub_keys.insert(src_pub);
continue;
if check_balance {
// Check src have enough balance
let src_amount_bal = db.get_balance(src_pub.clone(), tx.payment.amount.token_id)?;
if src_amount_bal < tx.payment.amount.amount {
rejected.push(tx.clone());
rejected_pub_keys.insert(src_pub);
continue;
}
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.amount.token_id),
(src_amount_bal - tx.payment.amount.amount).into(),
)])?;
let src_fee_bal = db.get_balance(src_pub.clone(), tx.payment.fee.token_id)?;
if src_fee_bal < tx.payment.fee.amount {
rejected.push(tx.clone());
rejected_pub_keys.insert(src_pub);
continue;
}
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.fee.token_id),
(src_fee_bal - tx.payment.fee.amount).into(),
)])?;
}
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.amount.token_id),
(src_amount_bal - tx.payment.amount.amount).into(),
)])?;
let src_fee_bal = db.get_balance(src_pub.clone(), tx.payment.fee.token_id)?;
if src_fee_bal < tx.payment.fee.amount {
rejected.push(tx.clone());
rejected_pub_keys.insert(src_pub);
continue;
}
isolated.update(&[WriteOp::Put(
crate::db::keys::account_balance(&src_pub, tx.payment.fee.token_id),
(src_fee_bal - tx.payment.fee.amount).into(),
)])?;

let mut updated_acc = MpnAccount {
address: tx.zk_address.0.decompress(),
Expand Down
1 change: 1 addition & 0 deletions src/mpn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ pub fn prepare_works<K: KvStore, B: Blockchain<K>>(
&mut mirror,
&deposits,
&mut new_account_indices,
true,
)?;
log::info!("Made MPN-Deposit block of {} txs.", transitions.len());
for (i, tx) in transitions.iter().enumerate() {
Expand Down
3 changes: 2 additions & 1 deletion src/mpn/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ mod tests {
conf.log4_deposit_batch_size,
&mut db,
&[initial_dep],
&mut new_account_indices
&mut new_account_indices,
false
)
.unwrap()
.2
Expand Down

0 comments on commit a56757e

Please sign in to comment.