Skip to content

Commit d9e310e

Browse files
authored
Merge pull request #159 from casper-network/add-logging-around-pruning
Clear named keys instead of prune
2 parents 89394d3 + 2bc125d commit d9e310e

File tree

4 files changed

+78
-142
lines changed

4 files changed

+78
-142
lines changed

execution_engine/src/core/engine_state/mod.rs

Lines changed: 55 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -284,53 +284,7 @@ where
284284
return Err(Error::InvalidProtocolVersion(new_protocol_version));
285285
}
286286

287-
let system_upgrader: SystemUpgrader<S> = SystemUpgrader::new(
288-
new_protocol_version,
289-
current_protocol_version,
290-
tracking_copy.clone(),
291-
);
292-
293-
info!("attempting prune of contract packages");
294-
295-
// Generate the list of keys to prune
296-
let keys_to_prune = system_upgrader
297-
.get_contracts_prune_list(correlation_id, upgrade_config.global_state_update())
298-
.unwrap_or_else(|upgrade_error| {
299-
warn!("error in generating prune list: {:?}", upgrade_error);
300-
vec![]
301-
});
302-
303-
info!("keys to prune {:?}", keys_to_prune);
304-
305-
let prune_config = PruneConfig::new(pre_state_hash, keys_to_prune);
306-
let post_prune_state_root_hash = match self.commit_prune(correlation_id, prune_config) {
307-
Ok(PruneResult::Success {
308-
post_state_hash: new_state_root_hash,
309-
..
310-
}) => {
311-
info!(
312-
"prune success with new state root hash {:?}",
313-
new_state_root_hash
314-
);
315-
new_state_root_hash
316-
}
317-
Ok(_) | Err(_) => {
318-
warn!("failed to prune, reusing previous state root hash");
319-
pre_state_hash
320-
}
321-
};
322-
323-
info!(
324-
"post-prune: pre-state-hash: {:?} post-state-hash: {:?}",
325-
pre_state_hash, post_prune_state_root_hash,
326-
);
327-
328-
let post_prune_tracking_copy = match self.tracking_copy(post_prune_state_root_hash)? {
329-
Some(tracking_copy) => Rc::new(RefCell::new(tracking_copy)),
330-
None => return Err(Error::RootNotFound(post_prune_state_root_hash)),
331-
};
332-
333-
let registry = if let Ok(registry) = post_prune_tracking_copy
287+
let registry = if let Ok(registry) = tracking_copy
334288
.borrow_mut()
335289
.get_system_contracts(correlation_id)
336290
{
@@ -383,7 +337,7 @@ where
383337

384338
// We write the checksums of the chainspec settings to global state
385339
// allowing verification of the chainspec data reported via the RPC.
386-
post_prune_tracking_copy.borrow_mut().write(
340+
tracking_copy.borrow_mut().write(
387341
Key::ChainspecRegistry,
388342
StoredValue::CLValue(cl_value_chainspec_registry),
389343
);
@@ -393,7 +347,7 @@ where
393347
let system_upgrader: SystemUpgrader<S> = SystemUpgrader::new(
394348
new_protocol_version,
395349
current_protocol_version,
396-
post_prune_tracking_copy.clone(),
350+
tracking_copy.clone(),
397351
);
398352

399353
system_upgrader
@@ -417,7 +371,7 @@ where
417371
// 3.1.1.1.1.7 new total validator slots is optional
418372
if let Some(new_validator_slots) = upgrade_config.new_validator_slots() {
419373
// 3.1.2.4 if new total validator slots is provided, update auction contract state
420-
let auction_contract = post_prune_tracking_copy
374+
let auction_contract = tracking_copy
421375
.borrow_mut()
422376
.get_contract(correlation_id, *auction_hash)?;
423377

@@ -426,14 +380,12 @@ where
426380
CLValue::from_t(new_validator_slots)
427381
.map_err(|_| Error::Bytesrepr("new_validator_slots".to_string()))?,
428382
);
429-
post_prune_tracking_copy
430-
.borrow_mut()
431-
.write(validator_slots_key, value);
383+
tracking_copy.borrow_mut().write(validator_slots_key, value);
432384
}
433385

434386
if let Some(new_auction_delay) = upgrade_config.new_auction_delay() {
435387
debug!(%new_auction_delay, "Auction delay changed as part of the upgrade");
436-
let auction_contract = post_prune_tracking_copy
388+
let auction_contract = tracking_copy
437389
.borrow_mut()
438390
.get_contract(correlation_id, *auction_hash)?;
439391

@@ -442,13 +394,11 @@ where
442394
CLValue::from_t(new_auction_delay)
443395
.map_err(|_| Error::Bytesrepr("new_auction_delay".to_string()))?,
444396
);
445-
post_prune_tracking_copy
446-
.borrow_mut()
447-
.write(auction_delay_key, value);
397+
tracking_copy.borrow_mut().write(auction_delay_key, value);
448398
}
449399

