Skip to content

Commit

Permalink
Bump protocol version to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Mar 30, 2023
1 parent 2f5340a commit a21524e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
45 changes: 25 additions & 20 deletions crates/sui-adapter/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,31 @@ fn execute_transaction<
Some(gas_object_ref.0)
};
temporary_store.charge_gas(gas_object_id, &mut gas_status, &mut result, gas);
if protocol_config.gas_v2_enabled() {
// The following is a breaking change since we are adding rebate to the
// 0x5 object in the resulting state.
let unmetered_storage_rebate = gas_status.unmetered_storage_rebate();
debug!(
"Amount of unmetered storage rebate from advance epoch tx: {:?}",
unmetered_storage_rebate
);
// If unmetered_storage_rebate is 0, we are most likely executing the genesis transaction.
// And in that case we cannot mutate the 0x5 object because it's newly created.
if unmetered_storage_rebate > 0 {
// Put all the storage rebate accumulated in the system transaction
// to the 0x5 object so that it's not lost.
let mut system_state_wrapper = temporary_store
.read_object(&SUI_SYSTEM_STATE_OBJECT_ID)
.expect("0x5 object must be muated in advance_epoch tx")
.clone();
// In unmetered execution, storage_rebate field of mutated object must be 0.
// If not, we would be dropping SUI on the floor by overriding it.
assert_eq!(system_state_wrapper.storage_rebate, 0);
system_state_wrapper.storage_rebate = unmetered_storage_rebate;
temporary_store.write_object(system_state_wrapper, WriteKind::Mutate);
}

}
#[cfg(debug_assertions)]
{
// Genesis transactions mint sui supply, and hence does not satisfy SUI conservation.
Expand Down Expand Up @@ -524,26 +549,6 @@ fn advance_epoch<S: BackingPackageStore + ParentSync + ChildObjectResolver>(
)
.expect("Advance epoch with safe mode must succeed");
}
if protocol_config.version.as_u64() > 1 {
// The following is a breaking change to version 1 since we are adding rebate to the
// 0x5 object in the resulting state.
let unmetered_storage_rebate = gas_status.unmetered_storage_rebate();
debug!(
"Amount of unmetered storage rebate from advance epoch tx: {:?}",
unmetered_storage_rebate
);
// Put all the storage rebate accumulated in the system transaction
// to the 0x5 object so that it's not lost.
let mut system_state_wrapper = temporary_store
.read_object(&SUI_SYSTEM_STATE_OBJECT_ID)
.expect("0x5 object must be muated in advance_epoch tx")
.clone();
// In unmetered execution, storage_rebate field of mutated object must be 0.
// If not, we would be dropping SUI on the floor by overriding it.
assert_eq!(system_state_wrapper.storage_rebate, 0);
system_state_wrapper.storage_rebate = unmetered_storage_rebate;
temporary_store.write_object(system_state_wrapper, WriteKind::Mutate);
}

for (version, modules, dependencies) in change_epoch.system_packages.into_iter() {
let modules: Vec<_> = modules
Expand Down
10 changes: 8 additions & 2 deletions crates/sui-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::{info, warn};

/// The minimum and maximum protocol versions supported by this build.
const MIN_PROTOCOL_VERSION: u64 = 1;
const MAX_PROTOCOL_VERSION: u64 = 1;
const MAX_PROTOCOL_VERSION: u64 = 3;

// Record history of protocol version allocations here:
//
Expand Down Expand Up @@ -1323,7 +1323,7 @@ impl ProtocolConfig {
address_from_u256_cost_base: Some(52),

// `dynamic_field` module
// Cost params for the Move native function `hash_type_and_key<K: copy + drop + store>(parent: address, k: K): address`
// Cost params for the Move native function `hash_type_and_key<K: copy + drop + store>(parent: address, k: K): address`
dynamic_field_hash_type_and_key_cost_base: Some(100),
dynamic_field_hash_type_and_key_type_cost_per_byte: Some(2),
dynamic_field_hash_type_and_key_value_cost_per_byte: Some(2),
Expand Down Expand Up @@ -1471,6 +1471,8 @@ impl ProtocolConfig {
// When adding a new constant, set it to None in the earliest version, like this:
// new_constant: None,
},
2 => Self::get_for_version_impl(version - 1),
3 => Self::get_for_version_impl(version - 1),

// Use this template when making changes:
//
Expand Down Expand Up @@ -1505,6 +1507,10 @@ impl ProtocolConfig {
OverrideGuard
})
}

pub fn gas_v2_enabled(&self) -> bool {
self.version.as_u64() >= 3
}
}

// Setters for tests
Expand Down
4 changes: 3 additions & 1 deletion crates/sui-types/src/temporary_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ impl<S: ObjectStore> TemporaryStore<S> {
WriteKind::Mutate => {
// get owner at beginning of tx, since that's what we have to authenticate against
// _new_obj.owner is not relevant here
let old_obj = self.store.get_object(id)?.unwrap();
let old_obj = self.store.get_object(id)?.unwrap_or_else(|| {
panic!("Mutated object must exist in the store: ID = {:?}", id)
});
match &old_obj.owner {
Owner::ObjectOwner(_parent) => {
objs_to_authenticate.push(*id);
Expand Down
4 changes: 2 additions & 2 deletions crates/sui/tests/protocol_version_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ mod sim_only_tests {
// a protocol version increment.
let system_state = test_cluster.wait_for_epoch(Some(1)).await;
assert_eq!(system_state.epoch(), 1);
assert_eq!(system_state.protocol_version(), 2); // protocol version increments
assert_eq!(system_state.protocol_version(), FINISH); // protocol version increments
assert!(system_state.safe_mode()); // enters safe mode

// We are getting out of safe mode soon.
Expand All @@ -580,7 +580,7 @@ mod sim_only_tests {
// This epoch change should execute successfully without any upgrade and get us out of safe mode.
let system_state = test_cluster.wait_for_epoch(Some(2)).await;
assert_eq!(system_state.epoch(), 2);
assert_eq!(system_state.protocol_version(), 2); // protocol version stays the same
assert_eq!(system_state.protocol_version(), FINISH); // protocol version stays the same
assert!(!system_state.safe_mode()); // out of safe mode
}

Expand Down

0 comments on commit a21524e

Please sign in to comment.