Skip to content

Commit

Permalink
Testing ink! contracts (Polkadot-Blockchain-Academy#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 authored Jan 12, 2024
1 parent 09909f1 commit 3e6cc8c
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 261 deletions.
261 changes: 0 additions & 261 deletions syllabus/4-Smart_Contracts/6-Wasm_ink!_slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -1600,264 +1600,3 @@ impl ChainExtension<Runtime> for HeavyCryptoOutsourcingExtension {
## Chain extension: reaching even further
<img style="margin-top: 100px;margin-bottom: 50px" width="800" src="./img/ink/chain-extension-reach.svg" />
---
## Testing contracts
---
## Testing contracts
<br/>
<img style="margin-top: 100px;margin-bottom: 50px" width="800" src="./img/ink/blockchain-onion-3.svg" />
---
## Testing contracts
<img style="margin-top: 100px;margin-bottom: 50px" width="1000" src="./img/ink/testing-contract-stack.svg" />
---
## Unit tests
<br/>
```rust [1-3]
#[ink::test]
fn erc20_transfer_works() {
let mut erc20 = Erc20::new(100);
assert_eq!(erc20.balance_of(BOB), 0);
// Alice transfers 10 tokens to Bob.
assert_eq!(erc20.transfer(BOB, 10), Ok(()));
// Bob owns 10 tokens.
assert_eq!(erc20.balance_of(BOB), 10);
let emitted_events = ink::env::test::recorded_events().collect::<Vec<_>>();
assert_eq!(emitted_events.len(), 2);
// Check first transfer event related to ERC-20 instantiation.
assert_transfer_event(
&emitted_events[0], None, Some(ALICE), 100,
);
// Check the second transfer event relating to the actual transfer.
assert_transfer_event(
&emitted_events[1], Some(ALICE), Some(BOB), 10,
);
}
```
---
## Unit tests
<br/>
```rust [5-9]
#[ink::test]
fn erc20_transfer_works() {
let mut erc20 = Erc20::new(100);
assert_eq!(erc20.balance_of(BOB), 0);
// Alice transfers 10 tokens to Bob.
assert_eq!(erc20.transfer(BOB, 10), Ok(()));
// Bob owns 10 tokens.
assert_eq!(erc20.balance_of(BOB), 10);
let emitted_events = ink::env::test::recorded_events().collect::<Vec<_>>();
assert_eq!(emitted_events.len(), 2);
// Check first transfer event related to ERC-20 instantiation.
assert_transfer_event(
&emitted_events[0], None, Some(ALICE), 100,
);
// Check the second transfer event relating to the actual transfer.
assert_transfer_event(
&emitted_events[1], Some(ALICE), Some(BOB), 10,
);
}
```
---
## Unit tests
<br/>
```rust [11-22]
#[ink::test]
fn erc20_transfer_works() {
let mut erc20 = Erc20::new(100);
assert_eq!(erc20.balance_of(BOB), 0);
// Alice transfers 10 tokens to Bob.
assert_eq!(erc20.transfer(BOB, 10), Ok(()));
// Bob owns 10 tokens.
assert_eq!(erc20.balance_of(BOB), 10);
let emitted_events = ink::env::test::recorded_events().collect::<Vec<_>>();
assert_eq!(emitted_events.len(), 2);
// Check first transfer event related to ERC-20 instantiation.
assert_transfer_event(
&emitted_events[0], None, Some(ALICE), 100,
);
// Check the second transfer event relating to the actual transfer.
assert_transfer_event(
&emitted_events[1], Some(ALICE), Some(BOB), 10,
);
}
```
---
## E2E tests
<br/>
```rust [1-7]
#[ink_e2e::test]
async fn e2e_transfer(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let constructor = Erc20Ref::new(total_supply);
let erc20 = client
.instantiate("erc20", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed");
let mut call = erc20.call::<Erc20>();
let total_supply_msg = call.total_supply();
let total_supply_res = client
.call_dry_run(&ink_e2e::bob(), &total_supply_msg, 0, None)
.await;
...
}
```
---
## E2E tests
<br/>
```rust [9-13]
#[ink_e2e::test]
async fn e2e_transfer(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let constructor = Erc20Ref::new(total_supply);
let erc20 = client
.instantiate("erc20", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed");
let mut call = erc20.call::<Erc20>();
let total_supply_msg = call.total_supply();
let total_supply_res = client
.call_dry_run(&ink_e2e::bob(), &total_supply_msg, 0, None)
.await;
...
}
```
---
## E2E tests
<br/>
```rust [14]
#[ink_e2e::test]
async fn e2e_transfer(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let constructor = Erc20Ref::new(total_supply);
let erc20 = client
.instantiate("erc20", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed");
let mut call = erc20.call::<Erc20>();
let total_supply_msg = call.total_supply();
let total_supply_res = client
.call_dry_run(&ink_e2e::bob(), &total_supply_msg, 0, None)
.await;
...
}
```
---
## E2E pipeline: traps, traps everywhere
<div style="font-size: 0.6em">
1. Preparing and encoding transaction data (_client side_)
1. Signing the transaction (_client side_)
1. Sending transaction to a node (_client side_)
1. Block and event subscribing (_client side_)
1. Transaction pool processing (_node side_)
1. Block building (_node side_)
1. Block dissemination (_node side_)
1. Import queue processing (_node side_)
1. Block finalizing (_node side_)
1. Block execution (_node side_)
1. Transaction execution (_runtime side_)
1. Event emitting (_node side_)
1. Event capturing (_client side_)
1. Event processing (_client side_)
1. State fetching via RPC calling (_client side_)
1. State report (_node side_)
1. State validation (_client side_)
</div>
---
## E2E pipeline: traps, traps everywhere
<img style="margin-top: 100px;margin-bottom: 50px" width="800" src="./img/ink/trap.gif" />
---
## Test core
<br/>
1. Preparing and encoding transaction data (_given_)
1. Transaction execution (_when_)
1. State validation (_then_)
---
## quasi-E2E tests
<br/>
Interact directly with runtime, skipping node layer.
---
## quasi-E2E tests
<br/>
```rust
#[test]
fn flipping() -> Result<(), Box<dyn Error>> {
let init_value = Session::<MinimalRuntime>::new(transcoder())?
.deploy_and(bytes(), "new", &["true".to_string()], vec![])?
.call_and("flip", &[])?
.call_and("flip", &[])?
.call_and("flip", &[])?
.call_and("get", &[])?
.last_call_return()
.expect("Call was successful");
assert_eq!(init_value, ok(Value::Bool(false)));
Ok(())
}
```
---
## Local playing with contracts using `drink-cli`
Loading

0 comments on commit 3e6cc8c

Please sign in to comment.