2018-05-30 23:57:13 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
extern crate blake2_rfc as blake2;
|
|
|
|
extern crate grin_chain as chain;
|
|
|
|
extern crate grin_core as core;
|
|
|
|
extern crate grin_keychain as keychain;
|
|
|
|
extern crate grin_pool as pool;
|
|
|
|
extern crate grin_util as util;
|
|
|
|
extern crate grin_wallet as wallet;
|
|
|
|
|
2018-07-30 11:33:28 +03:00
|
|
|
extern crate chrono;
|
2018-08-20 01:50:43 +03:00
|
|
|
extern crate rand;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
pub mod common;
|
|
|
|
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
use core::core::{Block, BlockHeader};
|
|
|
|
|
2018-08-20 01:50:43 +03:00
|
|
|
use chain::txhashset;
|
2018-05-30 23:57:13 +03:00
|
|
|
use chain::types::Tip;
|
2018-08-20 16:48:05 +03:00
|
|
|
use core::core::hash::Hashed;
|
2018-05-30 23:57:13 +03:00
|
|
|
use core::core::target::Difficulty;
|
2018-08-30 17:44:34 +03:00
|
|
|
use core::core::verifier_cache::LruVerifierCache;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2018-06-08 08:21:54 +03:00
|
|
|
use keychain::{ExtKeychain, Keychain};
|
2018-05-30 23:57:13 +03:00
|
|
|
use wallet::libtx;
|
|
|
|
|
|
|
|
use common::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transaction_pool_block_building() {
|
2018-08-20 01:50:43 +03:00
|
|
|
util::init_test_logger();
|
2018-06-08 08:21:54 +03:00
|
|
|
let keychain: ExtKeychain = Keychain::from_random_seed().unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
let db_root = ".grin_block_building".to_string();
|
|
|
|
clean_output_dir(db_root.clone());
|
|
|
|
let chain = ChainAdapter::init(db_root.clone()).unwrap();
|
|
|
|
|
2018-08-30 17:44:34 +03:00
|
|
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
// Initialize the chain/txhashset with an initial block
|
|
|
|
// so we have a non-empty UTXO set.
|
2018-08-20 01:50:43 +03:00
|
|
|
let add_block = |height, txs| {
|
2018-05-30 23:57:13 +03:00
|
|
|
let key_id = keychain.derive_key_id(height as u32).unwrap();
|
|
|
|
let reward = libtx::reward::output(&keychain, &key_id, 0, height).unwrap();
|
2018-08-30 17:44:34 +03:00
|
|
|
let mut block =
|
|
|
|
Block::new(&BlockHeader::default(), txs, Difficulty::one(), reward).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
let mut txhashset = chain.txhashset.write().unwrap();
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut batch = chain.store.batch().unwrap();
|
|
|
|
txhashset::extending(&mut txhashset, &mut batch, |extension| {
|
2018-08-20 16:48:05 +03:00
|
|
|
extension.apply_block(&block)?;
|
|
|
|
|
|
|
|
// Now set the roots and sizes as necessary on the block header.
|
|
|
|
let roots = extension.roots();
|
|
|
|
block.header.output_root = roots.output_root;
|
|
|
|
block.header.range_proof_root = roots.rproof_root;
|
|
|
|
block.header.kernel_root = roots.kernel_root;
|
|
|
|
let sizes = extension.sizes();
|
|
|
|
block.header.output_mmr_size = sizes.0;
|
|
|
|
block.header.kernel_mmr_size = sizes.2;
|
|
|
|
|
|
|
|
Ok(())
|
2018-06-22 11:08:06 +03:00
|
|
|
}).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
let tip = Tip::from_block(&block.header);
|
2018-06-22 11:08:06 +03:00
|
|
|
batch.save_block_header(&block.header).unwrap();
|
|
|
|
batch.save_head(&tip).unwrap();
|
|
|
|
batch.commit().unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
block.header
|
|
|
|
};
|
2018-08-20 01:50:43 +03:00
|
|
|
let header = add_block(1, vec![]);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
// Initialize a new pool with our chain adapter.
|
2018-08-30 17:44:34 +03:00
|
|
|
let pool = RwLock::new(test_setup(Arc::new(chain.clone()), verifier_cache));
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
2018-08-20 01:50:43 +03:00
|
|
|
// Mine that initial tx so we can spend it with multiple txs
|
|
|
|
let header = add_block(2, vec![initial_tx]);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
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().unwrap();
|
|
|
|
|
|
|
|
// Add the three root txs to the pool.
|
|
|
|
write_pool
|
2018-08-20 16:48:05 +03:00
|
|
|
.add_to_pool(test_source(), root_tx_1, false, &header.hash())
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
|
|
|
write_pool
|
2018-08-20 16:48:05 +03:00
|
|
|
.add_to_pool(test_source(), root_tx_2, false, &header.hash())
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
|
|
|
write_pool
|
2018-08-20 16:48:05 +03:00
|
|
|
.add_to_pool(test_source(), root_tx_3, false, &header.hash())
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Now add the two child txs to the pool.
|
|
|
|
write_pool
|
2018-08-20 16:48:05 +03:00
|
|
|
.add_to_pool(test_source(), child_tx_1.clone(), false, &header.hash())
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
|
|
|
write_pool
|
2018-08-20 16:48:05 +03:00
|
|
|
.add_to_pool(test_source(), child_tx_2.clone(), false, &header.hash())
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2018-08-20 01:50:43 +03:00
|
|
|
assert_eq!(write_pool.total_size(), 5);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let txs = {
|
|
|
|
let read_pool = pool.read().unwrap();
|
2018-08-20 01:50:43 +03:00
|
|
|
read_pool.prepare_mineable_transactions()
|
2018-05-30 23:57:13 +03:00
|
|
|
};
|
2018-08-20 01:50:43 +03:00
|
|
|
// children should have been aggregated into parents
|
|
|
|
assert_eq!(txs.len(), 3);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2018-08-20 16:48:05 +03:00
|
|
|
let mut block = {
|
2018-05-30 23:57:13 +03:00
|
|
|
let key_id = keychain.derive_key_id(2).unwrap();
|
|
|
|
let fees = txs.iter().map(|tx| tx.fee()).sum();
|
|
|
|
let reward = libtx::reward::output(&keychain, &key_id, fees, 0).unwrap();
|
|
|
|
Block::new(&header, txs, Difficulty::one(), reward)
|
|
|
|
}.unwrap();
|
|
|
|
|
|
|
|
{
|
2018-06-22 11:08:06 +03:00
|
|
|
let mut batch = chain.store.batch().unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
let mut txhashset = chain.txhashset.write().unwrap();
|
2018-06-22 11:08:06 +03:00
|
|
|
txhashset::extending(&mut txhashset, &mut batch, |extension| {
|
2018-05-30 23:57:13 +03:00
|
|
|
extension.apply_block(&block)?;
|
2018-08-20 16:48:05 +03:00
|
|
|
|
|
|
|
// Now set the roots and sizes as necessary on the block header.
|
|
|
|
let roots = extension.roots();
|
|
|
|
block.header.output_root = roots.output_root;
|
|
|
|
block.header.range_proof_root = roots.rproof_root;
|
|
|
|
block.header.kernel_root = roots.kernel_root;
|
|
|
|
let sizes = extension.sizes();
|
|
|
|
block.header.output_mmr_size = sizes.0;
|
|
|
|
block.header.kernel_mmr_size = sizes.2;
|
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
Ok(())
|
|
|
|
}).unwrap();
|
2018-08-20 16:48:05 +03:00
|
|
|
|
|
|
|
let tip = Tip::from_block(&block.header);
|
|
|
|
batch.save_block_header(&block.header).unwrap();
|
|
|
|
batch.save_head(&tip).unwrap();
|
2018-06-22 11:08:06 +03:00
|
|
|
batch.commit().unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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().unwrap();
|
|
|
|
write_pool.reconcile_block(&block).unwrap();
|
|
|
|
|
2018-08-20 01:50:43 +03:00
|
|
|
assert_eq!(write_pool.total_size(), 0);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
}
|