Skip to content

Commit

Permalink
add PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeantonio21 committed Nov 16, 2022
1 parent 85de3b0 commit f57c801
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions base_layer/key_manager/src/cipher_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,11 @@ impl Mnemonic<CipherSeed> for CipherSeed {
mod test {
use crc32fast::Hasher as CrcHasher;

use super::BIRTHDAY_GENESIS_FROM_UNIX_EPOCH;
use crate::{
cipher_seed::{CipherSeed, CIPHER_SEED_VERSION},
error::KeyManagerError,
get_birthday_from_unix_epoch_in_seconds,
mnemonic::{Mnemonic, MnemonicLanguage},
};

Expand Down Expand Up @@ -590,4 +592,46 @@ mod test {
"Should not be able to derive seed with wrong passphrase"
);
}

#[test]
fn birthday_from_unix_epoch_works_for_zero_duration() {
let birthday = 0u16;
let to_days = 0u16;

let birthday_genesis_time_in_seconds = get_birthday_from_unix_epoch_in_seconds(birthday, to_days);
assert_eq!(birthday_genesis_time_in_seconds, BIRTHDAY_GENESIS_FROM_UNIX_EPOCH);
}

#[test]
fn birthday_from_unix_epoch_works_for_large_to_days() {
let birthday = 10u16;
let to_days = 16u16;

let birthday_genesis_time_in_seconds = get_birthday_from_unix_epoch_in_seconds(birthday, to_days);
assert_eq!(birthday_genesis_time_in_seconds, BIRTHDAY_GENESIS_FROM_UNIX_EPOCH);
}

#[test]
fn birthday_from_unix_epoch_works_generally() {
let birthday = 100u16;
let to_days = 20u16;

let birthday_genesis_time_in_seconds = get_birthday_from_unix_epoch_in_seconds(birthday, to_days);
assert_eq!(
birthday_genesis_time_in_seconds,
BIRTHDAY_GENESIS_FROM_UNIX_EPOCH + u64::from(birthday - to_days) * 24 * 60 * 60
);
}

#[test]
fn birthday_is_computed_correctly_from_new_wallet() {
// birthday is at the half of the year 2022, namely 3th July 2022
let cipher_seed = CipherSeed::new_with_birthday(183u16);
let birthday = cipher_seed.birthday;
let birthday_from_unix_epoch = get_birthday_from_unix_epoch_in_seconds(birthday, 0u16);

// 1656806400 corresponds to the duration, in seconds, from unix epoch
// to 3th July 2022 00:00:00
assert_eq!(birthday_from_unix_epoch, 1656806400);
}
}
6 changes: 5 additions & 1 deletion base_layer/key_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub(crate) fn mac_domain_hasher<D: Digest + LengthExtensionAttackResistant>(
DomainSeparatedHasher::<D, KeyManagerDomain>::new_with_label(label)
}

