Przeglądaj źródła

Merge pull request #1607 from iorveth/babylon

remove_entity: Update reference counter of referenced entities after removal performed
Bedeho Mender 4 lat temu
rodzic
commit
2638266780

+ 10 - 2
runtime-modules/content-directory/src/lib.rs

@@ -1092,8 +1092,8 @@ decl_module! {
 
 
             let entity_values = entity.get_values();
             let entity_values = entity.get_values();
 
 
-            let unique_property_value_hashes = match StoredValuesForExistingProperties::<T>::from(&class_properties, &entity_values) {
+            let values_for_existing_properties = match StoredValuesForExistingProperties::<T>::from(&class_properties, &entity_values) {
-                Ok(values_for_existing_properties) => values_for_existing_properties.compute_unique_hashes(),
+                Ok(values_for_existing_properties) => values_for_existing_properties,
                 Err(e) => {
                 Err(e) => {
                     debug_assert!(false, "Should not fail! {:?}", e);
                     debug_assert!(false, "Should not fail! {:?}", e);
                     return Err(e.into())
                     return Err(e.into())
@@ -1104,6 +1104,14 @@ decl_module! {
             // == MUTATION SAFE ==
             // == MUTATION SAFE ==
             //
             //
 
 
+            let unique_property_value_hashes = values_for_existing_properties.compute_unique_hashes();
+
+            // Calculate entities reference counter side effects for current operation
+            let entities_inbound_rcs_delta = Self::calculate_entities_inbound_rcs_delta(entity_id, values_for_existing_properties, DeltaMode::Decrement);
+
+            // Update InboundReferenceCounter, based on previously calculated entities_inbound_rcs_delta, for each Entity involved
+            Self::update_entities_rcs(&entities_inbound_rcs_delta);
+
             // Remove property value entries, that should be unique on Class level
             // Remove property value entries, that should be unique on Class level
             Self::remove_unique_property_value_hashes(class_id, unique_property_value_hashes);
             Self::remove_unique_property_value_hashes(class_id, unique_property_value_hashes);
 
 

+ 6 - 0
runtime-modules/content-directory/src/tests/remove_entity.rs

@@ -292,5 +292,11 @@ fn remove_entity_rc_does_not_equal_to_zero() {
             Error::<Runtime>::EntityRcDoesNotEqualToZero,
             Error::<Runtime>::EntityRcDoesNotEqualToZero,
             number_of_events_before_call,
             number_of_events_before_call,
         );
         );
+
+        // Remove first entity, which have property values referencing second one.
+        assert_ok!(remove_entity(LEAD_ORIGIN, actor, FIRST_ENTITY_ID));
+
+        // Succesfully perform second entity removal
+        assert_ok!(remove_entity(LEAD_ORIGIN, actor, SECOND_ENTITY_ID));
     })
     })
 }
 }