Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

feat: cleanup, fix and enhance log asm tests #161

Merged
merged 8 commits into from
Jun 6, 2024
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
192 changes: 66 additions & 126 deletions src/tests/simple_tests/log.rs
Original file line number Diff line number Diff line change
@@ -1,153 +1,93 @@
use super::*;

#[test_log::test]
fn test_out_of_ergs_l1_message() {
let asm = r#"
.text
.file "Test_26"
.rodata.cst32
.p2align 5
.text
.globl __entry
__entry:
.main:
add 10000, r0, r1
add 1000, r0, r10
sstore r1, r10
event r1, r10
to_l1 r1, r10
context.set_ergs_per_pubdata r10
near_call r1, @inner, @handler
context.ergs_left r15
ret.ok r0
inner:
to_l1 r0, r1
ret.ok r0
handler:
ret.ok r0
"#;

run_and_try_create_witness_inner(asm, 50);
}

#[cfg(test)]
mod tests {
use crate::tests::simple_tests::asm_tests::run_asm_based_test;
use crate::tests::simple_tests::run_manually::Options;
use crate::tests::simple_tests::{asm_tests::run_asm_based_test, Options};

#[test_log::test]
/// Tests the case where we run out of gas during the precompile execution.
fn test_precompile_out_of_gas() {
fn test_snapshot_every_cycle(dir: &str, additional_contracts: &[i32]) {
run_asm_based_test(
"src/tests/simple_tests/testdata/log_precompile",
&[],
&format!("src/tests/simple_tests/testdata/log/{}", dir),
additional_contracts,
Options {
// Do only 1 cycle per VM snapshot to really test all the boundary conditions.
cycles_per_vm_snapshot: 1,
..Default::default()
},
)
}
fn test_common(dir: &str) {

#[test_log::test]
/// Tests the case where we do not have enough gas to send a message to l1
fn test_log_l1_message_out_of_gas() {
run_asm_based_test(
&format!("src/tests/simple_tests/testdata/{}", dir),
&[800000],
Options {
cycles_per_vm_snapshot: 1,
..Default::default()
},
"src/tests/simple_tests/testdata/log/l1_message_out_of_gas",
&[],
Default::default(),
)
}

#[test_log::test]
fn test_decommit_invalid() {
test_common("decommit_invalid")
fn test_log_l1_message_has_zero_pubdata_cost() {
run_asm_based_test(
"src/tests/simple_tests/testdata/log/l1_message_has_zero_pubdata_cost",
&[],
Default::default(),
)
}

#[test_log::test]
fn test_decommit_ok() {
test_common("decommit_ok");
test_common("decommit_ok_with_panic");
/// Tests the case where we run out of gas during the precompile execution.
fn test_log_precompile_out_of_gas() {
test_snapshot_every_cycle("precompile_out_of_gas", &[]);
}
}

#[test_log::test]
fn test_write_same_value() {
let asm = r#"
.text
.file "Test_26"
.rodata.cst32
.p2align 5
.text
.globl __entry
__entry:
.main:
near_call r0, @inner, @handler
context.ergs_left r15
ret.ok r0
inner:
add 10000, r0, r1
add 1000, r0, r10
sstore r1, r10
sstore r1, r0
ret.ok r0
handler:
ret.ok r0
"#;
#[test_log::test]
fn test_log_precompile_invalid_address() {
test_snapshot_every_cycle("precompile_invalid_address", &[65399]);
}

run_and_try_create_witness_inner(asm, 50);
}
#[test_log::test]
fn test_log_decommit_invalid() {
test_snapshot_every_cycle("decommit_invalid", &[800000])
}

#[test_log::test]
fn test_rollback_to_same_value_no_reads() {
let asm = r#"
.text
.file "Test_26"
.rodata.cst32
.p2align 5
.text
.globl __entry
__entry:
.main:
near_call r0, @inner, @handler
context.ergs_left r15
ret.ok r0
inner:
add 10000, r0, r1
add 1000, r0, r10
sstore r1, r10
ret.panic r0
handler:
ret.ok r0
"#;
#[test_log::test]
fn test_log_decommit_ok() {
test_snapshot_every_cycle("decommit_ok", &[800000]);
}

run_and_try_create_witness_inner(asm, 50);
}
#[test_log::test]
fn test_log_decommit_ok_twice() {
test_snapshot_every_cycle("decommit_ok_twice", &[800000]);
}

#[test_log::test]
fn test_log_decommit_ok_with_panic() {
test_snapshot_every_cycle("decommit_ok_with_panic", &[800000]);
}

#[test_log::test]
fn test_log_storage_clear_slot() {
run_asm_based_test(
"src/tests/simple_tests/testdata/log/storage/storage_clear_slot",
&[],
Default::default(),
)
}

#[test_log::test]
fn test_rollback_to_same_value_with_reads() {
let asm = r#"
.text
.file "Test_26"
.rodata.cst32
.p2align 5
.text
.globl __entry
__entry:
.main:
near_call r1, @inner, @handler
context.ergs_left r15
ret.ok r0
inner:
add 10000, r0, r1
add 1000, r0, r10
sstore r1, r10
ret.panic r0
handler:
add 10000, r0, r1
sload r1, r2
ret.ok r0
"#;
#[test_log::test]
fn test_log_storage_write_rollback_no_reads() {
run_asm_based_test(
"src/tests/simple_tests/testdata/log/storage/storage_write_rollback_no_reads",
&[],
Default::default(),
)
}

run_and_try_create_witness_inner(asm, 50);
#[test_log::test]
fn test_log_storage_write_rollback_reads() {
run_asm_based_test(
"src/tests/simple_tests/testdata/log/storage/storage_write_rollback_reads",
&[],
Default::default(),
)
}
}
9 changes: 5 additions & 4 deletions src/tests/simple_tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod tests {
#[test_log::test]
fn test_pubdata_and_storage_writes() {
run_asm_based_test(
"src/tests/simple_tests/testdata/storage_writes",
"src/tests/simple_tests/testdata/log/storage/storage_writes",
&[],
Options {
cycles_per_vm_snapshot: 1,
Expand All @@ -17,7 +17,7 @@ mod tests {
#[test_log::test]
fn test_storage_reads() {
run_asm_based_test(
"src/tests/simple_tests/testdata/storage_reads",
"src/tests/simple_tests/testdata/log/storage/storage_reads",
&[],
Options {
// Do only 1 cycle per VM snapshot to really test all the boundary conditions.
Expand All @@ -28,11 +28,12 @@ mod tests {
}

#[test_log::test]
fn test_pubdata_refunds() {
fn test_storage_pubdata_refunds() {
run_asm_based_test(
"src/tests/simple_tests/testdata/pubdata_refunds",
"src/tests/simple_tests/testdata/log/storage/storage_pubdata_refunds",
&[],
Options {
// Do only 1 cycle per VM snapshot to really test all the boundary conditions.
cycles_per_vm_snapshot: 1,
..Default::default()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
.rodata.cst32
.p2align 5
CPI0_0:
; this is the hash of the contract in 80000.asm
; this is the hash of the contract in 800000.asm
.cell 452312938437537823148903869859771978505772238111866864847149311043017845250
.text
.globl __entry
__entry:
.main:

add 10000, r0, r4

near_call r4, @inner, @handler

ret.ok r0

inner:
Expand All @@ -27,14 +26,13 @@
sub.s 2000, r9, r11
; assert(r9-2000 >= r10) - make sure that we really burned 2k gas
sub! r11, r10, r0
jump.lt @.panic

jump.lt @invalid_gas_burn

ret.ok r0

handler:
ret.panic r0

.panic:
ret.panic r0
invalid_gas_burn:
revert("Invalid gas burn in decommit")

10 changes: 10 additions & 0 deletions src/tests/simple_tests/testdata/log/decommit_ok_twice/800000.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.text
.file "Test_26"
.rodata.cst32
.p2align 5
.text
.globl __entry
__entry:
.main:
; empty contract
ret.ok r0
59 changes: 59 additions & 0 deletions src/tests/simple_tests/testdata/log/decommit_ok_twice/entry.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.text
.file "Test_26"
.rodata.cst32
.p2align 5
HASHES:
; this is the hash of the contract in 800000.asm
.cell <800000.asm>
ADDRESSES:
.cell 800000
.text
.globl __entry
__entry:
.main:
; create ABI fo far call
; use 0 for forwarding mode
add 0, r0, r1
shl.s 32, r1, r1
; give 10k gas
add 10000, r1, r1
shl.s 96, r1, r1
add 36, r1, r1
shl.s 32, r1, r1
add 64, r1, r1
shl.s 64, r1, r1

add @ADDRESSES[0], r0, r2

; far call should also decommit 800000
far_call r1, r2, @panic

; now we are trying to decommit it again
add 15000, r0, r4
near_call r4, @inner, @panic

ret.ok r0

inner:
add @HASHES[0], r0, r1
; add extra cost, we expect it to be refunded
; since we are decommiting already decomitted contract
add 2000, r0, r2
context.ergs_left r9
log.decommit r1, r2, r3
context.ergs_left r10

; so after the call, we should have burned some gas and received 2k refund.
sub.s 2000, r9, r11
; assert(r9-2000 <= r10) - make sure that we did not burn 2k gas
sub! r11, r10, r0
jump.gt @invalid_gas_burn

ret.ok r0

panic:
ret.panic r0

invalid_gas_burn:
revert("Invalid gas burn in decommit")

Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
.rodata.cst32
.p2align 5
CPI0_0:
; this is the hash of the contract in 80000.asm
; this is the hash of the contract in 800000.asm
.cell 452312938437537823148903869859771978505772238111866864847149311043017845250
.text
.globl __entry
__entry:
.main:

add 10000, r0, r4

near_call r4, @inner, @handler
; We should never get here - as the near_call should panic due to out of gas.
ret.panic r0

revert("Near call not reverted")

inner:
add @CPI0_0[0], r0, r1
Expand All @@ -28,7 +26,4 @@
handler:
; we expect the near_call to panic
ret.ok r0

.panic:
ret.panic r0

Loading
Loading