From 4b1f1bb06d9de1ea9859fd944ca7a7beb7fc8c8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=89lo=C3=AFs?= <c@elo.tf>
Date: Fri, 15 Jul 2022 15:59:41 +0200
Subject: [PATCH] fix(account): When a self-sufficient accounts receive money
 for the first time, that should trigger the random id assignment (!90)

* ci: not execute live tests in non-scheduled jobs

* fix(account): bug #85

When a self-sufficient accounts receive money for the first time, that should trigger the random id assignment

* tests: reproduce bug #85
---
 .cargo/config                           |  2 +-
 .gitlab-ci.yml                          |  4 +--
 pallets/duniter-account/src/lib.rs      |  8 ++---
 runtime/gdev/tests/integration_tests.rs | 41 +++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/.cargo/config b/.cargo/config
index e07889471..7c3f14a16 100644
--- a/.cargo/config
+++ b/.cargo/config
@@ -1,6 +1,6 @@
 [alias]
 cucumber = "test -p duniter-end2end-tests --test cucumber_tests --"
 sanity-gdev = "test -p duniter-live-tests --test sanity_gdev -- --nocapture"
-tu = "test --workspace --exclude duniter-end2end-tests"
+tu = "test --workspace --exclude duniter-end2end-tests --exclude duniter-live-tests"
 xtask = "run --package xtask --"
 
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index eff2a2845..e628a1551 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -158,7 +158,7 @@ tests_debug:
     DUNITER_BINARY_PATH: "../build/duniter"
     DUNITER_END2END_TESTS_SPAWN_NODE_TIMEOUT: "20"
   script:
-    - cargo test --workspace --exclude duniter-end2end-tests
+    - cargo test --workspace --exclude duniter-end2end-tests --exclude duniter-live-tests
     - cargo cucumber -i balance*
     - cargo cucumber -i monetary*
     - cargo cucumber -i transfer*
@@ -182,7 +182,7 @@ tests_release:
     DUNITER_BINARY_PATH: "../build/duniter"
     DUNITER_END2END_TESTS_SPAWN_NODE_TIMEOUT: "20"
   script:
-    - cargo test --workspace --exclude duniter-end2end-tests
+    - cargo test --workspace --exclude duniter-end2end-tests --exclude duniter-live-tests
     - cargo cucumber -i balance*
     - cargo cucumber -i monetary*
     - cargo cucumber -i transfer*
diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs
index 0615fbd99..17d890eaf 100644
--- a/pallets/duniter-account/src/lib.rs
+++ b/pallets/duniter-account/src/lib.rs
@@ -284,13 +284,11 @@ where
         let result = f(&mut some_data)?;
         let is_providing = some_data.is_some();
         if !was_providing && is_providing {
-            if !frame_system::Pallet::<T>::account_exists(account_id) {
-                // If the account does not exist, we should program its creation
-                PendingNewAccounts::<T>::insert(account_id, ());
-            } else {
-                // If the account already exists, we should register increment providers directly
+            if frame_system::Pallet::<T>::account_exists(account_id) {
+                // If the account is self-sufficient, we should increment providers directly
                 frame_system::Pallet::<T>::inc_providers(account_id);
             }
+            PendingNewAccounts::<T>::insert(account_id, ());
         } else if was_providing && !is_providing {
             match frame_system::Pallet::<T>::dec_providers(account_id)? {
                 frame_system::DecRefStatus::Reaped => return Ok(result),
diff --git a/runtime/gdev/tests/integration_tests.rs b/runtime/gdev/tests/integration_tests.rs
index 9647ceb01..d89336470 100644
--- a/runtime/gdev/tests/integration_tests.rs
+++ b/runtime/gdev/tests/integration_tests.rs
@@ -350,3 +350,44 @@ fn test_create_new_idty() {
             );
         });
 }
+
+#[test]
+fn test_create_new_idty_without_founds() {
+    ExtBuilder::new(1, 3, 4)
+        .with_initial_balances(vec![(AccountKeyring::Alice.to_account_id(), 1_000)])
+        .build()
+        .execute_with(|| {
+            run_to_block(2);
+
+            // Should be able to create an identity without founds
+            assert_ok!(Identity::create_identity(
+                frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
+                AccountKeyring::Eve.to_account_id(),
+            ));
+
+            // At next block, nothing should be preleved
+            run_to_block(3);
+            let events = System::events();
+            assert_eq!(events.len(), 0);
+
+            // Deposit some founds on the identity account,
+            // this should trigger the random id assignemt
+            assert_ok!(Balances::transfer(
+                frame_system::RawOrigin::Signed(AccountKeyring::Alice.to_account_id()).into(),
+                MultiAddress::Id(AccountKeyring::Eve.to_account_id()),
+                200
+            ));
+
+            // At next block, nothing should be preleved,
+            // and a random id request should be registered
+            run_to_block(4);
+            assert_eq!(
+                Balances::free_balance(AccountKeyring::Eve.to_account_id()),
+                200
+            );
+            assert_eq!(
+                Account::pending_random_id_assignments(0),
+                Some(AccountKeyring::Eve.to_account_id())
+            );
+        });
+}
-- 
GitLab