Skip to content

Commit

Permalink
lib: bugfix serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Apr 9, 2024
1 parent 6e6e1d5 commit 4c9a1ce
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 26 deletions.
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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std = ["amplify/std"]
log = ["std"]
alloc = ["amplify/alloc"]
curve25519 = ["curve25519-dalek"]
serde = ["serde_crate", "amplify/serde", "std"]
serde = ["serde_crate", "amplify/serde", "std", "strict_encoding/serde"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2"
Expand Down
12 changes: 2 additions & 10 deletions src/isa/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,7 @@ impl InstructionSet for Secp256k1Op {

#[cfg(feature = "secp256k1")]
#[inline]
fn isa_ids() -> IsaSeg {
let mut set = IsaSeg::new();
set.insert(constants::ISA_ID_SECP256K);
set
}
fn isa_ids() -> IsaSeg { IsaSeg::with(constants::ISA_ID_SECP256K) }

fn src_regs(&self) -> BTreeSet<Reg> {
match self {
Expand Down Expand Up @@ -1580,11 +1576,7 @@ impl InstructionSet for Curve25519Op {

#[cfg(feature = "curve25519")]
#[inline]
fn isa_ids() -> IsaSeg {
let mut set = IsaSeg::new();
set.insert(constants::ISA_ID_ED25519);
set
}
fn isa_ids() -> IsaSeg { IsaSeg::with(constants::ISA_ID_ED25519) }

fn src_regs(&self) -> BTreeSet<Reg> {
match self {
Expand Down
6 changes: 3 additions & 3 deletions src/library/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

#![allow(missing_docs)]

pub const CODE_SEGMENT_MAX_LEN: usize = 1 << 16;
pub const CODE_SEGMENT_MAX_LEN: usize = 0xFFFF;

pub const DATA_SEGMENT_MAX_LEN: usize = 1 << 16;
pub const DATA_SEGMENT_MAX_LEN: usize = 0xFFFF;

/// Maximum number of libraries that may be referenced (used by) any other library; i.e. limit for
/// the number of records inside program segment.
pub const LIBS_SEGMENT_MAX_COUNT: usize = 1 << 8;
pub const LIBS_SEGMENT_MAX_COUNT: usize = 0xFF;

/// Maximum total number of libraries which may be used by a single program; i.e. maximal number of
/// nodes in a library dependency tree.
Expand Down
20 changes: 16 additions & 4 deletions src/library/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use amplify::confinement::SmallBlob;
use amplify::{confinement, ByteArray, Bytes32};
use baid58::{Baid58ParseError, FromBaid58, ToBaid58};
use sha2::{Digest, Sha256};
use strict_encoding::{StrictDeserialize, StrictSerialize};

#[cfg(feature = "ascii-armor")]
pub use self::_armor::LibArmorError;
Expand Down Expand Up @@ -137,6 +138,9 @@ pub struct Lib {
pub libs: LibSeg,
}

impl StrictSerialize for Lib {}
impl StrictDeserialize for Lib {}

impl Display for Lib {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
writeln!(f, "ISAE: {}", &self.isae)?;
Expand Down Expand Up @@ -174,10 +178,11 @@ impl RustHash for Lib {

#[cfg(feature = "ascii-armor")]
mod _armor {
use amplify::confinement::{self, Confined, U24 as U24MAX};
use armor::{ArmorHeader, ArmorParseError, AsciiArmor, ASCII_ARMOR_ID};
use strict_encoding::DeserializeError;

use super::*;
use crate::data::encoding::{Decode, DecodeError, Encode};

const ASCII_ARMOR_ISAE: &str = "ISA-Extensions";
const ASCII_ARMOR_DEPENDENCY: &str = "Dependency";
Expand All @@ -190,9 +195,13 @@ mod _armor {
#[from]
Armor(ArmorParseError),

/// The provided data exceed maximum possible library size.
#[from(confinement::Error)]
TooLarge,

/// Library data deserialization error.
#[from]
Decode(DecodeError),
Decode(DeserializeError),
}

impl AsciiArmor for Lib {
Expand All @@ -210,11 +219,14 @@ mod _armor {
headers
}

fn to_ascii_armored_data(&self) -> Vec<u8> { self.serialize() }
fn to_ascii_armored_data(&self) -> Vec<u8> {
self.to_strict_serialized::<U24MAX>().expect("type guarantees").to_vec()
}

fn with_headers_data(_headers: Vec<ArmorHeader>, data: Vec<u8>) -> Result<Self, Self::Err> {
// TODO: check id, dependencies and ISAE
let me = Self::deserialize(data)?;
let data = Confined::try_from(data)?;
let me = Self::from_strict_serialized::<U24MAX>(data)?;
Ok(me)
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@

//! Strict types library generator methods.

use core::convert::TryFrom;

use strict_types::typelib::{CompileError, LibBuilder};
use strict_types::TypeLib;

use crate::library::LibSite;
use crate::library::{Lib, LibSite};
use crate::LIB_NAME_ALUVM;

/// Strict type id for the library providing data types from this crate.
pub const LIB_ID_ALUVM: &str =
"urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy";
"urn:ubideco:stl:APYERRUMyWqLadwTv8tEFifHMPGpL3xGFSBxwaKYpmcV#square-mammal-uncle";

fn _aluvm_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_ALUVM), tiny_bset! {
strict_types::stl::std_stl().to_dependency(),
strict_types::stl::strict_types_stl().to_dependency()
})
.transpile::<LibSite>()
.transpile::<Lib>()
.compile()
}

Expand Down
17 changes: 13 additions & 4 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
-----BEGIN STRICT TYPE LIB-----
Id: urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy
Id: urn:ubideco:stl:APYERRUMyWqLadwTv8tEFifHMPGpL3xGFSBxwaKYpmcV#square-mammal-uncle
Name: AluVM
Checksum-SHA256: 225116c3d4fca2a386113db3fd9ffcd2952f3a5e7ea4f1391dd0a6fc139e97ec
Dependency: EcCNgrgLaygt3tCZNu2ZVEzMzSAZYEUeTNAVi5E81YWi#aspirin-mango-average
Checksum-SHA256: d0b37000d730beef023af7883b4664c10ecce81ab144c4ac0cc4a85687b96fac

1wm|eR!sl^0ssX}X<|ua1pxpD002NB00&HIVpC~!Wd;HRY-wTvr!Z9lE%{u?@QI^
EqCb}2Q7OO^w+`_q*ddTXmHSf)18{G10006
1wm|eR!srQEFN!zncXl9K5w2;FV{y1jDTJCC^p$-mHEbO0#qjhQ*>kj15<Ql0RSh
2(Tr;j#yqcI82>cBr>9x-Cs#sheE97?nsOaXHkb)PY;b5{Lt$`p1^@?1b74+lZDj
=k00;ugEFN!zncXl9K5w2;FV{y1jDTJCC^p$-mHEbO0#qj_gwc#^4#qsMUl{*1zN
e>I^CwqAYJB+ZKALhJOg5MT00000000080000000006NpoRSWoHEe00{wQLP%5x#
+Iwz1XmoMZUe8>*JiqdOAW_7Xq?evcVyE5000000000$0000000003Ole{U1O#bw
VPydUTVY%eB()|DVs5)j?WBU==a=KzyTg3px>h43uaSoYV{c?-00;m8KmY&$0000
00RR600000000d-VbYTDp002M$0000000030{{R3000004Y-wV10n*|aTPJY^=0y
A22XT><RaQoljqI}$|D0ODWau{Om<3E}Vo78L0RRU806-uB225#UQ)Oob0RRaBr!
Z9lE%{u?@QI^EqCb}2Q7OO^w+`_q*ddTXmHSf)0000000000{{R30000002TW;VQ
)zT%1_A?YX<`AVFjWFA`CQ2GiK9iLKbGE6DZmrA4)G`0A&^0p`%?-7aBp(}00I

-----END STRICT TYPE LIB-----

Binary file modified stl/[email protected]
Binary file not shown.
21 changes: 19 additions & 2 deletions stl/[email protected]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-
Id: urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy
Id: urn:ubideco:stl:APYERRUMyWqLadwTv8tEFifHMPGpL3xGFSBxwaKYpmcV#square-mammal-uncle
Name: AluVM
Version: 0.1.0
Description: AluVM data type library
Expand All @@ -11,11 +11,28 @@
@context
typelib AluVM

-- no dependencies
import Std#EcCNgrgLaygt3tCZNu2ZVEzMzSAZYEUeTNAVi5E81YWi#aspirin-mango-average
use AlphaCaps#digital-mirage-dream


@mnemonic(spirit-example-carrot)
data IsaName : [Std.AlphaCaps#digital-mirage-dream ^ 2..0x8]

@mnemonic(blonde-format-kansas)
data IsaSeg : {IsaName ^ ..0x40}

@mnemonic(volcano-daniel-ticket)
data Lib : isae IsaSeg
, code [Byte]
, data [Byte]
, libs LibSeg

@mnemonic(rebel-factor-rodeo)
data LibId : [Byte ^ 32]

@mnemonic(imitate-cargo-region)
data LibSeg : {LibId ^ ..0xff}

@mnemonic(organic-escort-ceramic)
data LibSite : lib LibId, pos U16

Expand Down

0 comments on commit 4c9a1ce

Please sign in to comment.