@@ -14,15 +14,15 @@ use frame_system::{pallet_prelude::BlockNumberFor, EnsureRoot, EnsureSigned};
1414use shp_file_metadata:: { FileMetadata , Fingerprint } ;
1515use shp_traits:: {
1616 CommitRevealRandomnessInterface , CommitmentVerifier , MaybeDebug , ProofSubmittersInterface ,
17- TrieMutation , TrieProofDeltaApplier ,
17+ TrieMutation , TrieProofDeltaApplier , TrieRemoveMutation ,
1818} ;
1919use shp_treasury_funding:: NoCutTreasuryCutCalculator ;
2020use sp_core:: { hashing:: blake2_256, ConstU128 , ConstU32 , ConstU64 , Hasher , H256 } ;
2121use sp_runtime:: {
2222 traits:: { BlakeTwo256 , Convert , ConvertBack , IdentityLookup } ,
2323 BuildStorage , DispatchError , Perbill , SaturatedConversion ,
2424} ;
25- use sp_std:: collections:: btree_set:: BTreeSet ;
25+ use sp_std:: collections:: { btree_map :: BTreeMap , btree_set:: BTreeSet } ;
2626use sp_trie:: { CompactProof , LayoutV1 , MemoryDB , TrieConfiguration , TrieLayout } ;
2727
2828type Block = frame_system:: mocking:: MockBlock < Test > ;
@@ -422,53 +422,57 @@ where
422422 (
423423 MemoryDB < T :: Hash > ,
424424 Self :: Key ,
425- Vec < ( Self :: Key , Option < Vec < u8 > > ) > ,
425+ BTreeMap < Self :: Key , TrieMutation > ,
426426 ) ,
427427 DispatchError ,
428428 > {
429429 let last_key = mutations. last ( ) . unwrap ( ) . 0 ;
430430
431431 let db = MemoryDB :: < T :: Hash > :: default ( ) ;
432432
433- let mutated_keys_and_values = mutations
434- . iter ( )
435- . map ( |( key, mutation) | {
436- let value = match mutation {
437- TrieMutation :: Add ( add_mutation) => Some ( add_mutation. value . clone ( ) ) ,
438- TrieMutation :: Remove ( _) => {
439- let file_metadata: FileMetadata <
440- { shp_constants:: H_LENGTH } ,
441- { shp_constants:: FILE_CHUNK_SIZE } ,
442- { shp_constants:: FILE_SIZE_TO_CHALLENGES } ,
443- > = match FileMetadata :: new (
444- 1_u64 . encode ( ) ,
445- blake2_256 ( b"bucket" ) . as_ref ( ) . to_vec ( ) ,
446- b"path/to/file" . to_vec ( ) ,
447- 1 ,
448- Fingerprint :: default ( ) . into ( ) ,
449- ) {
450- Ok ( file_metadata) => file_metadata,
451- Err ( _) => {
452- return Err ( DispatchError :: Other ( "Failed to create file metadata" ) )
453- }
454- } ;
455-
456- if key. as_ref ( ) != [ 0 ; H_LENGTH ] {
457- Some ( file_metadata. encode ( ) )
458- } else {
459- Some ( vec ! [ 1 , 2 , 3 , 4 , 5 , 6 ] ) // We make it so the metadata is invalid for the empty key
433+ let mut applied_mutations = BTreeMap :: new ( ) ;
434+
435+ for ( key, mutation) in mutations {
436+ match mutation {
437+ TrieMutation :: Add ( add_mutation) => {
438+ applied_mutations. insert ( * key, add_mutation. clone ( ) . into ( ) ) ;
439+ }
440+ TrieMutation :: Remove ( _) => {
441+ let file_metadata: FileMetadata <
442+ { shp_constants:: H_LENGTH } ,
443+ { shp_constants:: FILE_CHUNK_SIZE } ,
444+ { shp_constants:: FILE_SIZE_TO_CHALLENGES } ,
445+ > = match FileMetadata :: new (
446+ 1_u64 . encode ( ) ,
447+ blake2_256 ( b"bucket" ) . as_ref ( ) . to_vec ( ) ,
448+ b"path/to/file" . to_vec ( ) ,
449+ 1 ,
450+ Fingerprint :: default ( ) . into ( ) ,
451+ ) {
452+ Ok ( file_metadata) => file_metadata,
453+ Err ( _) => {
454+ return Err ( DispatchError :: Other ( "Failed to create file metadata" ) )
460455 }
456+ } ;
457+
458+ if key. as_ref ( ) != [ 0 ; H_LENGTH ] {
459+ applied_mutations. insert (
460+ * key,
461+ TrieRemoveMutation :: with_value ( file_metadata. encode ( ) ) . into ( ) ,
462+ ) ;
463+ } else {
464+ applied_mutations. insert (
465+ * key,
466+ TrieRemoveMutation :: with_value ( vec ! [ 1 , 2 , 3 , 4 , 5 , 6 ] ) . into ( ) ,
467+ ) ; // We make it so the metadata is invalid for the empty key
461468 }
462- } ;
463- Ok ( ( * key, value) )
464- } )
465- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
466-
467- let mutated_keys_and_values = mutated_keys_and_values;
469+ }
470+ }
471+ }
468472
469473 // Return default db, the last key in mutations as the new root, and a
470474 // vector holding the supposedly mutated keys and values, so it is deterministic for testing.
471- Ok ( ( db, last_key, mutated_keys_and_values ) )
475+ Ok ( ( db, last_key, applied_mutations ) )
472476 }
473477}
474478
0 commit comments