Skip to content

Commit d8f7628

Browse files
committed
address PR reviews
1 parent 06a8b7b commit d8f7628

File tree

5 files changed

+84
-10
lines changed

5 files changed

+84
-10
lines changed

clarity/src/vm/analysis/read_only_checker/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ impl<'a, 'b> ReadOnlyChecker<'a, 'b> {
292292
}
293293
FromConsensusBuff => {
294294
// Check only the second+ arguments: the first argument is a type parameter
295+
check_argument_count(2, args)?;
295296
self.check_each_expression_is_read_only(&args[1..])
296297
}
297298
AtBlock => {

clarity/src/vm/analysis/type_checker/tests/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,54 @@ fn ascii_type(size: u32) -> TypeSignature {
7575
TypeSignature::SequenceType(StringType(ASCII(size.try_into().unwrap()))).into()
7676
}
7777

78+
#[test]
79+
fn test_from_consensus_buff() {
80+
let good = [
81+
("(from-consensus-buff int 0x00)", "(optional int)"),
82+
(
83+
"(from-consensus-buff { a: uint, b: principal } 0x00)",
84+
"(optional (tuple (a uint) (b principal)))",
85+
),
86+
];
87+
88+
let bad = [
89+
(
90+
"(from-consensus-buff)",
91+
CheckErrors::IncorrectArgumentCount(2, 0),
92+
),
93+
(
94+
"(from-consensus-buff 0x00 0x00 0x00)",
95+
CheckErrors::IncorrectArgumentCount(2, 3),
96+
),
97+
(
98+
"(from-consensus-buff 0x00 0x00)",
99+
CheckErrors::InvalidTypeDescription,
100+
),
101+
(
102+
"(from-consensus-buff int u6)",
103+
CheckErrors::TypeError(TypeSignature::max_buffer(), TypeSignature::UIntType),
104+
),
105+
(
106+
"(from-consensus-buff (buff 1048576) 0x00)",
107+
CheckErrors::ValueTooLarge,
108+
),
109+
];
110+
111+
for (good_test, expected) in good.iter() {
112+
let type_result = type_check_helper(good_test).unwrap();
113+
assert_eq!(expected, &type_result.to_string());
114+
115+
assert!(
116+
type_result.admits(&execute_v2(good_test).unwrap().unwrap()),
117+
"The analyzed type must admit the evaluated type"
118+
);
119+
}
120+
121+
for (bad_test, expected) in bad.iter() {
122+
assert_eq!(expected, &type_check_helper(&bad_test).unwrap_err().err);
123+
}
124+
}
125+
78126
#[test]
79127
fn test_to_consensus_buff() {
80128
let good = [
@@ -106,6 +154,11 @@ fn test_to_consensus_buff() {
106154
("(to-consensus-buff 0x00)", "(optional (buff 6))"),
107155
("(to-consensus-buff \"a\")", "(optional (buff 6))"),
108156
("(to-consensus-buff u\"ab\")", "(optional (buff 13))"),
157+
("(to-consensus-buff 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6)", "(optional (buff 151))"),
158+
("(to-consensus-buff 'STB44HYPYAT2BB2QE513NSP81HTMYWBJP02HPGK6.abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde)", "(optional (buff 151))"),
159+
("(to-consensus-buff true)", "(optional (buff 1))"),
160+
("(to-consensus-buff -1)", "(optional (buff 17))"),
161+
("(to-consensus-buff u1)", "(optional (buff 17))"),
109162
("(to-consensus-buff (list 1 2 3 4))", "(optional (buff 73))"),
110163
(
111164
"(to-consensus-buff { apple: u1, orange: 2, blue: true })",

clarity/src/vm/docs/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,8 @@ consensus buffer length based on the inferred type of the supplied value.
20002000
(to-consensus-buff true) ;; Returns (some 0x03)
20012001
(to-consensus-buff false) ;; Returns (some 0x04)
20022002
(to-consensus-buff none) ;; Returns (some 0x09)
2003+
(to-consensus-buff 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR) ;; Returns (some 0x051fa46ff88886c2ef9762d970b4d2c63678835bd39d)
2004+
(to-consensus-buff { abc: 3, def: 4 }) ;; Returns (some 0x0c00000002036162630000000000000000000000000000000003036465660000000000000000000000000000000004)
20032005
"#,
20042006
};
20052007

@@ -2020,6 +2022,8 @@ to deserialize the type, the method returns `none`.
20202022
(from-consensus-buff bool 0x0000000000000000000000000000000001) ;; Returns none
20212023
(from-consensus-buff bool 0x03) ;; Returns (some true)
20222024
(from-consensus-buff bool 0x04) ;; Returns (some false)
2025+
(from-consensus-buff principal 0x051fa46ff88886c2ef9762d970b4d2c63678835bd39d) ;; Returns (some SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
2026+
(from-consensus-buff { abc: int, def: int } 0x0c00000002036162630000000000000000000000000000000003036465660000000000000000000000000000000004) ;; Returns (some (tuple (abc 3) (def 4)))
20232027
"#,
20242028
};
20252029

clarity/src/vm/tests/simple_apply_eval.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,6 @@ fn test_from_consensus_buff_type_checks() {
322322
),
323323
];
324324

325-
for (input, expected) in vectors.iter() {
326-
let result = vm_execute_v2(input).expect_err("Should raise an error");
327-
eprintln!("{}", result.to_string());
328-
}
329-
330325
for (input, expected) in vectors.iter() {
331326
let result = vm_execute_v2(input).expect_err("Should raise an error");
332327
assert_eq!(&result.to_string(), expected);

clarity/src/vm/types/serialization.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ macro_rules! check_match {
294294
impl TypeSignature {
295295
/// Return the maximum length of the consensus serialization of a
296296
/// Clarity value of this type. The returned length *may* not fit
297-
/// in in a Clarity buffer! For example, the maximum serialized
297+
/// in a Clarity buffer! For example, the maximum serialized
298298
/// size of a `(buff 1024*1024)` is `1+1024*1024` because of the
299299
/// type prefix byte. However, that is 1 byte larger than the maximum
300300
/// buffer size in Clarity.
@@ -422,8 +422,7 @@ impl Value {
422422
r: &mut R,
423423
expected_type: Option<&TypeSignature>,
424424
) -> Result<Value, SerializationError> {
425-
let mut bound_reader = BoundReader::from_reader(r, BOUND_VALUE_SERIALIZATION_BYTES as u64);
426-
Value::inner_deserialize_read(&mut bound_reader, expected_type, 0)
425+
Self::deserialize_read_count(r, expected_type).map(|(value, _)| value)
427426
}
428427

429428
/// Deserialize just like `deserialize_read` but also
@@ -433,8 +432,30 @@ impl Value {
433432
expected_type: Option<&TypeSignature>,
434433
) -> Result<(Value, u64), SerializationError> {
435434
let mut bound_reader = BoundReader::from_reader(r, BOUND_VALUE_SERIALIZATION_BYTES as u64);
436-
Value::inner_deserialize_read(&mut bound_reader, expected_type, 0)
437-
.map(|value| (value, bound_reader.num_read()))
435+
let value = Value::inner_deserialize_read(&mut bound_reader, expected_type, 0)?;
436+
let bytes_read = bound_reader.num_read();
437+
if let Some(expected_type) = expected_type {
438+
let expect_size = match expected_type.max_serialized_size() {
439+
Ok(x) => x,
440+
Err(e) => {
441+
warn!(
442+
"Failed to determine max serialized size when checking expected_type argument";
443+
"err" => ?e
444+
);
445+
return Ok((value, bytes_read));
446+
}
447+
};
448+
449+
assert!(
450+
expect_size as u64 >= bytes_read,
451+
"Deserialized more bytes than expected size during deserialization. Expected size = {}, bytes read = {}, type = {}",
452+
expect_size,
453+
bytes_read,
454+
expected_type,
455+
);
456+
}
457+
458+
Ok((value, bytes_read))
438459
}
439460

440461
fn inner_deserialize_read<R: Read>(

0 commit comments

Comments
 (0)