Skip to content

Commit

Permalink
Implement logic let funding_amount = ED*cst - balance.free
Browse files Browse the repository at this point in the history
  • Loading branch information
echevrier committed Aug 26, 2021
1 parent cb85d30 commit 0a81f57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
6 changes: 2 additions & 4 deletions primitives/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ pub mod worker {
pub const SIGNING_KEY_SIZE: usize = 32;
// size of the MR enclave
pub const MR_ENCLAVE_SIZE: usize = 32;
// default amount the enclave is funded
pub const DEFAULT_ENCLAVE_FUNDS: u128 = 1_000_000_000_000;
// factor by which the amount of enclave funding is increased, if DEFAULT_ENCLAVE_FUNDS <= ExistentialDeposit
pub const MIN_FUND_INCREASE_FACTOR: u128 = 10;
// factor to tune the amount of enclave funding: funding_amount = MIN_FUND_INCREASE_FACTOR*existential_deposit - balance.free
pub const MIN_FUND_INCREASE_FACTOR: u128 = 200_000_000_000;
}

/// Settings concerning the enclave
Expand Down
24 changes: 14 additions & 10 deletions worker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use substratee_enclave_api::{
use substratee_node_primitives::SignedBlock;
use substratee_settings::{
files::{ENCRYPTED_STATE_FILE, SHARDS_PATH, SHIELDING_KEY_FILE, SIGNING_KEY_FILE},
worker::{DEFAULT_ENCLAVE_FUNDS, MIN_FUND_INCREASE_FACTOR},
worker::MIN_FUND_INCREASE_FACTOR,
};
use substratee_worker_api::direct_client::DirectClient;

Expand Down Expand Up @@ -619,8 +619,8 @@ fn ensure_account_has_funds(api: &mut Api<sr25519::Pair, WsRpcClient>, accountid
let alice_acc = AccountId32::from(*alice.public().as_array_ref());
info!("encoding Alice's AccountId = {:?}", alice_acc.encode());

let free = api.get_free_balance(&alice_acc);
info!(" Alice's free balance = {:?}", free);
let alice_free = api.get_free_balance(&alice_acc);
info!(" Alice's free balance = {:?}", alice_free);
let nonce = api.get_nonce_of(&alice_acc).unwrap();
info!(" Alice's Account Nonce is {}", nonce);

Expand All @@ -630,17 +630,21 @@ fn ensure_account_has_funds(api: &mut Api<sr25519::Pair, WsRpcClient>, accountid

let existential_deposit = api.get_existential_deposit().unwrap();
info!("Existential deposit is = {:?}", existential_deposit);
let min_fund = if DEFAULT_ENCLAVE_FUNDS <= existential_deposit {
MIN_FUND_INCREASE_FACTOR * existential_deposit
} else {
DEFAULT_ENCLAVE_FUNDS
};
if free < min_fund {
let funding_amount = existential_deposit * MIN_FUND_INCREASE_FACTOR - free;
info!("Funding amount is = {:?}", funding_amount);
if funding_amount > alice_free.unwrap() {
error!(
"funding amount is to high: please change MIN_FUND_INCREASE_FACTOR ({:?})",
funding_amount
);
}

if free < funding_amount {
let signer_orig = api.signer.clone();
api.signer = Some(alice);

println!("[+] bootstrap funding Enclave form Alice's funds");
let xt = api.balance_transfer(GenericAddress::Id(accountid.clone()), min_fund);
let xt = api.balance_transfer(GenericAddress::Id(accountid.clone()), funding_amount);
let xt_hash = api.send_extrinsic(xt.hex_encode(), XtStatus::InBlock).unwrap();
info!("[<] Extrinsic got finalized. Hash: {:?}\n", xt_hash);

Expand Down

0 comments on commit 0a81f57

Please sign in to comment.