adjust storage update evm-style (#7812)

This commit is contained in:
Nikolay Volf 2018-02-06 13:57:29 +03:00 committed by Marek Kotewicz
parent fb4582a90e
commit 0a7cebe316
3 changed files with 60 additions and 3 deletions

@ -1 +1 @@
Subproject commit ff8504a7f3b3fe78af47fa4e0c24e4a75a7306fd Subproject commit fb111c82deff8759f54a5038d07cecc77cb5a663

View File

@ -218,11 +218,21 @@ impl<'a> Runtime<'a> {
let key = self.h256_at(args.nth(0)?)?; let key = self.h256_at(args.nth(0)?)?;
let val_ptr: u32 = args.nth(1)?; let val_ptr: u32 = args.nth(1)?;
self.adjusted_charge(|schedule| schedule.sstore_set_gas as u64)?;
let val = self.h256_at(val_ptr)?; let val = self.h256_at(val_ptr)?;
let former_val = self.ext.storage_at(&key).map_err(|_| Error::StorageUpdateError)?;
if former_val == H256::zero() && val != H256::zero() {
self.adjusted_charge(|schedule| schedule.sstore_set_gas as u64)?;
} else {
self.adjusted_charge(|schedule| schedule.sstore_reset_gas as u64)?;
}
self.ext.set_storage(key, val).map_err(|_| Error::StorageUpdateError)?; self.ext.set_storage(key, val).map_err(|_| Error::StorageUpdateError)?;
if former_val != H256::zero() && val == H256::zero() {
self.ext.inc_sstore_clears();
}
Ok(()) Ok(())
} }

View File

@ -659,6 +659,53 @@ fn math_div() {
assert_eq!(gas_left, U256::from(90_607)); assert_eq!(gas_left, U256::from(90_607));
} }
#[test]
fn storage_metering() {
::ethcore_logger::init_log();
// #1
let mut ext = FakeExt::new();
let code = Arc::new(load_sample!("setter.wasm"));
let address: Address = "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6".parse().unwrap();
let mut params = ActionParams::default();
params.address = address.clone();
params.gas = U256::from(100_000);
params.code = Some(code.clone());
params.data = Some(vec![
0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d,
0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b,
]);
let gas_left = {
let mut interpreter = wasm_interpreter();
test_finalize(interpreter.exec(params, &mut ext)).unwrap()
};
// 0 -> not 0
assert_eq!(gas_left, U256::from(74_410));
// #2
let mut params = ActionParams::default();
params.address = address.clone();
params.gas = U256::from(100_000);
params.code = Some(code.clone());
params.data = Some(vec![
0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d, 0x9d,
0x6b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b,
]);
let gas_left = {
let mut interpreter = wasm_interpreter();
test_finalize(interpreter.exec(params, &mut ext)).unwrap()
};
// not 0 -> not 0
assert_eq!(gas_left, U256::from(89_410));
}
// This test checks the ability of wasm contract to invoke // This test checks the ability of wasm contract to invoke
// varios blockchain runtime methods // varios blockchain runtime methods
#[test] #[test]