@@ -17,7 +17,7 @@ use crate::{
1717} ;
1818use fastrace:: trace;
1919use indexer_common:: {
20- domain:: { ByteVec , CardanoRewardAddress , LedgerVersion , ledger} ,
20+ domain:: { ByteVec , CardanoRewardAddress , LedgerVersion , TimestampMs , TimestampSecs , ledger} ,
2121 infra:: sqlx:: U128BeBytes ,
2222} ;
2323use indoc:: indoc;
@@ -78,31 +78,32 @@ impl DustStorage for Storage {
7878 . fetch_optional ( & * self . pool )
7979 . await ?;
8080
81- if let Some ( ( value, ctime ) ) = result {
81+ if let Some ( ( value, ctime_raw ) ) = result {
8282 let value = u128:: from ( value) ;
8383 night_balance = value;
8484
8585 // DUST generation rate = STAR * generation_decay_rate SPECK/second.
8686 generation_rate = value. saturating_mul ( generation_decay_rate) ;
8787
88- // Calculate current capacity based on elapsed time since creation.
88+ // dust_generation_info.ctime is in seconds (ledger convention).
89+ let ctime = TimestampSecs ( ctime_raw as u64 ) ;
90+
8991 // Get current timestamp from latest block.
92+ // blocks.timestamp is in milliseconds (Substrate Timestamp pallet).
9093 let current_time_query = indoc ! { "
9194 SELECT timestamp
9295 FROM blocks
9396 ORDER BY height DESC
9497 LIMIT 1
9598 " } ;
9699
97- let current_timestamp = sqlx:: query_as :: < _ , ( i64 , ) > ( current_time_query)
100+ let now = sqlx:: query_as :: < _ , ( i64 , ) > ( current_time_query)
98101 . fetch_optional ( & * self . pool )
99102 . await ?
100- . map ( |( t, ) | t )
101- . unwrap_or ( ctime) ;
103+ . map ( |( t, ) | TimestampMs ( t as u64 ) )
104+ . unwrap_or ( ctime. to_ms ( ) ) ;
102105
103- // Calculate elapsed seconds since creation.
104- // Convert from milliseconds to seconds.
105- let elapsed_seconds = ( ( current_timestamp - ctime) . max ( 0 ) as u128 ) / 1000 ;
106+ let elapsed_seconds = now. elapsed_seconds_since ( ctime. to_ms ( ) ) ;
106107
107108 // Maximum capacity (static cap) = STAR * night_dust_ratio.
108109 max_capacity = value. saturating_mul ( night_dust_ratio) ;
@@ -111,7 +112,7 @@ impl DustStorage for Storage {
111112 // elapsed_seconds. Capped at max_capacity.
112113 let generated_capacity = value
113114 . saturating_mul ( generation_decay_rate)
114- . saturating_mul ( elapsed_seconds) ;
115+ . saturating_mul ( elapsed_seconds as u128 ) ;
115116 current_capacity = generated_capacity. min ( max_capacity) ;
116117 }
117118 }
0 commit comments