Skip to content

Commit fd288b5

Browse files
committed
Merge branch 'payref' of https://github.com/fluffypony/tari into payref
2 parents 664fe42 + dadc129 commit fd288b5

File tree

19 files changed

+185
-30
lines changed

19 files changed

+185
-30
lines changed

.github/workflows/build_binaries.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ env:
3131
# TS_BUILD: "debug"
3232
TS_BUILD: "release"
3333
TARI_NETWORK_DIR: testnet
34-
toolchain: nightly-2024-07-07
34+
toolchain: nightly-2025-01-01
3535
matrix-json-file: ".github/workflows/build_binaries.json"
3636
CARGO_HTTP_MULTIPLEXING: false
3737
CARGO_UNSTABLE_SPARSE_REGISTRY: true

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ name: CI
1616
env:
1717
toolchain: stable
1818
nightly_toolchain: nightly-2025-01-01
19-
nightly_test_toolchain: nightly-2024-08-03
19+
nightly_test_toolchain: nightly-2025-01-01
2020
CARGO_HTTP_MULTIPLEXING: false
2121
CARGO_TERM_COLOR: always
2222
CARGO_UNSTABLE_SPARSE_REGISTRY: true

.github/workflows/integration_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ name: Integration tests
2828

2929
env:
3030
toolchain: stable
31-
nightly_toolchain: nightly-2024-07-07
31+
nightly_toolchain: nightly-2025-01-01
3232

3333
concurrency:
3434
group: ${{ github.workflow }}-${{ github.ref }}