pub fn get_birthday_from_unix_epoch(birthday: u16, to_days: u16) -> u64 {
/// Computes the birthday duration, in seconds, from the unix epoch. Currently, birthday is stored
/// on the wallet as days since 2022-01-01, mainly to preserve space regarding u16 type. That said,
/// for wallet synchronization, it is necessary we are compatible with block timestamps (calculated
/// from unix epoch). This function adds this functionality
pub fn get_birthday_from_unix_epoch_in_seconds(birthday: u16, to_days: u16) -> u64 {
u64::from(birthday.saturating_sub(to_days)) * 24 * 60 * 60 + BIRTHDAY_GENESIS_FROM_UNIX_EPOCH
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use tari_core::{
transaction_components::{TransactionOutput, UnblindedOutput},
},
};
use tari_key_manager::get_birthday_from_unix_epoch;
use tari_key_manager::get_birthday_from_unix_epoch_in_seconds;
use tari_shutdown::ShutdownSignal;
use tari_utilities::hex::Hex;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -698,9 +698,9 @@ where
client: &mut BaseNodeWalletRpcClient,
) -> Result<HeightHash, UtxoScannerError> {
let birthday = self.resources.db.get_wallet_birthday()?;
// Calculate the unix epoch time of two weeks (14 days) before the wallet birthday.
// This is to avoid any weird time zone issues
let epoch_time = get_birthday_from_unix_epoch(birthday, 14u16);
// Calculate the unix epoch time of two weeks (14 days), in seconds, before the
// wallet birthday. The latter avoids any possible issues with reorgs.
let epoch_time = get_birthday_from_unix_epoch_in_seconds(birthday, 14u16);

let block_height = match client.get_height_at_time(epoch_time).await {
Ok(b) => b,
Expand Down
17 changes: 11 additions & 6 deletions base_layer/wallet/tests/utxo_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use tari_core::{
proto::base_node::{ChainMetadata, TipInfoResponse},
transactions::{tari_amount::MicroTari, transaction_components::UnblindedOutput, CryptoFactories},
};
use tari_key_manager::{cipher_seed::CipherSeed, get_birthday_from_unix_epoch};
use tari_key_manager::{cipher_seed::CipherSeed, get_birthday_from_unix_epoch_in_seconds};
use tari_service_framework::reply_channel;
use tari_shutdown::Shutdown;
use tari_test_utils::random;
Expand Down Expand Up @@ -288,7 +288,8 @@ async fn test_utxo_scanner_recovery() {
let mut test_interface = setup(UtxoScannerMode::Recovery, None, None, None).await;

let cipher_seed = CipherSeed::new();
let birthday_epoch_time = get_birthday_from_unix_epoch(cipher_seed.birthday(), 14u16);
// get birthday duration, in seconds, from unix epoch
let birthday_epoch_time = get_birthday_from_unix_epoch_in_seconds(cipher_seed.birthday(), 14u16);
test_interface.wallet_db.set_master_seed(cipher_seed).unwrap();

const NUM_BLOCKS: u64 = 11;
Expand Down Expand Up @@ -371,7 +372,8 @@ async fn test_utxo_scanner_recovery_with_restart() {
let mut test_interface = setup(UtxoScannerMode::Recovery, None, None, None).await;

let cipher_seed = CipherSeed::new();
let birthday_epoch_time = get_birthday_from_unix_epoch(cipher_seed.birthday(), 14);
// get birthday duration, in seconds, from unix epoch
let birthday_epoch_time = get_birthday_from_unix_epoch_in_seconds(cipher_seed.birthday(), 14);
test_interface.wallet_db.set_master_seed(cipher_seed).unwrap();

test_interface
Expand Down Expand Up @@ -535,7 +537,8 @@ async fn test_utxo_scanner_recovery_with_restart_and_reorg() {
let mut test_interface = setup(UtxoScannerMode::Recovery, None, None, None).await;

let cipher_seed = CipherSeed::new();
let birthday_epoch_time = get_birthday_from_unix_epoch(cipher_seed.birthday(), 14);
// get birthday duration, in seconds, from unix epoch
let birthday_epoch_time = get_birthday_from_unix_epoch_in_seconds(cipher_seed.birthday(), 14);
test_interface.wallet_db.set_master_seed(cipher_seed).unwrap();

const NUM_BLOCKS: u64 = 11;
Expand Down Expand Up @@ -704,7 +707,8 @@ async fn test_utxo_scanner_scanned_block_cache_clearing() {
}

let cipher_seed = CipherSeed::new();
let birthday_epoch_time = get_birthday_from_unix_epoch(cipher_seed.birthday(), 14);
// get birthday duration, in seconds, from unix epoch
let birthday_epoch_time = get_birthday_from_unix_epoch_in_seconds(cipher_seed.birthday(), 14);
test_interface.wallet_db.set_master_seed(cipher_seed).unwrap();

const NUM_BLOCKS: u64 = 11;
Expand Down Expand Up @@ -806,7 +810,8 @@ async fn test_utxo_scanner_one_sided_payments() {
.await;

let cipher_seed = CipherSeed::new();
let birthday_epoch_time = get_birthday_from_unix_epoch(cipher_seed.birthday(), 14u16);
// get birthday duration, in seconds, from unix epoch
let birthday_epoch_time = get_birthday_from_unix_epoch_in_seconds(cipher_seed.birthday(), 14u16);
test_interface.wallet_db.set_master_seed(cipher_seed).unwrap();

const NUM_BLOCKS: u64 = 11;
Expand Down

0 comments on commit f57c801

Please sign in to comment.