mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-21 03:21:08 +03:00
rustfmt
This commit is contained in:
parent
12fe928112
commit
2f38ae1caf
5 changed files with 142 additions and 139 deletions
|
@ -46,7 +46,7 @@ fn test_coinbase_maturity() {
|
||||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||||
|
|
||||||
{
|
{
|
||||||
let chain = chain::Chain::init(
|
let chain = chain::Chain::init(
|
||||||
".grin".to_string(),
|
".grin".to_string(),
|
||||||
Arc::new(NoopAdapter {}),
|
Arc::new(NoopAdapter {}),
|
||||||
genesis_block,
|
genesis_block,
|
||||||
|
@ -146,7 +146,8 @@ fn test_coinbase_maturity() {
|
||||||
|
|
||||||
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
||||||
let reward = libtx::reward::output(&keychain, &key_id1, 0, false).unwrap();
|
let reward = libtx::reward::output(&keychain, &key_id1, 0, false).unwrap();
|
||||||
let mut block = core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
let mut block =
|
||||||
|
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
||||||
|
|
||||||
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
||||||
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
||||||
|
@ -227,7 +228,8 @@ fn test_coinbase_maturity() {
|
||||||
let reward = libtx::reward::output(&keychain, &pk, 0, false).unwrap();
|
let reward = libtx::reward::output(&keychain, &pk, 0, false).unwrap();
|
||||||
let mut block =
|
let mut block =
|
||||||
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
core::core::Block::new(&prev, vec![], Difficulty::min(), reward).unwrap();
|
||||||
let next_header_info = consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
let next_header_info =
|
||||||
|
consensus::next_difficulty(1, chain.difficulty_iter().unwrap());
|
||||||
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
block.header.timestamp = prev.timestamp + Duration::seconds(60);
|
||||||
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
block.header.pow.secondary_scaling = next_header_info.secondary_scaling;
|
||||||
|
|
||||||
|
|
|
@ -42,84 +42,85 @@ fn test_transaction_pool_block_building() {
|
||||||
|
|
||||||
// Initialize the chain/txhashset with an initial block
|
// Initialize the chain/txhashset with an initial block
|
||||||
// so we have a non-empty UTXO set.
|
// so we have a non-empty UTXO set.
|
||||||
let add_block = |prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
let add_block =
|
||||||
let height = prev_header.height + 1;
|
|prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||||
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
let height = prev_header.height + 1;
|
||||||
let fee = txs.iter().map(|x| x.fee()).sum();
|
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
||||||
let reward = libtx::reward::output(&keychain, &key_id, fee, false).unwrap();
|
let fee = txs.iter().map(|x| x.fee()).sum();
|
||||||
let mut block = Block::new(&prev_header, txs, Difficulty::min(), reward).unwrap();
|
let reward = libtx::reward::output(&keychain, &key_id, fee, false).unwrap();
|
||||||
|
let mut block = Block::new(&prev_header, txs, Difficulty::min(), reward).unwrap();
|
||||||
|
|
||||||
// Set the prev_root to the prev hash for testing purposes (no MMR to obtain a root from).
|
// Set the prev_root to the prev hash for testing purposes (no MMR to obtain a root from).
|
||||||
block.header.prev_root = prev_header.hash();
|
block.header.prev_root = prev_header.hash();
|
||||||
|
|
||||||
chain.update_db_for_block(&block);
|
chain.update_db_for_block(&block);
|
||||||
block
|
block
|
||||||
};
|
|
||||||
|
|
||||||
let block = add_block(BlockHeader::default(), vec![], &mut chain);
|
|
||||||
let header = block.header;
|
|
||||||
|
|
||||||
// Now create tx to spend that first coinbase (now matured).
|
|
||||||
// Provides us with some useful outputs to test with.
|
|
||||||
let initial_tx =
|
|
||||||
test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
|
||||||
|
|
||||||
// Mine that initial tx so we can spend it with multiple txs
|
|
||||||
let block = add_block(header, vec![initial_tx], &mut chain);
|
|
||||||
let header = block.header;
|
|
||||||
|
|
||||||
// Initialize a new pool with our chain adapter.
|
|
||||||
let pool = RwLock::new(test_setup(Arc::new(chain.clone()), verifier_cache));
|
|
||||||
|
|
||||||
let root_tx_1 = test_transaction(&keychain, vec![10, 20], vec![24]);
|
|
||||||
let root_tx_2 = test_transaction(&keychain, vec![30], vec![28]);
|
|
||||||
let root_tx_3 = test_transaction(&keychain, vec![40], vec![38]);
|
|
||||||
|
|
||||||
let child_tx_1 = test_transaction(&keychain, vec![24], vec![22]);
|
|
||||||
let child_tx_2 = test_transaction(&keychain, vec![38], vec![32]);
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut write_pool = pool.write();
|
|
||||||
|
|
||||||
// Add the three root txs to the pool.
|
|
||||||
write_pool
|
|
||||||
.add_to_pool(test_source(), root_tx_1, false, &header)
|
|
||||||
.unwrap();
|
|
||||||
write_pool
|
|
||||||
.add_to_pool(test_source(), root_tx_2, false, &header)
|
|
||||||
.unwrap();
|
|
||||||
write_pool
|
|
||||||
.add_to_pool(test_source(), root_tx_3, false, &header)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Now add the two child txs to the pool.
|
|
||||||
write_pool
|
|
||||||
.add_to_pool(test_source(), child_tx_1.clone(), false, &header)
|
|
||||||
.unwrap();
|
|
||||||
write_pool
|
|
||||||
.add_to_pool(test_source(), child_tx_2.clone(), false, &header)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(write_pool.total_size(), 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
let txs = {
|
|
||||||
let read_pool = pool.read();
|
|
||||||
read_pool.prepare_mineable_transactions().unwrap()
|
|
||||||
};
|
};
|
||||||
// children should have been aggregated into parents
|
|
||||||
assert_eq!(txs.len(), 3);
|
|
||||||
|
|
||||||
let block = add_block(header, txs, &mut chain);
|
let block = add_block(BlockHeader::default(), vec![], &mut chain);
|
||||||
|
let header = block.header;
|
||||||
|
|
||||||
// Now reconcile the transaction pool with the new block
|
// Now create tx to spend that first coinbase (now matured).
|
||||||
// and check the resulting contents of the pool are what we expect.
|
// Provides us with some useful outputs to test with.
|
||||||
{
|
let initial_tx =
|
||||||
let mut write_pool = pool.write();
|
test_transaction_spending_coinbase(&keychain, &header, vec![10, 20, 30, 40]);
|
||||||
write_pool.reconcile_block(&block).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(write_pool.total_size(), 0);
|
// Mine that initial tx so we can spend it with multiple txs
|
||||||
}
|
let block = add_block(header, vec![initial_tx], &mut chain);
|
||||||
|
let header = block.header;
|
||||||
|
|
||||||
|
// Initialize a new pool with our chain adapter.
|
||||||
|
let pool = RwLock::new(test_setup(Arc::new(chain.clone()), verifier_cache));
|
||||||
|
|
||||||
|
let root_tx_1 = test_transaction(&keychain, vec![10, 20], vec![24]);
|
||||||
|
let root_tx_2 = test_transaction(&keychain, vec![30], vec![28]);
|
||||||
|
let root_tx_3 = test_transaction(&keychain, vec![40], vec![38]);
|
||||||
|
|
||||||
|
let child_tx_1 = test_transaction(&keychain, vec![24], vec![22]);
|
||||||
|
let child_tx_2 = test_transaction(&keychain, vec![38], vec![32]);
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut write_pool = pool.write();
|
||||||
|
|
||||||
|
// Add the three root txs to the pool.
|
||||||
|
write_pool
|
||||||
|
.add_to_pool(test_source(), root_tx_1, false, &header)
|
||||||
|
.unwrap();
|
||||||
|
write_pool
|
||||||
|
.add_to_pool(test_source(), root_tx_2, false, &header)
|
||||||
|
.unwrap();
|
||||||
|
write_pool
|
||||||
|
.add_to_pool(test_source(), root_tx_3, false, &header)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Now add the two child txs to the pool.
|
||||||
|
write_pool
|
||||||
|
.add_to_pool(test_source(), child_tx_1.clone(), false, &header)
|
||||||
|
.unwrap();
|
||||||
|
write_pool
|
||||||
|
.add_to_pool(test_source(), child_tx_2.clone(), false, &header)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(write_pool.total_size(), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
let txs = {
|
||||||
|
let read_pool = pool.read();
|
||||||
|
read_pool.prepare_mineable_transactions().unwrap()
|
||||||
|
};
|
||||||
|
// children should have been aggregated into parents
|
||||||
|
assert_eq!(txs.len(), 3);
|
||||||
|
|
||||||
|
let block = add_block(header, txs, &mut chain);
|
||||||
|
|
||||||
|
// Now reconcile the transaction pool with the new block
|
||||||
|
// and check the resulting contents of the pool are what we expect.
|
||||||
|
{
|
||||||
|
let mut write_pool = pool.write();
|
||||||
|
write_pool.reconcile_block(&block).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(write_pool.total_size(), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Cleanup db directory
|
// Cleanup db directory
|
||||||
clean_output_dir(db_root.clone());
|
clean_output_dir(db_root.clone());
|
||||||
|
|
|
@ -46,32 +46,34 @@ fn test_block_building_max_weight() {
|
||||||
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
||||||
|
|
||||||
// Convenient was to add a new block to the chain.
|
// Convenient was to add a new block to the chain.
|
||||||
let add_block = |prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
let add_block =
|
||||||
let height = prev_header.height + 1;
|
|prev_header: BlockHeader, txs: Vec<Transaction>, chain: &mut ChainAdapter| {
|
||||||
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
let height = prev_header.height + 1;
|
||||||
let fee = txs.iter().map(|x| x.fee()).sum();
|
let key_id = ExtKeychain::derive_key_id(1, height as u32, 0, 0, 0);
|
||||||
let reward = libtx::reward::output(&keychain, &key_id, fee, false).unwrap();
|
let fee = txs.iter().map(|x| x.fee()).sum();
|
||||||
let mut block = Block::new(&prev_header, txs, Difficulty::min(), reward).unwrap();
|
let reward = libtx::reward::output(&keychain, &key_id, fee, false).unwrap();
|
||||||
|
let mut block = Block::new(&prev_header, txs, Difficulty::min(), reward).unwrap();
|
||||||
|
|
||||||
// Set the prev_root to the prev hash for testing purposes (no MMR to obtain a root from).
|
// Set the prev_root to the prev hash for testing purposes (no MMR to obtain a root from).
|
||||||
block.header.prev_root = prev_header.hash();
|
block.header.prev_root = prev_header.hash();
|
||||||
|
|
||||||
chain.update_db_for_block(&block);
|
chain.update_db_for_block(&block);
|
||||||
block
|
block
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initialize the chain/txhashset with an initial block
|
// Initialize the chain/txhashset with an initial block
|
||||||
// so we have a non-empty UTXO set.
|
// so we have a non-empty UTXO set.
|
||||||
let block = add_block(BlockHeader::default(), vec![], &mut chain);
|
let block = add_block(BlockHeader::default(), vec![], &mut chain);
|
||||||
let header = block.header;
|
let header = block.header;
|
||||||
|
|
||||||
// Now create tx to spend that first coinbase (now matured).
|
// Now create tx to spend that first coinbase (now matured).
|
||||||
// Provides us with some useful outputs to test with.
|
// Provides us with some useful outputs to test with.
|
||||||
let initial_tx = test_transaction_spending_coinbase(&keychain, &header, vec![100, 200, 300]);
|
let initial_tx =
|
||||||
|
test_transaction_spending_coinbase(&keychain, &header, vec![100, 200, 300]);
|
||||||
|
|
||||||
// Mine that initial tx so we can spend it with multiple txs
|
// Mine that initial tx so we can spend it with multiple txs
|
||||||
let block = add_block(header, vec![initial_tx], &mut chain);
|
let block = add_block(header, vec![initial_tx], &mut chain);
|
||||||
let header = block.header;
|
let header = block.header;
|
||||||
|
|
||||||
// Initialize a new pool with our chain adapter.
|
// Initialize a new pool with our chain adapter.
|
||||||
let pool = RwLock::new(test_setup(Arc::new(chain.clone()), verifier_cache));
|
let pool = RwLock::new(test_setup(Arc::new(chain.clone()), verifier_cache));
|
||||||
|
@ -86,61 +88,61 @@ fn test_block_building_max_weight() {
|
||||||
test_transaction(&keychain, vec![290], vec![280, 4]),
|
test_transaction(&keychain, vec![290], vec![280, 4]),
|
||||||
];
|
];
|
||||||
|
|
||||||
// Populate our txpool with the txs.
|
// Populate our txpool with the txs.
|
||||||
{
|
{
|
||||||
let mut write_pool = pool.write();
|
let mut write_pool = pool.write();
|
||||||
for tx in txs {
|
for tx in txs {
|
||||||
write_pool
|
write_pool
|
||||||
.add_to_pool(test_source(), tx, false, &header)
|
.add_to_pool(test_source(), tx, false, &header)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check we added them all to the txpool successfully.
|
// Check we added them all to the txpool successfully.
|
||||||
assert_eq!(pool.read().total_size(), 5);
|
assert_eq!(pool.read().total_size(), 5);
|
||||||
|
|
||||||
// Prepare some "mineable txs" from the txpool.
|
// Prepare some "mineable txs" from the txpool.
|
||||||
// Note: We cannot fit all the txs from the txpool into a block.
|
// Note: We cannot fit all the txs from the txpool into a block.
|
||||||
let txs = pool.read().prepare_mineable_transactions().unwrap();
|
let txs = pool.read().prepare_mineable_transactions().unwrap();
|
||||||
|
|
||||||
// Check resulting tx aggregation is what we expect.
|
// Check resulting tx aggregation is what we expect.
|
||||||
// We expect to produce 2 aggregated txs based on txpool contents.
|
// We expect to produce 2 aggregated txs based on txpool contents.
|
||||||
assert_eq!(txs.len(), 2);
|
assert_eq!(txs.len(), 2);
|
||||||
|
|
||||||
// Check the tx we built is the aggregation of the correct set of underlying txs.
|
// Check the tx we built is the aggregation of the correct set of underlying txs.
|
||||||
// We included 4 out of the 5 txs here.
|
// We included 4 out of the 5 txs here.
|
||||||
assert_eq!(txs[0].kernels().len(), 1);
|
assert_eq!(txs[0].kernels().len(), 1);
|
||||||
assert_eq!(txs[1].kernels().len(), 2);
|
assert_eq!(txs[1].kernels().len(), 2);
|
||||||
|
|
||||||
// Check our weights after aggregation.
|
// Check our weights after aggregation.
|
||||||
assert_eq!(txs[0].inputs().len(), 1);
|
assert_eq!(txs[0].inputs().len(), 1);
|
||||||
assert_eq!(txs[0].outputs().len(), 1);
|
assert_eq!(txs[0].outputs().len(), 1);
|
||||||
assert_eq!(txs[0].kernels().len(), 1);
|
assert_eq!(txs[0].kernels().len(), 1);
|
||||||
assert_eq!(txs[0].tx_weight_as_block(), 25);
|
assert_eq!(txs[0].tx_weight_as_block(), 25);
|
||||||
|
|
||||||
assert_eq!(txs[1].inputs().len(), 1);
|
assert_eq!(txs[1].inputs().len(), 1);
|
||||||
assert_eq!(txs[1].outputs().len(), 3);
|
assert_eq!(txs[1].outputs().len(), 3);
|
||||||
assert_eq!(txs[1].kernels().len(), 2);
|
assert_eq!(txs[1].kernels().len(), 2);
|
||||||
assert_eq!(txs[1].tx_weight_as_block(), 70);
|
assert_eq!(txs[1].tx_weight_as_block(), 70);
|
||||||
|
|
||||||
let block = add_block(header, txs, &mut chain);
|
let block = add_block(header, txs, &mut chain);
|
||||||
|
|
||||||
// Check contents of the block itself (including coinbase reward).
|
// Check contents of the block itself (including coinbase reward).
|
||||||
assert_eq!(block.inputs().len(), 2);
|
assert_eq!(block.inputs().len(), 2);
|
||||||
assert_eq!(block.outputs().len(), 5);
|
assert_eq!(block.outputs().len(), 5);
|
||||||
assert_eq!(block.kernels().len(), 4);
|
assert_eq!(block.kernels().len(), 4);
|
||||||
|
|
||||||
// Now reconcile the transaction pool with the new block
|
// Now reconcile the transaction pool with the new block
|
||||||
// and check the resulting contents of the pool are what we expect.
|
// and check the resulting contents of the pool are what we expect.
|
||||||
{
|
{
|
||||||
let mut write_pool = pool.write();
|
let mut write_pool = pool.write();
|
||||||
write_pool.reconcile_block(&block).unwrap();
|
write_pool.reconcile_block(&block).unwrap();
|
||||||
|
|
||||||
// We should still have 2 tx in the pool after accepting the new block.
|
// We should still have 2 tx in the pool after accepting the new block.
|
||||||
// This one exceeded the max block weight when building the block so
|
// This one exceeded the max block weight when building the block so
|
||||||
// remained in the txpool.
|
// remained in the txpool.
|
||||||
assert_eq!(write_pool.total_size(), 2);
|
assert_eq!(write_pool.total_size(), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Cleanup db directory
|
// Cleanup db directory
|
||||||
clean_output_dir(db_root.clone());
|
clean_output_dir(db_root.clone());
|
||||||
|
|
|
@ -177,7 +177,6 @@ fn test_transaction_pool_block_reconciliation() {
|
||||||
|
|
||||||
// And reconcile the pool with this latest block.
|
// And reconcile the pool with this latest block.
|
||||||
{
|
{
|
||||||
|
|
||||||
let mut write_pool = pool.write();
|
let mut write_pool = pool.write();
|
||||||
write_pool.reconcile_block(&block).unwrap();
|
write_pool.reconcile_block(&block).unwrap();
|
||||||
|
|
||||||
|
|
|
@ -331,8 +331,7 @@ impl<T: PMMRable> PMMRBackend<T> {
|
||||||
pos as u64 - shift
|
pos as u64 - shift
|
||||||
});
|
});
|
||||||
|
|
||||||
self.hash_file
|
self.hash_file.save_prune(&pos_to_rm)?;
|
||||||
.save_prune(&pos_to_rm)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Save compact copy of the data file, skipping removed leaves.
|
// 2. Save compact copy of the data file, skipping removed leaves.
|
||||||
|
|
Loading…
Reference in a new issue