Skip to content

sighash input commitments - WIP #1910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions api-server/scanner-lib/src/sync/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use serialization::Encode;
use super::*;

use std::{
borrow::Cow,
convert::Infallible,
sync::{Arc, Mutex},
time::Duration,
Expand All @@ -46,7 +47,11 @@ use common::{
authorize_pubkey_spend::sign_public_key_spending,
standard_signature::StandardInputSignature, InputWitness,
},
sighash::{sighashtype::SigHashType, signature_hash},
sighash::{
input_commitment::{SighashInputCommitment, TrivialUtxoProvider},
sighashtype::SigHashType,
signature_hash,
},
},
stakelock::StakePoolData,
timelock::OutputTimeLock,
Expand Down Expand Up @@ -306,6 +311,17 @@ async fn randomized(#[case] seed: Seed) {
#[case(test_utils::random::Seed::from_entropy())]
#[tokio::test]
async fn compare_pool_rewards_with_chainstate_real_state(#[case] seed: Seed) {
use std::collections::BTreeMap;

use common::chain::{
signature::sighash::input_commitment::{
make_sighash_input_commitments_for_transaction_inputs, OrderInfo, PoolInfo,
},
OrderId,
};

logging::init_logging();

let mut rng = make_seedable_rng(seed);

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

let remaining_coins = remaining_coins - rng.gen_range(0..10);
let input1 = TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0);
let input2 = TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0);
let transaction = TransactionBuilder::new()
.add_input(
TxInput::from_utxo(OutPointSourceId::Transaction(prev_tx_id), 0),
InputWitness::NoSignature(None),
)
.add_input(
TxInput::from_utxo(OutPointSourceId::BlockReward(prev_block_hash.into()), 0),
InputWitness::NoSignature(None),
)
.add_input(input1.clone(), InputWitness::NoSignature(None))
.add_input(input2.clone(), InputWitness::NoSignature(None))
.add_output(TxOutput::Transfer(
OutputValue::Coin(Amount::from_atoms(remaining_coins)),
Destination::AnyoneCanSpend,
))
.build();

let utxos = [Some(coin_tx_out), Some(from_block_output)];
let decommissioned_pool_staker_balance = local_state
.storage()
.transaction_ro()
.await
.unwrap()
.get_pool_data(pool_id)
.await
.unwrap()
.unwrap()
.staker_balance()
.unwrap();
let input_commitments = make_sighash_input_commitments_for_transaction_inputs(
&[input1, input2],
&TrivialUtxoProvider(&utxos),
&BTreeMap::<PoolId, PoolInfo>::from([(
pool_id,
PoolInfo {
staker_balance: decommissioned_pool_staker_balance,
},
)]),
&BTreeMap::<OrderId, OrderInfo>::new(),
&chain_config,
tf.next_block_height(),
)
.unwrap();
let sighash = signature_hash(
SigHashType::default(),
transaction.transaction(),
&[Some(&coin_tx_out), Some(&from_block_output)],
&input_commitments,
1,
)
.unwrap();
Expand Down Expand Up @@ -786,7 +824,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
let sighash = signature_hash(
SigHashType::default(),
spend_transaction.transaction(),
&[Some(&lock_for_block_count), Some(&lock_until_height)],
&[
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_block_count)),
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_height)),
],
idx,
)
.unwrap();
Expand Down Expand Up @@ -864,7 +905,10 @@ async fn reorg_locked_balance(#[case] seed: Seed) {
let sighash = signature_hash(
SigHashType::default(),
spend_time_locked.transaction(),
&[Some(&lock_for_sec), Some(&lock_until_time)],
&[
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_for_sec)),
SighashInputCommitment::Utxo(Cow::Borrowed(&lock_until_time)),
],
idx,
)
.unwrap();
Expand Down
14 changes: 7 additions & 7 deletions api-server/stack-test-suite/tests/v2/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::RwLock;
use std::{borrow::Cow, sync::RwLock};

