Skip to content

Commit 6ab6dff

Browse files
committed
Introduce SighashInputCommitment struct and use it instead of Option<TxOutput>; some cleanup and test-related improvements
1 parent f096a12 commit 6ab6dff

72 files changed

Lines changed: 2572 additions & 1629 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

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

api-server/scanner-lib/src/sync/tests/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
mod simulation;
1717

1818
use std::{
19+
borrow::Cow,
1920
convert::Infallible,
2021
sync::{Arc, Mutex},
2122
time::Duration,
@@ -42,7 +43,14 @@ use common::{
4243
authorize_pubkey_spend::sign_public_key_spending,
4344
standard_signature::StandardInputSignature, InputWitness,
4445
},
45-
sighash::{sighashtype::SigHashType, signature_hash},
46+
sighash::{
47+
input_commitments::{
48+
make_sighash_input_commitments_for_transaction_inputs, SighashInputCommitment,
49+
TrivialUtxoProvider,
50+
},
51+
sighashtype::SigHashType,
52+
signature_hash,
53+
},
4654
},
4755
stakelock::StakePoolData,
4856
timelock::OutputTimeLock,
@@ -547,25 +555,27 @@ async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
547555
sync_and_compare(&mut tf, block, &mut local_state, pool_id).await;
548556

549557
let remaining_coins = remaining_coins - rng.gen_range(0..10);
558+
let input1 = TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0);
559+
let input2 = TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0);
550560
let transaction = TransactionBuilder::new()
551-
.add_input(
552-
TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0),
553-
InputWitness::NoSignature(None),
554-
)
555-
.add_input(
556-
TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0),
557-
InputWitness::NoSignature(None),
558-
)
561+
.add_input(input1.clone(), InputWitness::NoSignature(None))
562+
.add_input(input2.clone(), InputWitness::NoSignature(None))
559563
.add_output(TxOutput::Transfer(
560564
OutputValue::Coin(Amount::from_atoms(remaining_coins)),
561565
Destination::AnyoneCanSpend,
562566
))
563567
.build();
564568

569+
let utxos = [Some(coin_tx_out), Some(from_block_output)];
570+
let input_commitments = make_sighash_input_commitments_for_transaction_inputs(
571+
&[input1, input2],
572+
&TrivialUtxoProvider(&utxos),
573+
)
574+
.unwrap();
565575
let sighash = signature_hash(
566576
SigHashType::default(),
567577
transaction.transaction(),
568-
&[Some(&coin_tx_out), Some(&from_block_output)],
578+
&input_commitments,
569579
1,
570580
)
571581
.unwrap();
@@ -787,7 +797,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
787797
let sighash = signature_hash(
788798
SigHashType::default(),
789799
spend_transaction.transaction(),
790-
&[Some(&lock_for_block_count), Some(&lock_until_height)],
800+
&[
801+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_block_count)),
802+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_height)),
803+
],
791804
idx,
792805
)
793806
.unwrap();
@@ -865,7 +878,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
865878
let sighash = signature_hash(
866879
SigHashType::default(),
867880
spend_time_locked.transaction(),
868-
&[Some(&lock_for_sec), Some(&lock_until_time)],
881+
&[
882+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_sec)),
883+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_time)),
884+
],
869885
idx,
870886
)
871887
.unwrap();

api-server/stack-test-suite/tests/v2/address.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use std::sync::RwLock;
16+
use std::{borrow::Cow, sync::RwLock};
1717

