Skip to content

Commit

Permalink
Easy account creation (0LNetworkCommunity#781)
Browse files Browse the repository at this point in the history
* acount creation workflow now uses authkey prefix and coin sending.

* script api for balance transfer

* scaffold transfer cmd in txs

* who am i, functionon keygen, to find your authkey.

* patch genesis env issue
  • Loading branch information
0o-de-lally authored Oct 23, 2021
1 parent 68ef326 commit d0e8573
Show file tree
Hide file tree
Showing 41 changed files with 898 additions and 155 deletions.
29 changes: 13 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ reset:


backup:
cd ~ && rsync -av --exclude db/ --exclude logs/ ~/.0L ~/0L_backup_$(shell date +"%m-%d-%y-%T")
cd ~ && rsync -av --exclude db/ --exclude logs/ ~/.0L/* ~/0L_backup_$(shell date +"%m-%d-%y-%T")

confirm:
@read -p "Continue (y/n)?" CONT; \
Expand All @@ -145,22 +145,13 @@ confirm:
exit 1; \
fi \

danger-delete-all:
@echo THIS WILL WIPE ALL YOUR FILES in ${HOME}/.0L
@echo it will also make a backup at ${HOME}/backup_0L/
@echo the files github_token.txt, autopay_batch.json, set_layout, ./vdf_proofs/ and ./blocks/ will be returned to ${HOME}/.0L/
make confirm
make backup
rm -rf ${HOME}/.0L | true
mkdir ${HOME}/.0L/
make danger-restore

danger-restore:
cp ${HOME}/backup_0L/github_token.txt ${HOME}/.0L/ | true
cp ${HOME}/backup_0L/autopay_batch.json ${HOME}/.0L/ | true
rsync -rtv ${HOME}/backup_0L/blocks/ ${HOME}/.0L/blocks | true
rsync -rtv ${HOME}/backup_0L/vdf_proofs/ ${HOME}/.0L/vdf_proofs | true
rsync -rtv ${HOME}/backup_0L/set_layout.toml ${HOME}/.0L/ | true
cp ${HOME}/0L_backup/github_token.txt ${HOME}/.0L/ | true
cp ${HOME}/0L_backup/autopay_batch.json ${HOME}/.0L/ | true
rsync -rtv ${HOME}/0L_backup/blocks/ ${HOME}/.0L/blocks | true
rsync -rtv ${HOME}/0L_backup/vdf_proofs/ ${HOME}/.0L/vdf_proofs | true
rsync -rtv ${HOME}/0L_backup/set_layout.toml ${HOME}/.0L/ | true



Expand Down Expand Up @@ -212,6 +203,12 @@ gen-make-pull:
--shared-backend ${GENESIS_REMOTE} \
--pull-request-user ${GITHUB_USER}

gen-delete-fork:
cargo run -p diem-genesis-tool ${CARGO_ARGS} -- create-repo \
--repo-name ${REPO_NAME} \
--repo-owner ${REPO_ORG} \
--shared-backend ${GENESIS_REMOTE} \
--delete-repo-user ${GITHUB_USER}

gen-onboard:
cargo run -p onboard ${CARGO_ARGS} -- val --genesis-ceremony
Expand Down Expand Up @@ -308,7 +305,7 @@ genesis:
#### NODE MANAGEMENT ####
start:
# run in foreground. Only for testing, use a daemon for net.
NODE_ENV=error cargo run -p diem-node -- --config ${DATA_PATH}/validator.node.yaml
RUST_LOG=error cargo run -p diem-node -- --config ${DATA_PATH}/validator.node.yaml

# Start a fullnode instead of a validator node
start-full:
Expand Down
11 changes: 11 additions & 0 deletions config/management/genesis/src/ol_create_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct CreateGenesisRepo {
pub repo_name: String,
#[structopt(long)]
pub pull_request_user: Option<String>,
#[structopt(long)]
pub delete_repo_user: Option<String>,
}

impl CreateGenesisRepo {
Expand Down Expand Up @@ -55,6 +57,15 @@ impl CreateGenesisRepo {
e.to_string(),
)),
}
} else if let Some(user) = self.delete_repo_user{
match github.delete_own_repo(&user, &config.repository) {
Ok(_) => Ok("created pull request to genesis repo".to_string()),
Err(e) => Err(Error::StorageWriteError(
"github",
"pull request",
e.to_string(),
)),
}
} else {
// Fork the genesis coordination repo into a personal repo
match github.fork_genesis_repo(&self.repo_owner, &self.repo_name) {
Expand Down
25 changes: 14 additions & 11 deletions language/diem-framework/modules/0L/Globals.move
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ module Globals {
epoch_slow_wallet_unlock: u64,
}

////////////////////
//// Constants ////
///////////////////
const COIN_SCALING_FACTOR: u64 = 1000000;

/// Get the epoch length
public fun get_epoch_length(): u64 {
Expand All @@ -48,6 +46,11 @@ module Globals {
get_constants().max_validators_per_set
}

/// Get the epoch length
public fun get_coin_scaling_factor(): u64 {
COIN_SCALING_FACTOR
}

/// Get max validator per epoch
public fun get_subsidy_ceiling_gas(): u64 {
get_constants().subsidy_ceiling_gas
Expand All @@ -58,7 +61,7 @@ module Globals {
get_constants().vdf_difficulty
}

/// Get the current vdf_difficulty
/// Get the current vdf_difficulty
public fun get_vdf_security(): u64 {
512
}
Expand All @@ -81,14 +84,14 @@ module Globals {

/// Get the constants for the current network
fun get_constants(): GlobalConstants {
let coin_scale = 1000000; // Diem::scaling_factor<GAS::T>();
assert(coin_scale == Diem::scaling_factor<GAS::GAS>(), Errors::invalid_argument(070001));
// let coin_scale = 1000000; // Diem::scaling_factor<GAS::T>();
assert(COIN_SCALING_FACTOR == Diem::scaling_factor<GAS::GAS>(), Errors::invalid_argument(070001));

if (Testnet::is_testnet()) {
return GlobalConstants {
epoch_length: 60, // seconds
max_validators_per_set: 100,
subsidy_ceiling_gas: 296 * coin_scale,
subsidy_ceiling_gas: 296 * COIN_SCALING_FACTOR,
vdf_difficulty: 100,
epoch_mining_thres_lower: 1,
epoch_mining_thres_upper: 1000, // upper bound unlimited
Expand All @@ -98,9 +101,9 @@ module Globals {

if (StagingNet::is_staging_net()) {
return GlobalConstants {
epoch_length: 60 * 40, // 20 mins, enough for a hard miner proof.
epoch_length: 60 * 40, // 40 mins, enough for a hard miner proof.
max_validators_per_set: 100,
subsidy_ceiling_gas: 8640000 * coin_scale,
subsidy_ceiling_gas: 8640000 * COIN_SCALING_FACTOR,
vdf_difficulty: 120000000,
epoch_mining_thres_lower: 1,
epoch_mining_thres_upper: 72, // upper bound enforced at 20 mins per proof.
Expand All @@ -115,11 +118,11 @@ module Globals {
// target max block time: 2 secs
// target transaction per sec max gas: 20
// uses "scaled representation", since there are no decimals.
subsidy_ceiling_gas: 8640000 * coin_scale, // subsidy amount assumes 24 hour epoch lengths. Also needs to be adjusted for coin_scale the onchain representation of human readable value.
subsidy_ceiling_gas: 8640000 * COIN_SCALING_FACTOR, // subsidy amount assumes 24 hour epoch lengths. Also needs to be adjusted for coin_scale the onchain representation of human readable value.
vdf_difficulty: 120000000, // FYI approx 30 mins per proof on 2020 macbook pro 2.5 ghz quadcore
epoch_mining_thres_lower: 7, // NOTE: bootstrapping, allowance for operator error.
epoch_mining_thres_upper: 72, // upper bound enforced at 20 mins per proof.
epoch_slow_wallet_unlock: 1000 * coin_scale, // approx 10 years for largest accounts in genesis.
epoch_slow_wallet_unlock: 1000 * COIN_SCALING_FACTOR, // approx 10 years for largest accounts in genesis.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,27 @@ module AccountScripts {
use 0x1::DiemAccount;
use 0x1::GAS::GAS;
use 0x1::ValidatorConfig;

use 0x1::Globals;

public(script) fun create_user_by_coin_tx(
sender: signer,
account: address,
authkey_prefix: vector<u8>,
unscaled_value: u64,
) {
// IMPORTANT: the human representation of a value is unscaled. The user which expects to send 10 coins, will input that as an unscaled_value. This script converts it to the Move internal scale by multiplying by COIN_SCALING_FACTOR.
let value = unscaled_value * Globals::get_coin_scaling_factor();
let new_account_address = DiemAccount::create_user_account_with_coin(
&sender,
account,
authkey_prefix,
value,
);

// Check the account exists and the balance is 0
assert(DiemAccount::balance<GAS>(new_account_address) > 0, 01);
}

public(script) fun create_acc_user(
sender: signer,
challenge: vector<u8>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// For transferring balance between accounts.
address 0x1 {
module TransferScripts {
use 0x1::DiemAccount;
use 0x1::GAS::GAS;
use 0x1::Globals;
use 0x1::Signer;

public(script) fun balance_transfer(
sender: signer,
destination: address,
unscaled_value: u64,
) {
// IMPORTANT: the human representation of a value is unscaled. The user which expects to send 10 coins, will input that as an unscaled_value. This script converts it to the Move internal scale by multiplying by COIN_SCALING_FACTOR.
let value = unscaled_value * Globals::get_coin_scaling_factor();
let sender_addr = Signer::address_of(&sender);
let sender_balance_pre = DiemAccount::balance<GAS>(sender_addr);
let destination_balance_pre = DiemAccount::balance<GAS>(destination);

let with_cap = DiemAccount::extract_withdraw_capability(&sender);
DiemAccount::pay_from<GAS>(&with_cap, destination, value, b"balance_transfer", b"");
DiemAccount::restore_withdraw_capability(with_cap);

assert(DiemAccount::balance<GAS>(destination) > destination_balance_pre, 01);
assert(DiemAccount::balance<GAS>(sender_addr) < sender_balance_pre, 02);
}

}
}
53 changes: 41 additions & 12 deletions language/diem-framework/modules/DiemAccount.move
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ module DiemAccount {

const MAX_U64: u128 = 18446744073709551615;

/////// 0L /////////
/// The `DiemAccount` resource is not in the required state
const EACCOUNT: u64 = 12010;
/// Tried to deposit a coin whose value was zero
Expand Down Expand Up @@ -195,6 +194,10 @@ module DiemAccount {
const EWRITESET_MANAGER: u64 = 120123;
/// An account cannot be created at the reserved core code address of 0x1
const ECANNOT_CREATE_AT_CORE_CODE: u64 = 120124;

//////// 0L ////////
const EBELOW_MINIMUM_VALUE_BOOTSTRAP_COIN: u64 = 120125;

/////// 0L end /////////

/// Prologue errors. These are separated out from the other errors in this
Expand Down Expand Up @@ -472,7 +475,7 @@ module DiemAccount {
add_currencies_for_account<GAS>(&new_signer, false);
make_account(new_signer, auth_key_prefix);

onboarding_gas_transfer<GAS>(sender, new_account_address);
onboarding_gas_transfer<GAS>(sender, new_account_address, BOOTSTRAP_COIN_VALUE);
// Init the miner state
// this verifies the VDF proof, which we use to rate limit account creation.
// account will not be created if this step fails.
Expand All @@ -482,6 +485,26 @@ module DiemAccount {
new_account_address
}

/////// 0L ////////
// Function code: 01
public fun create_user_account_with_coin(
sender: &signer,
new_account: address,
new_account_authkey_prefix: vector<u8>,
value: u64,
):address acquires AccountOperationsCapability, Balance, CumulativeDeposits, DiemAccount {

// let (new_account_address, auth_key_prefix) = VDF::extract_address_from_challenge(challenge);
let new_signer = create_signer(new_account);
Roles::new_user_role_with_proof(&new_signer);
Event::publish_generator(&new_signer);
add_currencies_for_account<GAS>(&new_signer, false);
make_account(new_signer, new_account_authkey_prefix);

onboarding_gas_transfer<GAS>(sender, new_account, value);
new_account
}

/////// 0L ////////
// spec fun create_user_account {
// include AddCurrencyForAccountEnsures<Token>{addr: new_account_address};
Expand Down Expand Up @@ -576,9 +599,9 @@ module DiemAccount {


// Transfer for owner
onboarding_gas_transfer<GAS>(sender, new_account_address);
onboarding_gas_transfer<GAS>(sender, new_account_address, BOOTSTRAP_COIN_VALUE);
// Transfer for operator as well
onboarding_gas_transfer<GAS>(sender, op_address);
onboarding_gas_transfer<GAS>(sender, op_address, BOOTSTRAP_COIN_VALUE);

let new_signer = create_signer(new_account_address);
set_slow(&new_signer);
Expand Down Expand Up @@ -684,9 +707,9 @@ print(&509);
print(&510);
// the miner who is upgrading may have coins, but better safe...
// Transfer for owner
onboarding_gas_transfer<GAS>(sender, new_account_address);
onboarding_gas_transfer<GAS>(sender, new_account_address, BOOTSTRAP_COIN_VALUE);
// Transfer for operator as well
onboarding_gas_transfer<GAS>(sender, op_address);
onboarding_gas_transfer<GAS>(sender, op_address, BOOTSTRAP_COIN_VALUE);
print(&510);
let new_signer = create_signer(new_account_address);
set_slow(&new_signer);
Expand Down Expand Up @@ -1557,19 +1580,27 @@ print(&511);
// using "spec schema" ?
fun onboarding_gas_transfer<Token: store>(
payer_sig: &signer,
payee: address
payee: address,
value: u64,
) acquires DiemAccount, Balance, AccountOperationsCapability, CumulativeDeposits { //////// 0L ////////
let payer_addr = Signer::address_of(payer_sig);
let account_balance = borrow_global_mut<Balance<Token>>(payer_addr);
let balance_coin = &mut account_balance.coin;

// value needs to be greater than boostrapping value
assert(
value >= BOOTSTRAP_COIN_VALUE,
Errors::limit_exceeded(EBELOW_MINIMUM_VALUE_BOOTSTRAP_COIN)
);

// Doubly check balance exists.
assert(
Diem::value(balance_coin) > BOOTSTRAP_COIN_VALUE,
Diem::value(balance_coin) > value,
Errors::limit_exceeded(EINSUFFICIENT_BALANCE)
);
// Should abort if the
let metadata = b"onboarding coin transfer";
let coin_to_deposit = Diem::withdraw(balance_coin, BOOTSTRAP_COIN_VALUE);
let coin_to_deposit = Diem::withdraw(balance_coin, value);
deposit<Token>(
payer_addr,
payee,
Expand All @@ -1595,9 +1626,7 @@ print(&511);
oper: address,
) acquires DiemAccount, Balance, AccountOperationsCapability, CumulativeDeposits {
CoreAddresses::assert_vm(vm);
onboarding_gas_transfer<GAS>(owner_sig, oper);


onboarding_gas_transfer<GAS>(owner_sig, oper, BOOTSTRAP_COIN_VALUE);
}

/// Rotate the authentication key for the account under cap.account_address
Expand Down
Loading

0 comments on commit d0e8573

Please sign in to comment.