Skip to content

Commit 64ca109

Browse files
committed
Add dynafed::Params::into_compact
This method is to be used to convert full params into compact params.
1 parent b43962e commit 64ca109

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/dynafed.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,17 @@ impl Params {
138138
}
139139
}
140140

141-
/// Calculate the root of this [Params].
142-
pub fn calculate_root(&self) -> sha256::Midstate {
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 {
143145
fn serialize_hash<E: Encodable>(obj: &E) -> sha256d::Hash {
144146
let mut engine = sha256d::Hash::engine();
145147
obj.consensus_encode(&mut engine).expect("engines don't error");
146148
sha256d::Hash::from_engine(engine)
147149
}
148150

149-
if self.is_null() {
150-
return sha256::Midstate::from_inner([0u8; 32]);
151-
}
152-
153-
let extra_root = match *self {
151+
match *self {
154152
Params::Null => return sha256::Midstate::from_inner([0u8; 32]),
155153
Params::Compact { ref elided_root, .. } => *elided_root,
156154
Params::Full { ref fedpeg_program, ref fedpegscript, ref extension_space, .. } => {
@@ -161,7 +159,21 @@ impl Params {
161159
];
162160
::fast_merkle_root::fast_merkle_root(&leaves[..])
163161
},
164-
};
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+
165177
let leaves = [
166178
serialize_hash(self.signblockscript().unwrap()).into_inner(),
167179
serialize_hash(&self.signblock_witness_limit().unwrap()).into_inner(),
@@ -170,10 +182,38 @@ impl Params {
170182

171183
let leaves = [
172184
compact_root.into_inner(),
173-
extra_root.into_inner(),
185+
self.extra_root().into_inner(),
174186
];
175187
::fast_merkle_root::fast_merkle_root(&leaves[..])
176188
}
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+
}
177217
}
178218

179219
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)