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

Hackathon feat #1246

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3725d77
Scenario update flag on Account
newtmex Oct 2, 2023
60a50cd
Check balance for fungible ESDT for `CheckEsdtMap::Equal` variant whe…
newtmex Oct 3, 2023
71ee6cb
Acconts can have fungible ESDT roles set, but no balance
newtmex Oct 3, 2023
c8fa310
made `get_state` method public outside the framework
newtmex Oct 4, 2023
1c69944
Option to start trace with predefined ScenarioTrace
newtmex Oct 6, 2023
4daea89
Set gas_limit for the Txs in the testing framework
newtmex Oct 6, 2023
b49c143
rewrote check for nft balance and attributes
newtmex Oct 7, 2023
2f04a92
wildcard roles value on CheckEsdtData
newtmex Oct 7, 2023
6770a5f
Always serialize roles as "*"
newtmex Oct 7, 2023
2b32593
roles on esdt data fix
newtmex Oct 7, 2023
454b59b
default to Star
newtmex Oct 7, 2023
029520f
default roles to star
newtmex Oct 7, 2023
1ab2a11
rows into_raw_explicit
newtmex Oct 7, 2023
ae7c043
revert changes for roles
newtmex Oct 7, 2023
23a7c1d
added roles check feat
newtmex Oct 7, 2023
331fa08
Changed the roles type
newtmex Oct 8, 2023
b3b3ba9
serialize nonce as star since vm runner always checks it
newtmex Oct 8, 2023
505ec07
nonce to star if not specified
newtmex Oct 8, 2023
f13dba6
Ignore panic so we can write to trace
newtmex Oct 12, 2023
8285bab
wildcard refund values when unspecified
newtmex Oct 14, 2023
91c6cf2
tests fix
newtmex Oct 23, 2023
28341df
merged master
newtmex Oct 23, 2023
3571e90
Merge branch 'master' into hackathon-feat
newtmex Oct 23, 2023
315ed19
Added esdt_roles check
newtmex Oct 26, 2023
a086b0e
Merge branch 'hackathon-feat' of https://github.com/newtmex/mx-sdk-rs…
newtmex Oct 26, 2023
6148703
check `lastNonce`
newtmex Oct 26, 2023
9354a72
Merge branch 'master' into hackathon-feat
newtmex Dec 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"step": "checkState",
"accounts": {
"0x0000000000000000fb1397e8225ea85e0f0e6e8c7b126d0016ccbde0e667151e": {
"nonce": "*",
"balance": "0",
"esdt": {},
"storage": {
Expand Down Expand Up @@ -62,13 +63,15 @@
"expect": {
"out": [],
"status": "0",
"message": "str:"
"message": "str:",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
"0x0000000000000000fb1397e8225ea85e0f0e6e8c7b126d0016ccbde0e667151e": {
"nonce": "*",
"balance": "0",
"esdt": {},
"storage": {
Expand All @@ -95,7 +98,8 @@
"0x33"
],
"status": "0",
"message": "str:"
"message": "str:",
"refund": "*"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"step": "checkState",
"accounts": {
"0x00000000000000006c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925": {
"nonce": "*",
"balance": "0",
"esdt": {
"str:COOL-123456": {
Expand Down
7 changes: 6 additions & 1 deletion framework/scenario/src/facade/scenario_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ScenarioWorld {
}
}

pub(crate) fn get_state(&self) -> &BlockchainState {
pub fn get_state(&self) -> &BlockchainState {
&self.get_debugger_backend().vm_runner.blockchain_mock.state
}

Expand All @@ -113,6 +113,11 @@ impl ScenarioWorld {
self
}

pub fn start_trace_with(&mut self, trace: ScenarioTrace) -> &mut Self {
self.get_mut_debugger_backend().trace = Some(trace);
self
}

/// Tells the tests where the crate lies relative to the workspace.
/// This ensures that the paths are set correctly, including in debug mode.
pub fn set_current_dir_from_workspace(&mut self, relative_path: &str) -> &mut Self {
Expand Down
8 changes: 8 additions & 0 deletions framework/scenario/src/scenario/model/account_data/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct Account {
pub code: Option<BytesValue>,
pub owner: Option<AddressValue>,
pub developer_rewards: Option<BigUintValue>,
pub update: Option<bool>,
}

impl Account {
Expand Down Expand Up @@ -176,6 +177,11 @@ impl Account {
self.owner = Some(AddressValue::from(owner_expr));
self
}

pub fn update(mut self, update: bool) -> Self {
self.update = Some(update);
self
}
}

impl InterpretableFrom<AccountRaw> for Account {
Expand Down Expand Up @@ -214,6 +220,7 @@ impl InterpretableFrom<AccountRaw> for Account {
developer_rewards: from
.developer_rewards
.map(|b| BigUintValue::interpret_from(b, context)),
update: from.update,
}
}
}
Expand All @@ -238,6 +245,7 @@ impl IntoRaw<AccountRaw> for Account {
code: self.code.map(|n| n.original),
owner: self.owner.map(|n| n.original),
developer_rewards: self.developer_rewards.map(|n| n.original),
update: self.update,
}
}
}
129 changes: 101 additions & 28 deletions framework/scenario/src/scenario/model/account_data/account_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
scenario_model::CheckEsdtData,
};
use std::collections::BTreeMap;
use std::collections::{btree_map, BTreeMap};

#[derive(Debug, Default, Clone)]
pub struct CheckAccount {
Expand Down Expand Up @@ -86,7 +86,10 @@ impl CheckAccount {
self.esdt = CheckEsdtMap::Equal(new_check_esdt_map);
},
CheckEsdtMap::Equal(check_esdt_map) => {
if check_esdt_map.contents.contains_key(&token_id) {
if let btree_map::Entry::Vacant(e) = check_esdt_map.contents.entry(token_id.clone())
{
e.insert(CheckEsdt::Short(balance));
} else {
let prev_entry = check_esdt_map.contents.get_mut(&token_id).unwrap();
match prev_entry {
CheckEsdt::Short(prev_balance_check) => *prev_balance_check = balance,
Expand Down Expand Up @@ -115,35 +118,105 @@ impl CheckAccount {
BigUintValue: From<V>,
T: TopEncode,
{
let token_id = BytesKey::from(token_id_expr);
let insert_check_esdt = |map: &mut CheckEsdtMapContents| {
let token_id = BytesKey::from(token_id_expr);
let attributes_expr = if let Some(attributes_expr) = attributes_expr {
top_encode_to_vec_u8_or_panic(&attributes_expr)
} else {
Vec::<u8>::new()
};

if let CheckEsdtMap::Unspecified = &self.esdt {
let mut check_esdt = CheckEsdt::Full(CheckEsdtData::default());
check_esdt.add_balance_and_attributes_check(nonce_expr, balance_expr, attributes_expr);

if let Some(attributes_expr) = attributes_expr {
check_esdt.add_balance_and_attributes_check(
nonce_expr,
balance_expr,
top_encode_to_vec_u8_or_panic(&attributes_expr),
);
} else {
check_esdt.add_balance_and_attributes_check(
nonce_expr,
balance_expr,
Vec::<u8>::new(),
);
}

let mut new_esdt_map = BTreeMap::new();
let _ = new_esdt_map.insert(token_id, check_esdt);

let new_check_esdt_map = CheckEsdtMapContents {
contents: new_esdt_map,
other_esdts_allowed: true,
};
map.contents.insert(token_id, check_esdt)
};

self.esdt = CheckEsdtMap::Equal(new_check_esdt_map);
}
match &mut self.esdt {
CheckEsdtMap::Equal(map) => {
insert_check_esdt(map);
},
_ => {
let mut new_map = CheckEsdtMapContents {
contents: BTreeMap::new(),
other_esdts_allowed: true,
};

insert_check_esdt(&mut new_map);

self.esdt = CheckEsdtMap::Equal(new_map);
},
};

self
}

pub fn esdt_roles<K>(mut self, token_id_expr: K, roles: Vec<String>) -> Self
where
BytesKey: From<K>,
{
let insert_check_esdt = |map: &mut CheckEsdtMapContents| {
let token_id = BytesKey::from(token_id_expr);

map.contents
.entry(token_id)
.and_modify(|check_esdt| check_esdt.add_roles_check(roles.clone()))
.or_insert(CheckEsdt::Full(CheckEsdtData {
roles,
..Default::default()
}));
};

match &mut self.esdt {
CheckEsdtMap::Equal(map) => {
insert_check_esdt(map);
},
_ => {
let mut new_map = CheckEsdtMapContents {
contents: BTreeMap::new(),
other_esdts_allowed: true,
};

insert_check_esdt(&mut new_map);

self.esdt = CheckEsdtMap::Equal(new_map);
},
};

self
}

pub fn esdt_nft_last_nonce<K>(mut self, token_id_expr: K, last_nonce: u64) -> Self
where
BytesKey: From<K>,
{
let insert_check_esdt = |map: &mut CheckEsdtMapContents| {
let token_id = BytesKey::from(token_id_expr);

map.contents
.entry(token_id)
.and_modify(|check_esdt| check_esdt.add_last_nonce_check(last_nonce))
.or_insert(CheckEsdt::Full(CheckEsdtData {
last_nonce: CheckValue::Equal(last_nonce.into()),
..Default::default()
}));
};

match &mut self.esdt {
CheckEsdtMap::Equal(map) => {
insert_check_esdt(map);
},
_ => {
let mut new_map = CheckEsdtMapContents {
contents: BTreeMap::new(),
other_esdts_allowed: true,
};

insert_check_esdt(&mut new_map);

self.esdt = CheckEsdtMap::Equal(new_map);
},
};

self
}
Expand Down Expand Up @@ -192,7 +265,7 @@ impl IntoRaw<CheckAccountRaw> for CheckAccount {
fn into_raw(self) -> CheckAccountRaw {
CheckAccountRaw {
comment: self.comment,
nonce: self.nonce.into_raw(),
nonce: self.nonce.into_raw_explicit(),
balance: self.balance.into_raw(),
esdt: self.esdt.into_raw(),
username: self.username.into_raw(),
Expand Down
21 changes: 21 additions & 0 deletions framework/scenario/src/scenario/model/esdt_data/esdt_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ impl CheckEsdt {
}
}

pub fn add_roles_check(&mut self, roles: Vec<String>) {
self.convert_to_full();

if let CheckEsdt::Full(prev_esdt_check) = self {
prev_esdt_check.roles = roles;
}
}

pub fn add_last_nonce_check<N>(&mut self, nonce_expr: N)
where
U64Value: From<N>,
{
let last_nonce = U64Value::from(nonce_expr);

self.convert_to_full();

if let CheckEsdt::Full(prev_esdt_check) = self {
prev_esdt_check.last_nonce = CheckValue::Equal(last_nonce);
}
}

pub fn add_balance_check<N, V>(&mut self, nonce_expr: N, balance_expr: V)
where
U64Value: InterpretableFrom<N>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct CheckEsdtData {
pub instances: CheckEsdtInstances,
pub last_nonce: CheckValue<U64Value>,
pub frozen: CheckValue<U64Value>,
pub roles: Vec<String>
}

impl InterpretableFrom<CheckEsdtDataRaw> for CheckEsdtData {
Expand All @@ -21,6 +22,7 @@ impl InterpretableFrom<CheckEsdtDataRaw> for CheckEsdtData {
instances: CheckEsdtInstances::interpret_from(from.instances, context),
last_nonce: CheckValue::<U64Value>::interpret_from(from.last_nonce, context),
frozen: CheckValue::<U64Value>::interpret_from(from.frozen, context),
roles: from.roles
}
}
}
Expand All @@ -30,7 +32,7 @@ impl IntoRaw<CheckEsdtDataRaw> for CheckEsdtData {
CheckEsdtDataRaw {
instances: self.instances.into_raw(),
last_nonce: self.last_nonce.into_raw(),
roles: Vec::new(),
roles: self.roles,
frozen: self.frozen.into_raw(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl IntoRaw<TxExpectRaw> for TxExpect {
message: self.message.into_raw(),
logs: self.logs.into_raw(),
gas: self.gas.into_raw(),
refund: self.refund.into_raw(),
refund: self.refund.into_raw_explicit(),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion framework/scenario/src/scenario/run_vm/check_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ pub fn check_account_esdt(address: &AddressKey, expected: &CheckEsdtMap, actual:
match expected_value {
CheckEsdt::Short(expected_balance) => {
if expected_balance.value.is_zero() {
let actual_value_is_empty =
actual_value.is_empty() || actual_value.is_empty_with_roles();
assert!(
actual_value.is_empty(),
actual_value_is_empty,
"No balance expected for ESDT token address: {}. token name: {}. nonce: {}.",
address,
bytes_to_string(key.value.as_slice()),
Expand Down
Loading