Skip to content

Commit

Permalink
Fix balances logics
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Oct 5, 2023
1 parent 42315b7 commit 5d5b227
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion utils/runtime-fuzzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn execute_gear_call(
let allowed_to_spend_value = if fuzzing_config.allow_exceed_sender_balance {
u128::MAX
} else {
get_account_balance(&sender)
get_account_balance(&sender).saturating_sub(block_gas_cost())
};

match call {
Expand Down
5 changes: 0 additions & 5 deletions utils/runtime-fuzzer/src/runtime/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use gear_common::Origin;
use gear_runtime::Runtime;
use pallet_balances::Pallet as BalancesPallet;
use pallet_gear::BlockGasLimitOf;
use runtime_primitives::{AccountId, AccountPublic, Balance};
use sp_consensus_babe::AuthorityId as BabeId;
use sp_consensus_grandpa::AuthorityId as GrandpaId;
Expand Down Expand Up @@ -60,10 +59,6 @@ pub fn get_pub_key_from_seed<T: TPublic>(seed: &str) -> <T::Pair as Pair>::Publi
.public()
}

pub fn acc_max_balance() -> Balance {
BlockGasLimitOf::<Runtime>::get().saturating_mul(20) as u128
}

pub fn get_account_balance(account: &AccountId) -> Balance {
BalancesPallet::<Runtime>::free_balance(account)
}
15 changes: 12 additions & 3 deletions utils/runtime-fuzzer/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ use gear_runtime::{
AccountId, Balances, BankAddress, Runtime, RuntimeOrigin, SessionConfig, SessionKeys,
};
use pallet_balances::{GenesisConfig as BalancesConfig, Pallet as BalancesPallet};
use pallet_gear::BlockGasLimitOf;
use pallet_gear_bank::Config as GearBankConfig;
use runtime_primitives::Balance;
use sp_io::TestExternalities;

pub use account::{acc_max_balance, account, alice, get_account_balance};
pub use account::{account, alice, get_account_balance};
pub use block::{default_gas_limit, run_to_block, run_to_next_block};
pub use mailbox::get_mailbox_messages;

Expand All @@ -46,7 +47,10 @@ pub fn new_test_ext() -> TestExternalities {
let authorities = vec![authority_keys_from_seed("Authority")];
// Vector of tuples of accounts and their balances
let balances = vec![
(account(account::alice()), account::acc_max_balance()),
(
account(account::alice()),
block_gas_cost().saturating_mul(20),
),
(BankAddress::get(), Balances::minimum_balance()),
];

Expand Down Expand Up @@ -97,7 +101,12 @@ pub fn set_account_balance(account: AccountId, balance: Balance) -> DispatchResu
BalancesPallet::<Runtime>::set_balance(
RuntimeOrigin::root(),
account.into(),
<Runtime as GearBankConfig>::GasMultiplier::get().gas_to_value(balance as u64),
balance,
new_reserved,
)
}

pub fn block_gas_cost() -> Balance {
let block_gas_limit = BlockGasLimitOf::<Runtime>::get();
<Runtime as GearBankConfig>::GasMultiplier::get().gas_to_value(block_gas_limit)
}
30 changes: 16 additions & 14 deletions utils/runtime-fuzzer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{
block_gas_cost,
gear_calls::{
ExtrinsicGeneratorSet, MailboxProvider, RepeatedGenerator, SendMessageGenerator,
SendReplyGenerator, UploadProgramGenerator,
},
runtime::{self, acc_max_balance, default_gas_limit, get_mailbox_messages},
runtime::{self, default_gas_limit, get_mailbox_messages},
FuzzingConfig,
};
use gear_core::ids::MessageId;
Expand All @@ -33,21 +34,22 @@ pub fn min_unstructured_input_size() -> usize {
generators.unstructured_size_hint()
}

pub(crate) fn default_generator_set(test_input_id: String) -> ExtrinsicGeneratorSet {
const UPLOAD_PROGRAM_CALLS: usize = 10;
const SEND_MESSAGE_CALLS: usize = 15;
const SEND_REPLY_CALLS: usize = 1;
const UPLOAD_PROGRAM_CALLS: usize = 10;
const SEND_MESSAGE_CALLS: usize = 15;
const SEND_REPLY_CALLS: usize = 1;
const OVERALL_EXTRINSICS_COUNT: usize =
UPLOAD_PROGRAM_CALLS + SEND_MESSAGE_CALLS + SEND_REPLY_CALLS;

let min_value_sent = 0;
let max_value_sent =
acc_max_balance() / (UPLOAD_PROGRAM_CALLS + SEND_MESSAGE_CALLS + SEND_REPLY_CALLS) as u128;
const MIN_VALUE_SENT: u128 = 0;
const MAX_VALUE_SENT: u128 = 100_000;

pub(crate) fn default_generator_set(test_input_id: String) -> ExtrinsicGeneratorSet {
ExtrinsicGeneratorSet::new(vec![
RepeatedGenerator::new(
UPLOAD_PROGRAM_CALLS,
UploadProgramGenerator {
gas: default_gas_limit(),
value: min_value_sent..=max_value_sent,
value: MIN_VALUE_SENT..=MAX_VALUE_SENT,
test_input_id,
}
.into(),
Expand All @@ -56,7 +58,7 @@ pub(crate) fn default_generator_set(test_input_id: String) -> ExtrinsicGenerator
SEND_MESSAGE_CALLS,
SendMessageGenerator {
gas: default_gas_limit(),
value: min_value_sent..=max_value_sent,
value: MIN_VALUE_SENT..=MAX_VALUE_SENT,
prepaid: false,
}
.into(),
Expand All @@ -68,7 +70,7 @@ pub(crate) fn default_generator_set(test_input_id: String) -> ExtrinsicGenerator
account_id: runtime::account(runtime::alice()),
}),
gas: default_gas_limit(),
value: min_value_sent..=max_value_sent,
value: MIN_VALUE_SENT..=MAX_VALUE_SENT,
prepaid: false,
}
.into(),
Expand All @@ -77,11 +79,11 @@ pub(crate) fn default_generator_set(test_input_id: String) -> ExtrinsicGenerator
}

pub(crate) fn default_fuzzing_config() -> FuzzingConfig {
let min_sender_balance = acc_max_balance() / 2;
let max_sender_balance = acc_max_balance();
let sender_balance =
(block_gas_cost() + MAX_VALUE_SENT as u128) * OVERALL_EXTRINSICS_COUNT as u128;

FuzzingConfig {
initial_sender_balance: min_sender_balance..=max_sender_balance,
initial_sender_balance: sender_balance..=sender_balance,
allow_exceed_sender_balance: false,
}
}
Expand Down

0 comments on commit 5d5b227

Please sign in to comment.