-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set_code_hash with contract reconstruction #6985
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ use crate::{ | |
storage::{self, meter::Diff, WriteOutcome}, | ||
transient_storage::TransientStorage, | ||
BalanceOf, CodeInfo, CodeInfoOf, Config, ContractInfo, ContractInfoOf, DebugBuffer, Error, | ||
Event, ImmutableData, ImmutableDataOf, Pallet as Contracts, LOG_TARGET, | ||
Event, ExecStack, ImmutableData, ImmutableDataOf, Pallet as Contracts, WasmBlob, LOG_TARGET, | ||
}; | ||
use alloc::vec::Vec; | ||
use core::{fmt::Debug, marker::PhantomData, mem}; | ||
|
@@ -36,7 +36,7 @@ use frame_support::{ | |
storage::{with_transaction, TransactionOutcome}, | ||
traits::{ | ||
fungible::{Inspect, Mutate}, | ||
tokens::{Fortitude, Preservation}, | ||
tokens::{Balance, Fortitude, Preservation}, | ||
Contains, OriginTrait, Time, | ||
}, | ||
weights::Weight, | ||
|
@@ -1839,29 +1839,48 @@ where | |
/// | ||
/// The `set_code_hash` contract API stays disabled until this change is implemented. | ||
fn set_code_hash(&mut self, hash: H256) -> DispatchResult { | ||
let frame = top_frame_mut!(self); | ||
let code_info = CodeInfoOf::<T>::get(hash).ok_or(Error::<T>::CodeNotFound)?; | ||
|
||
let info = frame.contract_info(); | ||
let mut gas_meter = GasMeter::<T>::new(self.gas_meter().gas_left()); | ||
let executable = WasmBlob::<T>::from_storage(hash, &mut gas_meter) | ||
.map_err(|_| <Error<T>>::CodeNotFound)?; | ||
|
||
let prev_hash = info.code_hash; | ||
info.code_hash = hash; | ||
let frame = self.top_frame(); | ||
let account_id = frame.account_id.clone(); | ||
let address = T::AddressMapper::to_address(&account_id); | ||
let current_immutable = | ||
ImmutableDataOf::<T>::get(&address).ok_or(Error::<T>::ImmutableDataNotFound)?; | ||
|
||
// Run the constructor of the new code to check compatibility with current immutable data | ||
let result = executable | ||
.execute(self, ExportedFunction::Constructor, current_immutable.to_vec()) | ||
.map_err(|e| e.error)?; | ||
|
||
if result.did_revert() { | ||
return Err(Error::<T>::IncompatibleImmutableData.into()); | ||
} | ||
|
||
let code_info = CodeInfoOf::<T>::get(hash).ok_or(Error::<T>::CodeNotFound)?; | ||
let frame = self.top_frame_mut(); | ||
let info = frame.contract_info(); | ||
let prev_hash = info.code_hash; | ||
|
||
let old_base_deposit = info.storage_base_deposit(); | ||
let new_base_deposit = info.update_base_deposit(&code_info); | ||
let deposit = StorageDeposit::Charge(new_base_deposit) | ||
.saturating_sub(&StorageDeposit::Charge(old_base_deposit)); | ||
|
||
ExecStack::<T, WasmBlob<T>>::increment_refcount(hash)?; | ||
ExecStack::<T, WasmBlob<T>>::decrement_refcount(prev_hash); | ||
info.code_hash = hash; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should to be done before calling the constructor. Otherwise it will see the old code hash when querying it. |
||
|
||
frame.nested_storage.charge_deposit(frame.account_id.clone(), deposit); | ||
|
||
Self::increment_refcount(hash)?; | ||
Self::decrement_refcount(prev_hash); | ||
Contracts::<Self::T>::deposit_event(Event::ContractCodeUpdated { | ||
contract: T::AddressMapper::to_address(&frame.account_id), | ||
new_code_hash: hash, | ||
old_code_hash: prev_hash, | ||
}); | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -5076,4 +5095,111 @@ mod tests { | |
); | ||
}); | ||
} | ||
|
||
mod code_hash_update { | ||
use super::*; | ||
use core::assert_eq; | ||
|
||
#[test] | ||
fn set_code_hash_checks_compatibility_of_new_code_with_immutable_data() { | ||
use pallet_revive_fixtures::compile_module; | ||
|
||
parameter_types! { | ||
static SomeImmutableData: Vec<u8> = vec![2]; | ||
} | ||
|
||
let set_code_hash = MockLoader::insert(Call, |ctx, _| { | ||
let code_hash = H256::from_slice(&ctx.input_data); | ||
ctx.ext | ||
.set_code_hash(code_hash) | ||
.map(|_| ExecReturnValue::default()) | ||
.map_err(Into::into) | ||
}); | ||
|
||
ExtBuilder::default().build().execute_with(|| { | ||
place_contract(&CHARLIE, set_code_hash); | ||
|
||
let owner = ALICE; | ||
let (code, _) = compile_module("noop").unwrap(); | ||
let code_blob = WasmBlob::from_code(code, owner.clone()); | ||
assert_ok!(code_blob.clone()); | ||
let mut code_blob: WasmBlob<Test> = code_blob.unwrap(); | ||
let deposit = code_blob.store_code(true); | ||
assert_ok!(deposit); | ||
let new_code_hash = *code_blob.code_hash(); | ||
place_contract(&BOB, new_code_hash); | ||
|
||
<ImmutableDataOf<Test>>::insert::<_, ImmutableData>( | ||
CHARLIE_ADDR, | ||
SomeImmutableData::get().try_into().unwrap(), | ||
); | ||
|
||
// with enough balance to cover storage deposits | ||
set_balance(&ALICE, 1_000_000); | ||
let origin = Origin::from_account_id(ALICE); | ||
// with sufficient storage deposit limit | ||
let mut storage_meter = storage::meter::Meter::new(&origin, 1_000_000, 0).unwrap(); | ||
|
||
assert_ok!(MockStack::run_call( | ||
origin.clone(), | ||
CHARLIE_ADDR, | ||
&mut GasMeter::<Test>::new(GAS_LIMIT), | ||
&mut storage_meter, | ||
U256::zero(), | ||
new_code_hash.as_bytes().to_vec(), | ||
false, | ||
None, | ||
)); | ||
|
||
assert_eq!( | ||
ContractInfoOf::<Test>::get(&CHARLIE_ADDR).unwrap().code_hash, | ||
new_code_hash | ||
); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn set_code_hash_fails_if_new_code_cannot_be_found() { | ||
ExtBuilder::default().build().execute_with(|| { | ||
let code_hash_1 = MockLoader::insert(Constructor, |ctx, _| { | ||
ctx.ext.set_immutable_data(vec![1, 2, 3].try_into().unwrap())?; | ||
exec_success() | ||
}); | ||
|
||
let set_code_hash = MockLoader::insert(Call, |ctx, _| { | ||
let code_hash = H256::from_slice(&ctx.input_data); | ||
ctx.ext | ||
.set_code_hash(code_hash) | ||
.map(|_| ExecReturnValue::default()) | ||
.map_err(Into::into) | ||
}); | ||
|
||
place_contract(&BOB, code_hash_1); | ||
place_contract(&CHARLIE, set_code_hash); | ||
|
||
let origin = Origin::from_account_id(ALICE); | ||
let mut storage_meter = storage::meter::Meter::new(&origin, 0, 0).unwrap(); | ||
|
||
assert_eq!( | ||
MockStack::run_call( | ||
origin.clone(), | ||
CHARLIE_ADDR, | ||
&mut GasMeter::<Test>::new(GAS_LIMIT), | ||
&mut storage_meter, | ||
U256::zero(), | ||
code_hash_1.as_bytes().to_vec(), | ||
false, | ||
None, | ||
), | ||
Err(ExecError { | ||
error: Error::<Test>::CodeNotFound.into(), | ||
origin: ErrorOrigin::Callee, | ||
}) | ||
); | ||
|
||
let contract = ContractInfoOf::<Test>::get(&BOB_ADDR).unwrap(); | ||
assert_eq!(contract.code_hash, code_hash_1); | ||
}); | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those new error codes don't make sense since we don't check any compat with the old immutables (see other comment). I think we should add a single new error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constructors don't have access to immutables. They set the immutables. Usually by deriving them from the constructor arguments or environment properties (like the caller of the constructor).
So what should be passed here are not immutables but thet arguments to the constructor. In order to do that you need to add a new argument to this function (
Vec<u8>
) so that the contract callingset_code_hash
can provide the chosen constructor with its arguments.