1818
use api_web_server::{api::json_helpers::amount_to_json, CachedValues};
1919
use common::primitives::time::get_time;
@@ -126,7 +126,7 @@ async fn multiple_outputs_to_single_address(#[case] seed: Seed) {
126126
SigHashType::all(),
127127
alice_destination.clone(),
128128
&transaction,
129-
&[Some(&previous_tx_out)],
129+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
130130
0,
131131
&mut rng,
132132
)
@@ -198,7 +198,7 @@ async fn multiple_outputs_to_single_address(#[case] seed: Seed) {
198198
SigHashType::all(),
199199
alice_destination.clone(),
200200
&transaction,
201-
&[Some(&previous_tx_out)],
201+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
202202
0,
203203
&mut rng,
204204
)
@@ -368,7 +368,7 @@ async fn test_unlocking_for_locked_utxos(#[case] seed: Seed) {
368368
SigHashType::all(),
369369
alice_destination.clone(),
370370
&transaction,
371-
&[Some(&previous_tx_out)],
371+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
372372
0,
373373
&mut rng,
374374
)
@@ -440,7 +440,7 @@ async fn test_unlocking_for_locked_utxos(#[case] seed: Seed) {
440440
SigHashType::all(),
441441
alice_destination.clone(),
442442
&transaction,
443-
&[Some(&previous_tx_out)],
443+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
444444
0,
445445
&mut rng,
446446
)
@@ -618,7 +618,7 @@ async fn ok(#[case] seed: Seed) {
618618
SigHashType::all(),
619619
alice_destination.clone(),
620620
&transaction,
621-
&[Some(&previous_tx_out)],
621+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
622622
0,
623623
&mut rng,
624624
)
@@ -673,7 +673,7 @@ async fn ok(#[case] seed: Seed) {
673673
SigHashType::all(),
674674
alice_destination.clone(),
675675
&transaction,
676-
&[Some(&previous_tx_out)],
676+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
677677
0,
678678
&mut rng,
679679
)

api-server/stack-test-suite/tests/v2/address_all_utxos.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use std::{collections::BTreeMap, sync::RwLock};
16+
use std::{borrow::Cow, collections::BTreeMap, sync::RwLock};
1717

1818
use api_web_server::{api::json_helpers::utxo_outpoint_to_json, CachedValues};
1919
use common::{chain::UtxoOutPoint, primitives::time::get_time};
@@ -125,7 +125,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
125125
SigHashType::all(),
126126
alice_destination.clone(),
127127
&transaction,
128-
&[Some(&previous_tx_out)],
128+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
129129
0,
130130
&mut rng,
131131
)
@@ -201,7 +201,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
201201
SigHashType::all(),
202202
alice_destination.clone(),
203203
&transaction,
204-
&[Some(&previous_tx_out)],
204+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
205205
0,
206206
&mut rng,
207207
)
@@ -375,7 +375,7 @@ async fn ok(#[case] seed: Seed) {
375375
SigHashType::all(),
376376
alice_destination.clone(),
377377
&transaction,
378-
&[Some(&previous_tx_out)],
378+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
379379
0,
380380
&mut rng,
381381
)
@@ -453,7 +453,7 @@ async fn ok(#[case] seed: Seed) {
453453
SigHashType::all(),
454454
alice_destination.clone(),
455455
&transaction,
456-
&[Some(&previous_tx_out)],
456+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
457457
0,
458458
&mut rng,
459459
)

api-server/stack-test-suite/tests/v2/address_spendable_utxos.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use std::{collections::BTreeMap, sync::RwLock};
16+
use std::{borrow::Cow, collections::BTreeMap, sync::RwLock};
1717

1818
use api_web_server::{api::json_helpers::utxo_outpoint_to_json, CachedValues};
1919
use common::{chain::UtxoOutPoint, primitives::time::get_time};
@@ -128,7 +128,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
128128
SigHashType::all(),
129129
alice_destination.clone(),
130130
&transaction,
131-
&[Some(&previous_tx_out)],
131+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
132132
0,
133133
&mut rng,
134134
)
@@ -203,7 +203,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
203203
SigHashType::all(),
204204
alice_destination.clone(),
205205
&transaction,
206-
&[Some(&previous_tx_out)],
206+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
207207
0,
208208
&mut rng,
209209
)
@@ -377,7 +377,7 @@ async fn ok(#[case] seed: Seed) {
377377
SigHashType::all(),
378378
alice_destination.clone(),
379379
&transaction,
380-
&[Some(&previous_tx_out)],
380+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
381381
0,
382382
&mut rng,
383383
)
@@ -445,7 +445,7 @@ async fn ok(#[case] seed: Seed) {
445445
SigHashType::all(),
446446
alice_destination.clone(),
447447
&transaction,
448-
&[Some(&previous_tx_out)],
448+
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
449449
0,
450450
&mut rng,
451451
)

