Skip to content

Commit 04ad0d1

Browse files
committed
sighash input commitments - WIP
1 parent b178e6a commit 04ad0d1

File tree

78 files changed

+3405
-1428
lines changed

Some content is hidden

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

78 files changed

+3405
-1428
lines changed

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

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use serialization::Encode;
2323
use super::*;
2424

2525
use std::{
26+
borrow::Cow,
2627
convert::Infallible,
2728
sync::{Arc, Mutex},
2829
time::Duration,
@@ -46,7 +47,11 @@ use common::{
4647
authorize_pubkey_spend::sign_public_key_spending,
4748
standard_signature::StandardInputSignature, InputWitness,
4849
},
49-
sighash::{sighashtype::SigHashType, signature_hash},
50+
sighash::{
51+
input_commitment::{SighashInputCommitment, TrivialUtxoProvider},
52+
sighashtype::SigHashType,
53+
signature_hash,
54+
},
5055
},
5156
stakelock::StakePoolData,
5257
timelock::OutputTimeLock,
@@ -306,6 +311,17 @@ async fn randomized(#[case] seed: Seed) {
306311
#[case(test_utils::random::Seed::from_entropy())]
307312
#[tokio::test]
308313
async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
314+
use std::collections::BTreeMap;
315+
316+
use common::chain::{
317+
signature::sighash::input_commitment::{
318+
make_sighash_input_commitments_for_transaction_inputs, OrderInfo, PoolInfo,
319+
},
320+
OrderId,
321+
};
322+
323+
logging::init_logging();
324+
309325
let mut rng = make_seedable_rng(seed);
310326

311327
let initial_pledge = 40_000 * CoinUnit::ATOMS_PER_COIN + rng.gen_range(10000..100000);
@@ -546,25 +562,47 @@ async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
546562
sync_and_compare(&mut tf, block, &mut local_state, pool_id).await;
547563

548564
let remaining_coins = remaining_coins - rng.gen_range(0..10);
565+
let input1 = TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0);
566+
let input2 = TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0);
549567
let transaction = TransactionBuilder::new()
550-
.add_input(
551-
TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0),
552-
InputWitness::NoSignature(None),
553-
)
554-
.add_input(
555-
TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0),
556-
InputWitness::NoSignature(None),
557-
)
568+
.add_input(input1.clone(), InputWitness::NoSignature(None))
569+
.add_input(input2.clone(), InputWitness::NoSignature(None))
558570
.add_output(TxOutput::Transfer(
559571
OutputValue::Coin(Amount::from_atoms(remaining_coins)),
560572
Destination::AnyoneCanSpend,
561573
))
562574
.build();
563575

576+
let utxos = [Some(coin_tx_out), Some(from_block_output)];
577+
let decommissioned_pool_staker_balance = local_state
578+
.storage()
579+
.transaction_ro()
580+
.await
581+
.unwrap()
582+
.get_pool_data(pool_id)
583+
.await
584+
.unwrap()
585+
.unwrap()
586+
.staker_balance()
587+
.unwrap();
588+
let input_commitments = make_sighash_input_commitments_for_transaction_inputs(
589+
&[input1, input2],
590+
&TrivialUtxoProvider(&utxos),
591+
&BTreeMap::<PoolId, PoolInfo>::from([(
592+
pool_id,
593+
PoolInfo {
594+
staker_balance: decommissioned_pool_staker_balance,
595+
},
596+
)]),
597+
&BTreeMap::<OrderId, OrderInfo>::new(),
598+
&chain_config,
599+
tf.next_block_height(),
600+
)
601+
.unwrap();
564602
let sighash = signature_hash(
565603
SigHashType::default(),
566604
transaction.transaction(),
567-
&[Some(&coin_tx_out), Some(&from_block_output)],
605+
&input_commitments,
568606
1,
569607
)
570608
.unwrap();
@@ -786,7 +824,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
786824
let sighash = signature_hash(
787825
SigHashType::default(),
788826
spend_transaction.transaction(),
789-
&[Some(&lock_for_block_count), Some(&lock_until_height)],
827+
&[
828+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_block_count)),
829+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_height)),
830+
],
790831
idx,
791832
)
792833
.unwrap();
@@ -864,7 +905,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
864905
let sighash = signature_hash(
865906
SigHashType::default(),
866907
spend_time_locked.transaction(),
867-
&[Some(&lock_for_sec), Some(&lock_until_time)],
908+
&[
909+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_sec)),
910+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_time)),
911+
],
868912
idx,
869913
)
870914
.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
tokens::{make_token_id, IsTokenFreezable, TokenIssuance, TokenIssuanceV1, TokenTotalSupply},
1820
AccountNonce,
@@ -161,7 +163,10 @@ async fn ok(#[case] seed: Seed) {
161163
SigHashType::all(),
162164
dest,
163165
&transaction,
164-
&[Some(&input_utxo), None],
166+
&[
167+
SighashInputCommitment::Utxo(Cow::Borrowed(&input_utxo)),
168+
SighashInputCommitment::None,
169+
],
165170
0,
166171
&mut rng,
167172
)

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_commitment::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::{
@@ -127,7 +129,9 @@ async fn ok(#[case] seed: Seed) {
127129
SigHashType::all(),
128130
alice_destination.clone(),
129131
&tx,
130-
&[issue_nft_tx.outputs().first()],
132+
&[SighashInputCommitment::Utxo(Cow::Borrowed(
133+
&issue_nft_tx.outputs()[0],
134+
))],
131135
0,
132136
&mut rng,
133137
)

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::{
@@ -164,7 +166,12 @@ async fn ok_tokens(#[case] seed: Seed) {
164166
SigHashType::all(),
165167
alice_destination.clone(),
166168
&mint_transaction,
167-
&[Some(&issue_token_transaction.outputs()[0]), None],
169+
&[
170+
SighashInputCommitment::Utxo(Cow::Borrowed(
171+
&issue_token_transaction.outputs()[0],
172+
)),
173+
SighashInputCommitment::None,
174+
],
168175
1,
169176
&mut rng,
170177
)

0 commit comments

Comments
 (0)