From 8f62a370db01e4a17bee19abb92141ff220b6d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Leegwater=20Sim=C3=B5es?= Date: Tue, 21 Nov 2023 11:28:31 +0100 Subject: [PATCH] piecrust: add `root_equal_on_err` --- piecrust/Cargo.toml | 5 ++++ piecrust/tests/commit.rs | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/piecrust/Cargo.toml b/piecrust/Cargo.toml index f78fc1f0..27d9e3bd 100644 --- a/piecrust/Cargo.toml +++ b/piecrust/Cargo.toml @@ -46,6 +46,11 @@ name = "crossover" path = "tests/crossover.rs" required-features = ["debug"] +[[test]] +name = "commit" +path = "tests/commit.rs" +required-features = ["debug"] + [[test]] name = "debugger" path = "tests/debugger.rs" diff --git a/piecrust/tests/commit.rs b/piecrust/tests/commit.rs index 9ddb979b..5408dc70 100644 --- a/piecrust/tests/commit.rs +++ b/piecrust/tests/commit.rs @@ -232,6 +232,66 @@ fn multiple_commits() -> Result<(), Error> { Ok(()) } +#[test] +fn root_equal_on_err() -> Result<(), Error> { + let vm = VM::ephemeral()?; + + let mut session = vm.session(SessionData::builder())?; + + let callcenter_id = session.deploy( + contract_bytecode!("callcenter"), + ContractData::builder(OWNER), + LIMIT, + )?; + let counter_id = session.deploy( + contract_bytecode!("counter"), + ContractData::builder(OWNER), + LIMIT, + )?; + + let root = session.commit()?; + + let mut session = vm.session(SessionData::builder().base(root))?; + let mut session_alt = vm.session(SessionData::builder().base(root))?; + + assert_eq!( + session.root(), + session_alt.root(), + "Roots should be equal at the beginning" + ); + + session + .call::<_, ()>(callcenter_id, "panik", &counter_id, LIMIT) + .expect_err("Calling with too little gas should error"); + + assert_eq!( + session.root(), + session_alt.root(), + "Roots should be equal immediately after erroring call" + ); + + session.call::<_, ()>( + callcenter_id, + "increment_counter", + &counter_id, + LIMIT, + )?; + session_alt.call::<_, ()>( + callcenter_id, + "increment_counter", + &counter_id, + LIMIT, + )?; + + assert_eq!( + session.root(), + session_alt.root(), + "Roots should be equal after call" + ); + + Ok(()) +} + fn increment_counter_and_commit( mut session: Session, id: ContractId,