Skip to content

Commit 5a5f5ec

Browse files
sasuke0787Gabi ConeyBasil Von Hart
authored
[reauth] cleanup activity checks (#392)
Co-authored-by: Gabi Coney <[email protected]> Co-authored-by: Basil Von Hart <[email protected]>
1 parent f7ceeb4 commit 5a5f5ec

File tree

10 files changed

+62
-66
lines changed

10 files changed

+62
-66
lines changed

framework/libra-framework/sources/genesis.move

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ module diem_framework::genesis {
183183
diem_framework: &signer,
184184
core_resources_auth_key: vector<u8>,
185185
) {
186-
// let (burn_cap, mint_cap) = diem_coin::initialize(diem_framework);
187-
188186
let core_resources = account::create_account(@core_resources);
189187
account::rotate_authentication_key_internal(&core_resources, core_resources_auth_key);
190188

framework/libra-framework/sources/ol_sources/activity.move

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,30 @@ module ol_framework::activity {
6363

6464
public(friend) fun maybe_onboard(user_sig: &signer){
6565

66+
// genesis accounts should not start at 0
67+
let onboarding_usecs = timestamp::now_seconds();
68+
if (onboarding_usecs == 0) {
69+
onboarding_usecs = 1;
70+
};
71+
6672
if (!exists<Activity>(signer::address_of(user_sig))) {
6773
move_to<Activity>(user_sig, Activity {
6874
last_touch_usecs: 0, // how we identify if a users has used the account after a peer created it.
69-
onboarding_usecs: timestamp::now_seconds(),
75+
onboarding_usecs,
7076
})
7177
}
7278
}
7379

7480

7581
#[view]
7682
// check if this is an account that has activity
77-
public fun has_ever_been_touched(user: address): bool {
83+
public fun has_ever_been_touched(user: address): bool acquires Activity {
7884
// I was beat, incomplete
7985
// I've been had, I was sad and blue
8086
// But you made me feel
8187
// Yeah, you made me feel
8288
// Shiny and new
83-
exists<Activity>(user)
84-
85-
// TODO: check timestamp
86-
// if (exists<Activity>(user)){
87-
// let state = borrow_global<Activity>(user);
88-
// return state.last_touch_usecs > 0
89-
// };
90-
91-
// false
89+
get_last_touch_usecs(user) > 0
9290
}
9391

9492

@@ -118,8 +116,7 @@ module ol_framework::activity {
118116
// If the account is a founder/pre-v8 account has been migrated
119117
// then it would have an onboarding timestamp of 0
120118
public fun is_prehistoric(user: address): bool acquires Activity {
121-
let state = borrow_global<Activity>(user);
122-
state.onboarding_usecs == 0
119+
get_onboarding_usecs(user) == 0
123120
}
124121

125122
#[test_only]

framework/libra-framework/sources/ol_sources/mock.move

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ public fun ol_test_genesis(root: &signer) {
148148
system_addresses::assert_ol(root);
149149
genesis::setup();
150150
genesis::test_end_genesis(root);
151+
// should not be 0, activity.move checks this for inactive accounts
152+
timestamp::fast_forward_seconds(1);
151153

152154
let mint_cap = coin_init_minimal(root);
153155
libra_coin::restore_mint_cap(root, mint_cap);
@@ -573,16 +575,13 @@ public fun mock_vouch_score_50(framework: &signer, target_account: address): vec
573575
}
574576

575577
#[test_only]
576-
/// two state initializations happen on first
577-
/// transaction
578-
public fun simulate_transaction_validation(sender: &signer) {
579-
let time = timestamp::now_seconds();
580-
// will initialize structs if first time
581-
activity::increment(sender, time);
582-
578+
/// simulate the v7 account state migrating to the new data structures
579+
public fun simulate_v8_migration(sender: &signer) {
583580
// run migrations
584581
// Note, Activity and Founder struct should have been set above
585582
filo_migration::maybe_migrate(sender);
583+
// touching the account will also increment activity
584+
activity::increment(sender, timestamp::now_seconds());
586585
}
587586

588587
//////// META TESTS ////////

framework/libra-framework/sources/ol_sources/ol_account.spec.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ spec ol_framework::ol_account {
6666
aborts_if exists<slow_wallet::SlowWallet>(account_addr) &&
6767
slow_store.unlocked < amount;
6868

69-
aborts_if !activity::has_ever_been_touched(account_addr);
69+
aborts_if !activity::is_initialized(account_addr);
7070

7171
ensures result == Coin<LibraCoin>{value: amount};
7272
}

framework/libra-framework/sources/ol_sources/reauthorization.move

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module ol_framework::reauthorization {
1818

1919
public fun assert_v8_authorized(account: address) {
2020

21-
let migrated = activity::has_ever_been_touched(account);
21+
let migrated = activity::is_initialized(account);
2222
let founder = founder::is_founder(account);
2323
let is_cw = community_wallet::is_init(account);
2424

@@ -35,28 +35,32 @@ module ol_framework::reauthorization {
3535

3636
#[view]
3737
public fun is_v8_authorized(account: address): bool {
38-
// case 1: user onboarded since v8 started
39-
// has activity struct, and not founder, or community wallet
40-
if (
41-
activity::has_ever_been_touched(account) &&
42-
!founder::is_founder(account) &&
43-
!community_wallet::is_init(account)
44-
) {
45-
return true
38+
// Every newly create account since v8 will have the Activity struct.
39+
// Check if this is a case of an account that has yet to migrate from V7.
40+
// This also applies to donor-voice accounts.
41+
if (!activity::is_initialized(account)) {
42+
return false
4643
};
4744

48-
// case 2: a friendly founder with struct and vouches
49-
if (
50-
activity::has_ever_been_touched(account) &&
51-
founder::is_founder(account) &&
52-
!community_wallet::is_init(account)
53-
) {
45+
// first deal with the special case of the community wallet
46+
// case 1: a community wallet which was reauthorized
47+
if(community_wallet::is_init(account)) {
48+
return donor_voice_reauth::is_authorized(account)
49+
};
5450

51+
// case 2: deal with v7 accounts "founder"
52+
// and check that they are not bots
53+
if (founder::is_founder(account)) {
5554
return founder::has_friends(account)
5655
};
57-
// case 3: a community wallet which was reauthorized
58-
if(community_wallet::is_init(account)) {
59-
return donor_voice_reauth::is_authorized(account)
56+
57+
58+
// case 3: user onboarded since v8 started
59+
// check only if the account is malformed
60+
// i.e. has an activity struct but no activity
61+
62+
if (!activity::is_prehistoric(account)) {
63+
return true
6064
};
6165

6266
false

framework/libra-framework/sources/ol_sources/slow_wallet.move

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,6 @@ module ol_framework::slow_wallet {
306306
};
307307

308308
if (exists<SlowWallet>(addr)) {
309-
// if the account has never been activated, the unlocked amount is
310-
// zero despite the state (which is stale, until there is a migration).
311-
if (!reauthorization::is_v8_authorized(addr)) {
312-
return 0
313-
};
314309
let s = borrow_global<SlowWallet>(addr);
315310
return s.unlocked
316311
};

framework/libra-framework/sources/ol_sources/tests/cumu_deposits.test.move

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module ol_framework::test_cumu_deposits {
3939
ol_account::transfer(bob, @0x1000a, bob_donation); // this should track
4040

4141
let (a, b, c) = receipts::read_receipt(@0x1000b, @0x1000a);
42-
assert!(a == new_time, 7357004); // timestamp is not genesis
42+
assert!(a != 1, 7357004); // timestamp is not genesis
4343
assert!(b == bob_donation, 7357005); // last payment
4444
assert!(c == bob_donation, 7357006); // cumulative payments
4545

framework/libra-framework/sources/ol_sources/tests/filo_migration_test.move

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module ol_framework::test_filo_migration {
2121
assert!(account::exists_at(b_addr), 735701);
2222
assert!(!vouch::is_init(b_addr), 735701);
2323
assert!(!founder::is_founder(b_addr), 735702);
24-
assert!(!activity::has_ever_been_touched(b_addr), 735703);
24+
assert!(!activity::is_initialized(b_addr), 735703);
2525
// setup a v7 account without slow wallet.
2626
assert!(!slow_wallet::is_slow(b_addr), 735704);
2727
}
@@ -38,7 +38,7 @@ module ol_framework::test_filo_migration {
3838
/// error out
3939
fun v7_accept_tx_validation(framework: &signer, bob: &signer) {
4040
setup_one_v7_account(framework, bob);
41-
mock::simulate_transaction_validation(bob);
41+
mock::simulate_v8_migration(bob);
4242

4343
}
4444

@@ -48,16 +48,16 @@ module ol_framework::test_filo_migration {
4848
setup_one_v7_account(framework, bob);
4949

5050
//////// user sends migration tx ////////
51-
mock::simulate_transaction_validation(bob);
51+
mock::simulate_v8_migration(bob);
5252
// safety check: should not error if called again, lazy init
53-
mock::simulate_transaction_validation(bob);
53+
mock::simulate_v8_migration(bob);
5454
//////// end migration tx ////////
5555

5656
let b_addr = signer::address_of(bob);
5757
// now the post-migration state should exist
5858
assert!(vouch::is_init(b_addr), 735706);
5959
assert!(founder::is_founder(b_addr), 735707);
60-
assert!(activity::has_ever_been_touched(b_addr), 735708);
60+
assert!(activity::is_initialized(b_addr), 735708);
6161
assert!(slow_wallet::is_slow(b_addr), 735709);
6262
// however the vouch is not sufficient
6363
assert!(!founder::has_friends(b_addr), 7357010);
@@ -82,7 +82,7 @@ module ol_framework::test_filo_migration {
8282
//////// user sends migration tx ////////
8383
// The first time the user touches the account with a transaction
8484
// the migration should happen
85-
mock::simulate_transaction_validation(bob);
85+
mock::simulate_v8_migration(bob);
8686
//////// end migration tx ////////
8787

8888
let (unlocked, total) = ol_account::balance(b_addr);
@@ -150,7 +150,7 @@ module ol_framework::test_filo_migration {
150150
assert!(unlocked == 0, 735705);
151151
assert!(total == 1000, 735706);
152152

153-
assert!(!activity::has_ever_been_touched(b_addr), 735707);
153+
assert!(!activity::is_initialized(b_addr), 735707);
154154
// uses transfer entry function
155155
ol_account::transfer(bob, marlon, 33);
156156
}
@@ -169,12 +169,12 @@ module ol_framework::test_filo_migration {
169169
assert!(unlocked == 0, 735705);
170170
assert!(total == 1000, 735706);
171171

172-
assert!(!activity::has_ever_been_touched(b_addr), 735707);
172+
assert!(!activity::is_initialized(b_addr), 735707);
173173

174174
//////// user sends migration tx ////////
175175
// The first time the user touches the account with a transaction
176176
// the migration should happen
177-
mock::simulate_transaction_validation(bob);
177+
mock::simulate_v8_migration(bob);
178178
founder::test_mock_friendly(framework, bob);
179179
//////// end migration tx ////////
180180

@@ -196,18 +196,18 @@ module ol_framework::test_filo_migration {
196196
assert!(unlocked == 0, 735705);
197197
assert!(total == 1000, 735706);
198198

199-
assert!(!activity::has_ever_been_touched(b_addr), 735707);
199+
assert!(!activity::is_initialized(b_addr), 735707);
200200
assert!(!reauthorization::is_v8_authorized(b_addr), 735708);
201201

202202
//////// user sends migration tx ////////
203203
// The first time the user touches the account with a transaction
204204
// the migration should happen
205-
mock::simulate_transaction_validation(bob);
205+
mock::simulate_v8_migration(bob);
206206
//////// end migration tx ////////
207207

208208
assert!(vouch::is_init(b_addr), 735706);
209209
assert!(founder::is_founder(b_addr), 735707);
210-
assert!(activity::has_ever_been_touched(b_addr), 735708);
210+
assert!(activity::is_initialized(b_addr), 735708);
211211
assert!(slow_wallet::is_slow(b_addr), 735709);
212212
// however the vouch is not sufficient
213213
assert!(!founder::has_friends(b_addr), 7357010);
@@ -234,13 +234,13 @@ module ol_framework::test_filo_migration {
234234
assert!(unlocked == 0, 735705);
235235
assert!(total == 1000, 735706);
236236

237-
assert!(!activity::has_ever_been_touched(b_addr), 735707);
237+
assert!(!activity::is_initialized(b_addr), 735707);
238238
assert!(!reauthorization::is_v8_authorized(b_addr), 735708);
239239

240240
//////// user sends migration tx ////////
241241
// The first time the user touches the account with a transaction
242242
// the migration should happen
243-
mock::simulate_transaction_validation(bob);
243+
mock::simulate_v8_migration(bob);
244244
//////// end migration tx ////////
245245
slow_wallet::test_epoch_drip(framework, 100);
246246

@@ -261,13 +261,13 @@ module ol_framework::test_filo_migration {
261261
assert!(unlocked == 0, 735705);
262262
assert!(total == 1000, 735706);
263263

264-
assert!(!activity::has_ever_been_touched(b_addr), 735707);
264+
assert!(!activity::is_initialized(b_addr), 735707);
265265
assert!(!reauthorization::is_v8_authorized(b_addr), 735708);
266266

267267
//////// user sends migration tx ////////
268268
// The first time the user touches the account with a transaction
269269
// the migration should happen
270-
mock::simulate_transaction_validation(bob);
270+
mock::simulate_v8_migration(bob);
271271
//////// end migration tx ////////
272272

273273
founder::test_mock_friendly(framework, bob);

framework/libra-framework/sources/ol_sources/tests/page_rank.test.move

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module ol_framework::test_page_rank {
2525

2626
vector::for_each_ref(&roots_sig, |sig| {
2727
// make each account a v8 address
28-
mock::simulate_transaction_validation(sig);
28+
mock::simulate_v8_migration(sig);
2929
});
3030

3131
// make these accounts root of trust
@@ -317,7 +317,7 @@ module ol_framework::test_page_rank {
317317
let user_addr = signer::address_of(&seven_user_sig);
318318

319319
// a v7 user touches the account to get structs created
320-
mock::simulate_transaction_validation(&seven_user_sig);
320+
mock::simulate_v8_migration(&seven_user_sig);
321321
// check lazy migration worked
322322
let is_init = activity::is_initialized(user_addr);
323323
let pre = activity::is_prehistoric(user_addr);

framework/libra-framework/sources/timestamp.move

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ module diem_framework::timestamp {
5656
}
5757

5858
#[test_only]
59-
public fun set_time_has_started_for_testing(account: &signer) {
59+
public fun set_time_has_started_for_testing(account: &signer) acquires CurrentTimeMicroseconds {
6060
if (!exists<CurrentTimeMicroseconds>(@diem_framework)) {
6161
set_time_has_started(account);
6262
};
63+
// Tests shouldn't start at zero, no transactions ever run at 0
64+
// plus we use Activity timestamp at zero to check for malformed accounts.
65+
fast_forward_seconds(1);
6366
}
6467

6568
#[view]

0 commit comments

Comments
 (0)