Skip to content

Commit

Permalink
[feature] implement transfer limits for accounts not on chain (#187)
Browse files Browse the repository at this point in the history
Co-authored-by: 0o-de-lally <1364012+0o-de-lally@users.noreply.github.com>
  • Loading branch information
0xzoz and 0o-de-lally committed Aug 8, 2024
1 parent 9450d0e commit 0d19e25
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 278 deletions.
31 changes: 28 additions & 3 deletions framework/libra-framework/sources/ol_sources/ol_account.move
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ module ol_framework::ol_account {
/// why is VM trying to use this?
const ENOT_FOR_VM: u64 = 9;

/// you are trying to send a large coin transfer to an account that does not
/// yet exist. If you are trying to initialize this address send an amount
/// below 1,000 coins
const ETRANSFER_TOO_HIGH_FOR_INIT: u64 = 10;

/// what limit should be set for new account creation while using transfer()
const MAX_COINS_FOR_INITIALIZE: u64 = 1000 * 1000000;



struct BurnTracker has key {
prev_supply: u64,
prev_balance: u64,
Expand Down Expand Up @@ -155,13 +165,14 @@ module ol_framework::ol_account {
public entry fun transfer(sender: &signer, to: address, amount: u64)
acquires BurnTracker {
let payer = signer::address_of(sender);
maybe_sender_creates_account(sender, to);
maybe_sender_creates_account(sender, to, amount);
transfer_checks(payer, to, amount);
// both update burn tracker
let c = withdraw(sender, amount);
deposit_coins(to, c);
}


// transfer with capability, and do appropriate checks on both sides, and
// track the slow wallet
// NOTE: this requires that the account exists, since the SENDER signature is not used
Expand Down Expand Up @@ -215,8 +226,12 @@ module ol_framework::ol_account {
coin
}

fun maybe_sender_creates_account(sender: &signer, maybe_new_user: address) {
fun maybe_sender_creates_account(sender: &signer, maybe_new_user: address,
amount: u64) {
if (!account::exists_at(maybe_new_user)) {
// prevents someone's Terrible, Horrible, No Good, Very Bad Day
assert!(amount <= MAX_COINS_FOR_INITIALIZE, error::out_of_range(ETRANSFER_TOO_HIGH_FOR_INIT));

// creates the account address (with the same bytes as the authentication key).
create_impl(sender, maybe_new_user);
};
Expand Down Expand Up @@ -363,13 +378,23 @@ module ol_framework::ol_account {
let decimal_places = coin::decimals<LibraCoin>();
let scaling = math64::pow(10, (decimal_places as u64));
let value = fixed_point32::create_from_rational(unscaled_value, scaling);
// multply will TRUNCATE.
// multiply will TRUNCATE.
let integer_part = fixed_point32::multiply_u64(1, value);

let decimal_part = unscaled_value - (integer_part * scaling);

(integer_part, decimal_part)
}

#[view]
/// helper to safely convert from coin units (human readable) to the value scaled to
/// the on chain decimal precision
public fun scale_from_human(human: u64): u64 {
let decimal_places = coin::decimals<LibraCoin>();
let scaling = math64::pow(10, (decimal_places as u64));
return human * scaling
}

// on new account creation we need the burn tracker created
// note return quietly if it's already initialized, so we can use it
// in the creation and tx flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module ol_framework::test_account {
use ol_framework::ol_account;
use ol_framework::ancestry;
use diem_framework::coin;
use diem_std::debug::print;
// use diem_std::debug::print;

// scenario: testing trying send more funds than are unlocked
#[test(root = @ol_framework, alice_sig = @0x1000a)]
fun test_account_create(root: signer, alice_sig: signer) {
Expand All @@ -24,7 +25,6 @@ module ol_framework::test_account {

let addr_tree = ancestry::get_tree(alice_addr);
assert!(vector::length(&addr_tree) > 0, 7357001);
print(&addr_tree);
assert!(vector::contains(&addr_tree, &@0x1), 7357002);


Expand All @@ -36,7 +36,36 @@ module ol_framework::test_account {
assert!(vector::length(&addr_tree) > 1, 7357004);
assert!(vector::contains(&addr_tree, &alice_addr), 7357005);

print(&addr_tree);
}


#[test(root = @ol_framework, alice_sig = @0x1000a)]
#[expected_failure(abort_code = 131082, location = 0x1::ol_account)]
fun test_account_reject_create(root: signer, alice_sig: signer) {
let alice_addr = @0x1000a;
let bob_addr = @0x1000b;

mock::ol_test_genesis(&root);

let alice_balance = 10000 * 1000000; // with scaling
let bob_tx_too_much = 1100 * 1000000; // above limit

let mint_cap = libra_coin::extract_mint_cap(&root);
ol_account::create_account(&root, alice_addr);
ol_account::deposit_coins(alice_addr, coin::test_mint(alice_balance, &mint_cap));
coin::destroy_mint_cap(mint_cap);

let addr_tree = ancestry::get_tree(alice_addr);
assert!(vector::length(&addr_tree) > 0, 7357001);
assert!(vector::contains(&addr_tree, &@0x1), 7357002);


let (a_balance, _) = ol_account::balance(alice_addr);
assert!(a_balance == alice_balance, 735703);

ol_account::transfer(&alice_sig, bob_addr, bob_tx_too_much);
let addr_tree = ancestry::get_tree(bob_addr);
assert!(vector::length(&addr_tree) > 1, 7357004);
assert!(vector::contains(&addr_tree, &alice_addr), 7357005);
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b267dcd733538e5cd89e6aa6c0b42c45d96d5452ad4f7dc1dc2fa3f298f1b53a
744db89ac7b27b3faab28f85d44ee6fc5466a233e1b7cae9536167cc99c0208b
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Upgrade proposal for package `MoveStdlib`

// Framework commit hash: 0aca1662b767111e252f971abf31b4691f16eb9a
// Framework commit hash: 0170092294256afa10c4b20303201878e953f8d8
// Builder commit hash: db1137ba1f8e7301e325021f71f740063daaf76e

// Next step script hash: b169af1a1a4e0c345708e96223d16e8f2332e666ab101a63f467ea90f966a08b
// Next step script hash: 8ef57e7be9e89cb2d34f3cd012e8be950f97f664436fe8d23d6f0d5072b742ae

// source digest: CD5C8655F0340314CC68657DF89A58E257A0A88218E2B07A278A20B843E7A09E
script {
Expand All @@ -17,7 +17,7 @@ script {
let framework_signer = diem_governance::resolve_multi_step_proposal(
proposal_id,
@0000000000000000000000000000000000000000000000000000000000000001,
vector[177u8,105u8,175u8,26u8,26u8,78u8,12u8,52u8,87u8,8u8,233u8,98u8,35u8,209u8,110u8,143u8,35u8,50u8,230u8,102u8,171u8,16u8,26u8,99u8,244u8,103u8,234u8,144u8,249u8,102u8,160u8,139u8,],
vector[142u8,245u8,126u8,123u8,233u8,232u8,156u8,178u8,211u8,79u8,60u8,208u8,18u8,232u8,190u8,149u8,15u8,151u8,246u8,100u8,67u8,111u8,232u8,210u8,61u8,111u8,13u8,80u8,114u8,183u8,66u8,174u8,],
);
let code = vector::empty();
let code_chunk0 =
Expand Down Expand Up @@ -644,6 +644,6 @@ script {
112u8,116u8,105u8,111u8,110u8,0u8,0u8,0u8,6u8,115u8,116u8,114u8,105u8,110u8,103u8,0u8,0u8,0u8,0u8,0u8,
];
code::publish_package_txn(&framework_signer, metadata_chunk1, code);
version::upgrade_set_git(&framework_signer, x"0aca1662b767111e252f971abf31b4691f16eb9a")
version::upgrade_set_git(&framework_signer, x"0170092294256afa10c4b20303201878e953f8d8")
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b169af1a1a4e0c345708e96223d16e8f2332e666ab101a63f467ea90f966a08b
8ef57e7be9e89cb2d34f3cd012e8be950f97f664436fe8d23d6f0d5072b742ae
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Upgrade proposal for package `VendorStdlib`

// Framework commit hash: 0aca1662b767111e252f971abf31b4691f16eb9a
// Framework commit hash: 0170092294256afa10c4b20303201878e953f8d8
// Builder commit hash: db1137ba1f8e7301e325021f71f740063daaf76e

// Next step script hash: d65c8d49d68fe6c5be25786e4d4688f29f3f00a62a541b7942e078599b1b654a
// Next step script hash: 2c0b8c87330aa25f3ed395e53dc597a1fc768150255930d4c2f7d429a0764d30

// source digest: 5E12DDD8987B153D75378183FB77218A1FAB6038899EB9121ECD4BE94EC1D598
script {
Expand All @@ -17,7 +17,7 @@ script {
let framework_signer = diem_governance::resolve_multi_step_proposal(
proposal_id,
@0000000000000000000000000000000000000000000000000000000000000001,
vector[214u8,92u8,141u8,73u8,214u8,143u8,230u8,197u8,190u8,37u8,120u8,110u8,77u8,70u8,136u8,242u8,159u8,63u8,0u8,166u8,42u8,84u8,27u8,121u8,66u8,224u8,120u8,89u8,155u8,27u8,101u8,74u8,],
vector[44u8,11u8,140u8,135u8,51u8,10u8,162u8,95u8,62u8,211u8,149u8,229u8,61u8,197u8,151u8,161u8,252u8,118u8,129u8,80u8,37u8,89u8,48u8,212u8,194u8,247u8,212u8,41u8,160u8,118u8,77u8,48u8,],
);
let code = vector::empty();
let code_chunk0 =
Expand Down Expand Up @@ -2551,6 +2551,6 @@ script {
0u8,0u8,1u8,10u8,77u8,111u8,118u8,101u8,83u8,116u8,100u8,108u8,105u8,98u8,0u8,
];
code::publish_package_txn(&framework_signer, metadata_chunk1, code);
version::upgrade_set_git(&framework_signer, x"0aca1662b767111e252f971abf31b4691f16eb9a")
version::upgrade_set_git(&framework_signer, x"0170092294256afa10c4b20303201878e953f8d8")
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d65c8d49d68fe6c5be25786e4d4688f29f3f00a62a541b7942e078599b1b654a
2c0b8c87330aa25f3ed395e53dc597a1fc768150255930d4c2f7d429a0764d30
Loading

0 comments on commit 0d19e25

Please sign in to comment.