Skip to content

Commit

Permalink
feat!(runtime): Introduce pallet-gear-bank (#3052)
Browse files Browse the repository at this point in the history
Co-authored-by: nikvolf <[email protected]>
  • Loading branch information
breathx and NikVolf authored Aug 23, 2023
1 parent 21763e2 commit f7d9f90
Show file tree
Hide file tree
Showing 53 changed files with 2,941 additions and 372 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,3 @@ jobs:

- name: "Check fuzzer reproduction"
run: ./scripts/gear.sh test fuzz-repr

- run: sccache --show-stats
30 changes: 29 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pallet-gear-rpc-runtime-api = { version = "2.0.0", path = "pallets/gear/rpc/runt
pallet-gear-scheduler = { path = "pallets/gear-scheduler", default-features = false }
pallet-gear-staking-rewards = { version = "1.0.0", path = "pallets/staking-rewards", default-features = false }
pallet-gear-voucher = { version = "1.0.0", path = "pallets/gear-voucher", default-features = false }
pallet-gear-bank = { version = "1.0.0", path = "pallets/gear-bank", default-features = false }
runtime-common = { package = "gear-runtime-common", path = "runtime/common", default-features = false }
runtime-primitives = { package = "gear-runtime-primitives", path = "runtime/primitives", version = "0.1.0", default-features = false }
service = { package = "gear-service", path = "node/service", default-features = false }
Expand Down
15 changes: 15 additions & 0 deletions common/src/gas_provider/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ pub struct ChildrenRefs {
unspec_refs: u32,
}

impl<
ExternalId: Clone,
Id: Clone + Copy,
Balance: Default + Zero + Clone + Copy + sp_runtime::traits::Saturating,
> GasNode<ExternalId, Id, Balance>
{
/// Returns total gas value inside GasNode.
pub fn total_value(&self) -> Balance {
self.value()
.unwrap_or_default()
.saturating_add(self.lock().total_locked())
.saturating_add(self.system_reserve().unwrap_or_default())
}
}

impl<ExternalId: Clone, Id: Clone + Copy, Balance: Default + Zero + Clone + Copy>
GasNode<ExternalId, Id, Balance>
{
Expand Down
16 changes: 4 additions & 12 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,22 +316,14 @@ pub trait PaymentVoucher<AccountId, ProgramId, Balance> {
type VoucherId;
type Error;

fn redeem_with_id(
who: AccountId,
program: ProgramId,
amount: Balance,
) -> Result<Self::VoucherId, Self::Error>;
fn voucher_id(who: AccountId, program: ProgramId) -> Self::VoucherId;
}

impl<AccountId, ProgramId, Balance> PaymentVoucher<AccountId, ProgramId, Balance> for () {
impl<AccountId: Default, ProgramId, Balance> PaymentVoucher<AccountId, ProgramId, Balance> for () {
type VoucherId = AccountId;
type Error = &'static str;

fn redeem_with_id(
_who: AccountId,
_program: ProgramId,
_amount: Balance,
) -> Result<AccountId, Self::Error> {
Err("Payment vouchers are not supported")
fn voucher_id(_who: AccountId, _program: ProgramId) -> Self::VoucherId {
unimplemented!()
}
}
83 changes: 83 additions & 0 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use gsdk::{
},
pallet_balances::{pallet::Call as BalancesCall, AccountData},
pallet_gear::pallet::Call as GearCall,
pallet_gear_bank::pallet::BankAccount,
sp_weights::weight_v2::Weight,
},
system::Event as SystemEvent,
Expand Down Expand Up @@ -295,6 +296,33 @@ impl GearApi {
}
})?;

let src_program_account_bank_data = self
.bank_data_at(src_program_id, src_block_hash)
.await
.or_else(|e| {
if let Error::GearSDK(GsdkError::StorageNotFound) = e {
Ok(BankAccount { gas: 0, value: 0 })
} else {
Err(e)
}
})?;

let src_bank_account_data = self
.account_data_at(crate::bank_address(), src_block_hash)
.await
.or_else(|e| {
if let Error::GearSDK(GsdkError::StorageNotFound) = e {
Ok(AccountData {
free: 0u128,
reserved: 0,
misc_frozen: 0,
fee_frozen: 0,
})
} else {
Err(e)
}
})?;

let mut src_program = self
.0
.api()
Expand Down Expand Up @@ -356,6 +384,23 @@ impl GearApi {
)
.await?;

dest_node_api
.set_balance(
crate::bank_address(),
src_bank_account_data.free,
src_bank_account_data.reserved,
)
.await?;

dest_node_api
.0
.storage
.set_bank_account_storage(
src_program_id.into_account_id(),
src_program_account_bank_data,
)
.await?;

dest_node_api
.0
.storage
Expand All @@ -378,6 +423,17 @@ impl GearApi {
let src_account_data = self
.account_data_at(account_with_reserved_funds, src_block_hash)
.await?;
let src_account_bank_data = self
.bank_data_at(account_with_reserved_funds, src_block_hash)
.await
.or_else(|e| {
if let Error::GearSDK(GsdkError::StorageNotFound) = e {
Ok(BankAccount { gas: 0, value: 0 })
} else {
Err(e)
}
})?;

let dest_account_data = dest_node_api
.account_data(account_with_reserved_funds)
.await
Expand All @@ -393,6 +449,17 @@ impl GearApi {
Err(e)
}
})?;
let dest_account_bank_data = self
.bank_data_at(account_with_reserved_funds, None)
.await
.or_else(|e| {
if let Error::GearSDK(GsdkError::StorageNotFound) = e {
Ok(BankAccount { gas: 0, value: 0 })
} else {
Err(e)
}
})?;

dest_node_api
.set_balance(
account_with_reserved_funds.into_account_id(),
Expand All @@ -402,6 +469,22 @@ impl GearApi {
.saturating_add(src_account_data.reserved),
)
.await?;

dest_node_api
.0
.storage
.set_bank_account_storage(
account_with_reserved_funds.into_account_id(),
BankAccount {
gas: src_account_bank_data
.gas
.saturating_add(dest_account_bank_data.gas),
value: src_account_bank_data
.value
.saturating_add(dest_account_bank_data.value),
},
)
.await?;
}

let dest_gas_total_issuance =
Expand Down
15 changes: 14 additions & 1 deletion gclient/src/api/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use gsdk::{
ext::sp_core::{crypto::Ss58Codec, H256},
metadata::runtime_types::{
gear_common::storage::primitives::Interval, gear_core::message::user,
pallet_balances::AccountData,
pallet_balances::AccountData, pallet_gear_bank::pallet::BankAccount,
},
};

Expand Down Expand Up @@ -104,6 +104,19 @@ impl GearApi {
.data)
}

