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

Make the default accounts in integration test and e2e the same addresses #1954

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ schnorrkel = { version = "0.11.2", optional = true }
scale-decode = { workspace = true, optional = true }
scale-encode = { workspace = true, optional = true }
scale-info = { workspace = true, features = ["derive"], optional = true }
sp-keyring = { workspace = true }

[dev-dependencies]
ink = { path = "../ink" }
Expand Down
40 changes: 26 additions & 14 deletions crates/env/src/engine/off_chain/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,31 +317,39 @@ where
.set_balance(scale::Encode::encode(&default_accounts.charlie), some);
instance
.engine
.set_balance(scale::Encode::encode(&default_accounts.django), 0);
.set_balance(scale::Encode::encode(&default_accounts.dave), 0);
instance
.engine
.set_balance(scale::Encode::encode(&default_accounts.eve), 0);
instance
.engine
.set_balance(scale::Encode::encode(&default_accounts.frank), 0);
.set_balance(scale::Encode::encode(&default_accounts.ferdie), 0);
instance
.engine
.set_balance(scale::Encode::encode(&default_accounts.one), 0);
instance
.engine
.set_balance(scale::Encode::encode(&default_accounts.two), 0);
});
f(default_accounts)
}

/// Returns the default accounts for testing purposes:
/// Alice, Bob, Charlie, Django, Eve and Frank.
/// Alice, Bob, Charlie, Dave, Eve, Ferdie, One and Two.
pub fn default_accounts<T>() -> DefaultAccounts<T>
where
T: Environment,
<T as Environment>::AccountId: From<[u8; 32]>,
{
DefaultAccounts {
alice: T::AccountId::from([0x01; 32]),
bob: T::AccountId::from([0x02; 32]),
charlie: T::AccountId::from([0x03; 32]),
django: T::AccountId::from([0x04; 32]),
eve: T::AccountId::from([0x05; 32]),
frank: T::AccountId::from([0x06; 32]),
alice: T::AccountId::from(sp_keyring::sr25519::Keyring::Alice.to_raw_public()),
bob: T::AccountId::from(sp_keyring::sr25519::Keyring::Bob.to_raw_public()),
charlie: T::AccountId::from(sp_keyring::sr25519::Keyring::Charlie.to_raw_public()),
dave: T::AccountId::from(sp_keyring::sr25519::Keyring::Dave.to_raw_public()),
eve: T::AccountId::from(sp_keyring::sr25519::Keyring::Eve.to_raw_public()),
ferdie: T::AccountId::from(sp_keyring::sr25519::Keyring::Ferdie.to_raw_public()),
one: T::AccountId::from(sp_keyring::sr25519::Keyring::One.to_raw_public()),
two: T::AccountId::from(sp_keyring::sr25519::Keyring::Two.to_raw_public()),
}
}

Expand All @@ -356,12 +364,16 @@ where
pub bob: T::AccountId,
/// The predefined `CHARLIE` account holding some amounts of value.
pub charlie: T::AccountId,
/// The predefined `DJANGO` account holding no value.
pub django: T::AccountId,
/// The predefined `DAVE` account holding no value.
pub dave: T::AccountId,
/// The predefined `EVE` account holding no value.
pub eve: T::AccountId,
/// The predefined `FRANK` account holding no value.
pub frank: T::AccountId,
/// The predefined `FERDIE` account holding no value.
pub ferdie: T::AccountId,
/// The predefined `ONE` account holding no value.
pub one: T::AccountId,
/// The predefined `TWO` account holding no value.
pub two: T::AccountId,
}

/// Returns the recorded emitted events in order.
Expand Down Expand Up @@ -429,4 +441,4 @@ macro_rules! pay_with_call {
$crate::test::transfer_in::<Environment>($amount);
$contract.$message($ ($params) ,*)
}}
}
}
9 changes: 9 additions & 0 deletions integration-tests/default-accounts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
26 changes: 26 additions & 0 deletions integration-tests/default-accounts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "default-accounts"
version = "4.1.0"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../crates/ink", default-features = false }

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

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
]
ink-as-dependency = []
e2e-tests = []
124 changes: 124 additions & 0 deletions integration-tests/default-accounts/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#![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.dave;

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

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_ferdie_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_ferdie_account_id = integration_test_accounts.ferdie;

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

assert_eq!(integration_ferdie_account_id, e2e_ferdie_account_id);
}

#[ink::test]
fn test_one_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_one_account_id = integration_test_accounts.one;

let e2e_one_account_id: AccountId = ink_e2e::AccountKeyring::One.to_raw_public().into();

assert_eq!(integration_one_account_id, e2e_one_account_id);
}

#[ink::test]
fn test_two_account() {
let integration_test_accounts: DefaultAccounts<ink::env::DefaultEnvironment> =
ink::env::test::default_accounts::<ink::env::DefaultEnvironment>();
let integration_two_account_id = integration_test_accounts.two;

let e2e_two_account_id: AccountId = ink_e2e::AccountKeyring::Two.to_raw_public().into();

assert_eq!(integration_two_account_id, e2e_two_account_id);
}
}
}