Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hansieodendaal committed Nov 10, 2023
1 parent a57ab80 commit 4b10aea
Show file tree
Hide file tree
Showing 49 changed files with 138 additions and 93 deletions.
2 changes: 1 addition & 1 deletion base_layer/core/benches/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ mod benches {
test_helpers::blockchain::create_new_blockchain,
transactions::{
tari_amount::{uT, T},
test_helpers::create_memory_db_key_manager,
transaction_components::{OutputFeatures, Transaction, MAX_TRANSACTION_OUTPUTS},
CryptoFactories,
},
tx,
validation::transaction::TransactionFullValidator,
};
use tokio::runtime::Runtime;
use tari_core::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

async fn generate_transactions(
num_txs: usize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
},
transactions::{
tari_amount::T,
test_helpers::{create_memory_db_key_manager, schema_to_transaction, MemoryDbKeyManager},
test_helpers::{schema_to_transaction, MemoryDbKeyManager},
transaction_components::{Transaction, WalletOutput},
},
txn_schema,
Expand Down Expand Up @@ -96,7 +96,7 @@ async fn add_many_chained_blocks(
}

mod fetch_blocks {

use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;
use super::*;

#[test]
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/covenants/covenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ mod test {
test::{create_input, create_outputs},
Covenant,
},
transactions::test_helpers::{create_memory_db_key_manager, UtxoTestParams},
transactions::test_helpers::{UtxoTestParams},
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_succeeds_when_empty() {
Expand Down
8 changes: 4 additions & 4 deletions base_layer/core/src/covenants/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ mod test {
use super::*;
use crate::transactions::{
tari_amount::MicroMinotari,
test_helpers::create_memory_db_key_manager,
transaction_components::RangeProofType,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_returns_true_if_eq() {
Expand Down Expand Up @@ -490,7 +490,7 @@ mod test {

mod is_eq_input {
use super::*;
use crate::transactions::test_helpers::create_memory_db_key_manager;
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_returns_true_if_eq_input() {
Expand Down Expand Up @@ -561,6 +561,7 @@ mod test {

mod output_fields {
use super::*;
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

mod construct_challenge_from {
use blake2::Digest;
Expand All @@ -570,7 +571,6 @@ mod test {
use super::*;
use crate::transactions::{
tari_amount::MicroMinotari,
test_helpers::create_memory_db_key_manager,
transaction_components::RangeProofType,
};

Expand Down Expand Up @@ -622,9 +622,9 @@ mod test {
use super::*;
use crate::transactions::{
tari_amount::MicroMinotari,
test_helpers::create_memory_db_key_manager,
transaction_components::RangeProofType,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_retrieves_the_value_as_ref() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/absolute_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_all_out_if_height_not_reached() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/and.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_outputset_using_intersection() {
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/covenants/filters/field_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ mod test {
use crate::{
covenant,
covenants::test::{create_context, create_input, create_outputs},
transactions::{test_helpers::create_memory_db_key_manager, transaction_components::OutputType},
transactions::{transaction_components::OutputType},
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_uint() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/fields_hashed_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ mod test {
COVENANTS_FIELD_HASHER_LABEL,
},
transactions::{
test_helpers::create_memory_db_key_manager,
transaction_components::OutputFeatures,
},
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_outputs_with_fields_that_hash_to_given_hash() {
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/covenants/filters/fields_preserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::{test_helpers::create_memory_db_key_manager, transaction_components::OutputType},
transactions::{transaction_components::OutputType},
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_outputs_that_match_input_fields() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ mod tests {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_returns_the_outputset_unchanged() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_compliment_of_filter() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_outputset_using_union() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/output_hash_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ mod test {
filters::test::setup_filter_test,
test::{create_input, create_outputs},
},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_output_with_specific_hash() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/covenants/filters/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ mod test {
use crate::{
covenant,
covenants::{filters::test::setup_filter_test, test::create_input},
transactions::test_helpers::create_memory_db_key_manager,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn it_filters_outputset_using_symmetric_difference() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ mod tests {
use super::*;
use crate::transactions::{
tari_amount::{uT, MicroMinotari, T},
test_helpers::{create_memory_db_key_manager, create_tx, MemoryDbKeyManager},
};
use crate::transactions::key_manager::memory_db_key_manager::{create_memory_db_key_manager, MemoryDbKeyManager};
use crate::transactions::test_helpers::create_tx;

async fn create_tx_with_fee(fee_per_gram: MicroMinotari, key_manager: &MemoryDbKeyManager) -> Transaction {
let (tx, _, _) = create_tx(10 * T, fee_per_gram, 0, 1, 0, 1, Default::default(), key_manager)
Expand Down
3 changes: 2 additions & 1 deletion base_layer/core/src/mempool/sync_protocol/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ use crate::{
},
transactions::{
tari_amount::uT,
test_helpers::{create_memory_db_key_manager, create_tx},
test_helpers::{create_tx},
transaction_components::Transaction,
},
validation::mocks::MockValidator,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

pub async fn create_transactions(n: usize) -> Vec<Transaction> {
let key_manager = create_memory_db_key_manager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,12 +884,13 @@ mod test {
aggregated_body::AggregateBody,
fee::Fee,
tari_amount::MicroMinotari,
test_helpers::{create_memory_db_key_manager, TestParams, UtxoTestParams},
test_helpers::{TestParams, UtxoTestParams},
weight::TransactionWeight,
SenderTransactionProtocol,
},
tx,
};
use crate::transactions::key_manager::memory_db_key_manager::create_memory_db_key_manager;

#[tokio::test]
async fn test_find_duplicate_input() {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/test_helpers/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ use crate::{
proof_of_work::{AchievedTargetDifficulty, Difficulty, PowAlgorithm},
test_helpers::{block_spec::BlockSpecs, create_consensus_rules, BlockSpec},
transactions::{
test_helpers::{create_memory_db_key_manager, MemoryDbKeyManager},
transaction_components::{TransactionInput, TransactionKernel, TransactionOutput, WalletOutput},
CryptoFactories,
},
Expand All @@ -75,6 +74,7 @@ use crate::{
},
OutputSmt,
};
use crate::transactions::key_manager::memory_db_key_manager::{create_memory_db_key_manager, MemoryDbKeyManager};

/// Create a new blockchain database containing the genesis block
pub fn create_new_blockchain() -> BlockchainDatabase<TempDatabase> {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/transactions/coinbase_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ mod test {
use crate::transactions::{
aggregated_body::AggregateBody,
key_manager::{TransactionKeyManagerBranch, TransactionKeyManagerInterface, TxoStage},
test_helpers::{create_memory_db_key_manager, MemoryDbKeyManager},
transaction_components::TransactionKernelVersion,
};
use crate::transactions::key_manager::memory_db_key_manager::{create_memory_db_key_manager, MemoryDbKeyManager};

#[tokio::test]
#[allow(clippy::too_many_lines)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2023 The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{iter, mem::size_of, sync::Arc};

use chacha20poly1305::{Key, KeyInit, XChaCha20Poly1305};
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng, RngCore};
use tari_common::configuration::Network;
use tari_common_sqlite::connection::{DbConnection, DbConnectionUrl};
use tari_common_types::types::{Commitment, PrivateKey, PublicKey, Signature};
use tari_crypto::keys::{PublicKey as PK, SecretKey};
use tari_key_manager::{
cipher_seed::CipherSeed,
key_manager_service::{
storage::{database::KeyManagerDatabase, sqlite_db::KeyManagerSqliteDatabase},
KeyManagerInterface,
},
};
use crate::transactions::key_manager::TransactionKeyManagerWrapper;

pub type MemoryDbKeyManager = TransactionKeyManagerWrapper<KeyManagerSqliteDatabase<DbConnection>>;

fn random_string(len: usize) -> String {
iter::repeat(())
.map(|_| OsRng.sample(Alphanumeric) as char)
.take(len)
.collect()
}

pub fn create_memory_db_key_manager_with_range_proof_size(size: usize) -> MemoryDbKeyManager {
let connection = DbConnection::connect_url(&DbConnectionUrl::MemoryShared(random_string(8))).unwrap();
let cipher = CipherSeed::new();

let mut key = [0u8; size_of::<Key>()];
OsRng.fill_bytes(&mut key);
let key_ga = Key::from_slice(&key);
let db_cipher = XChaCha20Poly1305::new(key_ga);
let factory = CryptoFactories::new(size);

TransactionKeyManagerWrapper::<KeyManagerSqliteDatabase<DbConnection>>::new(
cipher,
KeyManagerDatabase::new(KeyManagerSqliteDatabase::init(connection, db_cipher)),
factory,
)
.unwrap()
}

pub fn create_memory_db_key_manager() -> MemoryDbKeyManager {
create_memory_db_key_manager_with_range_proof_size(64)
}
41 changes: 4 additions & 37 deletions base_layer/core/src/transactions/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{iter, mem::size_of, sync::Arc};
use std::{sync::Arc};

use chacha20poly1305::{Key, KeyInit, XChaCha20Poly1305};
use rand::{distributions::Alphanumeric, rngs::OsRng, Rng, RngCore};
use chacha20poly1305::{ KeyInit};
use rand::{rngs::OsRng};
use tari_common::configuration::Network;
use tari_common_sqlite::connection::{DbConnection, DbConnectionUrl};
use tari_common_types::types::{Commitment, PrivateKey, PublicKey, Signature};
use tari_crypto::keys::{PublicKey as PK, SecretKey};
use tari_key_manager::{
cipher_seed::CipherSeed,
key_manager_service::{
storage::{database::KeyManagerDatabase, sqlite_db::KeyManagerSqliteDatabase},
KeyManagerInterface,
},
};
Expand Down Expand Up @@ -70,6 +67,7 @@ use crate::{
SenderTransactionProtocol,
},
};
use crate::transactions::key_manager::memory_db_key_manager::MemoryDbKeyManager;

pub async fn create_test_input(amount: MicroMinotari, maturity: u64, key_manager: &MemoryDbKeyManager) -> WalletOutput {
let params = TestParams::new(key_manager).await;
Expand Down Expand Up @@ -890,34 +888,3 @@ pub async fn schema_to_transaction(

(txs, utxos)
}

pub type MemoryDbKeyManager = TransactionKeyManagerWrapper<KeyManagerSqliteDatabase<DbConnection>>;

fn random_string(len: usize) -> String {
iter::repeat(())
.map(|_| OsRng.sample(Alphanumeric) as char)
.take(len)
.collect()
}

pub fn create_memory_db_key_manager_with_range_proof_size(size: usize) -> MemoryDbKeyManager {
let connection = DbConnection::connect_url(&DbConnectionUrl::MemoryShared(random_string(8))).unwrap();
let cipher = CipherSeed::new();

let mut key = [0u8; size_of::<Key>()];
OsRng.fill_bytes(&mut key);
let key_ga = Key::from_slice(&key);
let db_cipher = XChaCha20Poly1305::new(key_ga);
let factory = CryptoFactories::new(size);

TransactionKeyManagerWrapper::<KeyManagerSqliteDatabase<DbConnection>>::new(
cipher,
KeyManagerDatabase::new(KeyManagerSqliteDatabase::init(connection, db_cipher)),
factory,
)
.unwrap()
}

pub fn create_memory_db_key_manager() -> MemoryDbKeyManager {
create_memory_db_key_manager_with_range_proof_size(64)
}
Loading

0 comments on commit 4b10aea

Please sign in to comment.