Skip to content

Commit

Permalink
feat!(runtime): Add field keep_alive to gear extrinsics defining ex…
Browse files Browse the repository at this point in the history
…istence requirement (#3425)
  • Loading branch information
breathx authored Oct 20, 2023
1 parent eed42e5 commit fababeb
Show file tree
Hide file tree
Showing 21 changed files with 688 additions and 167 deletions.
4 changes: 4 additions & 0 deletions gclient/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl GearApi {
init_payload: payload.as_ref().to_vec(),
gas_limit,
value,
keep_alive: false,
})
})
.collect();
Expand Down Expand Up @@ -765,6 +766,7 @@ impl GearApi {
payload: payload.as_ref().to_vec(),
gas_limit,
value,
keep_alive: false,
})
})
.collect();
Expand Down Expand Up @@ -898,6 +900,7 @@ impl GearApi {
payload: payload.as_ref().to_vec(),
gas_limit,
value,
keep_alive: false,
})
})
.collect();
Expand Down Expand Up @@ -1127,6 +1130,7 @@ impl GearApi {
init_payload: payload.as_ref().to_vec(),
gas_limit,
value,
keep_alive: false,
})
})
.collect();
Expand Down
6 changes: 6 additions & 0 deletions gsdk/src/metadata/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,6 +2450,7 @@ pub mod runtime_types {
init_payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
keep_alive: ::core::primitive::bool,
},
#[codec(index = 2)]
#[doc = "Creates program via `code_id` from storage."]
Expand All @@ -2474,6 +2475,7 @@ pub mod runtime_types {
init_payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
keep_alive: ::core::primitive::bool,
},
#[codec(index = 3)]
#[doc = "Sends a message to a program or to another account."]
Expand All @@ -2498,6 +2500,7 @@ pub mod runtime_types {
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
keep_alive: ::core::primitive::bool,
},
#[codec(index = 4)]
#[doc = "Send reply on message in `Mailbox`."]
Expand All @@ -2518,6 +2521,7 @@ pub mod runtime_types {
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: ::core::primitive::u128,
keep_alive: ::core::primitive::bool,
},
#[codec(index = 5)]
#[doc = "Claim value from message in `Mailbox`."]
Expand Down Expand Up @@ -3343,13 +3347,15 @@ pub mod runtime_types {
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: _0,
keep_alive: ::core::primitive::bool,
},
#[codec(index = 1)]
SendReply {
reply_to_id: runtime_types::gear_core::ids::MessageId,
payload: ::std::vec::Vec<::core::primitive::u8>,
gas_limit: ::core::primitive::u64,
value: _0,
keep_alive: ::core::primitive::bool,
},
}
}
Expand Down
8 changes: 8 additions & 0 deletions gsdk/src/metadata/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ fn gear_call_to_scale_value(call: GearCall) -> Value {
init_payload,
gas_limit,
value,
keep_alive,
} => Value::named_variant(
"upload_program",
[
Expand All @@ -196,6 +197,7 @@ fn gear_call_to_scale_value(call: GearCall) -> Value {
("init_payload", Value::from_bytes(init_payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("keep_alive", Value::bool(keep_alive)),
],
),
GearCall::create_program {
Expand All @@ -204,6 +206,7 @@ fn gear_call_to_scale_value(call: GearCall) -> Value {
init_payload,
gas_limit,
value,
keep_alive,
} => Value::named_variant(
"create_program",
[
Expand All @@ -212,34 +215,39 @@ fn gear_call_to_scale_value(call: GearCall) -> Value {
("init_payload", Value::from_bytes(init_payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("keep_alive", Value::bool(keep_alive)),
],
),
GearCall::send_message {
destination,
payload,
gas_limit,
value,
keep_alive,
} => Value::named_variant(
"send_message",
[
("destination", Value::from_bytes(destination.0)),
("payload", Value::from_bytes(payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("keep_alive", Value::bool(keep_alive)),
],
),
GearCall::send_reply {
reply_to_id,
payload,
gas_limit,
value,
keep_alive,
} => Value::named_variant(
"send_reply",
[
("reply_to_id", Value::from_bytes(reply_to_id.0)),
("payload", Value::from_bytes(payload)),
("gas_limit", Value::u128(gas_limit as u128)),
("value", Value::u128(value as u128)),
("keep_alive", Value::bool(keep_alive)),
],
),
GearCall::claim_value { message_id } => Value::named_variant(
Expand Down
4 changes: 4 additions & 0 deletions gsdk/src/signer/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(false),
],
)
.await
Expand Down Expand Up @@ -102,6 +103,7 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(false),
],
)
.await
Expand All @@ -123,6 +125,7 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(false),
],
)
.await
Expand Down Expand Up @@ -153,6 +156,7 @@ impl SignerCalls {
Value::from_bytes(payload),
Value::u128(gas_limit as u128),
Value::u128(value),
Value::bool(false),
],
)
.await
Expand Down
1 change: 1 addition & 0 deletions node/authorship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn checked_extrinsics(n: u32, signer: AccountId, nonce: &mut u32) -> Vec<Checked
init_payload: (i as u64).encode(),
gas_limit: DEFAULT_GAS_LIMIT,
value: 0,
keep_alive: false,
}),
};
*nonce += 1;
Expand Down
32 changes: 21 additions & 11 deletions pallets/gear-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
/// Transfers value from `account_id` to bank address.
fn deposit(account_id: &AccountIdOf<T>, value: BalanceOf<T>) -> Result<(), Error<T>> {
fn deposit(
account_id: &AccountIdOf<T>,
value: BalanceOf<T>,
keep_alive: bool,
) -> Result<(), Error<T>> {
let bank_address = T::BankAddress::get();

ensure!(
Expand All @@ -166,14 +170,15 @@ pub mod pallet {
Error::InsufficientDeposit
);

let existence_requirement = if keep_alive {
ExistenceRequirement::KeepAlive
} else {
ExistenceRequirement::AllowDeath
};

// Check on zero value is inside `pallet_balances` implementation.
CurrencyOf::<T>::transfer(
account_id,
&bank_address,
value,
ExistenceRequirement::AllowDeath,
)
.map_err(|_| Error::<T>::InsufficientBalance)
CurrencyOf::<T>::transfer(account_id, &bank_address, value, existence_requirement)
.map_err(|_| Error::<T>::InsufficientBalance)
}

/// Ensures that bank account is able to transfer requested value.
Expand Down Expand Up @@ -233,14 +238,18 @@ pub mod pallet {
Self::withdraw(&block_author, value)
}

pub fn deposit_gas(account_id: &AccountIdOf<T>, amount: u64) -> Result<(), Error<T>> {
pub fn deposit_gas(
account_id: &AccountIdOf<T>,
amount: u64,
keep_alive: bool,
) -> Result<(), Error<T>> {
if amount.is_zero() {
return Ok(());
}

let value = GasMultiplierOf::<T>::get().gas_to_value(amount);

Self::deposit(account_id, value)?;
Self::deposit(account_id, value, keep_alive)?;

Bank::<T>::mutate(account_id, |details| {
let details = details.get_or_insert_with(Default::default);
Expand Down Expand Up @@ -327,12 +336,13 @@ pub mod pallet {
pub fn deposit_value(
account_id: &AccountIdOf<T>,
value: BalanceOf<T>,
keep_alive: bool,
) -> Result<(), Error<T>> {
if value.is_zero() {
return Ok(());
}

Self::deposit(account_id, value)?;
Self::deposit(account_id, value, keep_alive)?;

Bank::<T>::mutate(account_id, |details| {
let details = details.get_or_insert_with(Default::default);
Expand Down
Loading

0 comments on commit fababeb

Please sign in to comment.