Skip to content
Snippets Groups Projects
Commit 56ad07ae authored by Pascal Engélibert's avatar Pascal Engélibert :bicyclist:
Browse files

[tests] blockchain: test block verification

parent 0d80c5b9
No related branches found
No related tags found
1 merge request!117WIP: Resolve "Add block checking (checking all rules of the unit protocol)"
...@@ -144,3 +144,101 @@ pub fn verify_block_validity<W: WebOfTrust>( ...@@ -144,3 +144,101 @@ pub fn verify_block_validity<W: WebOfTrust>(
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use super::*;
use dubp_documents_tests_tools::mocks::*;
use dup_crypto_tests_tools::mocks::*;
use durs_blockchain_dal::entities::block::DALBlock;
use durs_blockchain_dal::entities::identity::*;
#[test]
fn test_verify_blocks() {
let blocks_dbs = BlocksV10DBs::open(None);
let wot_dbs = WotsV10DBs::open(None);
let wot_index: HashMap<PubKey, NodeId> = HashMap::new();
let idty_a = identity::gen_mock_idty(pubkey('A'), BlockId(0));
let pubkey_a = idty_a.issuers()[0];
let idty_b = identity::gen_mock_idty(pubkey('B'), BlockId(0));
let pubkey_b = idty_b.issuers()[0];
let mut blocks = gen_empty_timed_blocks(2, 300);
// Valid block 0
blocks[0].issuers_frame = 1;
blocks[0].issuers.push(pubkey_a);
blocks[0].identities.push(idty_a.clone());
blocks[0].identities.push(idty_b.clone());
verify_genesis_block_validity(&blocks[0]).expect("Fail to valid genesis block");
blocks_dbs
.blockchain_db
.write(|db| {
db.insert(
blocks[0].number,
DALBlock {
block: blocks[0].clone(),
expire_certs: None,
},
);
})
.expect("Fail write to blockchain db");
wot_dbs
.identities_db
.write(|db| {
db.insert(
pubkey_a,
DALIdentity {
hash: String::new(),
state: DALIdentityState::Member(vec![]),
joined_on: blocks[0].blockstamp(),
expired_on: None,
revoked_on: None,
idty_doc: idty_a,
wot_id: NodeId(0),
ms_created_block_id: blocks[0].number,
ms_chainable_on: vec![0],
cert_chainable_on: vec![0],
},
);
db.insert(
pubkey_b,
DALIdentity {
hash: String::new(),
state: DALIdentityState::Member(vec![]),
joined_on: blocks[0].blockstamp(),
expired_on: None,
revoked_on: None,
idty_doc: idty_b,
wot_id: NodeId(1),
ms_created_block_id: blocks[0].number,
ms_chainable_on: vec![0],
cert_chainable_on: vec![0],
},
);
})
.expect("Fail write to idty db");
// Valid block 1
blocks[1].issuers_count = 1;
blocks[1].issuers_frame = 1;
blocks[1].issuers_frame_var = 5;
blocks[1].issuers.push(pubkey_b);
blocks[1].previous_issuer = Some(pubkey_a);
verify_block_validity(
&blocks[1],
&blocks_dbs.blockchain_db,
&wot_dbs.identities_db,
&wot_dbs.certs_db,
&wot_index,
&wot_dbs.wot_db,
)
.expect("Fail to valid block");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment