-
Notifications
You must be signed in to change notification settings - Fork 720
AAC: consensus tests for EarlyReturnError variants
#6736
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
Merged
jacinta-stacks
merged 7 commits into
stacks-network:develop
from
jacinta-stacks:chore/aac-early-return-error-consensus-tests
Dec 8, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a5a59e2
Add tests to EarlyReturnError::UnwrapFailed at deploy time
jacinta-stacks 541e449
Add test to AssertionFailed at deploy time
jacinta-stacks 16de730
Add runtime test to EarlyReturnError::AssertionFailed
jacinta-stacks f9db7be
Add runtime tests to EarlyReturnError::UnwrapFailed
jacinta-stacks 96c9e01
Rust fmt
jacinta-stacks 7be9d22
Merge branch 'develop' of https://github.com/stacks-network/stacks-co…
jacinta-stacks 147105c
Do not include a read only call in early return tests and create a se…
jacinta-stacks 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
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,228 @@ | ||
| // Copyright (C) 2025 Stacks Open Internet Foundation | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! This module contains consensus tests related to EarlyReturn errors. | ||
|
|
||
| use clarity::vm::{Value as ClarityValue, errors::EarlyReturnError, types::ResponseData}; | ||
|
|
||
| use crate::chainstate::tests::consensus::{ | ||
| contract_call_consensus_test, contract_deploy_consensus_test, | ||
| }; | ||
|
|
||
| /// Generates a coverage classification report for a specific [`EarlyReturnError`] variant. | ||
| /// | ||
| /// This method exists purely for **documentation and tracking purposes**. | ||
| /// It helps maintainers understand which error variants have been: | ||
| /// | ||
| /// - ✅ **Tested** — verified through consensus tests. | ||
| /// - ⚙️ **Ignored** — not tested on purpose. | ||
| /// - 🚫 **Unreachable** — not testable from consensus test side for reasons. | ||
| #[allow(dead_code)] | ||
| fn variant_coverage_report(variant: EarlyReturnError) { | ||
| enum VariantCoverage { | ||
| // Cannot occur through valid execution. The string is to explain the reason. | ||
| Unreachable_Functionally(&'static str), | ||
| // Unexpected error, that should never happen | ||
| Unreachable_ExpectLike, | ||
| // Defined but never used | ||
| Unreachable_NotUsed, | ||
| // Not tested on purpose. The string is to explain the reason. | ||
| Ignored(&'static str), | ||
| // Covered by consensus tests. The func lists is for to link the variant with the related tests | ||
| Tested(Vec<fn()>), | ||
| } | ||
|
|
||
| use EarlyReturnError::*; | ||
| use VariantCoverage::*; | ||
|
|
||
| _ = match variant { | ||
| UnwrapFailed(_) => Tested(vec![ | ||
| native_try_ret_err_cdeploy, | ||
| native_try_ret_err_ccall, | ||
| native_try_ret_none_cdeploy, | ||
| native_try_ret_none_ccall, | ||
| native_unwrap_err_or_ret_cdeploy, | ||
| native_unwrap_err_or_ret_ccall, | ||
| native_unwrap_or_ret_none_cdeploy, | ||
| native_unwrap_or_ret_none_ccall, | ||
| ]), | ||
| AssertionFailed(_) => Tested(vec![ | ||
| native_special_asserts_cdeploy, | ||
| native_special_asserts_ccall, | ||
| ]), | ||
| }; | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: attempting to `try!` unwrap an `err` response at deploy time. | ||
| /// Outcome: block accepted | ||
| #[test] | ||
| fn native_try_ret_err_cdeploy() { | ||
| contract_deploy_consensus_test!( | ||
| contract_name: "unwrap-try-resp", | ||
| contract_code: "(begin (try! (if true (err u200) (ok u1))))", | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: attempting to `try!` unwrap an `err` response at call time. | ||
| /// Outcome: block accepted | ||
| /// Note: [`clarity::vm::callables::DefinedFunction::execute_apply`] converts [`EarlyReturnError::UnwrapFailed`] | ||
| /// into a successful return wrapping the internal thrown value. | ||
| #[test] | ||
| fn native_try_ret_err_ccall() { | ||
| contract_call_consensus_test!( | ||
| contract_name: "unwrap-err", | ||
| contract_code: " | ||
| (define-read-only (trigger (resp (response uint uint))) | ||
| (begin | ||
| (try! resp) | ||
| (ok u1) | ||
| ) | ||
| ) | ||
| ", | ||
| function_name: "trigger", | ||
| function_args: &[ClarityValue::Response(ResponseData { | ||
| committed: false, | ||
| data: Box::new(ClarityValue::UInt(42)) | ||
| })], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: attempting to `try!` unwrap a `None` optional at deploy time. | ||
| /// Outcome: block accepted | ||
| #[test] | ||
| fn native_try_ret_none_cdeploy() { | ||
| contract_deploy_consensus_test!( | ||
| contract_name: "unwrap-try-opt", | ||
| contract_code: "(begin (try! (if true none (some true))))", | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: attempting to `try!` unwrap an `None` optional at call time. | ||
| /// Outcome: block accepted | ||
| /// Note: [`clarity::vm::callables::DefinedFunction::execute_apply`] converts [`EarlyReturnError::UnwrapFailed`] | ||
| /// into a successful return wrapping the internal thrown value. | ||
| #[test] | ||
| fn native_try_ret_none_ccall() { | ||
| contract_call_consensus_test!( | ||
| contract_name: "unwrap-try-opt", | ||
| contract_code: " | ||
| (define-read-only (trigger (opt (optional bool))) | ||
| (begin | ||
| (try! opt) | ||
| (some true) | ||
| ) | ||
| ) | ||
| ", | ||
| function_name: "trigger", | ||
| function_args: &[ClarityValue::none()], | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: calling `unwrap-err!` on an `(ok ...)` value at deploy time. | ||
| /// Outcome: block accepted | ||
| #[test] | ||
| fn native_unwrap_err_or_ret_cdeploy() { | ||
| contract_deploy_consensus_test!( | ||
| contract_name: "unwrap-err", | ||
| contract_code: "(begin (unwrap-err! (if true (ok u3) (err u1)) (err u9)))", | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: calling `unwrap-err!` on an `(ok ...)` value at call time. | ||
| /// Outcome: block accepted | ||
| /// Note: [`clarity::vm::callables::DefinedFunction::execute_apply`] converts [`EarlyReturnError::UnwrapFailed`] | ||
| /// into a successful return wrapping the internal thrown value. | ||
| #[test] | ||
| fn native_unwrap_err_or_ret_ccall() { | ||
| contract_call_consensus_test!( | ||
| contract_name: "unwrap-err", | ||
| contract_code: " | ||
| (define-public (trigger) | ||
| (begin | ||
| (unwrap-err! (if true (ok u3) (err u1)) (err u9)) | ||
| (ok u1) | ||
| ) | ||
| ) | ||
| ", | ||
| function_name: "trigger", | ||
| function_args: &[], | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: calling `unwrap!` on a `None` optional at deploy time. | ||
| /// Outcome: block accepted | ||
| #[test] | ||
| fn native_unwrap_or_ret_none_cdeploy() { | ||
| contract_deploy_consensus_test!( | ||
| contract_name: "unwrap-opt", | ||
| contract_code: "(begin (unwrap! (if true none (some true)) (err u9)))", | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::UnwrapFailed`] | ||
| /// Caused by: calling `unwrap!` on a `None` optional at call time. | ||
| /// Outcome: block accepted | ||
| /// Note: [`clarity::vm::callables::DefinedFunction::execute_apply`] converts [`EarlyReturnError::UnwrapFailed`] | ||
| /// into a successful return wrapping the internal thrown value. | ||
| #[test] | ||
| fn native_unwrap_or_ret_none_ccall() { | ||
| contract_call_consensus_test!( | ||
| contract_name: "unwrap-opt", | ||
| contract_code: " | ||
| (define-read-only (trigger (opt (optional bool))) | ||
| (begin | ||
| (unwrap! opt (err false)) | ||
| (ok true) | ||
| ) | ||
| ) | ||
| ", | ||
| function_name: "trigger", | ||
| function_args: &[ClarityValue::none()], | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::AssertionFailed`] | ||
| /// Caused by: failing `asserts!` condition at deploy time. | ||
| /// Outcome: block accepted | ||
| #[test] | ||
| fn native_special_asserts_cdeploy() { | ||
| contract_deploy_consensus_test!( | ||
| contract_name: "asserts-fail", | ||
| contract_code: "(begin (asserts! (is-eq 1 0) (err u0)) (ok u1))", | ||
| ); | ||
| } | ||
|
|
||
| /// Error: [`EarlyReturnError::AssertionFailed`] | ||
| /// Caused by: failing `asserts!` condition at call time. | ||
| /// Outcome: block accepted | ||
| /// Note: [`clarity::vm::callables::DefinedFunction::execute_apply`] converts [`EarlyReturnError::AssertionFailed`] | ||
| /// into a successful return wrapping the internal thrown value. | ||
| #[test] | ||
| fn native_special_asserts_ccall() { | ||
| contract_call_consensus_test!( | ||
| contract_name: "asserts-fail", | ||
| contract_code: "(define-public (trigger) (begin (asserts! false (err u0)) (ok u1)))", | ||
| function_name: "trigger", | ||
| function_args: &[], | ||
| ); | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.