base_layer/core/src/chain_storage/blockchain_database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ fn check_for_valid_height<T: BlockchainBackend>(db: &T, height: u64) -> Result<(
18051805
/// Removes blocks from the db from current tip to specified height.
18061806
/// Returns the blocks removed, ordered from tip to height.
18071807
#[allow(clippy::too_many_lines)]
1808-
fn rewind_to_height<T: BlockchainBackend>(
1808+
pub(crate) fn rewind_to_height<T: BlockchainBackend>(
18091809
db: &mut T,
18101810
target_height: u64,
18111811
) -> Result<Vec<Arc<ChainBlock>>, ChainStorageError> {

base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use crate::{
7878
UpdateBlockAccumulatedData,
7979
},
8080
chain_storage::{
81+
blockchain_database::rewind_to_height,
8182
db_transaction::{DbKey, DbTransaction, DbValue, WriteOperation},
8283
error::{ChainStorageError, OrNotFound},
8384
lmdb_db::{
@@ -311,7 +312,7 @@ impl LMDBDatabase {
311312
consensus_manager: ConsensusManager,
312313
) -> Result<Self, ChainStorageError> {
313314
let env = store.env();
314-
let db = Self {
315+
let mut db = Self {
315316
metadata_db: get_database(store, LMDB_DB_METADATA)?,
316317
headers_db: get_database(store, LMDB_DB_HEADERS)?,
317318
header_accumulated_data_db: get_database(store, LMDB_DB_HEADER_ACCUMULATED_DATA)?,
@@ -350,7 +351,7 @@ impl LMDBDatabase {
350351
consensus_manager,
351352
};
352353

353-
run_migrations(&db)?;
354+
run_migrations(&mut db)?;
354355

355356
Ok(db)
356357
}
@@ -2954,8 +2955,8 @@ impl fmt::Display for MetadataValue {
29542955
}
29552956

29562957
#[allow(clippy::too_many_lines)]
2957-
fn run_migrations(db: &LMDBDatabase) -> Result<(), ChainStorageError> {
2958-
const MIGRATION_VERSION: u64 = 2;
2958+
fn run_migrations(db: &mut LMDBDatabase) -> Result<(), ChainStorageError> {
2959+
const MIGRATION_VERSION: u64 = 3;
29592960
let txn = db.read_transaction()?;
29602961
let k = MetadataKey::MigrationVersion;
29612962
let val = lmdb_get::<_, MetadataValue>(&txn, &db.metadata_db, &k.as_u32())?;
@@ -3083,8 +3084,7 @@ fn run_migrations(db: &LMDBDatabase) -> Result<(), ChainStorageError> {
30833084
}
30843085
txn.commit()?;
30853086
}
3086-
3087-
if migrate_from_version == 1 {
3087+
if migrate_from_version == 2 {
30883088
// Migration v1 -> v2: Rebuild PayRef index for existing nodes
30893089
info!(target: LOG_TARGET, "Migrating to v2: Checking if PayRef index needs rebuilding");
30903090

@@ -3159,6 +3159,39 @@ fn run_migrations(db: &LMDBDatabase) -> Result<(), ChainStorageError> {
31593159
}
31603160

31613161
info!(target: LOG_TARGET, "PayRef index rebuild completed: {} outputs indexed", rebuild_count);
3162+
3163+
if migrate_from_version == 1 {
3164+
let known_good_difficulties = get_correct_accumulated_difficulty();
3165+
if known_good_difficulties.is_empty() {
3166+
info!(target: LOG_TARGET, "No migration to perform for version network");
3167+
continue;
3168+
}
3169+
let mut last_correct_height = 0;
3170+
for (height, correct_difficulty) in known_good_difficulties {
3171+
let txn = db.read_transaction()?;
3172+
let accum_data: Option<BlockHeaderAccumulatedData> =
3173+
lmdb_get(&txn, &db.header_accumulated_data_db, &height)?;
3174+
if let Some(accum_data) = accum_data {
3175+
if accum_data.total_accumulated_difficulty == correct_difficulty {
3176+
info!(
3177+
target: LOG_TARGET,
3178+
"Block height {} already has correct accumulated difficulty",
3179+
height
3180+
);
3181+
last_correct_height = height;
3182+
}
3183+
} else {
3184+
info!(target: LOG_TARGET, "No accumulated difficulty found for block height {}", height);
3185+
break;
3186+
}
3187+
}
3188+
if last_correct_height == 0 {
3189+
// this will happen only happen if the db is below the fork height of the RxT fork
3190+
info!(target: LOG_TARGET, "No migration to perform for version network");
3191+
continue;
3192+
}
3193+
// lets rewind to last known good accumulated difficulty so the db can be correctly calculated again
3194+
rewind_to_height(db, last_correct_height)?;
31623195
}
31633196
}
31643197
if last_migrated_version != MIGRATION_VERSION {
@@ -3216,3 +3249,54 @@ pub struct OldChainTipData {
32163249
pub hash: HashOutput,
32173250
pub total_accumulated_difficulty: U256,
32183251
}
3252+
3253+
fn get_correct_accumulated_difficulty() -> Vec<(u64, U512)> {
3254+
#[cfg(tari_target_network_mainnet)]
3255+
{
3256+
vec![
3257+
(
3258+
14999,
3259+
U512::from_dec_str("230963847231029670329787338266632060").expect("should not fail"),
3260+
),
3261+
(
3262+
16000,
3263+
U512::from_dec_str("37870972808147006178902366165325544920691850526080").expect("should not fail"),
3264+
),
3265+
(
3266+
17000,
3267+
U512::from_dec_str("123219722351554302645774736761840507999792186766920").expect("should not fail"),
3268+
),
3269+
(
3270+
18000,
3271+
U512::from_dec_str("245169616636012105701848119083014332169855273375890").expect("should not fail"),
3272+
),
3273+
(
3274+
19000,
3275+
U512::from_dec_str("428081108397470519627923902616128115025981546384670").expect("should not fail"),
3276+
),
3277+
(
3278+
20000,
3279+
U512::from_dec_str("678404434598953994059276298108149917133080906779800").expect("should not fail"),
3280+
),
3281+
]
3282+
}
3283+
#[cfg(tari_target_network_nextnet)]
3284+
{
3285+
vec![
3286+
(
3287+
1499,
3288+
U512::from_dec_str("17340317256602964156796").expect("should not fail"),
3289+
),
3290+
(
3291+
2000,
3292+
U512::from_dec_str("267045542397987769905169797604842").expect("should not fail"),
3293+
),
3294+
(
3295+
3000,
3296+
U512::from_dec_str("2261524423095838119669981829692352").expect("should not fail"),
3297+
),
3298+
]
3299+
}
3300+
#[cfg(not(any(tari_target_network_mainnet, tari_target_network_nextnet)))]
3301+
vec![]
3302+
}

base_layer/core/src/chain_storage/target_difficulties.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ impl TargetDifficulties {
4747
}
4848

4949
pub fn update_algos(&mut self, consensus_rules: &ConsensusManager, height: u64) -> Result<(), String> {
50-
let permitted_algos = consensus_rules
51-
.consensus_constants(height)
52-
.current_permitted_pow_algos();
50+
let consensus_constants = consensus_rules.consensus_constants(height);
51+
let permitted_algos = consensus_constants.current_permitted_pow_algos();
5352
let current_keys: Vec<PowAlgorithm> = self.algos.keys().copied().collect();
5453
for algo in current_keys {
5554
if !permitted_algos.contains(&algo) {
@@ -60,6 +59,10 @@ impl TargetDifficulties {
6059
if let std::collections::hash_map::Entry::Vacant(e) = self.algos.entry(algo) {
6160
let target_difficulty_window = consensus_rules.new_target_difficulty(algo, height)?;
6261
e.insert(target_difficulty_window);
62+
} else if let Some(target_diff) = self.algos.get_mut(&algo) {
63+
target_diff.update_target_time(consensus_constants.pow_target_block_interval(algo))?
64+
} else {
65+
// clippy, this else should never be hit
6366
}
6467
}
6568
Ok(())

base_layer/core/src/proof_of_work/lwma_diff.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ impl LinearWeightedMovingAverage {
176176
}
177177
self.target_difficulties.push_back((timestamp, target_difficulty));
178178
}
179+
180+
pub fn update_target_time(&mut self, target_time: u64) -> Result<(), String> {
181+
if target_time == 0 {
182+
return Err(
183+
"LinearWeightedMovingAverage::update_target_time(...) expected `target_time` to be greater than 0, \
184+
but 0 was given"
185+
.into(),
186+
);
187+
}
188+
if target_time.checked_mul(LWMA_MAX_BLOCK_TIME_RATIO).is_none() {
189+
return Err(format!(
190+
"LinearWeightedMovingAverage::update_target_time(...) expected `target_time` to be at least {} times \
191+
smaller than `u64::MAX`",
192+
LWMA_MAX_BLOCK_TIME_RATIO,
193+
));
194+
}
195+
self.target_time = u128::from(target_time);
196+
self.max_block_time = target_time * LWMA_MAX_BLOCK_TIME_RATIO;
197+
Ok(())
198+
}
179199
}
180200

181201
impl DifficultyAdjustment for LinearWeightedMovingAverage {

base_layer/core/src/proof_of_work/target_difficulty_window.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ impl TargetDifficultyWindow {
7676
let difficulty = self.lwma.get_difficulty().unwrap_or(min);
7777
cmp::max(min, cmp::min(max, difficulty))
7878
}
79+
80+
pub fn update_target_time(&mut self, target_time: u64) -> Result<(), String> {
81+
self.lwma.update_target_time(target_time)
82+
}
7983
}
8084

8185
#[cfg(test)]

base_layer/core/tests/tests/node_state_machine.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ async fn test_listening_initial_fallen_behind() {
284284

285285
#[tokio::test]
286286
async fn test_event_channel() {
287+
let network = Network::Esmeralda;
288+
if std::env::var("TARI_NETWORK").is_err() {
289+
std::env::set_var("TARI_NETWORK", network.as_key_str());
290+
}
291+
if Network::get_current_or_user_setting_or_default() != network {
292+
let _ = Network::set_current(network);
293+
}
294+
let current_network = Network::get_current_or_user_setting_or_default();
295+
if current_network != network {
296+
panic!("could not set network");
297+
}
287298
// env_logger::init(); // Set `$env:RUST_LOG = "trace"`
288299
let temp_dir = tempdir().unwrap();
289300
let (node, consensus_manager) = BaseNodeBuilder::new(Network::Esmeralda.into())

base_layer/tari_mining_helper_ffi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub unsafe extern "C" fn byte_vector_get_at(ptr: *mut ByteVector, position: c_ui
150150
ptr::swap(error_out, &mut error as *mut c_int);
151151
return 0u8;
152152
}
153-
(*ptr).0[position as usize]
153+
(&(*ptr).0)[position as usize]
154154
}
155155

156156
/// Gets the number of elements in a ByteVector

0 commit comments

Comments
 (0)