Skip to content
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

Add KIP-10 Transaction Introspection Opcodes, 8-byte arithmetic and Hard Fork Support #487

Merged
merged 99 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
0e6e748
implement new opcodes
biryukovmaxim Jun 14, 2024
2076757
example of mutual tx
biryukovmaxim Jun 16, 2024
50fd953
add docs describing scenario
biryukovmaxim Jun 16, 2024
7afb955
introduce feature gate for new features
biryukovmaxim Jun 16, 2024
993e91e
introduce hf feature that enables txscript hf feature
biryukovmaxim Jun 16, 2024
a0be8af
style: fmt and clippy fix
biryukovmaxim Jun 16, 2024
f1eed92
implement new opcodes
biryukovmaxim Jun 14, 2024
7b02bb2
example of mutual tx
biryukovmaxim Jun 16, 2024
a679525
add docs describing scenario
biryukovmaxim Jun 16, 2024
2e61b44
introduce feature gate for new features
biryukovmaxim Jun 16, 2024
f7a3f77
style: fmt and clippy fix
biryukovmaxim Jun 16, 2024
4afb1dc
remove unused feature
biryukovmaxim Jun 16, 2024
cd5c169
fmt
biryukovmaxim Jun 16, 2024
96cfd54
make opcode invalid in case of feature disabled
biryukovmaxim Jun 16, 2024
399fcd7
feature gate test
biryukovmaxim Jun 16, 2024
8cff7fe
change test set based on feature
biryukovmaxim Jun 16, 2024
c882d32
rename InputSPK -> InputSpk
biryukovmaxim Jun 16, 2024
1c19ae1
enable kip10 opcodes based on daa_score in runtime
biryukovmaxim Jun 16, 2024
4147473
use dummy kip10 activation daa score in params
biryukovmaxim Jun 16, 2024
c32c34b
use dummy kip10 activation daa score in params
biryukovmaxim Jun 16, 2024
915b5d3
suppress clippy lint
biryukovmaxim Jun 16, 2024
7294020
add example with shared key
biryukovmaxim Jun 17, 2024
3c4e029
Merge branch 'master' into kip-10-mutual-tx
biryukovmaxim Sep 28, 2024
8b4d0be
fix clippy
biryukovmaxim Sep 28, 2024
2892374
remove useless check from example
biryukovmaxim Oct 11, 2024
c86881f
add one-time borrowing example
biryukovmaxim Oct 15, 2024
e5e8b64
Implement one-time and two-times threshold borrowing scenarios
biryukovmaxim Oct 15, 2024
ef4e7ee
fix: fix incorrect sig-op count
biryukovmaxim Oct 15, 2024
7b6cf0d
correct error description
biryukovmaxim Oct 22, 2024
77f0719
Merge branch 'master' into kip-10-mutual-tx
biryukovmaxim Oct 22, 2024
cdb43ae
style: fmt
biryukovmaxim Oct 22, 2024
e39a2db
Merge branch 'master' into kip-10-mutual-tx
biryukovmaxim Oct 31, 2024
730df36
pass kip-10 flag in constructor params
biryukovmaxim Oct 31, 2024
a777a12
remove borrow scenario from tests.
biryukovmaxim Nov 2, 2024
c216370
introduce method that converts spk to bytes.
biryukovmaxim Nov 2, 2024
20a8df6
return comment describing where invalid opcodes starts from.
biryukovmaxim Nov 2, 2024
1a9c508
fix wring error messages
biryukovmaxim Nov 2, 2024
164d044
support introspection by index
biryukovmaxim Nov 6, 2024
91298d5
test input spk
biryukovmaxim Nov 6, 2024
a117c0e
test output spk
biryukovmaxim Nov 6, 2024
3b2849f
tests refactor
biryukovmaxim Nov 6, 2024
d1cb457
support 8-byte arithmetics
biryukovmaxim Nov 7, 2024
8069be5
Standartize fork activation logic (#588)
someone235 Nov 5, 2024
dced99f
Refactoring for cleaner pruning proof module (#589)
coderofstuff Nov 6, 2024
2974b97
only enable 8 byte arithmetics for kip10
biryukovmaxim Nov 7, 2024
186db46
use i64 value in 9-byte tests
biryukovmaxim Nov 7, 2024
96fb3df
fix tests covering kip10 and i64 deserialization
biryukovmaxim Nov 7, 2024
2810a88
fix test according to 8-byte math
biryukovmaxim Nov 7, 2024
246537b
finish test covering kip10 opcodes: input/output/amount/spk
biryukovmaxim Nov 7, 2024
a432e96
fix kip10 examples
biryukovmaxim Nov 7, 2024
a09fdb7
rename test
biryukovmaxim Nov 7, 2024
372a7ad
feat: add input index op
biryukovmaxim Nov 7, 2024
7e81eb2
feat: add input/outpiut opcodes
biryukovmaxim Nov 7, 2024
a30976d
reseve opcodes
biryukovmaxim Nov 7, 2024
25243f5
fix example
biryukovmaxim Nov 7, 2024
66d6bad
introspection opcodes are reserverd, not disables
biryukovmaxim Nov 7, 2024
2ba5d47
use ForkActivation type
biryukovmaxim Nov 7, 2024
658782a
Merge branch 'master' into kip-10-mutual-tx
biryukovmaxim Nov 7, 2024
3eae88a
cicd: run kip-10 example
biryukovmaxim Nov 8, 2024
8422331
move spk encoding to txscript module
biryukovmaxim Nov 9, 2024
046308f
rework bound check ot input/output index
biryukovmaxim Nov 9, 2024
828a5bc
fix tests by importing spkencoding trait
biryukovmaxim Nov 9, 2024
3259cc6
replace todo in descripotions of over[under]flow errors
biryukovmaxim Nov 9, 2024
5f8351d
reorder new opcodes, reserve script sig opcode, remove txid
biryukovmaxim Nov 9, 2024
955424c
fix bitcoin script tests
biryukovmaxim Nov 9, 2024
5689ed5
add simple opcode tests
biryukovmaxim Nov 9, 2024
e9829e2
rename id(which represents input index) to idx
biryukovmaxim Nov 9, 2024
7705ba3
fix comments
biryukovmaxim Nov 9, 2024
1f38b64
add input spk tests
biryukovmaxim Nov 9, 2024
4a3a44b
refactor test cases
biryukovmaxim Nov 9, 2024
5a00231
refactor(txscript): Enforce input index invariant via assertion
biryukovmaxim Nov 9, 2024
df7c243
refactor error types to contain correct info
biryukovmaxim Nov 9, 2024
523ee9a
rename id to idx
biryukovmaxim Nov 9, 2024
a5c6077
rename opcode
biryukovmaxim Nov 9, 2024
da7d81b
make construction of TxScriptEngine from transaction input infallible
biryukovmaxim Nov 10, 2024
4644179
style: format combinators chain
biryukovmaxim Nov 10, 2024
0fa920e
add integration test covering activation of kip10
biryukovmaxim Nov 10, 2024
83ad761
rename kip10_activation_daa_score to kip10_activation
biryukovmaxim Nov 10, 2024
07b80dd
Update crypto/txscript/src/lib.rs
biryukovmaxim Nov 10, 2024
b50b7b3
rework assert
biryukovmaxim Nov 10, 2024
78f2291
Merge branch 'master' into kip-10-mutual-tx
biryukovmaxim Nov 10, 2024
7fc9828
verify that block is disqualified in case of it has tx which requires
biryukovmaxim Nov 10, 2024
4d79485
revert changer to infallible api
biryukovmaxim Nov 10, 2024
4fe40f8
add doc comments
biryukovmaxim Nov 10, 2024
898a56a
Update crypto/txscript/src/opcodes/mod.rs
biryukovmaxim Nov 11, 2024
3af28ed
Update crypto/txscript/src/opcodes/mod.rs
biryukovmaxim Nov 11, 2024
88399da
add required import
biryukovmaxim Nov 11, 2024
863a9a1
refactor: SigHashReusedValuesUnsync doesnt neet to be mutable
biryukovmaxim Nov 11, 2024
103684e
fix test description
biryukovmaxim Nov 11, 2024
2db1df5
rework example
biryukovmaxim Nov 11, 2024
1e48ab4
9 byte integers must fail to serialize
biryukovmaxim Nov 11, 2024
aaeed6d
add todo
biryukovmaxim Nov 11, 2024
0befcaa
rewrite todo
biryukovmaxim Nov 11, 2024
19a8c52
remove redundant code
biryukovmaxim Nov 11, 2024
08d57c0
Merge pull request #6 from biryukovmaxim/fallible-serialization
biryukovmaxim Nov 11, 2024
ca02b61
remove redundant mut in example
biryukovmaxim Nov 12, 2024
3323e24
remove redundant mut in example
biryukovmaxim Nov 12, 2024
83711f4
remove redundant mut in example
biryukovmaxim Nov 12, 2024
74468e5
cicd: apply lint to examples
biryukovmaxim Nov 12, 2024
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ jobs:

- name: Run cargo doc
run: cargo doc --release --no-deps --workspace
- name: Run kip-10 example
run: cargo run --example kip-10


# test-release:
# name: Test Suite Release
Expand Down Expand Up @@ -210,7 +213,7 @@ jobs:
run: cargo fmt --all -- --check

- name: Run cargo clippy
run: cargo clippy --workspace --tests --benches -- -D warnings
run: cargo clippy --workspace --tests --benches --examples -- -D warnings


check-wasm32:
Expand Down
7 changes: 4 additions & 3 deletions consensus/benches/check_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn benchmark_check_scripts(c: &mut Criterion) {
let cache = Cache::new(inputs_count as u64);
b.iter(|| {
cache.clear();
check_scripts_sequential(black_box(&cache), black_box(&tx.as_verifiable())).unwrap();
check_scripts_sequential(black_box(&cache), black_box(&tx.as_verifiable()), false).unwrap();
})
});

Expand All @@ -93,7 +93,7 @@ fn benchmark_check_scripts(c: &mut Criterion) {
let cache = Cache::new(inputs_count as u64);
b.iter(|| {
cache.clear();
check_scripts_par_iter(black_box(&cache), black_box(&tx.as_verifiable())).unwrap();
check_scripts_par_iter(black_box(&cache), black_box(&tx.as_verifiable()), false).unwrap();
})
});

