-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3132 from stacks-network/feat/to-from-buff
Implement consensus de/serialization in Clarity
- Loading branch information
Showing
12 changed files
with
713 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.