450400
if let Some(new_locked_funds_period) = upgrade_config.new_locked_funds_period_millis() {
451-
let auction_contract = post_prune_tracking_copy
401+
let auction_contract = tracking_copy
452402
.borrow_mut()
453403
.get_contract(correlation_id, *auction_hash)?;
454404

@@ -457,7 +407,7 @@ where
457407
CLValue::from_t(new_locked_funds_period)
458408
.map_err(|_| Error::Bytesrepr("new_locked_funds_period".to_string()))?,
459409
);
460-
post_prune_tracking_copy
410+
tracking_copy
461411
.borrow_mut()
462412
.write(locked_funds_period_key, value);
463413
}
@@ -468,7 +418,7 @@ where
468418
Ratio::new(numer.into(), denom.into())
469419
};
470420

471-
let mint_contract = post_prune_tracking_copy
421+
let mint_contract = tracking_copy
472422
.borrow_mut()
473423
.get_contract(correlation_id, *mint_hash)?;
474424

@@ -477,13 +427,15 @@ where
477427
CLValue::from_t(new_round_seigniorage_rate)
478428
.map_err(|_| Error::Bytesrepr("new_round_seigniorage_rate".to_string()))?,
479429
);
480-
post_prune_tracking_copy
430+
tracking_copy
481431
.borrow_mut()
482432
.write(locked_funds_period_key, value);
483433
}
484434

435+
// We insert the new unbonding delay once the purses to be paid out have been transformed
436+
// based on the previous unbonding delay.
485437
if let Some(new_unbonding_delay) = upgrade_config.new_unbonding_delay() {
486-
let auction_contract = post_prune_tracking_copy
438+
let auction_contract = tracking_copy
487439
.borrow_mut()
488440
.get_contract(correlation_id, *auction_hash)?;
489441

@@ -492,48 +444,72 @@ where
492444
CLValue::from_t(new_unbonding_delay)
493445
.map_err(|_| Error::Bytesrepr("new_unbonding_delay".to_string()))?,
494446
);
495-
post_prune_tracking_copy
496-
.borrow_mut()
497-
.write(unbonding_delay_key, value);
447+
tracking_copy.borrow_mut().write(unbonding_delay_key, value);
498448
}
499449

