Skip to content

Commit

Permalink
Automatic project update
Browse files Browse the repository at this point in the history
  • Loading branch information
tonjen committed Jan 27, 2022
1 parent ed63292 commit 4067496
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 27 deletions.
2 changes: 1 addition & 1 deletion commit_hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
44cb7e493735f425c1191e5df6239a4a6fd53d26
f36ed7353e15dbc472170b28c9a63fca6a64f3a1
2 changes: 1 addition & 1 deletion deps_map.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ton-block":"20dd6ecb7dec5163d7316fdacd9bb2876008a8c7","ton-types":"164867fa842b1efd0eeb378e727a17677ae8a322"}
{"ton-block":"4a3bf9a65e3bb578ad5833003d6c8978888199e3","ton-types":"007da5416f1508fdd5fd23be8099a5e0f06f564e"}
7 changes: 5 additions & 2 deletions ton_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = [ 'Aaron Gallagher <[email protected]>', 'Connie Hilarides <[email protected]
description = 'Minimal wrappers for TON serialization using TL-schema'
edition = '2018'
name = 'ton_api'
version = '0.2.117'
version = '0.2.120'

[dependencies]
byteorder = '1.4'
Expand All @@ -15,11 +15,14 @@ ordered-float = '2.8'
secstr = '0.4'
serde = '1.0'
serde_derive = '1.0'
ton_block = { git = 'https://github.com/tonlabs/ton-labs-block', tag = '1.7.36' }
ton_block = { git = 'https://github.com/tonlabs/ton-labs-block', tag = '1.7.37' }
ton_types = { git = 'https://github.com/tonlabs/ton-labs-types', tag = '1.10.12' }

[build-dependencies]
failure = '0.1.7'
serde_json = '1.0.52'
ton_tl_codegen = { path = '../ton_tl_codegen' }

[dev-dependencies]
base64 = '0.13'

92 changes: 91 additions & 1 deletion ton_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#![allow(clippy::unreadable_literal)]
#![deny(private_in_public)]

use crate::ton_prelude::TLObject;
use failure::Fail;
use std::{any::Any, fmt, hash::Hash, io::{self, Read, Write}};

use ton_block::{BlockIdExt, ShardIdent};
use ton_types::Result;
use ton_types::UInt256;
use ton_types::{fail, UInt256};

macro_rules! _invalid_id {
($id:ident) => {
Expand Down Expand Up @@ -325,3 +326,92 @@ impl BareSerialize for UInt256 {
Ok(())
}
}

// Deserialize boxed TL object from bytes
pub fn deserialize_boxed(bytes: &[u8]) -> Result<TLObject> {
let mut reader = bytes;
Deserializer::new(&mut reader).read_boxed::<TLObject>()
}

/// Deserialize bundle of boxed TL objects from bytes
pub fn deserialize_boxed_bundle(bytes: &[u8]) -> Result<Vec<TLObject>> {
let mut reader = bytes;
let mut de = Deserializer::new(&mut reader);
let mut ret = Vec::new();
loop {
match de.read_boxed::<TLObject>() {
Ok(object) => ret.push(object),
Err(_) => if ret.is_empty() {
fail!("Deserialization error")
} else {
break
}
}
}
Ok(ret)
}

/// Serialize boxed TL object into bytes
pub fn serialize_boxed<T: BoxedSerialize>(object: &T) -> Result<Vec<u8>> {
let mut ret = Vec::new();
Serializer::new(&mut ret).write_boxed(object)?;
Ok(ret)
}

/// Serialize boxed TL object into bytes with appending
pub fn serialize_boxed_append<T: BoxedSerialize>(buf: &mut Vec<u8>, object: &T) -> Result<()> {
Serializer::new(buf).write_boxed(object)?;
Ok(())
}

/// Serialize boxed TL object into bytes in-place
pub fn serialize_boxed_inplace<T: BoxedSerialize>(buf: &mut Vec<u8>, object: &T) -> Result<()> {
buf.truncate(0);
serialize_boxed_append(buf, object)
}

/// Serialize non-boxed TL object into bytes
pub fn serialize_bare<T: BareSerialize>(object: &T) -> Result<Vec<u8>> {
let mut buf = Vec::new();
Serializer::new(&mut buf).write_into_boxed(object)?;
Ok(buf)
}

/// Serialize non-boxed TL object into bytes in-place
pub fn serialize_bare_inplace<T: BareSerialize>(buf: &mut Vec<u8>, object: &T) -> Result<()> {
buf.truncate(0);
Serializer::new(buf).write_into_boxed(object)
}

/// Get TL tag from non-boxed object
pub fn tag_from_bare_object<T: BareSerialize>(object: &T) -> u32 {
let ConstructorNumber(tag) = object.constructor();
tag
}

/// Get TL tag from non-boxed type
pub fn tag_from_bare_type<T: Default + IntoBoxed>() -> u32 {
let (ConstructorNumber(tag), _) = T::default().into_boxed().serialize_boxed();
tag
}

/// Get TL tag from boxed object
pub fn tag_from_boxed_object<T: BoxedSerialize>(object: &T) -> u32 {
let (ConstructorNumber(tag), _) = object.serialize_boxed();
tag
}

/// Get TL tag from boxed type
pub fn tag_from_boxed_type<T: Default + BoxedSerialize>() -> u32 {
let (ConstructorNumber(tag), _) = T::default().serialize_boxed();
tag
}

/// Get TL tag from data bytes
pub fn tag_from_data(data: &[u8]) -> u32 {
if data.len() < 4 {
0
} else {
u32::from_le_bytes([data[0], data[1], data[2], data[3]])
}
}
26 changes: 17 additions & 9 deletions ton_api/src/ton_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@

#![allow(non_camel_case_types)]

use std::fmt;
use std::hash::{Hash, Hasher};
use std::io::{Read, Write};
use std::marker::PhantomData;
use crate::{
AnyBoxedSerialize, BareDeserialize, BareSerialize, BoxedDeserialize, BoxedSerialize,
ConstructorNumber, Deserializer, Result, Serializer, ton::Bool
};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use extfmt::Hexlify;
use ordered_float::OrderedFloat;
use serde_derive::{Deserialize, Serialize};

use crate::{AnyBoxedSerialize, BareDeserialize, BareSerialize, BoxedDeserialize, BoxedSerialize, ConstructorNumber, Deserializer, Result, Serializer};
use crate::ton::Bool;
use std::{any::type_name, fmt, hash::{Hash, Hasher}, io::{Read, Write}, marker::PhantomData};
use ton_types::error;

const MAX_BYTES_DEBUG_LEN: usize = 4;

Expand Down Expand Up @@ -370,7 +369,11 @@ macro_rules! impl_vector {
{
fn deserialize_bare(de: &mut Deserializer) -> Result<Self> {
let count = de.read_i32::<LittleEndian>()?;
let mut ret = Vec::with_capacity(count as usize);
let mut ret = Vec::new();
ret.try_reserve_exact(count as usize)
.map_err(
|e| error!("count {} is too big for {}: {}", count, type_name::<Self>(), e)
)?;
for _ in 0..count {
ret.push(de.$read_method()?);
}
Expand Down Expand Up @@ -425,7 +428,12 @@ impl BareDeserialize for Vec<u8> {
(de.read_u24::<LittleEndian>()? as usize, 4)
};

let mut buf = vec![0; len];
let mut buf = Vec::new();
buf.try_reserve_exact(len)
.map_err(
|e| error!("count {} is too big for {}: {}", len, type_name::<Self>(), e)
)?;
buf.resize(len, 0);
de.read_exact(&mut buf)?;
have_read += len;
let remainder = have_read % 4;
Expand Down
3 changes: 2 additions & 1 deletion ton_api/tl/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"exclude_types": ["TestObject", "testObject", "testInt", "testString", "testVectorBytes", "getTestObject"],
"need_box": ["ConfirmValidation"],
"need_determiner": [],
"replace_with_bytes": [],
"additional_derives": {
"BlockIdExt": ["Hash", "Serialize", "Deserialize"]
"BlockIdExt": ["Serialize", "Deserialize"]
}
}
17 changes: 5 additions & 12 deletions ton_tl_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ pub struct Config {
exclude_types: HashSet<String>,
need_box: HashSet<String>,
need_determiner: HashSet<String>,
replace_with_bytes: HashSet<String>,
additional_derives: HashMap<String, Vec<String>>,
}

Expand Down Expand Up @@ -1319,18 +1320,10 @@ impl Constructor<Type, Field> {
}

fn resolved_fields(&self, config: &Option<Config>, resolve_map: &TypeResolutionMap) -> Vec<FieldIR> {
let replace_string_with_bytes = match self.original_variant.as_str() {
// types
"resPQ" |
"p_q_inner_data" |
"p_q_inner_data_temp" |
"server_DH_params_ok" |
"server_DH_inner_data" |
"client_DH_inner_data" |
// functions
"req_DH_params" |
"set_client_DH_params" => true,
_ => false,
let replace_string_with_bytes = if let Some(config) = config {
config.replace_with_bytes.contains(&self.original_variant)
} else {
false
};

let mut ret: Vec<_> = self.fields.iter()
Expand Down

0 comments on commit 4067496

Please sign in to comment.