2020-01-20 14:40:58 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2018-05-30 23:57:13 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
pub mod common;
|
|
|
|
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::core::core::verifier_cache::LruVerifierCache;
|
2020-06-07 11:26:08 +03:00
|
|
|
use self::core::core::{transaction, Weighting};
|
|
|
|
use self::core::global;
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::keychain::{ExtKeychain, Keychain};
|
2019-07-04 13:56:42 +03:00
|
|
|
use self::pool::TxSource;
|
2018-12-08 02:59:40 +03:00
|
|
|
use self::util::RwLock;
|
|
|
|
use crate::common::*;
|
|
|
|
use grin_core as core;
|
|
|
|
use grin_keychain as keychain;
|
2019-07-04 13:56:42 +03:00
|
|
|
use grin_pool as pool;
|
2018-12-08 02:59:40 +03:00
|
|
|
use grin_util as util;
|
2018-10-20 03:13:07 +03:00
|
|
|
use std::sync::Arc;
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
/// Test we can add some txs to the pool (both stempool and txpool).
|
|
|
|
#[test]
|
|
|
|
fn test_the_transaction_pool() {
|
2020-06-07 11:26:08 +03:00
|
|
|
util::init_test_logger();
|
|
|
|
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
|
2018-12-29 01:46:21 +03:00
|
|
|
let keychain: ExtKeychain = Keychain::from_random_seed(false).unwrap();
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
let db_root = "target/.transaction_pool";
|
|
|
|
clean_output_dir(db_root.into());
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
let genesis = genesis_block(&keychain);
|
|
|
|
let chain = Arc::new(init_chain(db_root, genesis));
|
2018-08-30 17:44:34 +03:00
|
|
|
let verifier_cache = Arc::new(RwLock::new(LruVerifierCache::new()));
|
|
|
|
|
2018-09-24 11:24:10 +03:00
|
|
|
// Initialize a new pool with our chain adapter.
|
2020-06-07 11:26:08 +03:00
|
|
|
let mut pool = init_transaction_pool(
|
|
|
|
Arc::new(ChainAdapter {
|
|
|
|
chain: chain.clone(),
|
|
|
|
}),
|
|
|
|
verifier_cache.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
add_some_blocks(&chain, 3, &keychain);
|
|
|
|
let header = chain.head_header().unwrap();
|
|
|
|
|
|
|
|
let header_1 = chain.get_header_by_height(1).unwrap();
|
|
|
|
let initial_tx = test_transaction_spending_coinbase(
|
|
|
|
&keychain,
|
|
|
|
&header_1,
|
|
|
|
vec![500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400],
|
|
|
|
);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
// Add this tx to the pool (stem=false, direct to txpool).
|
|
|
|
{
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), initial_tx, false, &header)
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 1);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:27:46 +03:00
|
|
|
// Test adding a tx that "double spends" an output currently spent by a tx
|
|
|
|
// already in the txpool. In this case we attempt to spend the original coinbase twice.
|
|
|
|
{
|
|
|
|
let tx = test_transaction_spending_coinbase(&keychain, &header, vec![501]);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool.add_to_pool(test_source(), tx, false, &header).is_err());
|
2018-12-05 13:27:46 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 23:57:13 +03:00
|
|
|
// tx1 spends some outputs from the initial test tx.
|
|
|
|
let tx1 = test_transaction(&keychain, vec![500, 600], vec![499, 599]);
|
|
|
|
// tx2 spends some outputs from both tx1 and the initial test tx.
|
|
|
|
let tx2 = test_transaction(&keychain, vec![499, 700], vec![498]);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check we have a single initial tx in the pool.
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 1);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
2019-01-08 02:09:04 +03:00
|
|
|
// First, add a simple tx directly to the txpool (stem = false).
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx1.clone(), false, &header)
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 2);
|
2018-05-30 23:57:13 +03:00
|
|
|
|
|
|
|
// Add another tx spending outputs from the previous tx.
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx2.clone(), false, &header)
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 3);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test adding the exact same tx multiple times (same kernel signature).
|
2019-01-08 02:09:04 +03:00
|
|
|
// This will fail for stem=false during tx aggregation due to duplicate
|
|
|
|
// outputs and duplicate kernels.
|
2018-05-30 23:57:13 +03:00
|
|
|
{
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool
|
2019-01-08 02:09:04 +03:00
|
|
|
.add_to_pool(test_source(), tx1.clone(), false, &header)
|
2018-12-08 02:59:40 +03:00
|
|
|
.is_err());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:27:46 +03:00
|
|
|
// Test adding a duplicate tx with the same input and outputs.
|
|
|
|
// Note: not the *same* tx, just same underlying inputs/outputs.
|
2018-05-30 23:57:13 +03:00
|
|
|
{
|
|
|
|
let tx1a = test_transaction(&keychain, vec![500, 600], vec![499, 599]);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool
|
2019-01-08 02:09:04 +03:00
|
|
|
.add_to_pool(test_source(), tx1a, false, &header)
|
2018-12-08 02:59:40 +03:00
|
|
|
.is_err());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test adding a tx attempting to spend a non-existent output.
|
|
|
|
{
|
|
|
|
let bad_tx = test_transaction(&keychain, vec![10_001], vec![10_000]);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool
|
2019-01-08 02:09:04 +03:00
|
|
|
.add_to_pool(test_source(), bad_tx, false, &header)
|
2018-12-08 02:59:40 +03:00
|
|
|
.is_err());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test adding a tx that would result in a duplicate output (conflicts with
|
|
|
|
// output from tx2). For reasons of security all outputs in the UTXO set must
|
|
|
|
// be unique. Otherwise spending one will almost certainly cause the other
|
|
|
|
// to be immediately stolen via a "replay" tx.
|
|
|
|
{
|
|
|
|
let tx = test_transaction(&keychain, vec![900], vec![498]);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool.add_to_pool(test_source(), tx, false, &header).is_err());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm the tx pool correctly identifies an invalid tx (already spent).
|
|
|
|
{
|
|
|
|
let tx3 = test_transaction(&keychain, vec![500], vec![497]);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool
|
2019-01-08 02:09:04 +03:00
|
|
|
.add_to_pool(test_source(), tx3, false, &header)
|
2018-12-08 02:59:40 +03:00
|
|
|
.is_err());
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 3);
|
2019-01-08 02:09:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now add a couple of txs to the stempool (stem = true).
|
|
|
|
{
|
|
|
|
let tx = test_transaction(&keychain, vec![599], vec![598]);
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx, true, &header).unwrap();
|
2019-01-08 02:09:04 +03:00
|
|
|
let tx2 = test_transaction(&keychain, vec![598], vec![597]);
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx2, true, &header).unwrap();
|
|
|
|
assert_eq!(pool.total_size(), 3);
|
|
|
|
assert_eq!(pool.stempool.size(), 2);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check we can take some entries from the stempool and "fluff" them into the
|
|
|
|
// txpool. This also exercises multi-kernel txs.
|
|
|
|
{
|
2020-06-07 11:26:08 +03:00
|
|
|
let agg_tx = pool.stempool.all_transactions_aggregate().unwrap().unwrap();
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(agg_tx.kernels().len(), 2);
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), agg_tx, false, &header)
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 4);
|
|
|
|
assert!(pool.stempool.is_empty());
|
2019-01-08 02:09:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adding a duplicate tx to the stempool will result in it being fluffed.
|
|
|
|
// This handles the case of the stem path having a cycle in it.
|
|
|
|
{
|
|
|
|
let tx = test_transaction(&keychain, vec![597], vec![596]);
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx.clone(), true, &header)
|
2019-01-08 02:09:04 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 4);
|
2020-06-10 18:38:29 +03:00
|
|
|
assert_eq!(pool.txpool.size(), 4);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.stempool.size(), 1);
|
2019-01-08 02:09:04 +03:00
|
|
|
|
|
|
|
// Duplicate stem tx so fluff, adding it to txpool and removing it from stempool.
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), tx.clone(), true, &header)
|
2019-01-08 02:09:04 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 5);
|
2020-06-10 18:38:29 +03:00
|
|
|
assert_eq!(pool.txpool.size(), 5);
|
2020-06-07 11:26:08 +03:00
|
|
|
assert!(pool.stempool.is_empty());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now check we can correctly deaggregate a multi-kernel tx based on current
|
|
|
|
// contents of the txpool.
|
|
|
|
// We will do this be adding a new tx to the pool
|
|
|
|
// that is a superset of a tx already in the pool.
|
|
|
|
{
|
|
|
|
let tx4 = test_transaction(&keychain, vec![800], vec![799]);
|
|
|
|
// tx1 and tx2 are already in the txpool (in aggregated form)
|
|
|
|
// tx4 is the "new" part of this aggregated tx that we care about
|
2018-09-24 11:24:10 +03:00
|
|
|
let agg_tx = transaction::aggregate(vec![tx1.clone(), tx2.clone(), tx4]).unwrap();
|
|
|
|
|
2019-01-25 23:48:15 +03:00
|
|
|
agg_tx
|
|
|
|
.validate(Weighting::AsTransaction, verifier_cache.clone())
|
|
|
|
.unwrap();
|
2018-09-24 11:24:10 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
pool.add_to_pool(test_source(), agg_tx, false, &header)
|
2018-05-30 23:57:13 +03:00
|
|
|
.unwrap();
|
2020-06-07 11:26:08 +03:00
|
|
|
assert_eq!(pool.total_size(), 6);
|
|
|
|
let entry = pool.txpool.entries.last().unwrap();
|
2018-08-16 00:14:48 +03:00
|
|
|
assert_eq!(entry.tx.kernels().len(), 1);
|
2019-07-04 13:56:42 +03:00
|
|
|
assert_eq!(entry.src, TxSource::Deaggregate);
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check we cannot "double spend" an output spent in a previous block.
|
|
|
|
// We use the initial coinbase output here for convenience.
|
|
|
|
{
|
2020-06-07 11:26:08 +03:00
|
|
|
let double_spend_tx = test_transaction_spending_coinbase(&keychain, &header, vec![1000]);
|
2019-04-16 01:00:24 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// check we cannot add a double spend to the stempool
|
|
|
|
assert!(pool
|
|
|
|
.add_to_pool(test_source(), double_spend_tx.clone(), true, &header)
|
|
|
|
.is_err());
|
2019-04-16 01:00:24 +03:00
|
|
|
|
2020-06-07 11:26:08 +03:00
|
|
|
// check we cannot add a double spend to the txpool
|
|
|
|
assert!(pool
|
|
|
|
.add_to_pool(test_source(), double_spend_tx.clone(), false, &header)
|
|
|
|
.is_err());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|
2020-06-07 11:26:08 +03:00
|
|
|
|
2019-04-16 01:00:24 +03:00
|
|
|
// Cleanup db directory
|
2020-06-07 11:26:08 +03:00
|
|
|
clean_output_dir(db_root.into());
|
2018-05-30 23:57:13 +03:00
|
|
|
}
|