Skip to content

Commit

Permalink
Revert "[account] Retry when creating delegate account (#110)"
Browse files Browse the repository at this point in the history
This reverts commit 7a38b4b.
  • Loading branch information
nkysg committed Mar 3, 2023
1 parent b60c9d3 commit bc729fc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 130 deletions.
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: 465BD18AAFF08B609377C24DF93A365C461646250126145426395BBBD12FD8CC
source_digest: 5F36AADE9312D2934E215C4971621A24AF18EA5B56A52D2BE1D8E173174D0F3C
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file modified build/StarcoinFramework/bytecode_modules/Account.mv
Binary file not shown.
73 changes: 17 additions & 56 deletions build/StarcoinFramework/docs/Account.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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 `generate_fresh_address`](#0x1_Account_generate_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 @@ -1303,7 +1302,23 @@ 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> new_address = <a href="Account.md#0x1_Account_generate_fresh_address">generate_fresh_address</a>(sender);
<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);
<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 @@ -1324,60 +1339,6 @@ Generate an new address and create a new account, then delegate the account and



</details>

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

## Function `generate_fresh_address`

Generate a fresh address from the sender address and the sender's sequence number, and ensure the generated address is not exists onchain.


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



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="Account.md#0x1_Account_generate_fresh_address">generate_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": 638014,
"gas_used": 618395,
"status": "Executed"
}

Expand Down
7 changes: 0 additions & 7 deletions integration-tests/account/delegate_account_retry.exp

This file was deleted.

27 changes: 0 additions & 27 deletions integration-tests/account/delegate_account_retry.move

This file was deleted.

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": 878109,
"gas_used": 858490,
"status": "Executed"
}

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

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

task 5 'run'. lines 53-61:
{
"gas_used": 752248,
"gas_used": 732629,
"status": {
"MoveAbort": {
"location": {
Expand Down
52 changes: 17 additions & 35 deletions sources/Account.move
Original file line number Diff line number Diff line change
Expand Up @@ -333,46 +333,28 @@ 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 new_address = generate_fresh_address(sender);
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);

let seed_bytes = BCS::to_bytes(&sender_address);
Vector::append(&mut seed_bytes, BCS::to_bytes(&sequence_number));
Vector::append(&mut seed_bytes, BCS::to_bytes(&stc_balance));

let seed_hash = Hash::sha3_256(seed_bytes);
let i = 0;
let address_bytes = Vector::empty();
while (i < ADDRESS_LENGTH) {
Vector::push_back(&mut address_bytes, *Vector::borrow(&seed_hash,i));
i = i + 1;
};
let new_address = BCS::to_address(address_bytes);
Self::create_account_with_address<STC>(new_address);
let new_signer = Self::create_signer(new_address);
(new_address, Self::remove_signer_capability(&new_signer))
}

/// Generate a fresh address from the sender address and the sender's sequence number, and ensure the generated address is not exists onchain.
public fun generate_fresh_address(sender: &signer) : address acquires Balance, Account {
let try_times = 0u64;
while(true){
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);

let seed_bytes = BCS::to_bytes(&sender_address);
Vector::append(&mut seed_bytes, BCS::to_bytes(&sequence_number));
Vector::append(&mut seed_bytes, BCS::to_bytes(&stc_balance));
Vector::append(&mut seed_bytes, BCS::to_bytes(&try_times));

let seed_hash = Hash::sha3_256(seed_bytes);

let i = 0;
let address_bytes = Vector::empty();
while (i < ADDRESS_LENGTH) {
Vector::push_back(&mut address_bytes, *Vector::borrow(&seed_hash,i));
i = i + 1;
};
let new_address = BCS::to_address(address_bytes);
if (Self::exists_at(new_address)){
try_times = try_times + 1;
continue
}else{
return new_address
}
};
//unreachable
abort 0
}

spec create_delegate_account {
pragma verify = false;
//TODO write spec
Expand Down

0 comments on commit bc729fc

Please sign in to comment.