Skip to content

Commit 5f8e92c

Browse files
committed
sighash input commitments - WIP
1 parent b05e3f2 commit 5f8e92c

File tree

121 files changed

+6984
-2404
lines changed

Some content is hidden

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

121 files changed

+6984
-2404
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ merkletree-mintlayer = "0.1"
187187
mockall = "0.13"
188188
num = "0.4"
189189
num-derive = "0.4"
190-
num-traits = "0.2"
190+
num-traits = { version = "0.2", default-features = false }
191191
once_cell = "1.13"
192192
oneshot = "0.1"
193-
parity-scale-codec = "3.1"
193+
parity-scale-codec = { version = "3.7", default-features = false }
194194
parking_lot = "0.12"
195195
paste = "1.0"
196196
probabilistic-collections = "0.7"
@@ -227,7 +227,7 @@ snowstorm = "0.4"
227227
socket2 = "0.5"
228228
sscanf = "0.4"
229229
static_assertions = "1.1"
230-
strum = { version = "0.26", features = ["derive"] }
230+
strum = { version = "0.26", default-features = false, features = ["derive"] }
231231
syn = "2.0"
232232
tap = "1.0"
233233
tempfile = "3.3"

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

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
mod simulation;
1717

1818
use std::{
19+
borrow::Cow,
20+
collections::BTreeMap,
1921
convert::Infallible,
2022
sync::{Arc, Mutex},
2123
time::Duration,
@@ -42,12 +44,19 @@ use common::{
4244
authorize_pubkey_spend::sign_public_key_spending,
4345
standard_signature::StandardInputSignature, InputWitness,
4446
},
45-
sighash::{sighashtype::SigHashType, signature_hash},
47+
sighash::{
48+
input_commitments::{
49+
make_sighash_input_commitments_for_transaction_inputs_at_height, OrderInfo,
50+
PoolInfo, SighashInputCommitment, TrivialUtxoProvider,
51+
},
52+
sighashtype::SigHashType,
53+
signature_hash,
54+
},
4655
},
4756
stakelock::StakePoolData,
4857
timelock::OutputTimeLock,
49-
CoinUnit, Destination, OutPointSourceId, PoolId, SignedTransaction, TxInput, TxOutput,
50-
UtxoOutPoint,
58+
CoinUnit, Destination, OrderId, OutPointSourceId, PoolId, SignedTransaction, TxInput,
59+
TxOutput, UtxoOutPoint,
5160
},
5261
primitives::{per_thousand::PerThousand, Amount, CoinOrTokenId, Idable, H256},
5362
};
@@ -547,25 +556,47 @@ async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
547556
sync_and_compare(&mut tf, block, &mut local_state, pool_id).await;
548557

549558
let remaining_coins = remaining_coins - rng.gen_range(0..10);
559+
let input1 = TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0);
560+
let input2 = TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0);
550561
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-
)
562+
.add_input(input1.clone(), InputWitness::NoSignature(None))
563+
.add_input(input2.clone(), InputWitness::NoSignature(None))
559564
.add_output(TxOutput::Transfer(
560565
OutputValue::Coin(Amount::from_atoms(remaining_coins)),
561566
Destination::AnyoneCanSpend,
562567
))
563568
.build();
564569

570+
let utxos = [Some(coin_tx_out), Some(from_block_output)];
571+
let decommissioned_pool_staker_balance = local_state
572+
.storage()
573+
.transaction_ro()
574+
.await
575+
.unwrap()
576+
.get_pool_data(pool_id)
577+
.await
578+
.unwrap()
579+
.unwrap()
580+
.staker_balance()
581+
.unwrap();
582+
let input_commitments = make_sighash_input_commitments_for_transaction_inputs_at_height(
583+
&[input1, input2],
584+
&TrivialUtxoProvider(&utxos),
585+
&BTreeMap::<PoolId, PoolInfo>::from([(
586+
pool_id,
587+
PoolInfo {
588+
staker_balance: decommissioned_pool_staker_balance,
589+
},
590+
)]),
591+
&BTreeMap::<OrderId, OrderInfo>::new(),
592+
&chain_config,
593+
tf.next_block_height(),
594+
)
595+
.unwrap();
565596
let sighash = signature_hash(
566597
SigHashType::default(),
567598
transaction.transaction(),
568-
&[Some(&coin_tx_out), Some(&from_block_output)],
599+
&input_commitments,
569600
1,
570601
)
571602
.unwrap();
@@ -787,7 +818,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
787818
let sighash = signature_hash(
788819
SigHashType::default(),
789820
spend_transaction.transaction(),
790-
&[Some(&lock_for_block_count), Some(&lock_until_height)],
821+
&[
822+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_block_count)),
823+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_height)),
824+
],
791825
idx,
792826
)
793827
.unwrap();
@@ -865,7 +899,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
865899
let sighash = signature_hash(
866900
SigHashType::default(),
867901
spend_time_locked.transaction(),
868-
&[Some(&lock_for_sec), Some(&lock_until_time)],
902+
&[
903+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_sec)),
904+
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_time)),
905+
],
869906
idx,
870907
)
871908
.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
)

0 commit comments

Comments
 (0)