/// Get bank account data by `account_id` at specified block.
pub(crate) async fn bank_data_at(
&self,
account_id: impl IntoAccountId32,
block_hash: Option<H256>,
) -> Result<BankAccount<u128>> {
Ok(self
.0
.api()
.bank_info_at(account_id.into_account_id(), block_hash)
.await?)
}

/// Get the total balance of the account identified by `account_id`.
///
/// Total balance includes free and reserved funds.
Expand Down
8 changes: 8 additions & 0 deletions gclient/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

use crate::{Error, Result};
pub use gear_utils::now_micros;
use gsdk::ext::sp_runtime::AccountId32;
use std::{fs, path::Path};
use wabt::Wat2Wasm;

Expand Down Expand Up @@ -64,3 +65,10 @@ pub fn code_from_os(path: impl AsRef<Path>) -> Result<Vec<u8>> {
pub fn hex_to_vec(string: impl AsRef<str>) -> Result<Vec<u8>> {
hex::decode(string.as_ref().trim_start_matches("0x")).map_err(Into::into)
}

/// Returns default bank address.
pub fn bank_address() -> AccountId32 {
const BANK_ADDRESS: [u8; 32] = *b"gearbankgearbankgearbankgearbank";

BANK_ADDRESS.into()
}
Loading

0 comments on commit f7d9f90

Please sign in to comment.