-
Notifications
You must be signed in to change notification settings - Fork 10
Initial Transaction Validations #129
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f17a18b
feat: first transaction validation rules
yHSJ 3cb5233
chore: use transaction hash in TransactionRuleViolation errors
yHSJ 12fa6b7
auxiliary data hash validation
yHSJ 2747e25
chore: address pr feedback
yHSJ d3bdd2b
fix: rewrite block size validation logic
yHSJ 613e34a
chore: address pr feedback
yHSJ 562c10b
chore: remove out of date test
yHSJ f8c4566
chore: allow clippy panic
yHSJ c78f21a
chore: cleanup and remove panics
yHSJ 2f93cfc
chore: remove clones in ledger rules validations where unnecessary
KtorZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,43 @@ | ||
use crate::rules::RuleViolation; | ||
use amaru_kernel::{protocol_parameters::ProtocolParameters, HeaderBody}; | ||
pub struct BlockBodySizeTooBig { | ||
use amaru_kernel::{to_cbor, HeaderBody, MintedBlock}; | ||
pub struct BlockBodySizeMismatch { | ||
pub supplied: usize, | ||
pub max: usize, | ||
pub actual: usize, | ||
} | ||
|
||
impl From<BlockBodySizeTooBig> for RuleViolation { | ||
fn from(value: BlockBodySizeTooBig) -> Self { | ||
RuleViolation::BlockBodySizeTooBig(value) | ||
impl From<BlockBodySizeMismatch> for RuleViolation { | ||
fn from(value: BlockBodySizeMismatch) -> Self { | ||
RuleViolation::BlockBodySizeMismatch(value) | ||
} | ||
} | ||
|
||
// This validation checks that the purported block size in the header is within the bounds. | ||
// The logic that confirms the purported block size matches the actual block size should take place during serialization | ||
#[allow(clippy::panic)] | ||
/// This validation checks that the purported block body size matches the actual block body size. | ||
/// The validation of the bounds happens in the networking layer | ||
pub fn block_body_size_valid( | ||
block_header: &HeaderBody, | ||
protocol_params: &ProtocolParameters, | ||
) -> Result<(), BlockBodySizeTooBig> { | ||
let max_block_size = protocol_params | ||
.max_block_body_size | ||
.try_into() | ||
.unwrap_or_else(|_| panic!("Failed to convert u32 to usize")); | ||
|
||
let block_body_size = block_header | ||
.block_body_size | ||
.try_into() | ||
.unwrap_or_else(|_| panic!("Failed to convert u64 to usize")); | ||
|
||
if block_body_size > max_block_size { | ||
Err(BlockBodySizeTooBig { | ||
supplied: block_body_size, | ||
max: max_block_size, | ||
block: &MintedBlock<'_>, | ||
) -> Result<(), BlockBodySizeMismatch> { | ||
let bh_size = block_header.block_body_size as usize; | ||
let actual_block_size = calculate_block_body_size(block); | ||
|
||
if bh_size != actual_block_size { | ||
Err(BlockBodySizeMismatch { | ||
supplied: bh_size, | ||
actual: actual_block_size, | ||
}) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use amaru_kernel::{cbor, Block, MintedBlock}; | ||
#[test] | ||
fn test_block_body_size_valid() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it ok to delete this test? |
||
// These bytes are Conway3.block from Pallas https://github.com/txpipe/pallas/blob/main/test_data/conway3.block | ||
let bytes = hex::decode("820785828a1a00153df41a01aa8a0458201bbf3961f179735b68d8f85bcff85b1eaaa6ec3fa6218e4b6f4be7c6129e37ba5820472a53a312467a3b66ede974399b40d1ea428017bc83cf9647d421b21d1cb74358206ee6456894a5931829207e497e0be77898d090d0ac0477a276712dee34e51e05825840d35e871ff75c9a243b02c648bccc5edf2860edba0cc2014c264bbbdb51b2df50eff2db2da1803aa55c9797e0cc25bdb4486a4059c4687364ad66ed15b4ec199f58508af7f535948fac488dc74123d19c205ea2b02cbbf91104bbad140d4ba4bb4d75f7fdb762586802f116bdba3ecaa0840614a2b96d619006c3274b590bcd2599e39a17951cbc3db6348fa2688158384f081901965820d8038b5679ffc770b060578bcd7b33045f2c3aa5acc7bd8cde8b705cfe673d7584582030449be32ae7b8363fde830fc9624945862b281e481ec7f5997c75d1f2316c560018ca5840f5d96ce2055a67709c8e6809c882f71ebd7fc6350018d36d803a55b9230ec6c4cbcd41a09255db45214e278f89b39005ac0f213473acbf455165cdcaa9558e0c8209005901c02ba5dda40daa84b3f9c524016c21d7ce13f585062e35298aa31ea590fee809e75ae999dff9b3ee188e01cfcecc384faba50ca673af2388c3cf7407206019920e99e195bc8e6d1a42ef2b7fb549a8da0591180da17db7a24334b098bfef839334761ec51c2bd8a044fd1785b4e216f811dbdcba63eb853a477d3ea87a3b2d61ccfeae74765c51ec1313ffb121573bae4fc3a742825168760f615a0b2b6ef8a42084f9465501774310772de17a574d8d6bef6b14f4277c8b792b4f60f6408262e7aee5e95b8539df07f953d16b209b6d8fa598a6c51ab90659523720c98ffd254bf305106c0b9c6938c33323e191b5afbad8939270c76a82dc2124525aab11396b9de746be6d7fae2c1592c6546474cebe07d1f48c05f36f762d218d9d2ca3e67c27f0a3d82cdd1bab4afa7f3f5d3ecb10c6449300c01b55e5d83f6cefc6a12382577fc7f3de09146b5f9d78f48113622ee923c3484e53bff74df65895ec0ddd43bc9f00bf330681811d5d20d0e30eed4e0d4cc2c75d1499e05572b13fb4e7b0dabf6e36d1988b47fbdecffc01316885f802cd6c60e044bf50a15418530d628cffd506d4eb0db6155be94ce84fbf6529ee06ec78e9c3009c0f5504978dd150926281a400d90102828258202e6b2226fd74ab0cadc53aaa18759752752bd9b616ea48c0e7b7be77d1af4bf400825820d5dc99581e5f479d006aca0cd836c2bb7ddcd4a243f8e9485d3c969df66462cb00018182583900bbe56449ba4ee08c471d69978e01db384d31e29133af4546e6057335061771ead84921c0ca49a4b48ab03c2ad1b45a182a46485ed1c965411b0000000ba4332169021a0002c71d14d9010281841b0000000ba43b7400581de0061771ead84921c0ca49a4b48ab03c2ad1b45a182a46485ed1c965418400f6a2001bffffffffffffffff09d81e821bfffffffffffffffe1bfffffffffffffffff68275687474703a2f2f636f73746d646c732e74657374735820931f1d8cdfdc82050bd2baadfe384df8bf99b00e36cb12bfb8795beab3ac7fe581a100d9010281825820794ff60d3c35b97f55896d1b2a455fe5e89b77fb8094d27063ff1f260d21a67358403894a10bf9fca0592391cdeabd39891fc2f960fae5a2743c73391c495dfdf4ba4f1cb5ede761bebd7996eba6bbe4c126bcd1849afb9504f4ae7fb4544a93ff0ea080").expect("Failed to decode Conway3.block hex"); | ||
|
||
let (_, minted_block): (u16, MintedBlock<'_>) = | ||
cbor::decode(bytes.as_slice()).expect("Failed to parse Conway3.block bytes"); | ||
fn calculate_block_body_size(block: &MintedBlock<'_>) -> usize { | ||
let tx_bodies_raw = to_cbor(&block.transaction_bodies); | ||
let tx_witness_sets_raw = to_cbor(&block.transaction_witness_sets); | ||
let auxiliary_data_raw = to_cbor(&block.auxiliary_data_set); | ||
let invalid_transactions_raw = to_cbor(&block.invalid_transactions); | ||
|
||
let block: Block = minted_block.into(); | ||
|
||
let pp = ProtocolParameters::default(); | ||
assert!(block_body_size_valid(&block.header.header_body, &pp).is_ok()) | ||
} | ||
tx_bodies_raw.len() | ||
+ tx_witness_sets_raw.len() | ||
+ auxiliary_data_raw.len() | ||
+ invalid_transactions_raw.len() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok to delete this test?