@@ -201,7 +201,102 @@ impl Drive {
201201#[ cfg( test) ]
202202mod tests {
203203 use super :: * ;
204+ use crate :: util:: test_helpers:: setup:: setup_drive_with_initial_state_structure;
205+ use dpp:: address_funds:: PlatformAddress ;
206+ use dpp:: balances:: credits:: CreditOperation ;
204207 use platform_version:: version:: PlatformVersion ;
208+ use std:: collections:: BTreeMap ;
209+
210+ #[ test]
211+ fn should_prove_and_verify_compacted_address_balance_changes_roundtrip ( ) {
212+ let drive = setup_drive_with_initial_state_structure ( None ) ;
213+ let platform_version = PlatformVersion :: latest ( ) ;
214+
215+ let address_1 = PlatformAddress :: P2pkh ( [ 10 ; 20 ] ) ;
216+ let address_2 = PlatformAddress :: P2sh ( [ 11 ; 20 ] ) ;
217+
218+ // Store enough blocks of changes to trigger compaction
219+ // The compaction threshold is controlled by platform_version
220+ // We insert many blocks to ensure compaction happens
221+ let max_blocks = platform_version
222+ . drive
223+ . methods
224+ . saved_block_transactions
225+ . max_blocks_before_compaction as u64 ;
226+
227+ for block_height in 1u64 ..=( max_blocks + 1 ) {
228+ let mut changes = BTreeMap :: new ( ) ;
229+ changes. insert (
230+ address_1,
231+ CreditOperation :: AddToCredits ( block_height * 1000 ) ,
232+ ) ;
233+ if block_height % 2 == 0 {
234+ changes. insert ( address_2, CreditOperation :: AddToCredits ( block_height * 500 ) ) ;
235+ }
236+ drive
237+ . store_address_balances_for_block (
238+ & changes,
239+ block_height,
240+ block_height * 1000 ,
241+ None ,
242+ platform_version,
243+ )
244+ . expect ( "should store balances" ) ;
245+ }
246+
247+ // Prove compacted changes from block 1
248+ let proof = drive
249+ . prove_compacted_address_balance_changes ( 1 , None , None , platform_version)
250+ . expect ( "should prove compacted address balance changes" ) ;
251+
252+ // Verify the proof
253+ let ( root_hash, compacted_changes) = Drive :: verify_compacted_address_balance_changes (
254+ proof. as_slice ( ) ,
255+ 1 ,
256+ None ,
257+ platform_version,
258+ )
259+ . expect ( "should verify proof" ) ;
260+
261+ assert ! ( !root_hash. is_empty( ) , "root hash should not be empty" ) ;
262+ // We should have at least one compacted entry
263+ assert ! (
264+ !compacted_changes. is_empty( ) ,
265+ "should have at least one compacted entry"
266+ ) ;
267+
268+ // All entries should have valid block ranges
269+ for ( start, end, changes) in & compacted_changes {
270+ assert ! ( * start <= * end, "start should be <= end" ) ;
271+ assert ! ( !changes. is_empty( ) , "each entry should have changes" ) ;
272+ }
273+ }
274+
275+ #[ test]
276+ fn should_prove_and_verify_empty_compacted_address_balance_changes ( ) {
277+ let drive = setup_drive_with_initial_state_structure ( None ) ;
278+ let platform_version = PlatformVersion :: latest ( ) ;
279+
280+ // Prove compacted changes from block 100 with no data stored
281+ let proof = drive
282+ . prove_compacted_address_balance_changes ( 100 , None , None , platform_version)
283+ . expect ( "should prove empty compacted changes" ) ;
284+
285+ // Verify the proof
286+ let ( root_hash, compacted_changes) = Drive :: verify_compacted_address_balance_changes (
287+ proof. as_slice ( ) ,
288+ 100 ,
289+ None ,
290+ platform_version,
291+ )
292+ . expect ( "should verify proof" ) ;
293+
294+ assert ! ( !root_hash. is_empty( ) , "root hash should not be empty" ) ;
295+ assert ! (
296+ compacted_changes. is_empty( ) ,
297+ "should have no compacted entries when no data stored"
298+ ) ;
299+ }
205300
206301 #[ test]
207302 fn test_verify_compacted_address_balance_changes_proof ( ) {
@@ -247,7 +342,7 @@ mod tests {
247342 let start_block_height = 329u64 ;
248343 let platform_version = PlatformVersion :: latest ( ) ;
249344
250- let result = Drive :: verify_compacted_address_balance_changes_v0 (
345+ let result = Drive :: verify_compacted_address_balance_changes (
251346 & proof,
252347 start_block_height,
253348 None ,
0 commit comments