@@ -129,19 +129,26 @@ impl Params {
129129 }
130130 }
131131
132- /// Calculate the root of this [Params].
133- pub fn calculate_root ( & self ) -> sha256:: Midstate {
132+ /// Get the elided_root. Is [None] for non-[Compact] params.
133+ pub fn elided_root ( & self ) -> Option < & sha256:: Midstate > {
134+ match * self {
135+ Params :: Null => None ,
136+ Params :: Compact { ref elided_root, ..} => Some ( elided_root) ,
137+ Params :: Full { .. } => None ,
138+ }
139+ }
140+
141+ /// Return the `extra root` of this params.
142+ /// The extra root commits to the consensus parameters unrelated to
143+ /// blocksigning: `fedpeg_program`, `fedpegscript` and `extension_space`.
144+ fn extra_root ( & self ) -> sha256:: Midstate {
134145 fn serialize_hash < E : Encodable > ( obj : & E ) -> sha256d:: Hash {
135146 let mut engine = sha256d:: Hash :: engine ( ) ;
136147 obj. consensus_encode ( & mut engine) . expect ( "engines don't error" ) ;
137148 sha256d:: Hash :: from_engine ( engine)
138149 }
139150
140- if self . is_null ( ) {
141- return sha256:: Midstate :: from_inner ( [ 0u8 ; 32 ] ) ;
142- }
143-
144- let extra_root = match * self {
151+ match * self {
145152 Params :: Null => return sha256:: Midstate :: from_inner ( [ 0u8 ; 32 ] ) ,
146153 Params :: Compact { ref elided_root, .. } => * elided_root,
147154 Params :: Full { ref fedpeg_program, ref fedpegscript, ref extension_space, .. } => {
@@ -152,7 +159,21 @@ impl Params {
152159 ] ;
153160 :: fast_merkle_root:: fast_merkle_root ( & leaves[ ..] )
154161 } ,
155- } ;
162+ }
163+ }
164+
165+ /// Calculate the root of this [Params].
166+ pub fn calculate_root ( & self ) -> sha256:: Midstate {
167+ fn serialize_hash < E : Encodable > ( obj : & E ) -> sha256d:: Hash {
168+ let mut engine = sha256d:: Hash :: engine ( ) ;
169+ obj. consensus_encode ( & mut engine) . expect ( "engines don't error" ) ;
170+ sha256d:: Hash :: from_engine ( engine)
171+ }
172+
173+ if self . is_null ( ) {
174+ return sha256:: Midstate :: from_inner ( [ 0u8 ; 32 ] ) ;
175+ }
176+
156177 let leaves = [
157178 serialize_hash ( self . signblockscript ( ) . unwrap ( ) ) . into_inner ( ) ,
158179 serialize_hash ( & self . signblock_witness_limit ( ) . unwrap ( ) ) . into_inner ( ) ,
@@ -161,10 +182,38 @@ impl Params {
161182
162183 let leaves = [
163184 compact_root. into_inner ( ) ,
164- extra_root. into_inner ( ) ,
185+ self . extra_root ( ) . into_inner ( ) ,
165186 ] ;
166187 :: fast_merkle_root:: fast_merkle_root ( & leaves[ ..] )
167188 }
189+
190+ /// Turns paramers into compact parameters.
191+ /// This returns self for compact params and [None] for null ones.
192+ pub fn into_compact ( self ) -> Option < Params > {
193+ // Avoid calcualting when it's not needed.
194+ let mut extra_root = None ;
195+ if self . is_full ( ) {
196+ extra_root = Some ( self . extra_root ( ) ) ;
197+ }
198+
199+ match self {
200+ Params :: Null => None ,
201+ Params :: Compact { signblockscript, signblock_witness_limit, elided_root } => {
202+ Some ( Params :: Compact {
203+ signblockscript : signblockscript,
204+ signblock_witness_limit,
205+ elided_root : elided_root,
206+ } )
207+ }
208+ Params :: Full { signblockscript, signblock_witness_limit, ..} => {
209+ Some ( Params :: Compact {
210+ signblockscript : signblockscript,
211+ signblock_witness_limit,
212+ elided_root : extra_root. unwrap ( ) ,
213+ } )
214+ }
215+ }
216+ }
168217}
169218
170219#[ cfg( feature = "serde" ) ]
@@ -480,4 +529,20 @@ mod tests {
480529 "113160f76dc17fe367a2def79aefe06feeea9c795310c9e88aeedc23e145982e"
481530 ) ;
482531 }
532+
533+ #[ test]
534+ fn into_compact_test ( ) {
535+ let full = Params :: Full {
536+ signblockscript : vec ! [ 0x01 , 0x02 ] . into ( ) ,
537+ signblock_witness_limit : 3 ,
538+ fedpeg_program : vec ! [ 0x04 , 0x05 ] . into ( ) ,
539+ fedpegscript : vec ! [ 0x06 , 0x07 ] ,
540+ extension_space : vec ! [ vec![ 0x08 , 0x09 ] , vec![ 0x0a ] ] ,
541+ } ;
542+ let extra_root = full. extra_root ( ) ;
543+
544+ let compact = full. into_compact ( ) . unwrap ( ) ;
545+ assert_eq ! ( compact. elided_root( ) , Some ( & extra_root) ) ;
546+ assert_eq ! ( compact. extra_root( ) , extra_root) ;
547+ }
483548}
0 commit comments