Skip to content
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

feat(tariscript)!: use varints in tari script (de)serialization #4460

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions infrastructure/tari_script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ tari_utilities = { git = "https://github.com/tari-project/tari_utilities.git", t

blake2 = "0.9"
digest = "0.9.0"
integer-encoding = "3.0.2"
serde = "1.0.136"
sha2 = "0.9"
sha3 = "0.9"
Expand Down
51 changes: 24 additions & 27 deletions infrastructure/tari_script/src/op_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use std::{fmt, ops::Deref};

use integer_encoding::VarInt;
use tari_crypto::ristretto::RistrettoPublicKey;
use tari_utilities::{hex::Hex, ByteArray, ByteArrayError};

Expand Down Expand Up @@ -291,18 +292,12 @@ impl Opcode {
use Opcode::*;
match *code {
OP_CHECK_HEIGHT_VERIFY => {
if bytes.len() < 9 {
return Err(ScriptError::InvalidData);
}
let height = slice_to_u64(&bytes[1..9]);
Ok((CheckHeightVerify(height), &bytes[9..]))
let (height, size) = u64::decode_var(&bytes[1..]).ok_or(ScriptError::InvalidData)?;
Ok((CheckHeightVerify(height), &bytes[size + 1..]))
},
OP_CHECK_HEIGHT => {
if bytes.len() < 9 {
return Err(ScriptError::InvalidData);
}
let height = slice_to_u64(&bytes[1..9]);
Ok((CheckHeight(height), &bytes[9..]))
let (height, size) = u64::decode_var(&bytes[1..]).ok_or(ScriptError::InvalidData)?;
Ok((CheckHeight(height), &bytes[size + 1..]))
},
OP_COMPARE_HEIGHT_VERIFY => Ok((CompareHeightVerify, &bytes[1..])),
OP_COMPARE_HEIGHT => Ok((CompareHeight, &bytes[1..])),
Expand All @@ -317,11 +312,8 @@ impl Opcode {
Ok((PushHash(hash), &bytes[33..]))
},
OP_PUSH_INT => {
if bytes.len() < 9 {
return Err(ScriptError::InvalidData);
}
let n = slice_to_i64(&bytes[1..9]);
Ok((PushInt(n), &bytes[9..]))
let (n, size) = i64::decode_var(&bytes[1..]).ok_or(ScriptError::InvalidData)?;
Ok((PushInt(n), &bytes[size + 1..]))
},
OP_PUSH_PUBKEY => {
if bytes.len() < 33 {
Expand Down Expand Up @@ -415,11 +407,15 @@ impl Opcode {
match self {
CheckHeightVerify(height) => {
array.push(OP_CHECK_HEIGHT_VERIFY);
array.extend_from_slice(&height.to_le_bytes());
let mut buf = [0 as u8; 10];
let used = height.encode_var(&mut buf[..]);
array.extend_from_slice(&buf[0..used]);
},
CheckHeight(height) => {
array.push(OP_CHECK_HEIGHT);
array.extend_from_slice(&height.to_le_bytes());
let mut buf = [0 as u8; 10];
let used = height.encode_var(&mut buf[..]);
array.extend_from_slice(&buf[0..used]);
},
CompareHeightVerify => array.push(OP_COMPARE_HEIGHT_VERIFY),
CompareHeight => array.push(OP_COMPARE_HEIGHT),
Expand All @@ -432,7 +428,9 @@ impl Opcode {
},
PushInt(n) => {
array.push(OP_PUSH_INT);
array.extend_from_slice(&n.to_le_bytes());
let mut buf = [0 as u8; 10];
let used = n.encode_var(&mut buf[..]);
array.extend_from_slice(&buf[0..used]);
},
PushPubKey(p) => {
array.push(OP_PUSH_PUBKEY);
Expand Down Expand Up @@ -623,18 +621,15 @@ mod test {
fn check_height() {
fn test_check_height(op: &Opcode, val: u8, display: &str) {
// Serialize
assert!(matches!(
Opcode::read_next(&[val, 1, 2, 3]),
Err(ScriptError::InvalidData)
));
let s = &[val, 63, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3];
assert!(matches!(Opcode::read_next(&[val, 255]), Err(ScriptError::InvalidData)));
let s = &[val, 63, 1, 2, 3];
let (opcode, rem) = Opcode::read_next(s).unwrap();
assert_eq!(opcode, *op);
assert_eq!(rem, &[1, 2, 3]);
// Deserialise
let mut arr = vec![1, 2, 3];
op.to_bytes(&mut arr);
assert_eq!(&arr, &[1, 2, 3, val, 63, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(&arr, &[1, 2, 3, val, 63]);
// Format
assert_eq!(format!("{}", op).as_str(), display);
}
Expand All @@ -645,16 +640,18 @@ mod test {
#[test]
fn push_int() {
// Serialise
assert!(matches!(Opcode::read_next(b"\x7dshort"), Err(ScriptError::InvalidData)));
let s = &[OP_PUSH_INT, 1, 1, 0, 0, 0, 0, 0, 0];
assert!(matches!(Opcode::read_next(&[0x7d, 255]), Err(ScriptError::InvalidData)));
let s = &[OP_PUSH_INT, 130, 4];
let (opcode, rem) = Opcode::read_next(s).unwrap();
let mut arr = vec![];
Opcode::PushInt(257).to_bytes(&mut arr);
assert!(matches!(opcode, Opcode::PushInt(257)));
assert!(rem.is_empty());
// Deserialise
let op = Opcode::PushInt(257);
let mut arr = vec![];
op.to_bytes(&mut arr);
assert_eq!(&arr, &[OP_PUSH_INT, 1, 1, 0, 0, 0, 0, 0, 0]);
assert_eq!(&arr, &[OP_PUSH_INT, 130, 4]);
// Format
assert_eq!(format!("{}", op).as_str(), "PushInt(257)");
}
Expand Down