Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[account] Retry when create delegate account #110

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/StarcoinFramework/BuildInfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ compiled_package_info:
? address: "0x00000000000000000000000000000001"
name: YieldFarmingV2
: StarcoinFramework
source_digest: 5F36AADE9312D2934E215C4971621A24AF18EA5B56A52D2BE1D8E173174D0F3C
source_digest: A137A258F3E4E5CA6B5832BE67F36B82879C5CCE6F32B927253D4529DF3DAC46
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file modified build/StarcoinFramework/bytecode_modules/Account.mv
Binary file not shown.
72 changes: 55 additions & 17 deletions build/StarcoinFramework/docs/Account.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The module for the account resource that governs every account
- [Function `create_account_with_initial_amount`](#0x1_Account_create_account_with_initial_amount)
- [Function `create_account_with_initial_amount_v2`](#0x1_Account_create_account_with_initial_amount_v2)
- [Function `create_delegate_account`](#0x1_Account_create_delegate_account)
- [Function `create_fresh_address`](#0x1_Account_create_fresh_address)
- [Function `deposit_to_self`](#0x1_Account_deposit_to_self)
- [Function `deposit`](#0x1_Account_deposit)
- [Function `deposit_with_metadata`](#0x1_Account_deposit_with_metadata)
Expand Down Expand Up @@ -1302,23 +1303,7 @@ Generate an new address and create a new account, then delegate the account and


<pre><code><b>public</b> <b>fun</b> <a href="Account.md#0x1_Account_create_delegate_account">create_delegate_account</a>(sender: &signer) : (<b>address</b>, <a href="Account.md#0x1_Account_SignerCapability">SignerCapability</a>) <b>acquires</b> <a href="Account.md#0x1_Account_Balance">Balance</a>, <a href="Account.md#0x1_Account">Account</a>, <a href="Account.md#0x1_Account_EventStore">EventStore</a> {
<b>let</b> sender_address = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender);
<b>let</b> sequence_number = <a href="Account.md#0x1_Account_sequence_number">Self::sequence_number</a>(sender_address);
// <b>use</b> stc balance <b>as</b> part of seed, just for new <b>address</b> more random.
<b>let</b> stc_balance = <a href="Account.md#0x1_Account_balance">Self::balance</a>&lt;<a href="STC.md#0x1_STC">STC</a>&gt;(sender_address);

<b>let</b> seed_bytes = <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sender_address);
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sequence_number));
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&stc_balance));

<b>let</b> seed_hash = <a href="Hash.md#0x1_Hash_sha3_256">Hash::sha3_256</a>(seed_bytes);
<b>let</b> i = 0;
<b>let</b> address_bytes = <a href="Vector.md#0x1_Vector_empty">Vector::empty</a>();
<b>while</b> (i &lt; <a href="Account.md#0x1_Account_ADDRESS_LENGTH">ADDRESS_LENGTH</a>) {
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> address_bytes, *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&seed_hash,i));
i = i + 1;
};
<b>let</b> new_address = <a href="BCS.md#0x1_BCS_to_address">BCS::to_address</a>(address_bytes);
<b>let</b> new_address = <a href="Account.md#0x1_Account_create_fresh_address">create_fresh_address</a>(sender);
<a href="Account.md#0x1_Account_create_account_with_address">Self::create_account_with_address</a>&lt;<a href="STC.md#0x1_STC">STC</a>&gt;(new_address);
<b>let</b> new_signer = <a href="Account.md#0x1_Account_create_signer">Self::create_signer</a>(new_address);
(new_address, <a href="Account.md#0x1_Account_remove_signer_capability">Self::remove_signer_capability</a>(&new_signer))
Expand All @@ -1339,6 +1324,59 @@ Generate an new address and create a new account, then delegate the account and



</details>

<a name="0x1_Account_create_fresh_address"></a>

## Function `create_fresh_address`



<pre><code><b>fun</b> <a href="Account.md#0x1_Account_create_fresh_address">create_fresh_address</a>(sender: &signer): <b>address</b>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="Account.md#0x1_Account_create_fresh_address">create_fresh_address</a>(sender: &signer) : <b>address</b> <b>acquires</b> <a href="Account.md#0x1_Account_Balance">Balance</a>, <a href="Account.md#0x1_Account">Account</a> {
<b>let</b> try_times = 0u64;
<b>while</b>(<b>true</b>){
<b>let</b> sender_address = <a href="Signer.md#0x1_Signer_address_of">Signer::address_of</a>(sender);
<b>let</b> sequence_number = <a href="Account.md#0x1_Account_sequence_number">Self::sequence_number</a>(sender_address);
// <b>use</b> stc balance <b>as</b> part of seed, just for new <b>address</b> more random.
<b>let</b> stc_balance = <a href="Account.md#0x1_Account_balance">Self::balance</a>&lt;<a href="STC.md#0x1_STC">STC</a>&gt;(sender_address);

<b>let</b> seed_bytes = <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sender_address);
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&sequence_number));
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&stc_balance));
<a href="Vector.md#0x1_Vector_append">Vector::append</a>(&<b>mut</b> seed_bytes, <a href="BCS.md#0x1_BCS_to_bytes">BCS::to_bytes</a>(&try_times));

