From 7a38b4b8b99f6d61eaa5e38479ad0b1ab2292bc4 Mon Sep 17 00:00:00 2001 From: jolestar Date: Wed, 7 Sep 2022 09:25:59 +0800 Subject: [PATCH] [account] Retry when creating delegate account (#110) * [account] Retry when creating delegate account --- build/StarcoinFramework/BuildInfo.yaml | 2 +- .../bytecode_modules/Account.mv | Bin 6111 -> 6206 bytes build/StarcoinFramework/docs/Account.md | 73 ++++++++++++++---- .../source_maps/Account.mvsm | Bin 46526 -> 47563 bytes .../account/delegate_account.exp | 2 +- .../account/delegate_account_retry.exp | 7 ++ .../account/delegate_account_retry.move | 27 +++++++ integration-tests/daospace/dao_account.exp | 6 +- sources/Account.move | 52 +++++++++---- 9 files changed, 130 insertions(+), 39 deletions(-) create mode 100644 integration-tests/account/delegate_account_retry.exp create mode 100644 integration-tests/account/delegate_account_retry.move diff --git a/build/StarcoinFramework/BuildInfo.yaml b/build/StarcoinFramework/BuildInfo.yaml index 4fbcde53..7f16c6f0 100644 --- a/build/StarcoinFramework/BuildInfo.yaml +++ b/build/StarcoinFramework/BuildInfo.yaml @@ -312,7 +312,7 @@ compiled_package_info: ? address: "0x00000000000000000000000000000001" name: YieldFarmingV2 : StarcoinFramework - source_digest: 5F36AADE9312D2934E215C4971621A24AF18EA5B56A52D2BE1D8E173174D0F3C + source_digest: 465BD18AAFF08B609377C24DF93A365C461646250126145426395BBBD12FD8CC build_flags: dev_mode: false test_mode: false diff --git a/build/StarcoinFramework/bytecode_modules/Account.mv b/build/StarcoinFramework/bytecode_modules/Account.mv index f13513b1b5be1b3e88e92f2d8f66d17693815b8f..b197aad86af195affe2f63f2b4bd7eae51c9e3f6 100644 GIT binary patch delta 2331 zcmZ8iTWnlM8J_>2IcLt9IeT_zua3QI?|Nf9$Bw=6Eg9PzCw3CY&aJ5%r*>~9ZIYH- zT9QJ;bz3NxPzk9z2nh*+DxQ$~fRrbA0A6`P;;Ao@c%%ry8}JDK9Iq?DlD{+a-{znH z|NhxOb$)YV{e${nwP1|J!0K4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__H~7nil)Nk(~F zo{YJjZ-h#Af>0U7r{w}&^?de9<;?z>o}a5voFC#~i2KSUFScn(<;tnV)e4AGzwfLn zTkdG`l7=Q9H$JF{dPYt;q4ufmntbX|s>Pg^b3>{;!B?CtOtwF+j(4fWz%GWK_OzNi z#_tYPl>VG5x9CYF(>QV0A%}747~gl2ylYJ~PSWIWsT^%_u1^jXRJ&*AG|nG3d58L3nW5Q# E1Lnl(%m4rY diff --git a/build/StarcoinFramework/docs/Account.md b/build/StarcoinFramework/docs/Account.md index 9c337ccb..826f5009 100644 --- a/build/StarcoinFramework/docs/Account.md +++ b/build/StarcoinFramework/docs/Account.md @@ -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 `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) @@ -1302,23 +1303,7 @@ Generate an new address and create a new account, then delegate the account and
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);
-
-    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);
+    let new_address = generate_fresh_address(sender);
     Self::create_account_with_address<STC>(new_address);
     let new_signer = Self::create_signer(new_address);
     (new_address, Self::remove_signer_capability(&new_signer))
@@ -1339,6 +1324,60 @@ Generate an new address and create a new account, then delegate the account and
 
 
 
+
+
+
+
+## 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.
+
+
+
public fun generate_fresh_address(sender: &signer): address
+
+ + + +
+Implementation + + +
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
+}
+
+ + +
diff --git a/build/StarcoinFramework/source_maps/Account.mvsm b/build/StarcoinFramework/source_maps/Account.mvsm index 1cbc24c3fbd72faf8fcb614d321f702a1f085fe0..543155e5c234b6bbac0655e734e2d3a2d77d2147 100644 GIT binary patch literal 47563 zcmbuIcX(A*x`$U1AP{;H5L8Gg(mSGnLdpRmA&E&S(UC()4k1DkNCFDdI|5<>K|rto zVjXZCIw&wIh$u=?x|BgwL`58NM7-~U>>2Oe=ic8sUY_Uuhv(hj+I#Kwt+w|*S+?hv zQb#{teW>E+pOim%$1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2k0&2UReD=Zz0#iiUW(ig$)Dv4N3?gDt4pq1M8oyJc9fUSVqPf)bn5+8NZvd zlNne7`O{Gu954*rXFZldHIqXY0vo!rvzFjZ03B3!VNC=*G`g3q!|zSX+XXuO7GbRh z9e!@@b@=6`I8GtxXFVRQBG7>vk7aBfsJ*b(fexw)thJznYAn`Epr6aQXV=eVwo=|Z zpr6a^#Cjj}bD4Lsc7T2^vl;6r(9dP|W1WIVvd@1}ZR+PTZkhVIOq*0DdC<>gx?$Z6 z`ngPREDz}CGC^1gpr6aUfwc?tD_tL8?E(Et*AlF~;4}(Pi5=|A>fZGVr*3R~e1<18 z)0gZ?P4tpt){W0fNg3jc&GPp54e$&Jb{cx(J?R;qxLB_z-j|S-8s|+)OAY6rGctVM zv;m$}Uuv4y7aJGnN%uBzuJFXAC8hc}BrP+^>r2Y?4fSNCh4Jr6shL>`2}yBDo>Z@| zUu<%0YMiI0bG4^mY`o8#5u2JBTUD|z!Q;79ZVjiYIY(wvVyY*@7n_{niH#pZ$;qBX zb1n6qYdk5j$q8v0DO{MjUJ6d)GPyi+<+Yq^Ip9*|VpA^}=HGoONtr3J-njlD20bS1PSih3NYCn*oD^4ed{Sy$T54uerkC@DqWrTA+8`~9 zV&c<0nN^MA8H`oiiT;<8>A5Y-L&bfmSt*$+2;s{+!Q~5#+S-3`+9$ vkI#r56q`&lrpNY6N>1|9gQ{(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); + let new_address = generate_fresh_address(sender); Self::create_account_with_address(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(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