From 3de78fd575a550f2f0bf5de5cc3b1661bc02528d Mon Sep 17 00:00:00 2001 From: Ilyar <761285+ilyar@users.noreply.github.com> Date: Sun, 13 Aug 2023 16:55:32 +0200 Subject: [PATCH] add test --- .gitignore | 4 ++++ asset/Makefile | 4 ++++ asset/simple.code | 4 ++++ src/lib.rs | 1 + src/test.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) create mode 100644 asset/Makefile create mode 100644 asset/simple.code create mode 100644 src/test.rs diff --git a/.gitignore b/.gitignore index 2e6beaa..3b063db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# build artifacts +asset/*.boc +asset/*.dbg.json + # Generated by Cargo # will have compiled files and executables target/ diff --git a/asset/Makefile b/asset/Makefile new file mode 100644 index 0000000..9204076 --- /dev/null +++ b/asset/Makefile @@ -0,0 +1,4 @@ +setup: + cargo install --git https://github.com/tonlabs/ever-assembler.git +build: + asm simple.code diff --git a/asset/simple.code b/asset/simple.code new file mode 100644 index 0000000..1e71bda --- /dev/null +++ b/asset/simple.code @@ -0,0 +1,4 @@ +SETCP0 +PUSHINT 40 +PUSHINT 2 +ADD diff --git a/src/lib.rs b/src/lib.rs index a551c20..cd76bef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,5 +24,6 @@ pub mod smart_contract_info; pub use self::smart_contract_info::SmartContractInfo; pub mod error; pub mod utils; +mod test; include!("../common/src/info.rs"); diff --git a/src/test.rs b/src/test.rs new file mode 100644 index 0000000..dbe344f --- /dev/null +++ b/src/test.rs @@ -0,0 +1,59 @@ +use ton_types::{SliceData, Cell}; +use crate::executor::Engine; +use crate::stack::{savelist::SaveList, StackItem}; + +static DEFAULT_CAPABILITIES: u64 = 0x572e; + +fn read_boc(filename: &str) -> Vec { + let mut bytes = Vec::new(); + let mut file = std::fs::File::open(filename).unwrap(); + std::io::Read::read_to_end(&mut file, &mut bytes).unwrap(); + bytes +} + +fn load_boc(filename: &str) -> Cell { + let bytes = read_boc(filename); + ton_types::read_single_root_boc(bytes).unwrap() +} + +#[test] +fn test_simple() { + let code = load_boc("asset/simple.boc"); + let mut ctrls = SaveList::default(); + let params = vec!( + StackItem::int(0x76ef1ea), // magic - should be changed because of structure change + StackItem::int(0), // actions + StackItem::int(0), // msgs + StackItem::int(0), // unix time + StackItem::int(0), // logical time + StackItem::int(0), // transaction time + StackItem::int(0), // rand seed + StackItem::tuple(vec!( + StackItem::int(1000000000), // balance + StackItem::None // balance other + )), + StackItem::default(), // myself + StackItem::None, // global config params + StackItem::None, + StackItem::int(0), + ); + ctrls.put(7, &mut StackItem::tuple(vec!(StackItem::tuple(params)))).unwrap(); + + let mut engine = Engine::with_capabilities(DEFAULT_CAPABILITIES).setup_with_libraries( + SliceData::load_cell_ref(&code).unwrap(), + Some(ctrls), + None, + None, + vec!()); + engine.dump_ctrls(false); + engine.execute().unwrap(); + let stack = engine.stack().get(0).as_integer().unwrap(); + println!("stack {:?}", stack); + println!("C3 {:?}", engine.ctrl(3)); + let output = engine.ctrl(4).unwrap().as_cell().unwrap(); + println!("C4 {:?}", output); + let actions = engine.ctrl(5).unwrap().as_cell().unwrap(); + println!("C5 {:?}", actions); + println!("{:?}", engine.gas_used()); + assert_eq!(engine.gas_used(), 93); +}