diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs index be7fcaa59eeb4aecbf809d8152941fefb2cfe523..c6a0fb33a96edb9a09370ae90ca4538407545c1e 100644 --- a/pallets/duniter-account/src/lib.rs +++ b/pallets/duniter-account/src/lib.rs @@ -185,7 +185,7 @@ pub mod pallet { frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id); debug_assert!( res.is_ok(), - "Cannot fail because providers are incremented just before" + "Cannot fail because any account with funds should have providers" ); T::OnUnbalanced::on_unbalanced(imbalance); let request_id = pallet_provide_randomness::Pallet::<T>::force_request( @@ -282,12 +282,8 @@ 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, ()); - } - // We should increment the "balances" provider 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 5c8c50df92cc07b4e21714dbe50a92155c564583..9a1ca4833c8cfe78dd4e4a727ca122bb6895dd5d 100644 --- a/runtime/gdev/tests/integration_tests.rs +++ b/runtime/gdev/tests/integration_tests.rs @@ -282,30 +282,19 @@ fn test_create_new_account() { MultiAddress::Id(AccountKeyring::Eve.to_account_id()), 500 )); - let events = System::events(); - //println!("{:#?}", events); - 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()[2].event, - Event::Balances(pallet_balances::Event::Transfer { - from: AccountKeyring::Alice.to_account_id(), - to: AccountKeyring::Eve.to_account_id(), - amount: 500, - }) - ); + //println!("{:#?}", System::events()); + System::assert_has_event(Event::System(frame_system::Event::NewAccount { + account: AccountKeyring::Eve.to_account_id(), + })); + System::assert_has_event(Event::Balances(pallet_balances::Event::Endowed { + account: AccountKeyring::Eve.to_account_id(), + free_balance: 500, + })); + System::assert_has_event(Event::Balances(pallet_balances::Event::Transfer { + from: AccountKeyring::Alice.to_account_id(), + to: AccountKeyring::Eve.to_account_id(), + amount: 500, + })); // At next block, the new account must be created, // and new account tax should be collected and deposited in the treasury @@ -400,3 +389,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()) + ); + }); +}