-
Notifications
You must be signed in to change notification settings - Fork 696
Implement consensus de/serialization in Clarity #3132
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad0b56e
feat: initial impl of to- and from- consensus buff
kantai 1f6c2f6
enable methods, add documentation
kantai d6a6180
feat: to/from-consensus-buff implementation and tests
kantai 06a8b7b
Merge remote-tracking branch 'origin/next' into feat/to-from-buff
kantai d8f7628
address PR reviews
kantai 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
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
39 changes: 39 additions & 0 deletions
39
clarity/src/vm/analysis/type_checker/natives/conversions.rs
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,39 @@ | ||
use crate::vm::analysis::read_only_checker::check_argument_count; | ||
use crate::vm::analysis::type_checker::contexts::TypingContext; | ||
use crate::vm::analysis::type_checker::{TypeChecker, TypeResult}; | ||
use crate::vm::analysis::CheckError; | ||
use crate::vm::types::{BufferLength, SequenceSubtype, TypeSignature}; | ||
use crate::vm::SymbolicExpression; | ||
|
||
/// to-consensus-buff admits exactly one argument: | ||
/// * the Clarity value to serialize | ||
/// it returns an `(optional (buff x))` where `x` is the maximum possible | ||
/// consensus buffer length based on the inferred type of the supplied value. | ||
pub fn check_special_to_consensus_buff( | ||
checker: &mut TypeChecker, | ||
args: &[SymbolicExpression], | ||
context: &TypingContext, | ||
) -> TypeResult { | ||
check_argument_count(1, args)?; | ||
let input_type = checker.type_check(&args[0], context)?; | ||
let buffer_max_len = BufferLength::try_from(input_type.max_serialized_size()?)?; | ||
TypeSignature::new_option(TypeSignature::SequenceType(SequenceSubtype::BufferType( | ||
buffer_max_len, | ||
))) | ||
.map_err(CheckError::from) | ||
} | ||
|
||
/// from-consensus-buff admits exactly two arguments: | ||
/// * a type signature indicating the expected return type `t1` | ||
/// * a buffer (of up to max length) | ||
/// it returns an `(optional t1)` | ||
pub fn check_special_from_consensus_buff( | ||
checker: &mut TypeChecker, | ||
args: &[SymbolicExpression], | ||
context: &TypingContext, | ||
) -> TypeResult { | ||
check_argument_count(2, args)?; | ||
let result_type = TypeSignature::parse_type_repr(&args[0], checker)?; | ||
checker.type_check_expects(&args[1], context, &TypeSignature::max_buffer())?; | ||
TypeSignature::new_option(result_type).map_err(CheckError::from) | ||
} |
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
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.