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

default_accounts #36

Merged
merged 7 commits into from
Sep 8, 2023
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
32 changes: 32 additions & 0 deletions test-cases/default-accounts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "default-accounts"
version = "0.1.0"
edition = "2021"
authors = ["[your_name] <[your_email]>"]

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []

[dependencies]
ink = { version = "4.2.1", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = "4.2.1"

[profile.dev]
overflow-checks = false

[profile.release]
overflow-checks = false
44 changes: 44 additions & 0 deletions test-cases/default-accounts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Function `default_accounts`

## Description

The aim of this function is to provide default test accounts, each given a fantasy name from "A" to "F" (Alice, Bob, etc.). The intention is for these accounts to exhibit determinism, thereby ensuring a predictable test environment. In this sense, it is reasonable to anticipate obtaining the same addresses both in Integration and in End-to-End scenarios.

On Integration, function resides within [`ink_env::test`](https://paritytech.github.io/ink/ink_env/test/index.html)

```rust
pub fn default_accounts<T>() -> DefaultAccounts<T>
```

and each address holds different amounts of value. There are in total 6 test default accounts ("A" to "F").

On End-to-End there's no such implementation but an [`ink_e2e::AccountKeyring`](https://paritytech.github.io/ink/ink_e2e/enum.AccountKeyring.html) `enum` type which provides the default test accounts. There are in total 8 test default accounts ("A" to "F" and two more extra).

## Related ink! functions and types

- [`default_accounts`](https://paritytech.github.io/ink/ink_env/test/fn.default_accounts.html)
- [`DefaultAccounts`](https://paritytech.github.io/ink/ink_env/test/struct.DefaultAccounts.html)
- [`AccountKeyring`](https://paritytech.github.io/ink/ink_e2e/enum.AccountKeyring.html)

## Test case

There is a test case corresponding to each default account in the End-to-End environment, checking that the address is the same in Integration one. After this exploration-instance, it is desibrable to also examine balance consistency.

| \# | Test | Integration | E2E |
| --- | --------------------------------------------------------------- | :---------: | :-: |
| 1 | The "A" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "B" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "C" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "D" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "E" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "F" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "One" default account in Integration is the same as in E2E. | ❌ | ✅ |
| 2 | The "Two" default account in Integration is the same as in E2E. | ❌ | ✅ |

## Comparison Integration vs E2E

All default accounts addresses are different. Furthermore, there is a disparity in the fantasy names assigned to accounts "D" and "F." Additionally, the default accounts "One" and "Two" are two additional accounts present in E2E but absent in the Integration environment.

## Result

This is relatively trivial. It can be implemented in a single day. For backwards compatibility django and frank should retain their current names, and dave and ferdie added as their respective aliases.
132 changes: 132 additions & 0 deletions test-cases/default-accounts/lib.rs
sofinico marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[ink::contract]
mod custom_default_accounts {

#[ink(storage)]
pub struct CustomDefaultAccounts {}

impl CustomDefaultAccounts {
/// Creates a new Template contract.
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}

#[ink(message)]
pub fn message(&self) {}
}

impl Default for CustomDefaultAccounts {
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
use ink::env::test::DefaultAccounts;
use ink_e2e;

#[ink::test]
fn test_alice_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_alice_account_id = integration_test_accounts.alice;

let e2e_alice_account_id: AccountId =
ink_e2e::AccountKeyring::Alice.to_raw_public().into();

assert_eq!(integration_alice_account_id, e2e_alice_account_id);
}

#[ink::test]
fn test_bob_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_bob_account_id = integration_test_accounts.bob;

let e2e_bob_account_id: AccountId = ink_e2e::AccountKeyring::Bob.to_raw_public().into();

assert_eq!(integration_bob_account_id, e2e_bob_account_id);
}

#[ink::test]
fn test_charlie_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_charlie_account_id = integration_test_accounts.charlie;

let e2e_charlie_account_id: AccountId =
ink_e2e::AccountKeyring::Charlie.to_raw_public().into();

assert_eq!(integration_charlie_account_id, e2e_charlie_account_id);
}

#[ink::test]
fn test_dave_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_dave_account_id = integration_test_accounts.django;

let e2e_dave_account_id: AccountId =
ink_e2e::AccountKeyring::Dave.to_raw_public().into();

// There is no Django in the e2e test accounts, there is no Dave in the integration test accounts
assert_eq!(integration_dave_account_id, e2e_dave_account_id);
}

#[ink::test]
fn test_eve_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_eve_account_id = integration_test_accounts.eve;

let e2e_eve_account_id: AccountId = ink_e2e::AccountKeyring::Eve.to_raw_public().into();

assert_eq!(integration_eve_account_id, e2e_eve_account_id);
}

#[ink::test]
fn test_frank_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_frank_account_id = integration_test_accounts.frank;

let e2e_frank_account_id: AccountId =
ink_e2e::AccountKeyring::Ferdie.to_raw_public().into();

// There is no Frank in the e2e test accounts, there is no Ferdie in the integration test accounts
assert_eq!(integration_frank_account_id, e2e_frank_account_id);
}

#[ink::test]
fn test_one_account() {
let e2e_one_account_id: AccountId = ink_e2e::AccountKeyring::One.to_raw_public().into();

let integration_one_account_id: AccountId = AccountId::from([0x01; 32]);

println!(
"There is no One in Integration test accounts, which is \nOne: {:?}",
e2e_one_account_id
);

assert_eq!(integration_one_account_id, e2e_one_account_id);
}

#[ink::test]
fn test_two_account() {
let e2e_two_account_id: AccountId = ink_e2e::AccountKeyring::Two.to_raw_public().into();

let integration_two_account_id: AccountId = AccountId::from([0x02; 32]);

println!(
"There is no Two in Integration test accounts, which is \nTwo: {:?}",
e2e_two_account_id
);

assert_eq!(integration_two_account_id, e2e_two_account_id);
}
}
}