@@ -22,40 +22,39 @@ use crate::{
2222///
2323/// Returns `None` if the divisor would overflow a `u64`.
2424pub fn halving_divisor ( height : Height , network : & Network ) -> Option < u64 > {
25+ // Some far-future shifts can be more than 63 bits
26+ 1u64 . checked_shl ( num_halvings ( height, network) )
27+ }
28+
29+ /// The halving index for a block height and network.
30+ ///
31+ /// `Halving(height)`, as described in [protocol specification §7.8][7.8]
32+ ///
33+ /// [7.8]: https://zips.z.cash/protocol/protocol.pdf#subsidies
34+ pub fn num_halvings ( height : Height , network : & Network ) -> u32 {
35+ let slow_start_shift = network. slow_start_shift ( ) ;
2536 let blossom_height = Blossom
2637 . activation_height ( network)
2738 . expect ( "blossom activation height should be available" ) ;
2839
29- if height < blossom_height {
30- let pre_blossom_height = height - network. slow_start_shift ( ) ;
31- let halving_shift = pre_blossom_height / PRE_BLOSSOM_HALVING_INTERVAL ;
32-
33- let halving_div = 1u64
34- . checked_shl (
35- halving_shift
36- . try_into ( )
37- . expect ( "already checked for negatives" ) ,
38- )
39- . expect ( "pre-blossom heights produce small shifts" ) ;
40-
41- Some ( halving_div)
40+ let halving_index = if height < slow_start_shift {
41+ 0
42+ } else if height < blossom_height {
43+ let pre_blossom_height = height - slow_start_shift;
44+ pre_blossom_height / network. pre_blossom_halving_interval ( )
4245 } else {
43- let pre_blossom_height = blossom_height - network . slow_start_shift ( ) ;
46+ let pre_blossom_height = blossom_height - slow_start_shift;
4447 let scaled_pre_blossom_height =
4548 pre_blossom_height * HeightDiff :: from ( BLOSSOM_POW_TARGET_SPACING_RATIO ) ;
4649
4750 let post_blossom_height = height - blossom_height;
4851
49- let halving_shift =
50- ( scaled_pre_blossom_height + post_blossom_height ) / POST_BLOSSOM_HALVING_INTERVAL ;
52+ ( scaled_pre_blossom_height + post_blossom_height ) / network . post_blossom_halving_interval ( )
53+ } ;
5154
52- // Some far-future shifts can be more than 63 bits
53- 1u64 . checked_shl (
54- halving_shift
55- . try_into ( )
56- . expect ( "already checked for negatives" ) ,
57- )
58- }
55+ halving_index
56+ . try_into ( )
57+ . expect ( "already checked for negatives" )
5958}
6059
6160#[ cfg( zcash_unstable = "nsm" ) ]
0 commit comments