Skip to content

Commit a812f72

Browse files
committed
Use SighashInputCommitment<'static> in tests instead of a separate struct; parity-scale-codec updated to be able to derive Decode for SighashInputCommitment
1 parent 08130cf commit a812f72

File tree

12 files changed

+148
-300
lines changed

12 files changed

+148
-300
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ num-derive = "0.4"
190190
num-traits = "0.2"
191191
once_cell = "1.13"
192192
oneshot = "0.1"
193-
parity-scale-codec = "3.1"
193+
parity-scale-codec = "3.7"
194194
parking_lot = "0.12"
195195
paste = "1.0"
196196
probabilistic-collections = "0.7"

common/src/chain/transaction/signature/inputsig/authorize_pubkey_spend.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ pub fn sign_public_key_spending<R: Rng + CryptoRng>(
6868

6969
#[cfg(test)]
7070
mod test {
71-
use itertools::Itertools as _;
7271
use rstest::rstest;
7372

7473
use crate::{
@@ -102,7 +101,6 @@ mod test {
102101
let destination = Destination::PublicKey(public_key);
103102

104103
let input_commitments = generate_input_commitments(&mut rng, 1);
105-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
106104

107105
let tx = generate_unsigned_tx(&mut rng, &destination, input_commitments.len(), 2).unwrap();
108106

@@ -112,7 +110,7 @@ mod test {
112110
sighash_type,
113111
destination.clone(),
114112
&tx,
115-
&input_commitments_refs,
113+
&input_commitments,
116114
1,
117115
&mut rng,
118116
);
@@ -132,7 +130,6 @@ mod test {
132130
let destination = Destination::PublicKeyHash(PublicKeyHash::from(&public_key));
133131

134132
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
135-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
136133

137134
let tx = generate_unsigned_tx(
138135
&mut rng,
@@ -148,7 +145,7 @@ mod test {
148145
sighash_type,
149146
destination.clone(),
150147
&tx,
151-
&input_commitments_refs,
148+
&input_commitments,
152149
rng.gen_range(0..INPUTS_COUNT),
153150
&mut rng,
154151
)
@@ -173,7 +170,6 @@ mod test {
173170
let destination = Destination::PublicKey(public_key);
174171

175172
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
176-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
177173

178174
let tx = generate_unsigned_tx(
179175
&mut rng,
@@ -189,7 +185,7 @@ mod test {
189185
sighash_type,
190186
destination.clone(),
191187
&tx,
192-
&input_commitments_refs,
188+
&input_commitments,
193189
rng.gen_range(0..INPUTS_COUNT),
194190
&mut rng,
195191
)
@@ -220,7 +216,6 @@ mod test {
220216
let destination = Destination::PublicKey(public_key.clone());
221217

222218
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
223-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
224219

225220
let tx = generate_unsigned_tx(
226221
&mut rng,
@@ -237,15 +232,15 @@ mod test {
237232
sighash_type,
238233
destination.clone(),
239234
&tx,
240-
&input_commitments_refs,
235+
&input_commitments,
241236
input,
242237
&mut rng,
243238
)
244239
.unwrap();
245240
let spender_signature =
246241
AuthorizedPublicKeySpend::from_data(witness.raw_signature()).unwrap();
247242
let sighash =
248-
signature_hash(witness.sighash_type(), &tx, &input_commitments_refs, input)
243+
signature_hash(witness.sighash_type(), &tx, &input_commitments, input)
249244
.unwrap();
250245
verify_public_key_spending(&public_key, &spender_signature, &sighash)
251246
.unwrap_or_else(|_| panic!("{sighash_type:X?}"));
@@ -263,7 +258,6 @@ mod test {
263258
let destination = Destination::PublicKey(public_key.clone());
264259

265260
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
266-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
267261

268262
let tx = generate_unsigned_tx(
269263
&mut rng,
@@ -280,13 +274,13 @@ mod test {
280274
sighash_type,
281275
destination.clone(),
282276
&tx,
283-
&input_commitments_refs,
277+
&input_commitments,
284278
input,
285279
&mut rng,
286280
)
287281
.unwrap();
288282
let sighash =
289-
signature_hash(witness.sighash_type(), &tx, &input_commitments_refs, input)
283+
signature_hash(witness.sighash_type(), &tx, &input_commitments, input)
290284
.unwrap();
291285
sign_public_key_spending(&private_key, &public_key, &sighash, &mut rng)
292286
.unwrap_or_else(|_| panic!("{sighash_type:X?}"));

common/src/chain/transaction/signature/inputsig/authorize_pubkeyhash_spend.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ fn sign_public_key_hash_spending_impl<R: Rng + CryptoRng>(
9999

100100
#[cfg(test)]
101101
mod test {
102-
use itertools::Itertools as _;
103102
use rstest::rstest;
104103

105104
use crate::chain::{
@@ -131,7 +130,6 @@ mod test {
131130
let destination = Destination::PublicKeyHash(pubkey_hash);
132131

133132
let input_commitments = generate_input_commitments(&mut rng, 1);
134-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
135133

136134
let tx = generate_unsigned_tx(&mut rng, &destination, input_commitments.len(), 2).unwrap();
137135

@@ -141,7 +139,7 @@ mod test {
141139
sighash_type,
142140
destination.clone(),
143141
&tx,
144-
&input_commitments_refs,
142+
&input_commitments,
145143
1,
146144
&mut rng,
147145
);
@@ -161,7 +159,6 @@ mod test {
161159
let destination = Destination::PublicKey(public_key);
162160

163161
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
164-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
165162

166163
let tx = generate_unsigned_tx(
167164
&mut rng,
@@ -177,7 +174,7 @@ mod test {
177174
sighash_type,
178175
destination.clone(),
179176
&tx,
180-
&input_commitments_refs,
177+
&input_commitments,
181178
rng.gen_range(0..INPUTS_COUNT),
182179
&mut rng,
183180
)
@@ -204,7 +201,6 @@ mod test {
204201
let destination = Destination::PublicKeyHash(pubkey_hash);
205202

206203
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
207-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
208204

209205
let tx = generate_unsigned_tx(
210206
&mut rng,
@@ -220,7 +216,7 @@ mod test {
220216
sighash_type,
221217
destination.clone(),
222218
&tx,
223-
&input_commitments_refs,
219+
&input_commitments,
224220
rng.gen_range(0..INPUTS_COUNT),
225221
&mut rng,
226222
)
@@ -254,7 +250,6 @@ mod test {
254250
let destination = Destination::PublicKeyHash(pubkey_hash);
255251

256252
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
257-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
258253

259254
let tx = generate_unsigned_tx(
260255
&mut rng,
@@ -271,15 +266,15 @@ mod test {
271266
sighash_type,
272267
destination.clone(),
273268
&tx,
274-
&input_commitments_refs,
269+
&input_commitments,
275270
input,
276271
&mut rng,
277272
)
278273
.unwrap();
279274
let spender_signature =
280275
AuthorizedPublicKeyHashSpend::from_data(witness.raw_signature()).unwrap();
281276
let sighash =
282-
signature_hash(witness.sighash_type(), &tx, &input_commitments_refs, input)
277+
signature_hash(witness.sighash_type(), &tx, &input_commitments, input)
283278
.unwrap();
284279

285280
verify_public_key_hash_spending(&pubkey_hash, &spender_signature, &sighash)
@@ -299,7 +294,6 @@ mod test {
299294
let pubkey_hash = PublicKeyHash::from(&public_key);
300295

301296
let input_commitments = generate_input_commitments(&mut rng, INPUTS_COUNT);
302-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
303297

304298
let tx = generate_unsigned_tx(
305299
&mut rng,
@@ -316,13 +310,13 @@ mod test {
316310
sighash_type,
317311
destination.clone(),
318312
&tx,
319-
&input_commitments_refs,
313+
&input_commitments,
320314
input,
321315
&mut rng,
322316
)
323317
.unwrap();
324318
let sighash =
325-
signature_hash(witness.sighash_type(), &tx, &input_commitments_refs, input)
319+
signature_hash(witness.sighash_type(), &tx, &input_commitments, input)
326320
.unwrap();
327321

328322
sign_public_key_hash_spending(&private_key, &pubkey_hash, &sighash, &mut rng)

common/src/chain/transaction/signature/inputsig/standard_signature.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ mod test {
252252
let destination = Destination::PublicKeyHash(PublicKeyHash::from(&public_key));
253253

254254
let input_commitments = generate_input_commitments(&mut rng, 1);
255-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
256255

257256
let tx = generate_unsigned_tx(&mut rng, &destination, input_commitments.len(), 2).unwrap();
258257

@@ -264,7 +263,7 @@ mod test {
264263
sighash_type,
265264
destination.clone(),
266265
&tx,
267-
&input_commitments_refs,
266+
&input_commitments,
268267
INPUT_NUM,
269268
&mut rng,
270269
),
@@ -284,7 +283,6 @@ mod test {
284283
let destination = Destination::PublicKey(public_key);
285284

286285
let input_commitments = generate_input_commitments(&mut rng, 1);
287-
let input_commitments_refs = input_commitments.iter().map(|comm| comm.into()).collect_vec();
288286

289287
let tx = generate_unsigned_tx(&mut rng, &destination, input_commitments.len(), 2).unwrap();
290288

@@ -296,7 +294,7 @@ mod test {
296294
sighash_type,
297295
destination.clone(),
298296
&tx,
299-
&input_commitments_refs,
297+
&input_commitments,
300298
INPUT_NUM,
301299
&mut rng
302300
),
@@ -323,8 +321,6 @@ mod test {
323321
for (sighash_type, destination) in sig_hash_types().cartesian_product(outpoints.into_iter())
324322
{
325323
let input_commitments = generate_input_commitments(&mut rng, 1);
326-
let input_commitments_refs =
327-
input_commitments.iter().map(|comm| comm.into()).collect_vec();
328324

329325
let tx =
330326
generate_unsigned_tx(&mut rng, &destination, input_commitments.len(), 2).unwrap();
@@ -333,7 +329,7 @@ mod test {
333329
sighash_type,
334330
destination.clone(),
335331
&tx,
336-
&input_commitments_refs,
332+
&input_commitments,
337333
INPUT_NUM,
338334
&mut rng,
339335
)
@@ -342,7 +338,7 @@ mod test {
342338
let sighash = signature_hash(
343339
witness.sighash_type(),
344340
&tx,
345-
&input_commitments_refs,
341+
&input_commitments,
346342
INPUT_NUM,
347343
)
348344
.unwrap();

common/src/chain/transaction/signature/sighash/hashable.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ impl SignatureHashableElement for SignatureHashableInputs<'_> {
128128

129129
#[cfg(test)]
130130
mod tests {
131-
use itertools::Itertools as _;
132131
use randomness::{CryptoRng, Rng};
133132
use rstest::rstest;
134133
use test_utils::random::{make_seedable_rng, Seed};
@@ -216,9 +215,7 @@ mod tests {
216215
) {
217216
let inputs = (0..inputs_count).map(|_| generate_random_input(rng)).collect::<Vec<_>>();
218217

219-
let input_commitment_vals = generate_input_commitments(rng, inputs_utxos_count);
220-
let input_commitments = input_commitment_vals.iter().map(|comm| comm.into()).collect_vec();
221-
218+
let input_commitments = generate_input_commitments(rng, inputs_utxos_count);
222219
let hashable_inputs_result = SignatureHashableInputs::new(&inputs, &input_commitments);
223220

224221
if inputs_count != inputs_utxos_count {

common/src/chain/transaction/signature/sighash/input_commitment.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use std::{borrow::Cow, collections::BTreeMap};
1717

18-
use serialization::Encode;
18+
use serialization::{Decode, Encode};
1919
use utils::cow_utils::CowUtils as _;
2020

2121
use crate::{
@@ -36,7 +36,7 @@ use crate::{
3636
/// with the `Option`, provided that only `None` and `Utxo` variants are used.
3737
/// 2) The `ProduceBlockFromStakeUtxo`, `FillOrderAccountCommand` and `ConcludeOrderAccountCommand`
3838
/// commitments are enabled since `SighashInputCommitmentVersion::V1`.
39-
#[derive(Clone, Debug, Encode)]
39+
#[derive(Clone, Debug, Encode, Decode)]
4040
pub enum SighashInputCommitment<'a> {
4141
/// No extra commitment.
4242
///
@@ -96,6 +96,7 @@ pub enum SighashInputCommitment<'a> {
9696
}
9797

9898
impl<'a> SighashInputCommitment<'a> {
99+
// FIXME rename to to_owned? Same for CowUtils
99100
pub fn deep_clone(&self) -> SighashInputCommitment<'static> {
100101
match self {
101102
SighashInputCommitment::None => SighashInputCommitment::None,

0 commit comments

Comments
 (0)