500-
info!("apply the accepted modifications to global state");
501450
// apply the accepted modifications to global state.
502451
for (key, value) in upgrade_config.global_state_update() {
503452
// Skip the key hashes associated with values as we mark these as records to be
504453
// pruned.
505454
let is_unit_value = value.is_unit_cl_value();
506455
if key.into_hash().is_some() && is_unit_value {
507-
debug!(
508-
"found marker record for package prune; skipping normal operation of write {}",
456+
info!(
457+
"found marker record for package overwrite {}",
509458
key.to_formatted_string()
510459
);
511-
continue;
460+
{
461+
let contract_package =
462+
match tracking_copy.borrow_mut().read(correlation_id, key) {
463+
Ok(Some(StoredValue::ContractPackage(contract_package))) => {
464+
contract_package
465+
}
466+
Ok(_) | Err(_) => {
467+
error!(
468+
"error in retrieving contract package: {}",
469+
key.to_formatted_string()
470+
);
471+
continue;
472+
}
473+
};
474+
for contract_hash in contract_package.versions().values() {
475+
let contract_key = Key::Hash(contract_hash.value());
476+
let mut contract = match tracking_copy
477+
.borrow_mut()
478+
.read(correlation_id, &contract_key)
479+
{
480+
Ok(Some(StoredValue::Contract(contract))) => contract,
481+
Ok(_) | Err(_) => {
482+
error!(
483+
"error in retrieving contract : {}",
484+
contract_key.to_formatted_string()
485+
);
486+
continue;
487+
}
488+
};
489+
contract.clear_named_keys();
490+
tracking_copy
491+
.borrow_mut()
492+
.write(contract_key, StoredValue::Contract(contract));
493+
}
494+
};
512495
} else {
513496
info!("update for key: {}", key.to_formatted_string());
514-
post_prune_tracking_copy
515-
.borrow_mut()
516-
.write(*key, value.clone());
497+
tracking_copy.borrow_mut().write(*key, value.clone());
517498
}
518499
}
519-
info!("finished applying accepted modifications to global state");
520500

521-
let execution_effect = post_prune_tracking_copy.borrow().effect();
501+
let execution_effect = tracking_copy.borrow().effect();
522502

523503
// commit
524504
let post_state_hash = self
525505
.state
526506
.commit(
527507
correlation_id,
528-
post_prune_state_root_hash,
508+
pre_state_hash,
529509
execution_effect.transforms.to_owned(),
530510
)
531511
.map_err(Into::into)?;
532512

533-
info!(
534-
"upgrade success with post state hash: {:?}",
535-
post_state_hash
536-
);
537513
// return result and effects
538514
Ok(UpgradeSuccess {
539515
post_state_hash,

execution_engine/src/core/engine_state/upgrade.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! Support for applying upgrades on the execution engine.
2-
use log::{info, warn};
32
use std::{cell::RefCell, collections::BTreeMap, fmt, rc::Rc};
43

54
use num_rational::Ratio;
@@ -412,61 +411,4 @@ where
412411

413412
Ok(())
414413
}
415-
416-
pub(crate) fn get_contracts_prune_list(
417-
&self,
418-
correlation_id: CorrelationId,
419-
global_state_update: &BTreeMap<Key, StoredValue>,
420-
) -> Result<Vec<Key>, ProtocolUpgradeError> {
421-
let mut keys_to_prune = vec![];
422-
for (key, _) in global_state_update.iter() {
423-
if key.into_hash().is_none() {
424-
continue;
425-
}
426-
427-
let maybe_contract_package = self
428-
.tracking_copy
429-
.borrow_mut()
430-
.read(correlation_id, key)
431-
.ok();
432-
if let Some(Some(StoredValue::ContractPackage(contract_package))) =
433-
maybe_contract_package
434-
{
435-
info!("Found contract package {:?}", key.to_formatted_string());
436-
let contract_versions = contract_package.versions();
437-
let mut found_all_keys = true;
438-
let mut keys_for_package = vec![];
439-
for contract_hash in contract_versions.values() {
440-
let contract = self
441-
.tracking_copy
442-
.borrow_mut()
443-
.read(correlation_id, &Key::Hash(contract_hash.value()))
444-
.ok();
445-
if let Some(Some(StoredValue::Contract(contract))) = contract {
446-
let contract_wasm_hash = contract.contract_wasm_hash();
447-
keys_for_package.push(Key::Hash(contract_hash.value()));
448-
keys_for_package.push(Key::Hash(contract_wasm_hash.value()))
449-
} else {
450-
found_all_keys = false;
451-
warn!(
452-
"Unable to find contract record for contract hash: {:?}",
453-
contract_hash
454-
);
455-
break;
456-
}
457-
}
458-
if found_all_keys {
459-
keys_to_prune.extend(keys_for_package);
460-
}
461-
keys_to_prune.push(*key);
462-
} else {
463-
warn!(
464-
"Expected Contract package found either None or different stored value {:?}",
465-
key
466-
);
467-
}
468-
}
469-
470-
Ok(keys_to_prune)
471-
}
472414
}

execution_engine_testing/tests/src/test/regression/regression_20240726.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use casper_engine_test_support::{
66
use once_cell::sync::Lazy;
77
use std::collections::BTreeMap;
88

9-
use casper_types::{runtime_args, CLValue, EraId, ProtocolVersion, RuntimeArgs, StoredValue, URef};
9+
use casper_types::{
10+
runtime_args, CLValue, EraId, Key, ProtocolVersion, RuntimeArgs, StoredValue, URef,
11+
};
1012

1113
const PURSE_FIXTURE: &str = "purse_fixture";
1214
const PURSE_WASM: &str = "regression-20240726.wasm";
@@ -77,10 +79,21 @@ fn should_not_allow_forged_urefs_to_be_saved_to_named_keys() {
7779
.expect_upgrade_success()
7880
.commit();
7981

80-
if let Err(error) = builder.query(None, contract_to_prune, &[]) {
81-
assert!(error.contains("ValueNotFound"))
82-
} else {
83-
panic!("does not contain value not found");
82+
let contract_package = builder
83+
.query(None, contract_to_prune, &[])
84+
.expect("must get stored value")
85+
.as_contract_package()
86+
.cloned()
87+
.expect("must get package");
88+
89+
for hash in contract_package.versions().values() {
90+
let contract = builder
91+
.query(None, Key::Hash(hash.value()), &[])
92+
.expect("must get stored value")
93+
.as_contract()
94+
.cloned()
95+
.expect("must get contract");
96+
assert!(contract.named_keys().is_empty());
8497
}
8598

8699
let hardcoded_uref = builder

types/src/contracts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,11 @@ impl Contract {
11731173
self.named_keys.remove(key)
11741174
}
11751175

1176+
/// Clears the named keys for the contract.
1177+
pub fn clear_named_keys(&mut self) {
1178+
self.named_keys = NamedKeys::new();
1179+
}
1180+
11761181
/// Set protocol_version.
11771182
pub fn set_protocol_version(&mut self, protocol_version: ProtocolVersion) {
11781183
self.protocol_version = protocol_version;

0 commit comments

Comments
 (0)