From 12ac2190332c30e8eac8ff3a3b8d1fedd286cfb7 Mon Sep 17 00:00:00 2001
From: librelois <c@elo.tf>
Date: Sat, 11 Jun 2022 20:27:34 +0200
Subject: [PATCH] =?UTF-8?q?fix(accounts):=C2=A0providers=20should=20be=20i?=
 =?UTF-8?q?nc=20as=20soon=20as=20the=20account=20recv=20money?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

providers should be incremented as soon as the account receive funds
---
 pallets/duniter-account/src/lib.rs      | 17 ++++++-------
 runtime/gdev/tests/integration_tests.rs | 32 +++++++++++++++----------
 2 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs
index 0615fbd99..be7fcaa59 100644
--- a/pallets/duniter-account/src/lib.rs
+++ b/pallets/duniter-account/src/lib.rs
@@ -164,14 +164,12 @@ pub mod pallet {
                     let price = T::NewAccountPrice::get();
                     if account_data.free >= T::ExistentialDeposit::get() + price {
                         // The account can pay the new account price, we should:
-                        // 1. Increment providers to create the account for frame_system point of view
-                        // 2. Withdraw the "new account price" amount
-                        // 3. Increment consumers to prevent the destruction of the account before
+                        // 1. Withdraw the "new account price" amount
+                        // 2. Increment consumers to prevent the destruction of the account before
                         // the random id is assigned
-                        // 4. Manage the funds collected
-                        // 5. Submit random id generation request
-                        // 6. Save the id of the random generation request.
-                        frame_system::Pallet::<T>::inc_providers(&account_id);
+                        // 3. Manage the funds collected
+                        // 4. Submit random id generation request
+                        // 5. Save the id of the random generation request.
                         let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw(
                             &account_id,
                             price,
@@ -287,10 +285,9 @@ where
             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
-                frame_system::Pallet::<T>::inc_providers(account_id);
             }
+            // We should increment the "balances" provider
+            frame_system::Pallet::<T>::inc_providers(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 9f67f73a2..29b97c40a 100644
--- a/runtime/gdev/tests/integration_tests.rs
+++ b/runtime/gdev/tests/integration_tests.rs
@@ -169,16 +169,22 @@ fn test_create_new_account_with_insufficient_balance() {
             ));
             let events = System::events();
             //println!("{:#?}", events);
-            assert_eq!(events.len(), 2);
+            assert_eq!(events.len(), 3);
             assert_eq!(
                 System::events()[0].event,
+                Event::System(frame_system::Event::NewAccount {
+                    account: AccountKeyring::Eve.to_account_id(),
+                })
+            );
+            assert_eq!(
+                System::events()[1].event,
                 Event::Balances(pallet_balances::Event::Endowed {
                     account: AccountKeyring::Eve.to_account_id(),
                     free_balance: 400,
                 })
             );
             assert_eq!(
-                System::events()[1].event,
+                System::events()[2].event,
                 Event::Balances(pallet_balances::Event::Transfer {
                     from: AccountKeyring::Alice.to_account_id(),
                     to: AccountKeyring::Eve.to_account_id(),
@@ -234,16 +240,22 @@ fn test_create_new_account() {
             ));
             let events = System::events();
             //println!("{:#?}", events);
-            assert_eq!(events.len(), 2);
+            assert_eq!(events.len(), 3);
             assert_eq!(
                 System::events()[0].event,
+                Event::System(frame_system::Event::NewAccount {
+                    account: AccountKeyring::Eve.to_account_id(),
+                })
+            );
+            assert_eq!(
+                System::events()[1].event,
                 Event::Balances(pallet_balances::Event::Endowed {
                     account: AccountKeyring::Eve.to_account_id(),
                     free_balance: 500,
                 })
             );
             assert_eq!(
-                System::events()[1].event,
+                System::events()[2].event,
                 Event::Balances(pallet_balances::Event::Transfer {
                     from: AccountKeyring::Alice.to_account_id(),
                     to: AccountKeyring::Eve.to_account_id(),
@@ -256,29 +268,23 @@ fn test_create_new_account() {
             run_to_block(3);
             let events = System::events();
             println!("{:#?}", events);
-            assert_eq!(events.len(), 4);
+            assert_eq!(events.len(), 3);
             assert_eq!(
                 System::events()[0].event,
-                Event::System(frame_system::Event::NewAccount {
-                    account: AccountKeyring::Eve.to_account_id(),
-                })
-            );
-            assert_eq!(
-                System::events()[1].event,
                 Event::Balances(pallet_balances::Event::Withdraw {
                     who: AccountKeyring::Eve.to_account_id(),
                     amount: 300,
                 })
             );
             assert_eq!(
-                System::events()[2].event,
+                System::events()[1].event,
                 Event::Balances(pallet_balances::Event::Deposit {
                     who: Treasury::account_id(),
                     amount: 300,
                 })
             );
             assert_eq!(
-                System::events()[3].event,
+                System::events()[2].event,
                 Event::Treasury(pallet_treasury::Event::Deposit { value: 300 })
             );
             assert_eq!(
-- 
GitLab