Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gists-immunefi/cff368edc61eea5231d011aa3fce6c14 to your computer and use it in GitHub Desktop.
Save gists-immunefi/cff368edc61eea5231d011aa3fce6c14 to your computer and use it in GitHub Desktop.
#[test]
fn use_ccp_to_memory_clear() {
let mut test_context = TestBuilder::new(2322u64);
let gas_limit = 10_000_000;
let program = vec![
op::ret(RegId::ZERO), // super short contract
];
let contract_id = test_context.setup_contract(program, None, None).contract_id;
let (script, _) = script_with_data_offset!(
data_offset,
vec![
op::movi(0x10, data_offset as Immediate18), //pointer to address
op::sub(0x11, RegId::HP, RegId::SP), //store size of unallocated memory in register 0x11
op::subi(0x12, 0x11, 1), //pointer to last writeable byte
op::movi(0x13, 0xff), //value to write
op::cfe(0x11), //extend the stack to fill the whole memory
op::log(RegId::CGAS, 0x00, 0x00, 0x00), //log remaining gas
// following block uses mcl to clear memory:
op::sb(0x12, 0x13, 0), //write to last writeable byte
op::lb(0x14, 0x12, 0), //load the value back to check with log
op::log(0x00, 0x00, 0x00, 0x14), //log the value
op::mcl(RegId::SSP, 0x11), //clear whole area between SSP and SP
op::lb(0x14, 0x12, 0), //load last writeable byte to check if it was cleared
op::log(RegId::CGAS, 0x00, 0x00, 0x14), //log remaining gas and check that value was used
// following block uses ccp to clear memory:
op::sb(0x12, 0x13, 0), //repeat write to last writeable byte
op::lb(0x14, 0x12, 0), //repeat load the value back to check with log
op::log(0x00, 0x00, 0x00, 0x14), //log the value
op::ccp(RegId::SSP, 0x10, 0x11, 0x11), //clear whole area between SSP and SP (dst, pointer to contractId, code offset, length)
op::lb(0x14, 0x12, 0), //load last writeable byte to check if it was cleared
op::log(RegId::CGAS, 0x00, 0x00, 0x14), //log remaining gas and check that value was used
op::ret(RegId::ONE),
],
test_context.get_tx_params().tx_offset()
);
let mut script_data = contract_id.to_vec();
script_data.extend([0u8; WORD_SIZE * 2]);
let result = test_context
.start_script(script, script_data)
.script_gas_limit(gas_limit)
.contract_input(contract_id)
.fee_input()
.contract_output(&contract_id)
.execute();
let receipts = result.receipts();
//print receipts
for receipt in receipts.iter() {
println!("{:?}", receipt);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment