Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Adjust storage update evm-style #7812

Merged
merged 1 commit into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethcore/res/wasm-tests
14 changes: 12 additions & 2 deletions ethcore/wasm/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,21 @@ impl<'a> Runtime<'a> {
let key = self.h256_at(args.nth(0)?)?;
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 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)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the decision table for this check looks like this:

case rate
0 → 0 reset
0 → ≠0 set
≠0 → 0 reset
≠0 → ≠0 reset

Is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= 0 -> 0

also leads to storage_clears ++ which is later handled by inter-vm rules

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, thanks.

}

self.ext.set_storage(key, val).map_err(|_| Error::StorageUpdateError)?;

if former_val != H256::zero() && val == H256::zero() {
self.ext.inc_sstore_clears();
}

Ok(())
}

Expand Down
47 changes: 47 additions & 0 deletions ethcore/wasm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,53 @@ fn math_div() {
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
// varios blockchain runtime methods
#[test]
Expand Down