Skip to content

Commit

Permalink
chore: fix unwraps in memory key manager (#6396)
Browse files Browse the repository at this point in the history
Description
---
Fixes a bunch of unwraps in the memory key manager. 

Motivation and Context
---
We should not use unwraps outside fo tests
  • Loading branch information
SWvheerden authored Jul 11, 2024
1 parent 50925b4 commit 89d66b8
Show file tree
Hide file tree
Showing 58 changed files with 236 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a> BlockTemplateProtocol<'a> {
consensus_manager: ConsensusManager,
wallet_payment_address: TariAddress,
) -> Result<BlockTemplateProtocol<'a>, MmProxyError> {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager()?;
Ok(Self {
config,
base_node_client,
Expand Down
7 changes: 6 additions & 1 deletion applications/minotari_miner/src/run_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ pub async fn start_miner(cli: Cli) -> Result<(), ExitError> {
config.set_base_path(cli.common.get_base_path());

debug!(target: LOG_TARGET_FILE, "{:?}", config);
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().map_err(|err| {
ExitError::new(
ExitCode::KeyManagerServiceError,
"'wallet_payment_address' ".to_owned() + &err.to_string(),
)
})?;
let wallet_payment_address = wallet_payment_address(config.wallet_payment_address.clone(), config.network)
.map_err(|err| {
ExitError::new(
Expand Down
11 changes: 9 additions & 2 deletions applications/minotari_node/src/grpc/base_node_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,12 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
prev_coinbase_value = u128::from(coinbase.value);
}

let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().map_err(|e| {
obscure_error_if_true(
report_error_flag,
Status::internal(format!("Key manager error: '{}'", e)),
)
})?;
let height = new_template.header.height;
// The script key is not used in the Diffie-Hellmann protocol, so we assign default.
let script_key_id = TariKeyId::default();
Expand Down Expand Up @@ -1022,7 +1027,9 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
Status::invalid_argument("Malformed coinbase amounts".to_string()),
));
}
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().map_err(|s| {
obscure_error_if_true(report_error_flag, Status::internal(format!("Key manager error: {}", s)))
})?;
let height = block_template.header.height;
// The script key is not used in the Diffie-Hellmann protocol, so we assign default.
let script_key_id = TariKeyId::default();
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/benches/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod benches {
num_outputs: usize,
features: OutputFeatures,
) -> std::io::Result<Vec<Arc<Transaction>>> {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let mut txs = Vec::new();
for _ in 0..num_txs {
let (tx, _, _) =
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/blocks/faucets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod test {
) -> (Vec<TransactionOutput>, TransactionKernel) {
let mut list_of_spend_keys = Vec::new();
let mut total_script_key = PublicKey::default();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
for address in &addresses {
list_of_spend_keys.push(address.public_spend_key().clone());
total_script_key = total_script_key + address.public_spend_key();
Expand Down
52 changes: 26 additions & 26 deletions base_layer/core/src/chain_storage/tests/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_all() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(4, &db, &key_manager).await;
let blocks = db.fetch_blocks(.., true).unwrap();
assert_eq!(blocks.len(), 5);
Expand All @@ -139,7 +139,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_one() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (new_blocks, _) = add_many_chained_blocks(1, &db, &key_manager).await;
let blocks = db.fetch_blocks(1..=1, true).unwrap();
assert_eq!(blocks.len(), 1);
Expand All @@ -149,7 +149,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_nothing_if_asking_for_blocks_out_of_range() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(1, &db, &key_manager).await;
let blocks = db.fetch_blocks(2.., true).unwrap();
assert!(blocks.is_empty());
Expand All @@ -158,7 +158,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_blocks_between_bounds_exclusive() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let blocks = db.fetch_blocks(3..5, true).unwrap();
assert_eq!(blocks.len(), 2);
Expand All @@ -169,7 +169,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_blocks_between_bounds_inclusive() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let blocks = db.fetch_blocks(3..=5, true).unwrap();
assert_eq!(blocks.len(), 3);
Expand All @@ -181,7 +181,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_blocks_to_the_tip() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let blocks = db.fetch_blocks(3.., true).unwrap();
assert_eq!(blocks.len(), 3);
Expand All @@ -193,7 +193,7 @@ mod fetch_blocks {
#[tokio::test]
async fn it_returns_blocks_from_genesis() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let blocks = db.fetch_blocks(..=3, true).unwrap();
assert_eq!(blocks.len(), 4);
Expand Down Expand Up @@ -224,7 +224,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_all() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(4, &db, &key_manager).await;
let headers = db.fetch_headers(..).unwrap();
assert_eq!(headers.len(), 5);
Expand All @@ -236,7 +236,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_nothing_if_asking_for_blocks_out_of_range() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(1, &db, &key_manager).await;
let headers = db.fetch_headers(2..).unwrap();
assert!(headers.is_empty());
Expand All @@ -245,7 +245,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_blocks_between_bounds_exclusive() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let headers = db.fetch_headers(3..5).unwrap();
assert_eq!(headers.len(), 2);
Expand All @@ -256,7 +256,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_blocks_between_bounds_inclusive() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let headers = db.fetch_headers(3..=5).unwrap();
assert_eq!(headers.len(), 3);
Expand All @@ -267,7 +267,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_blocks_to_the_tip() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let headers = db.fetch_headers(3..).unwrap();
assert_eq!(headers.len(), 3);
Expand All @@ -279,7 +279,7 @@ mod fetch_headers {
#[tokio::test]
async fn it_returns_blocks_from_genesis() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let headers = db.fetch_headers(..=3).unwrap();
assert_eq!(headers.len(), 4);
Expand Down Expand Up @@ -307,7 +307,7 @@ mod find_headers_after_hash {
async fn it_returns_from_genesis() {
let db = setup();
let genesis_hash = db.fetch_block(0, true).unwrap().block().hash();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(1, &db, &key_manager).await;
let hashes = vec![genesis_hash];
let (index, headers) = db.find_headers_after_hash(hashes, 1).unwrap().unwrap();
Expand All @@ -318,7 +318,7 @@ mod find_headers_after_hash {
#[tokio::test]
async fn it_returns_the_first_headers_found() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let hashes = (1..=3)
.rev()
Expand All @@ -334,7 +334,7 @@ mod find_headers_after_hash {
async fn fnit_ignores_unknown_hashes() {
let db = setup();

let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let hashes = (2..=4)
.map(|i| db.fetch_block(i, true).unwrap().block().hash())
Expand Down Expand Up @@ -362,7 +362,7 @@ mod fetch_block_hashes_from_header_tip {
#[tokio::test]
async fn it_returns_empty_set_for_big_offset() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
add_many_chained_blocks(5, &db, &key_manager).await;
let hashes = db.fetch_block_hashes_from_header_tip(3, 6).unwrap();
assert!(hashes.is_empty());
Expand All @@ -371,7 +371,7 @@ mod fetch_block_hashes_from_header_tip {
#[tokio::test]
async fn it_returns_n_hashes_from_tip() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (blocks, _) = add_many_chained_blocks(5, &db, &key_manager).await;
let hashes = db.fetch_block_hashes_from_header_tip(3, 1).unwrap();
assert_eq!(hashes.len(), 3);
Expand All @@ -383,7 +383,7 @@ mod fetch_block_hashes_from_header_tip {
#[tokio::test]
async fn it_returns_hashes_without_overlapping() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (blocks, _) = add_many_chained_blocks(3, &db, &key_manager).await;
let hashes = db.fetch_block_hashes_from_header_tip(2, 0).unwrap();
assert_eq!(hashes[0], blocks[2].hash());
Expand All @@ -396,7 +396,7 @@ mod fetch_block_hashes_from_header_tip {
async fn it_returns_all_hashes_from_tip() {
let db = setup();
let genesis = db.fetch_tip_header().unwrap();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (blocks, _) = add_many_chained_blocks(5, &db, &key_manager).await;
let hashes = db.fetch_block_hashes_from_header_tip(10, 0).unwrap();
assert_eq!(hashes.len(), 6);
Expand Down Expand Up @@ -424,7 +424,7 @@ mod fetch_total_size_stats {
async fn it_measures_the_number_of_entries() {
let db = setup();
let genesis_output_count = db.fetch_header(0).unwrap().unwrap().output_smt_size;
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let _block_and_outputs = add_many_chained_blocks(2, &db, &key_manager).await;
let stats = db.fetch_total_size_stats().unwrap();
assert_eq!(
Expand Down Expand Up @@ -481,7 +481,7 @@ mod fetch_header_containing_kernel_mmr {
async fn it_returns_corresponding_header() {
let db = setup();
let genesis = db.fetch_block(0, true).unwrap();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (blocks, outputs) = add_many_chained_blocks(1, &db, &key_manager).await;
let num_genesis_kernels = genesis.block().body.kernels().len() as u64;

Expand Down Expand Up @@ -531,7 +531,7 @@ mod clear_all_pending_headers {
async fn it_clears_no_headers() {
let db = setup();
assert_eq!(db.clear_all_pending_headers().unwrap(), 0);
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let _block_and_outputs = add_many_chained_blocks(2, &db, &key_manager).await;
db.clear_all_pending_headers().unwrap();
let last_header = db.fetch_last_header().unwrap();
Expand All @@ -541,7 +541,7 @@ mod clear_all_pending_headers {
#[tokio::test]
async fn it_clears_headers_after_tip() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let _blocks_and_outputs = add_many_chained_blocks(2, &db, &key_manager).await;
let prev_block = db.fetch_block(2, true).unwrap();
let mut prev_accum = prev_block.accumulated_data().clone();
Expand Down Expand Up @@ -601,7 +601,7 @@ mod validator_node_merkle_root {

#[tokio::test]
async fn it_has_the_correct_genesis_merkle_root() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let vn_mmr = ValidatorNodeBMT::create(Vec::new());
let db = setup();
let (blocks, _outputs) = add_many_chained_blocks(1, &db, &key_manager).await;
Expand All @@ -611,7 +611,7 @@ mod validator_node_merkle_root {
#[tokio::test]
async fn it_has_the_correct_merkle_root_for_current_vn_set() {
let db = setup();
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let (blocks, outputs) = add_many_chained_blocks(1, &db, &key_manager).await;

let (sk, public_key) = PublicKey::random_keypair(&mut OsRng);
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/covenants/covenant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ mod test {

#[tokio::test]
async fn it_succeeds_when_empty() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let outputs = create_outputs(10, UtxoTestParams::default(), &key_manager).await;
let input = create_input(&key_manager).await;
let covenant = covenant!();
Expand All @@ -204,7 +204,7 @@ mod test {

#[tokio::test]
async fn it_executes_the_covenant() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let mut outputs = create_outputs(10, UtxoTestParams::default(), &key_manager).await;
outputs[4].features.maturity = 42;
outputs[5].features.maturity = 42;
Expand All @@ -221,7 +221,7 @@ mod test {

#[tokio::test]
async fn test_borsh_de_serialization() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let mut outputs = create_outputs(10, UtxoTestParams::default(), &key_manager).await;
outputs[4].features.maturity = 42;
outputs[5].features.maturity = 42;
Expand Down
10 changes: 5 additions & 5 deletions base_layer/core/src/covenants/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ mod test {

#[tokio::test]
async fn it_returns_true_if_eq() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let side_chain_features = make_sample_sidechain_feature();
let output = create_outputs(
1,
Expand Down Expand Up @@ -444,7 +444,7 @@ mod test {

#[tokio::test]
async fn it_returns_false_if_not_eq() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let side_chain_features = make_sample_sidechain_feature();
let output = create_outputs(
1,
Expand Down Expand Up @@ -495,7 +495,7 @@ mod test {

#[tokio::test]
async fn it_returns_true_if_eq_input() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let output = create_outputs(
1,
UtxoTestParams {
Expand Down Expand Up @@ -572,7 +572,7 @@ mod test {

#[tokio::test]
async fn it_constructs_challenge_using_consensus_encoding() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let features = OutputFeatures {
maturity: 42,
output_type: OutputType::Coinbase,
Expand Down Expand Up @@ -620,7 +620,7 @@ mod test {

#[tokio::test]
async fn it_retrieves_the_value_as_ref() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let features = OutputFeatures {
maturity: 42,
range_proof_type: RangeProofType::RevealedValue,
Expand Down
6 changes: 3 additions & 3 deletions base_layer/core/src/covenants/filters/absolute_height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod test {

#[tokio::test]
async fn it_filters_all_out_if_height_not_reached() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let covenant = covenant!(absolute_height(@uint(100)));
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(&covenant, &input, 42, |_| {}, &key_manager).await;
Expand All @@ -85,7 +85,7 @@ mod test {

#[tokio::test]
async fn it_filters_all_in_if_height_reached() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let covenant = covenant!(absolute_height(@uint(100)));
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(&covenant, &input, 100, |_| {}, &key_manager).await;
Expand All @@ -98,7 +98,7 @@ mod test {

#[tokio::test]
async fn it_filters_all_in_if_height_exceeded() {
let key_manager = create_memory_db_key_manager();
let key_manager = create_memory_db_key_manager().unwrap();
let covenant = covenant!(absolute_height(@uint(42)));
let input = create_input(&key_manager).await;
let (mut context, outputs) = setup_filter_test(&covenant, &input, 100, |_| {}, &key_manager).await;
Expand Down
Loading

0 comments on commit 89d66b8

Please sign in to comment.