This repository was archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
script: Add some exhaustive testing #124
Open
iljakuklic
wants to merge
1
commit into
staging
Choose a base branch
from
script-exhaustive-tests
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use crate::*; | ||
|
||
// Test the interpreter on all 4-byte combinations of non-trivial opcodes. | ||
#[test] | ||
//#[ignore = "This test is too expensive to run by default"] | ||
fn test_4opc_sequences() { | ||
use hex::FromHex; | ||
use std::io::{BufRead, BufReader}; | ||
|
||
// The test vectors are encoded in a gzipped CSV file. | ||
// Each line in the file is has the following comma-separated filelds: | ||
// 1) The hex-encoded bitcoin script | ||
// 2) The expected outcome, which is either 0 (script should fail) or 1 (script should succceed) | ||
// 3) If the expected outcome is 1 (success), then a sequence of comma-separated hex-encoded | ||
// stack items after the execution of the script follows. | ||
// | ||
// The test vectors were obtained obtained by running the script interpreter in Tapscript mode | ||
// on all 4-opcode sequences of a subset of opcodes. Notable omissions include: | ||
// * `OP_NOP_N`, `OP_SUCCESS_N`: These are trivial and including them would make the file much | ||
// larger (and test run time much longer) with little benefit. | ||
// * Opcodes dealing with checking signatures: These behave differently in Bitcoin. | ||
// * `OP_PUSHDATA_N`: Some these should be included in the future here or in a separate test. | ||
let test_vectors_raw = include_bytes!("test_vectors_4opc.csv.gz").as_ref(); | ||
let test_vectors = BufReader::new(flate2::bufread::GzDecoder::new(test_vectors_raw)); | ||
|
||
let mut fails = 0u32; | ||
for line in test_vectors.lines().map(|l| l.expect("can't get a line")) { | ||
let mut parts = line.split(','); | ||
// Load the script. | ||
let script: Script = Vec::from_hex(parts.next().expect("no script")) | ||
.expect("script not in hex format") | ||
.into(); | ||
|
||
// Load the expected outcome. It should be either 0, or 1 followed by stack items. | ||
let expected: Option<Stack> = match parts.next().expect("no desired outcome") { | ||
"0" => None, | ||
"1" => { | ||
// For successful outcome, read the expected sequence of items on the stack | ||
let stack: Vec<_> = | ||
parts.map(|s| Vec::from_hex(s).expect("hex item").into()).collect(); | ||
Some(stack.into()) | ||
} | ||
_ => unreachable!("bad format"), | ||
}; | ||
|
||
// Run the script and record mismatches between expected and actual outputs. | ||
let result = run_script(&TestContext::default(), &script, vec![].into()).ok(); | ||
if expected != result { | ||
eprintln!("FAIL {:?}: {:?} vs. {:?}", script, result, expected); | ||
fails += 1; | ||
} | ||
} | ||
|
||
// Let the test fail if we have at least one mismatch. | ||
assert!(fails == 0, "{} tests failed", fails); | ||
} |
Binary file not shown.
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
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.
Did you mean to comment the ignore?
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.
Not sure. It runs pretty quickly in release mode, but under debug, I will have to measure.
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.
Running this test with debug build takes 147 seconds on my laptop.
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.
What do we aim for in these tests? Are tests that run a long-ish time under debug acceptable or do we want to disable them by default (make opt-in)?
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.
147s is okay for me but is probably approaching the limit of "I'm getting bored waiting for this"-ness so maybe making them opt-in is a good idea. If someone touches the script stuff then they know to run them anyway so we're probably safe.
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.
In release mode, it is about 5s on my laptop btw