api-server/stack-test-suite/tests/v2/address_token_authority.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::borrow::Cow;
17+
1618
use common::chain::{
1719
make_token_id,
1820
tokens::{IsTokenFreezable, TokenIssuance, TokenIssuanceV1, TokenTotalSupply},
@@ -164,7 +166,10 @@ async fn ok(#[case] seed: Seed) {
164166
SigHashType::all(),
165167
dest,
166168
&transaction,
167-
&[Some(&input_utxo), None],
169+
&[
170+
SighashInputCommitment::Utxo(Cow::Borrowed(&input_utxo)),
171+
SighashInputCommitment::None,
172+
],
168173
0,
169174
&mut rng,
170175
)

api-server/stack-test-suite/tests/v2/htlc.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::borrow::Cow;
17+
1618
use common::chain::{
1719
classic_multisig::ClassicMultisigChallenge,
1820
htlc::HtlcSecret,
@@ -129,7 +131,9 @@ async fn spend(#[case] seed: Seed) {
129131
SigHashType::all(),
130132
Destination::PublicKeyHash((&PublicKey::from_private_key(&bob_sk)).into()),
131133
&tx2,
132-
&[Some(&tx_1.transaction().outputs()[0])],
134+
&[SighashInputCommitment::Utxo(Cow::Borrowed(
135+
&tx_1.transaction().outputs()[0],
136+
))],
133137
0,
134138
secret,
135139
&mut rng,
@@ -284,7 +288,9 @@ async fn refund(#[case] seed: Seed) {
284288
let sighash = signature_hash(
285289
SigHashType::all(),
286290
&tx2,
287-
&[Some(&tx_1.transaction().outputs()[0])],
291+
&[SighashInputCommitment::Utxo(Cow::Borrowed(
292+
&tx_1.transaction().outputs()[0],
293+
))],
288294
0,
289295
)
290296
.unwrap();
@@ -303,7 +309,9 @@ async fn refund(#[case] seed: Seed) {
303309
&authorization,
304310
SigHashType::all(),
305311
&tx2,
306-
&[Some(&tx_1.transaction().outputs()[0])],
312+
&[SighashInputCommitment::Utxo(Cow::Borrowed(
313+
&tx_1.transaction().outputs()[0],
314+
))],
307315
0,
308316
)
309317
.unwrap();

api-server/stack-test-suite/tests/v2/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use common::{
6565
output_value::OutputValue,
6666
signature::{
6767
inputsig::{standard_signature::StandardInputSignature, InputWitness},
68-
sighash::sighashtype::SigHashType,
68+
sighash::{input_commitments::SighashInputCommitment, sighashtype::SigHashType},
6969
},
7070
transaction::output::timelock::OutputTimeLock,
7171
Destination, OutPointSourceId, SignedTransaction, Transaction, TxInput, TxOutput,

api-server/stack-test-suite/tests/v2/nft.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::borrow::Cow;
17+
1618
use api_server_common::storage::storage_api::NftWithOwner;
1719
use api_web_server::api::json_helpers::nft_with_owner_to_json;
1820
use common::{
@@ -131,7 +133,9 @@ async fn ok(#[case] seed: Seed) {
131133
SigHashType::all(),
132134
alice_destination.clone(),
133135
&tx,
134-
&[issue_nft_tx.outputs().first()],
136+
&[SighashInputCommitment::Utxo(Cow::Borrowed(
137+
&issue_nft_tx.outputs()[0],
138+
))],
135139
0,
136140
&mut rng,
137141
)

api-server/stack-test-suite/tests/v2/statistics.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::borrow::Cow;
17+
1618
use api_web_server::api::json_helpers::amount_to_json;
1719
use common::{
1820
chain::{
@@ -167,7 +169,12 @@ async fn ok_tokens(#[case] seed: Seed) {
167169
SigHashType::all(),
168170
alice_destination.clone(),
169171
&mint_transaction,
170-
&[Some(&issue_token_transaction.outputs()[0]), None],
172+
&[
173+
SighashInputCommitment::Utxo(Cow::Borrowed(
174+
&issue_token_transaction.outputs()[0],
175+
)),
176+
SighashInputCommitment::None,
177+
],
171178
1,
172179
&mut rng,
173180
)

0 commit comments

Comments
 (0)