Skip to content

Commit 9e5da96

Browse files
committed
sighash input commitments - WIP
1 parent 6301aa6 commit 9e5da96

File tree

81 files changed

+3703
-1493
lines changed

Some content is hidden

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

81 files changed

+3703
-1493
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
@@ -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,11 @@ 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_commitment::{SighashInputCommitment, TrivialUtxoProvider},
48+
sighashtype::SigHashType,
49+
signature_hash,
50+
},
4651
},
4752
stakelock::StakePoolData,
4853
timelock::OutputTimeLock,
@@ -310,6 +315,17 @@ async fn randomized(#[case] seed: Seed) {
310315
#[case(test_utils::random::Seed::from_entropy())]
311316
#[tokio::test]
312317
async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
318+
use std::collections::BTreeMap;
319+
320+
use common::chain::{
321+
signature::sighash::input_commitment::{
322+
make_sighash_input_commitments_for_transaction_inputs, OrderInfo, PoolInfo,
323+
},
324+
OrderId,
325+
};
326+
327+
logging::init_logging();
328+
313329
let mut rng = make_seedable_rng(seed);
314330

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

549565
let remaining_coins = remaining_coins - rng.gen_range(0..10);
566+
let input1 = TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0);
567+
let input2 = TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0);
550568
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-
)
569+
.add_input(input1.clone(), InputWitness::NoSignature(None))
570+
.add_input(input2.clone(), InputWitness::NoSignature(None))
559571
.add_output(TxOutput::Transfer(
560572
OutputValue::Coin(Amount::from_atoms(remaining_coins)),
561573
Destination::AnyoneCanSpend,
562574
))
563575
.build();
564576

577+
let utxos = [Some(coin_tx_out), Some(from_block_output)];
578+
let decommissioned_pool_staker_balance = local_state
579+
.storage()
580+
.transaction_ro()
581+
.await
582+
.unwrap()
583+
.get_pool_data(pool_id)
584+
.await
585+
.unwrap()
586+
.unwrap()
587+
.staker_balance()
588+
.unwrap();
589+
let input_commitments = make_sighash_input_commitments_for_transaction_inputs(
590+
&[input1, input2],
591+
&TrivialUtxoProvider(&utxos),
592+
&BTreeMap::<PoolId, PoolInfo>::from([(
593+
pool_id,
594+
PoolInfo {
595+
staker_balance: decommissioned_pool_staker_balance,
596+
},
597+
)]),
598+
&BTreeMap::<OrderId, OrderInfo>::new(),
599+
&chain_config,
600+
tf.next_block_height(),
601+
)
602+
.unwrap();
565603
let sighash = signature_hash(
566604
SigHashType::default(),
567605
transaction.transaction(),
568-
&[Some(&coin_tx_out), Some(&from_block_output)],
606+
&input_commitments,
569607
1,
570608
)
571609
.unwrap();
@@ -787,7 +825,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
787825
let sighash = signature_hash(
788826
SigHashType::default(),
789827
spend_transaction.transaction(),
790-
&[Some(&lock_for_block_count), Some(&lock_until_height)],
828+
&[
829+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_block_count)),
830+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_height)),
831+
],
791832
idx,
792833
)
793834
.unwrap();
@@ -865,7 +906,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
865906
let sighash = signature_hash(
866907
SigHashType::default(),
867908
spend_time_locked.transaction(),
868-
&[Some(&lock_for_sec), Some(&lock_until_time)],
909+
&[
910+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_sec)),
911+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_time)),
912+
],
869913
idx,
870914
)
871915
.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_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::{
@@ -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)