Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gtest): Improve value and ED managment #4111

Merged
merged 35 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
a60dc13
State todos
techraed Aug 1, 2024
5b040b1
Change block execution model
techraed Aug 7, 2024
32ced45
State proper todos
techraed Aug 7, 2024
b23c60a
Fix most of workspace tests
techraed Aug 8, 2024
7d8ac30
Adjust signals test
techraed Aug 9, 2024
389bd45
Merge branch 'master' of github.com:gear-tech/gear into st-block-exec…
techraed Aug 9, 2024
17bcfa2
Fix workspace tests
techraed Aug 9, 2024
aeabdb2
Fix test
techraed Aug 9, 2024
977913e
feat(gtest): Decrease value after message sent
ByteNacked Aug 4, 2024
d42557b
WIP
ByteNacked Aug 11, 2024
aad49be
Update tests
ByteNacked Aug 11, 2024
2b52970
Update test, fix ED charging
ByteNacked Aug 12, 2024
322bd57
Clean up, return send_value on child program creation
ByteNacked Aug 12, 2024
6d8115d
Fix mailbox test
ByteNacked Aug 12, 2024
cd44374
Update cargo-gbuild tests
ByteNacked Aug 12, 2024
1867a54
Fix CI
techraed Aug 12, 2024
9183214
Review fixes
ByteNacked Aug 12, 2024
6eda532
Update gtest doc-comments
ByteNacked Aug 12, 2024
032c74e
Merge branch 'master' of github.com:gear-tech/gear into st-block-exec…
techraed Aug 13, 2024
e684bb4
Merge remote-tracking branch 'origin/st-block-exec-gtest' into rmasl-…
ByteNacked Aug 13, 2024
7ce2a48
Fix tests
ByteNacked Aug 13, 2024
e66c7e7
Fix tests II
ByteNacked Aug 13, 2024
8edf691
Add test and additional check
ByteNacked Aug 13, 2024
3fb0d65
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 14, 2024
e2594b2
Fix doc-comment
ByteNacked Aug 14, 2024
09e7882
refactor Actors storages
ByteNacked Aug 15, 2024
e32e4ee
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 15, 2024
fffbe78
Fix fn withdraw_gas params
ByteNacked Aug 15, 2024
26548fc
Update gtest/src/lib.rs
ByteNacked Aug 19, 2024
ccd3104
Review fixes
ByteNacked Aug 19, 2024
6ddf71a
Review fixes II
ByteNacked Aug 19, 2024
9421b16
Merge remote-tracking branch 'origin/master' into rmasl-gtest-improve…
ByteNacked Aug 19, 2024
820cbd1
Fix docs
ByteNacked Aug 19, 2024
8c17db4
Fix docs II
ByteNacked Aug 19, 2024
1317078
Review fixes III
ByteNacked Aug 19, 2024
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
14 changes: 8 additions & 6 deletions examples/autoreply/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ mod tests {
let from = 42;

// Init Program-1
let res = prog1.send(from, ActorId::zero());
assert!(!res.main_failed());
let init_msg1 = prog1.send(from, ActorId::zero());

// Init Program-2 with Program-1 as destination
let prog2 = Program::current(&system);
let res = prog2.send(from, prog1_id);
assert!(!res.main_failed());
let init_msg2 = prog2.send(from, prog1_id);

// Send a message from Program-2 to Program-1
let res = prog2.send_bytes(from, b"Let's go!");
assert!(!res.main_failed());
let msg3 = prog2.send_bytes(from, b"Let's go!");

let res = system.run_next_block();
for msg in [init_msg1, init_msg2, msg3] {
assert!(res.succeed.contains(&msg));
}

// Check whether the auto-reply was received
let reply_received: bool = prog2
Expand Down
10 changes: 7 additions & 3 deletions examples/custom/src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ mod tests {

let from = 42;

let res = program.send(from, InitMessage::BTree);
program.send(from, InitMessage::BTree);
let res = system.run_next_block();
let log = Log::builder().source(program.id()).dest(from);
assert!(res.contains(&log));
}
Expand All @@ -119,7 +120,7 @@ mod tests {

let from = 42;

let _res = program.send(from, InitMessage::BTree);
program.send(from, InitMessage::BTree);

IntoIterator::into_iter([
Request::Insert(0, 1),
Expand All @@ -131,7 +132,10 @@ mod tests {
Request::Clear,
Request::List,
])
.map(|r| program.send(from, r))
.map(|r| {
program.send(from, r);
system.run_next_block()
})
.zip(IntoIterator::into_iter([
Reply::Value(None),
Reply::Value(Some(1)),
Expand Down
40 changes: 26 additions & 14 deletions examples/distributor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ mod tests {

let from = 42;

let res = program.send_bytes(from, b"init");
program.send_bytes(from, b"init");
let res = system.run_next_block();
let log = Log::builder().source(program.id()).dest(from);
assert!(res.contains(&log));
}
Expand All @@ -83,16 +84,19 @@ mod tests {

let from = 42;

let _res = program.send_bytes(from, b"init");
// Init
program.send_bytes(from, b"init");

let res = program.send(from, Request::Receive(10));
program.send(from, Request::Receive(10));
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program.send(from, Request::Report);
program.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
Expand All @@ -111,22 +115,24 @@ mod tests {
let from = 42;

let program_1 = Program::current_with_id(system, program_1_id);
let _res = program_1.send_bytes(from, b"init");
program_1.send_bytes(from, b"init");

let program_2 = Program::current_with_id(system, program_2_id);
let _res = program_2.send_bytes(from, b"init");
program_2.send_bytes(from, b"init");

let program_3 = Program::current_with_id(system, program_3_id);
let _res = program_3.send_bytes(from, b"init");
program_3.send_bytes(from, b"init");

let res = program_1.send(from, Request::Join(program_2_id.into()));
program_1.send(from, Request::Join(program_2_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Join(program_3_id.into()));
program_1.send(from, Request::Join(program_3_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand All @@ -143,21 +149,24 @@ mod tests {

let from = 42;

let res = program_1.send(from, Request::Receive(11));
program_1.send(from, Request::Receive(11));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_2.send(from, Request::Report);
program_2.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program_2.id())
.dest(from)
.payload(Reply::Amount(5));
assert!(res.contains(&log));

let res = program_1.send(from, Request::Report);
program_1.send(from, Request::Report);
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand All @@ -175,10 +184,13 @@ mod tests {
let from = 42;

let program_4 = Program::current_with_id(&system, program_4_id);
let _res = program_4.send_bytes(from, b"init");
program_4.send_bytes(from, b"init");

IntoIterator::into_iter([Request::Receive(11), Request::Join(program_4_id.into())])
.map(|request| program_1.send(from, request))
.map(|request| {
program_1.send(from, request);
system.run_next_block()
})
.zip(IntoIterator::into_iter([Reply::Success, Reply::Success]))
.for_each(|(result, reply)| {
let log = Log::builder()
Expand Down
18 changes: 14 additions & 4 deletions examples/gas-burned/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send_bytes(from, "init");
let init_gas_burned = res.main_gas_burned();
let init_msg_id = program.send_bytes(from, "init");
let res = system.run_next_block();
let init_gas_burned = res
.gas_burned
.get(&init_msg_id)
.copied()
.expect("internal error: init message isn't sent");
log::debug!("Init gas burned: {init_gas_burned}");
assert!(init_gas_burned > Gas::zero());

let res = program.send_bytes(from, "handle");
let handle_gas_burned = res.main_gas_burned();
let handle_msg_id = program.send_bytes(from, "handle");
let res = system.run_next_block();
let handle_gas_burned = res
.gas_burned
.get(&handle_msg_id)
.copied()
.expect("internal error: init message isn't sent");
log::debug!("Handle gas burned: {handle_gas_burned}");
assert!(handle_gas_burned > init_gas_burned);
}
Expand Down
1 change: 1 addition & 0 deletions examples/new-meta/tests/read_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,6 @@ fn initialize_current_program(system: &System) -> Program {
currency: "USD".into(),
},
);
system.run_next_block();
program
}
39 changes: 24 additions & 15 deletions examples/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send(from, Request::IsReady);
assert!(res.main_failed());
let msg_id = program.send(from, Request::IsReady);
let res = system.run_next_block();
assert!(res.failed.contains(&msg_id));
}

#[test]
Expand All @@ -83,9 +84,10 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let res = program.send(from, Initialization { status: 5 });
let msg_id = program.send(from, Initialization { status: 5 });
let log = Log::builder().source(program.id()).dest(from);
assert!(!res.main_failed());
let res = system.run_next_block();
assert!(res.succeed.contains(&msg_id));
assert!(res.contains(&log));
}

Expand All @@ -97,23 +99,26 @@ mod tests {
let from = 42;

let program = Program::current(&system);
let _res = program.send(from, Initialization { status: 5 });
program.send(from, Initialization { status: 5 });

let res = program.send(from, Request::IsReady);
program.send(from, Request::IsReady);
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Yes);
let res = system.run_next_block();
assert!(res.contains(&log));

let res = program.send(from, Request::Begin(Operation { to_status: 7 }));
program.send(from, Request::Begin(Operation { to_status: 7 }));
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program.send(from, Request::Commit);
program.send(from, Request::Commit);
let res = system.run_next_block();
let log = Log::builder()
.source(program.id())
.dest(from)
Expand All @@ -133,36 +138,40 @@ mod tests {
let program_3_id = 3;

let program_1 = Program::current_with_id(&system, program_1_id);
let _res = program_1.send(from, Initialization { status: 5 });
program_1.send(from, Initialization { status: 5 });

let program_2 = Program::current_with_id(&system, program_2_id);
let _res = program_2.send(from, Initialization { status: 5 });
program_2.send(from, Initialization { status: 5 });

let program_3 = Program::current_with_id(&system, program_3_id);
let _res = program_3.send(from, Initialization { status: 9 });
program_3.send(from, Initialization { status: 9 });

let res = program_1.send(from, Request::Add(program_2_id.into()));
program_1.send(from, Request::Add(program_2_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Add(program_3_id.into()));
program_1.send(from, Request::Add(program_3_id.into()));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Begin(Operation { to_status: 7 }));
program_1.send(from, Request::Begin(Operation { to_status: 7 }));
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
.payload(Reply::Success);
assert!(res.contains(&log));

let res = program_1.send(from, Request::Commit);
program_1.send(from, Request::Commit);
let res = system.run_next_block();
let log = Log::builder()
.source(program_1.id())
.dest(from)
Expand Down
48 changes: 23 additions & 25 deletions examples/program-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ mod tests {
sys.mint_to(user_id, 100 * UNITS);

// Send `init` msg to factory
let res = factory.send_bytes_with_value(user_id, "EMPTY", 10 * UNITS);
assert!(!res.main_failed());
let msg_id = factory.send_bytes_with_value(user_id, "EMPTY", 10 * UNITS);
let res = sys.run_next_block();
assert!(res.succeed.contains(&msg_id));
assert!(sys.is_active_program(100));

factory
Expand All @@ -97,13 +98,11 @@ mod tests {
let factory = prepare_factory(&sys);

// Send `handle` msg to factory to create a new child
let res = factory.send_bytes(10001, CreateProgram::Default.encode());
let child_id_expected = calculate_program_id(
CHILD_CODE_HASH.into(),
&0i32.to_le_bytes(),
Some(res.sent_message_id()),
);
assert!(!res.main_failed());
let msg_id = factory.send_bytes(10001, CreateProgram::Default.encode());
let res = sys.run_next_block();
let child_id_expected =
calculate_program_id(CHILD_CODE_HASH.into(), &0i32.to_le_bytes(), Some(msg_id));
assert!(res.succeed.contains(&msg_id));
assert!(sys.is_active_program(child_id_expected));
}

Expand All @@ -117,24 +116,24 @@ mod tests {
let payload = CreateProgram::Custom(vec![(CHILD_CODE_HASH, salt.to_vec(), 100_000_000)]);

// Send `handle` msg to factory to create a new child
let res = factory.send_bytes(10001, payload.encode());
let msg_id = factory.send_bytes(10001, payload.encode());
let res = sys.run_next_block();

let child_id_expected =
calculate_program_id(CHILD_CODE_HASH.into(), &salt, Some(res.sent_message_id()));
let child_id_expected = calculate_program_id(CHILD_CODE_HASH.into(), &salt, Some(msg_id));

assert!(!res.main_failed());
assert!(res.succeed.contains(&msg_id));
assert!(sys.is_active_program(child_id_expected));

// Send `handle` msg to create a duplicate
let res = factory.send_bytes(10001, payload.encode());
let msg_id = factory.send_bytes(10001, payload.encode());
let res = sys.run_next_block();

let child_id_expected =
calculate_program_id(CHILD_CODE_HASH.into(), &salt, Some(res.sent_message_id()));
let child_id_expected = calculate_program_id(CHILD_CODE_HASH.into(), &salt, Some(msg_id));

assert!(!res.main_failed());
assert!(res.succeed.contains(&msg_id));
assert!(sys.is_active_program(child_id_expected));

assert_eq!(res.total_processed(), 3 + 1 + 1); // +1 for the original message, initiated by user +1 for auto generated replies
assert_eq!(res.total_processed, 3 + 1 + 1); // +1 for the original message, initiated by user +1 for auto generated replies
}

#[test]
Expand All @@ -147,13 +146,11 @@ mod tests {
let non_existing_code_hash = [10u8; 32];
let salt = b"some_salt";
let payload = CreateProgram::Custom(vec![(non_existing_code_hash, salt.to_vec(), 100_000)]);
let res = factory.send_bytes(10001, payload.encode());
let fictional_program_id = calculate_program_id(
non_existing_code_hash.into(),
salt,
Some(res.sent_message_id()),
);
assert!(!res.main_failed());
let msg_id = factory.send_bytes(10001, payload.encode());
let res = sys.run_next_block();
let fictional_program_id =
calculate_program_id(non_existing_code_hash.into(), salt, Some(msg_id));
assert!(res.succeed.contains(&msg_id));
// No new program with fictional id
assert!(!sys.is_active_program(fictional_program_id));
}
Expand All @@ -175,5 +172,6 @@ mod tests {
100_000,
)]);
factory.send_bytes(10001, payload.encode());
let _ = sys.run_next_block();
}
}
Loading