Expand All @@ -107,7 +107,8 @@ fn benchmark_check_scripts(c: &mut Criterion) {
let cache = Cache::new(inputs_count as u64);
b.iter(|| {
cache.clear();
check_scripts_par_iter_pool(black_box(&cache), black_box(&tx.as_verifiable()), black_box(&pool)).unwrap();
check_scripts_par_iter_pool(black_box(&cache), black_box(&tx.as_verifiable()), black_box(&pool), false)
.unwrap();
})
});
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/client/src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn calc_schnorr_signature_hash(
let utxo = cctx::UtxoEntry::from(utxo.as_ref());

let hash_type = SIG_HASH_ALL;
let mut reused_values = SigHashReusedValues::new();
let reused_values = SigHashReusedValuesUnsync::new();

// let input = verifiable_tx.populated_input(input_index);
// let tx = verifiable_tx.tx();
Expand Down
18 changes: 17 additions & 1 deletion consensus/core/src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ pub struct Params {
/// DAA score from which storage mass calculation and transaction mass field are activated as a consensus rule
pub storage_mass_activation: ForkActivation,

/// DAA score from which tx engine:
/// 1. Supports 8-byte integer arithmetic operations (previously limited to 4 bytes)
/// 2. Supports transaction introspection opcodes:
/// - OpTxInputCount (0xb3): Get number of inputs
/// - OpTxOutputCount (0xb4): Get number of outputs
/// - OpTxInputIndex (0xb9): Get current input index
/// - OpTxInputAmount (0xbe): Get input amount
/// - OpTxInputSpk (0xbf): Get input script public key
/// - OpTxOutputAmount (0xc2): Get output amount
/// - OpTxOutputSpk (0xc3): Get output script public key
pub kip10_activation: ForkActivation,

/// DAA score after which the pre-deflationary period switches to the deflationary period
pub deflationary_phase_daa_score: u64,

Expand Down Expand Up @@ -380,6 +392,7 @@ pub const MAINNET_PARAMS: Params = Params {

storage_mass_parameter: STORAGE_MASS_PARAMETER,
storage_mass_activation: ForkActivation::never(),
kip10_activation: ForkActivation::never(),

// deflationary_phase_daa_score is the DAA score after which the pre-deflationary period
// switches to the deflationary period. This number is calculated as follows:
Expand Down Expand Up @@ -443,7 +456,7 @@ pub const TESTNET_PARAMS: Params = Params {

storage_mass_parameter: STORAGE_MASS_PARAMETER,
storage_mass_activation: ForkActivation::never(),

kip10_activation: ForkActivation::never(),
// deflationary_phase_daa_score is the DAA score after which the pre-deflationary period
// switches to the deflationary period. This number is calculated as follows:
// We define a year as 365.25 days
Expand Down Expand Up @@ -513,6 +526,7 @@ pub const TESTNET11_PARAMS: Params = Params {

storage_mass_parameter: STORAGE_MASS_PARAMETER,
storage_mass_activation: ForkActivation::always(),
kip10_activation: ForkActivation::never(),

skip_proof_of_work: false,
max_block_level: 250,
Expand Down Expand Up @@ -566,6 +580,7 @@ pub const SIMNET_PARAMS: Params = Params {

storage_mass_parameter: STORAGE_MASS_PARAMETER,
storage_mass_activation: ForkActivation::always(),
kip10_activation: ForkActivation::never(),

skip_proof_of_work: true, // For simnet only, PoW can be simulated by default
max_block_level: 250,
Expand Down Expand Up @@ -612,6 +627,7 @@ pub const DEVNET_PARAMS: Params = Params {

storage_mass_parameter: STORAGE_MASS_PARAMETER,
storage_mass_activation: ForkActivation::never(),
kip10_activation: ForkActivation::never(),

// deflationary_phase_daa_score is the DAA score after which the pre-deflationary period
// switches to the deflationary period. This number is calculated as follows:
Expand Down
14 changes: 14 additions & 0 deletions consensus/core/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ pub trait VerifiableTransaction {
fn id(&self) -> TransactionId {
self.tx().id()
}

fn utxo(&self, index: usize) -> Option<&UtxoEntry>;
}

/// A custom iterator written only so that `populated_inputs` has a known return type and can de defined on the trait level
Expand Down Expand Up @@ -342,6 +344,10 @@ impl<'a> VerifiableTransaction for PopulatedTransaction<'a> {
fn populated_input(&self, index: usize) -> (&TransactionInput, &UtxoEntry) {
(&self.tx.inputs[index], &self.entries[index])
}

fn utxo(&self, index: usize) -> Option<&UtxoEntry> {
self.entries.get(index)
}
}

/// Represents a validated transaction with populated UTXO entry data and a calculated fee
Expand Down Expand Up @@ -370,6 +376,10 @@ impl<'a> VerifiableTransaction for ValidatedTransaction<'a> {
fn populated_input(&self, index: usize) -> (&TransactionInput, &UtxoEntry) {
(&self.tx.inputs[index], &self.entries[index])
}

fn utxo(&self, index: usize) -> Option<&UtxoEntry> {
self.entries.get(index)
}
}

impl AsRef<Transaction> for Transaction {
Expand Down Expand Up @@ -507,6 +517,10 @@ impl<T: AsRef<Transaction>> VerifiableTransaction for MutableTransactionVerifiab
self.inner.entries[index].as_ref().expect("expected to be called only following full UTXO population"),
)
}

fn utxo(&self, index: usize) -> Option<&UtxoEntry> {
self.inner.entries.get(index).and_then(Option::as_ref)
}
}

/// Specialized impl for `T=Arc<Transaction>`
Expand Down
1 change: 1 addition & 0 deletions consensus/src/consensus/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl ConsensusServices {
tx_script_cache_counters,
mass_calculator.clone(),
params.storage_mass_activation,
params.kip10_activation,
);

let pruning_point_manager = PruningPointManager::new(
Expand Down
26 changes: 18 additions & 8 deletions consensus/src/consensus/test_consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ use kaspa_hashes::Hash;
use kaspa_notify::subscription::context::SubscriptionContext;
use parking_lot::RwLock;

use kaspa_database::create_temp_db;
use kaspa_database::prelude::ConnBuilder;
use std::future::Future;
use std::{sync::Arc, thread::JoinHandle};

use super::services::{DbDagTraversalManager, DbGhostdagManager, DbWindowManager};
use super::Consensus;
use crate::pipeline::virtual_processor::test_block_builder::TestBlockBuilder;
use crate::processes::window::WindowManager;
use crate::{
Expand All @@ -35,9 +32,10 @@ use crate::{
pipeline::{body_processor::BlockBodyProcessor, virtual_processor::VirtualStateProcessor, ProcessingCounters},
test_helpers::header_from_precomputed_hash,
};

use super::services::{DbDagTraversalManager, DbGhostdagManager, DbWindowManager};
use super::Consensus;
use kaspa_database::create_temp_db;
use kaspa_database::prelude::ConnBuilder;
use std::future::Future;
use std::{sync::Arc, thread::JoinHandle};

pub struct TestConsensus {
params: Params,
Expand Down Expand Up @@ -138,6 +136,12 @@ impl TestConsensus {
self.validate_and_insert_block(self.build_block_with_parents(hash, parents).to_immutable()).virtual_state_task
}

/// Adds a valid block with the given transactions and parents to the consensus.
///
/// # Panics
///
/// Panics if block builder validation rules are violated.
/// See `kaspa_consensus_core::errors::block::RuleError` for the complete list of possible validation rules.
pub fn add_utxo_valid_block_with_parents(
&self,
hash: Hash,
Expand All @@ -149,6 +153,12 @@ impl TestConsensus {
.virtual_state_task
}

/// Builds a valid block with the given transactions, parents, and miner data.
///
/// # Panics
///
/// Panics if block builder validation rules are violated.
/// See `kaspa_consensus_core::errors::block::RuleError` for the complete list of possible validation rules.
pub fn build_utxo_valid_block_with_parents(
&self,
hash: Hash,
Expand Down
6 changes: 6 additions & 0 deletions consensus/src/processes/transaction_validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ pub struct TransactionValidator {

/// Storage mass hardfork DAA score
storage_mass_activation: ForkActivation,
/// KIP-10 hardfork DAA score
kip10_activation: ForkActivation,
}

impl TransactionValidator {
#[allow(clippy::too_many_arguments)]
pub fn new(
max_tx_inputs: usize,
max_tx_outputs: usize,
Expand All @@ -42,6 +45,7 @@ impl TransactionValidator {
counters: Arc<TxScriptCacheCounters>,
mass_calculator: MassCalculator,
storage_mass_activation: ForkActivation,
kip10_activation: ForkActivation,
) -> Self {
Self {
max_tx_inputs,
Expand All @@ -54,6 +58,7 @@ impl TransactionValidator {
sig_cache: Cache::with_counters(10_000, counters),
mass_calculator,
storage_mass_activation,
kip10_activation,
}
}

Expand All @@ -78,6 +83,7 @@ impl TransactionValidator {
sig_cache: Cache::with_counters(10_000, counters),
mass_calculator: MassCalculator::new(0, 0, 0, 0),
storage_mass_activation: ForkActivation::never(),
kip10_activation: ForkActivation::never(),
}
}
}
Loading