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

check for coinbase maturity #128

Merged
Merged
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
3 changes: 1 addition & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ version = "0.1.0"
edition = "2024_07"

[scripts]
get_block= "./scripts/data/get_block.sh"
get_block_py= "python3 ./scripts/data/get_block.py"
get_block= "python3 ./scripts/data/get_block.py"
get_blocks= "./scripts/data/get_blocks.sh"
light_client= "./scripts/data/light_client.sh"

Expand Down
7 changes: 4 additions & 3 deletions scripts/data/get_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def outpoint(prev_output):
data: {tx_output({'vout': [prev_tx['vout'][prev_output['vout']]]})}
block_height: {prev_block['height']}_u32,
block_time: {prev_block['time']}_u32,
is_coinbase: {str(prev_tx['vin'][0].get('coinbase') is not None).lower()}
}}'''


Expand All @@ -85,6 +86,7 @@ def tx_input_coinbase(tx_in):
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
}},
}}'''

Expand Down Expand Up @@ -152,15 +154,14 @@ def main():
file_payload = f'''// THIS CODE IS GENERATED BY SCRIPT, DO NOT EDIT IT MANUALLY

use raito::types::transaction::{{Transaction, TxIn, TxOut, OutPoint}};
use raito::types::block::{{Block, Header}};
use raito::types::block::{{Block, Header, TransactionData}};
use raito::utils::hex::from_hex;
use raito::utils::hash::Hash;

// block_hash: {block_hash}
pub fn block_{block["height"]}() -> Block {{
Block {{
header: {header(block)},
txs: array![{txs_process(txsraw, block["nTx"])}].span(),
data: TransactionData::Transactions(array![{txs_process(txsraw, block["nTx"])}].span()),
}}
}}'''