<b>let</b> seed_hash = <a href="Hash.md#0x1_Hash_sha3_256">Hash::sha3_256</a>(seed_bytes);

<b>let</b> i = 0;
<b>let</b> address_bytes = <a href="Vector.md#0x1_Vector_empty">Vector::empty</a>();
<b>while</b> (i &lt; <a href="Account.md#0x1_Account_ADDRESS_LENGTH">ADDRESS_LENGTH</a>) {
<a href="Vector.md#0x1_Vector_push_back">Vector::push_back</a>(&<b>mut</b> address_bytes, *<a href="Vector.md#0x1_Vector_borrow">Vector::borrow</a>(&seed_hash,i));
i = i + 1;
};
<b>let</b> new_address = <a href="BCS.md#0x1_BCS_to_address">BCS::to_address</a>(address_bytes);
<b>if</b> (<a href="Account.md#0x1_Account_exists_at">Self::exists_at</a>(new_address)){
try_times = try_times + 1;
<b>continue</b>
}<b>else</b>{
<b>return</b> new_address
}
};
//unreachable
<b>abort</b> 0
}
</code></pre>



</details>

<a name="0x1_Account_deposit_to_self"></a>
Expand Down
Binary file modified build/StarcoinFramework/source_maps/Account.mvsm
Binary file not shown.
2 changes: 1 addition & 1 deletion integration-tests/account/delegate_account.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 7 tasks

task 5 'run'. lines 27-44:
{
"gas_used": 618395,
"gas_used": 638014,
"status": "Executed"
}

Expand Down
7 changes: 7 additions & 0 deletions integration-tests/account/delegate_account_retry.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
processed 3 tasks

task 2 'run'. lines 5-26:
{
"gas_used": 13887841,
"status": "Executed"
}
27 changes: 27 additions & 0 deletions integration-tests/account/delegate_account_retry.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//# init -n dev

//# faucet --addr alice --amount 2000000000

//# run --signers alice

script {
use StarcoinFramework::Account;

fun main(sender: signer) {
let (_new_address, signer_cap) = Account::create_delegate_account(&sender);
let new_account_signer = Account::create_signer_with_cap(&signer_cap);

let (address_0, cap_0) = Account::create_delegate_account(&new_account_signer);
let i = 1;
while(i < 10){
let (address_i, cap_i) = Account::create_delegate_account(&new_account_signer);
assert!(address_0 != address_i, 1000);
Account::destroy_signer_cap(cap_i);
i = i + 1;
};
Account::destroy_signer_cap(cap_0);
Account::destroy_signer_cap(signer_cap);
}
}
// check: EXECUTED

6 changes: 3 additions & 3 deletions integration-tests/daospace/dao_account.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ processed 6 tasks

task 2 'run'. lines 6-23:
{
"gas_used": 858490,
"gas_used": 878109,
"status": "Executed"
}

Expand All @@ -14,7 +14,7 @@ task 3 'run'. lines 28-40:

task 4 'run'. lines 42-51:
{
"gas_used": 733860,
"gas_used": 753479,
"status": {
"MoveAbort": {
"location": {
Expand All @@ -30,7 +30,7 @@ task 4 'run'. lines 42-51:

task 5 'run'. lines 53-61:
{
"gas_used": 732629,
"gas_used": 752248,
"status": {
"MoveAbort": {
"location": {
Expand Down
52 changes: 35 additions & 17 deletions sources/Account.move
Original file line number Diff line number Diff line change
Expand Up @@ -333,28 +333,46 @@ module Account {

/// Generate an new address and create a new account, then delegate the account and return the new account address and `SignerCapability`
public fun create_delegate_account(sender: &signer) : (address, SignerCapability) acquires Balance, Account, EventStore {
let sender_address = Signer::address_of(sender);
let sequence_number = Self::sequence_number(sender_address);
// use stc balance as part of seed, just for new address more random.
let stc_balance = Self::balance<STC>(sender_address);