use api_web_server::{api::json_helpers::amount_to_json, CachedValues};
use common::primitives::time::get_time;
Expand Down Expand Up @@ -126,7 +126,7 @@ async fn multiple_outputs_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -198,7 +198,7 @@ async fn multiple_outputs_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -368,7 +368,7 @@ async fn test_unlocking_for_locked_utxos(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -440,7 +440,7 @@ async fn test_unlocking_for_locked_utxos(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -618,7 +618,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -673,7 +673,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down
10 changes: 5 additions & 5 deletions api-server/stack-test-suite/tests/v2/address_all_utxos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{collections::BTreeMap, sync::RwLock};
use std::{borrow::Cow, collections::BTreeMap, sync::RwLock};

use api_web_server::{api::json_helpers::utxo_outpoint_to_json, CachedValues};
use common::{chain::UtxoOutPoint, primitives::time::get_time};
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -201,7 +201,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -375,7 +375,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -453,7 +453,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{collections::BTreeMap, sync::RwLock};
use std::{borrow::Cow, collections::BTreeMap, sync::RwLock};

use api_web_server::{api::json_helpers::utxo_outpoint_to_json, CachedValues};
use common::{chain::UtxoOutPoint, primitives::time::get_time};
Expand Down Expand Up @@ -128,7 +128,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -203,7 +203,7 @@ async fn multiple_utxos_to_single_address(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -377,7 +377,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down Expand Up @@ -445,7 +445,7 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&transaction,
&[Some(&previous_tx_out)],
&[SighashInputCommitment::Utxo(Cow::Borrowed(&previous_tx_out))],
0,
&mut rng,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use common::chain::{
tokens::{make_token_id, IsTokenFreezable, TokenIssuance, TokenIssuanceV1, TokenTotalSupply},
AccountNonce,
Expand Down Expand Up @@ -161,7 +163,10 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
dest,
&transaction,
&[Some(&input_utxo), None],
&[
SighashInputCommitment::Utxo(Cow::Borrowed(&input_utxo)),
SighashInputCommitment::None,
],
0,
&mut rng,
)
Expand Down
14 changes: 11 additions & 3 deletions api-server/stack-test-suite/tests/v2/htlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use common::chain::{
classic_multisig::ClassicMultisigChallenge,
htlc::HtlcSecret,
Expand Down Expand Up @@ -129,7 +131,9 @@ async fn spend(#[case] seed: Seed) {
SigHashType::all(),
Destination::PublicKeyHash((&PublicKey::from_private_key(&bob_sk)).into()),
&tx2,
&[Some(&tx_1.transaction().outputs()[0])],
&[SighashInputCommitment::Utxo(Cow::Borrowed(
&tx_1.transaction().outputs()[0],
))],
0,
secret,
&mut rng,
Expand Down Expand Up @@ -284,7 +288,9 @@ async fn refund(#[case] seed: Seed) {
let sighash = signature_hash(
SigHashType::all(),
&tx2,
&[Some(&tx_1.transaction().outputs()[0])],
&[SighashInputCommitment::Utxo(Cow::Borrowed(
&tx_1.transaction().outputs()[0],
))],
0,
)
.unwrap();
Expand All @@ -303,7 +309,9 @@ async fn refund(#[case] seed: Seed) {
&authorization,
SigHashType::all(),
&tx2,
&[Some(&tx_1.transaction().outputs()[0])],
&[SighashInputCommitment::Utxo(Cow::Borrowed(
&tx_1.transaction().outputs()[0],
))],
0,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion api-server/stack-test-suite/tests/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use common::{
output_value::OutputValue,
signature::{
inputsig::{standard_signature::StandardInputSignature, InputWitness},
sighash::sighashtype::SigHashType,
sighash::{input_commitment::SighashInputCommitment, sighashtype::SigHashType},
},
transaction::output::timelock::OutputTimeLock,
Destination, OutPointSourceId, SignedTransaction, Transaction, TxInput, TxOutput,
Expand Down
6 changes: 5 additions & 1 deletion api-server/stack-test-suite/tests/v2/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use api_server_common::storage::storage_api::NftWithOwner;
use api_web_server::api::json_helpers::nft_with_owner_to_json;
use common::{
Expand Down Expand Up @@ -127,7 +129,9 @@ async fn ok(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&tx,
&[issue_nft_tx.outputs().first()],
&[SighashInputCommitment::Utxo(Cow::Borrowed(
&issue_nft_tx.outputs()[0],
))],
0,
&mut rng,
)
Expand Down
9 changes: 8 additions & 1 deletion api-server/stack-test-suite/tests/v2/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;

use api_web_server::api::json_helpers::amount_to_json;
use common::{
chain::{
Expand Down Expand Up @@ -164,7 +166,12 @@ async fn ok_tokens(#[case] seed: Seed) {
SigHashType::all(),
alice_destination.clone(),
&mint_transaction,
&[Some(&issue_token_transaction.outputs()[0]), None],
&[
SighashInputCommitment::Utxo(Cow::Borrowed(
&issue_token_transaction.outputs()[0],
)),
SighashInputCommitment::None,
],
1,
&mut rng,
)
Expand Down
Loading
Loading