Expand Down
2 changes: 1 addition & 1 deletion scripts/data/get_blocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ get_single_block() {
main() {
local block_hashes=(
"00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee" # Block containing first P2P tx to Hal Finney (170)
"00000000132fbe8314fc571c0be60b31ccd461c9ee85f42bde8c6d160a9dacc0" # Bloc containing first off ramp tx from Martti Malmi (24835)
"00000000132fbe8314fc571c0be60b31ccd461c9ee85f42bde8c6d160a9dacc0" # Block containing first off ramp tx from Martti Malmi (24835)
"00000000152340ca42227603908689183edc47355204e7aca59383b0aaac1fd8" # Block containing pizza tx (57043)
"000000000000011f9791dcfdfe0e402b79a165a3b781bafcc918b6f2166d577c" # Small Block (150013)
"000000000000048b95347e83192f69cf0366076336c639f9b7228e9ba171342e" # First halving block (210000)
Expand Down
20 changes: 20 additions & 0 deletions src/codec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
};
let bytes = outpoint.encode();

Expand All @@ -234,6 +235,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
};
let bytes = outpoint.encode();

Expand All @@ -257,6 +259,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span()
};
Expand Down Expand Up @@ -285,6 +288,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span()
};
Expand Down Expand Up @@ -316,6 +320,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span()
},
Expand All @@ -332,6 +337,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span()
},
Expand All @@ -348,6 +354,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span()
}
Expand Down Expand Up @@ -404,6 +411,7 @@ mod tests {
},
block_height: 149994_u32,
block_time: 1319114701_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -426,6 +434,7 @@ mod tests {
},
block_height: 150006_u32,
block_time: 1319124571_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -448,6 +457,7 @@ mod tests {
},
block_height: 150005_u32,
block_time: 1319122014_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -470,6 +480,7 @@ mod tests {
},
block_height: 149935_u32,
block_time: 1319066844_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -492,6 +503,7 @@ mod tests {
},
block_height: 149940_u32,
block_time: 1319069371_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -514,6 +526,7 @@ mod tests {
},
block_height: 149895_u32,
block_time: 1319035283_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -536,6 +549,7 @@ mod tests {
},
block_height: 149814_u32,
block_time: 1318990721_u32,
is_coinbase: true,
},
},
TxIn {
Expand All @@ -558,6 +572,7 @@ mod tests {
},
block_height: 149984_u32,
block_time: 1319104909_u32,
is_coinbase: true,
},
},
]
Expand Down Expand Up @@ -612,6 +627,7 @@ mod tests {
},
block_height: 206120_u32,
block_time: 1351856022_u32,
is_coinbase: true,
},
},
]
Expand Down Expand Up @@ -706,6 +722,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![
from_hex(
Expand Down Expand Up @@ -767,6 +784,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![
from_hex(
Expand All @@ -789,6 +807,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![
from_hex(
Expand Down Expand Up @@ -852,6 +871,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![
from_hex(
Expand Down
5 changes: 5 additions & 0 deletions src/types/transaction.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ pub struct OutPoint {
/// containing this output was mined).
/// Can be set to default if locktime feature (time relative) is disabled.
pub block_time: u32,
// Determine if the outpoint is a coinbase transaction
// Has 100 or more block confirmation,
// is added when block are queried
pub is_coinbase: bool
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Output of a transaction.
Expand Down Expand Up @@ -172,6 +176,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down
7 changes: 7 additions & 0 deletions src/validation/coinbase.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
},
Expand All @@ -121,6 +122,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down Expand Up @@ -154,6 +156,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down Expand Up @@ -187,6 +190,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down Expand Up @@ -219,6 +223,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down Expand Up @@ -252,6 +257,7 @@ mod tests {
data: TxOut { value: 0_64, ..Default::default(), },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down Expand Up @@ -285,6 +291,7 @@ mod tests {
data: Default::default(),
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: false,
},
witness: array![].span(),
}
Expand Down
35 changes: 33 additions & 2 deletions src/validation/transaction.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Transaction validation helpers.

use crate::types::transaction::Transaction;
use crate::types::transaction::{Transaction, TransactionTrait};

/// Validate transaction and return transaction fee.
///
Expand Down Expand Up @@ -29,6 +29,34 @@ pub fn validate_transaction(
// - https://github.com/bitcoin/bitcoin/blob/master/src/consensus/tx_verify.cpp
// - https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp

let mut maturity_result = Option::None;

// Coinbase maturity test
for input in *tx
maciejka marked this conversation as resolved.
Show resolved Hide resolved
.inputs {
if *input.previous_output.is_coinbase {
let coinbase_block_height = *input.previous_output.block_height;
if block_height <= coinbase_block_height + 100 {
maturity_result =
Option::Some(
format!(
"[validate_transaction] coinbase input: ({}, {}) of tx: {} not mature (current height: {}, coinbase height: {})",
*input.previous_output.txid,
*input.previous_output.vout,
tx.txid(),
block_height,
coinbase_block_height
)
);
break;
}
}
};

if let Option::Some(result) = maturity_result {
return Result::Err(result);
}

let mut total_input_amount = 0;
for input in *tx.inputs {
total_input_amount += *input.previous_output.data.value;
Expand Down Expand Up @@ -76,6 +104,7 @@ mod tests {
data: TxOut { value: 100, ..Default::default() },
block_height: Default::default(),
block_time: Default::default(),
is_coinbase: true,
},
witness: array![].span(),
}
Expand All @@ -94,7 +123,9 @@ mod tests {
lock_time: 0
};

let fee = validate_transaction(@tx, 0, 0).unwrap();
assert!(validate_transaction(@tx, 0, 0).is_err());

let fee = validate_transaction(@tx, 101, 0).unwrap();
assert_eq!(fee, 10);
}
}
21 changes: 0 additions & 21 deletions tests/blocks.cairo
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
pub mod block_0;
pub mod block_170;
pub mod block_24835;
pub mod block_57043;
pub mod block_150013;
pub mod block_210000;
pub mod block_757739;
pub mod block_757753;
// pub mod block_210000;
// mod block_420000;
// mod block_478558;
// mod block_481824;
// mod block_491407;
// mod block_630000;
// mod block_709632;
// mod block_774628;
// mod block_840000;

// Blocks that are commented out are too big and make compilation when running tests almost
// impossible.


Loading