From e92e5b176886b3f04d8cfbb41714ab8f7d0b8046 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 17 Oct 2024 16:50:05 +0200 Subject: [PATCH] operation: test state serialization limits --- Cargo.lock | 6 +++--- Cargo.toml | 3 ++- src/operation/mod.rs | 5 ++++- src/operation/state.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78a384b9..357318d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -487,6 +487,7 @@ checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", "hashbrown", + "serde", ] [[package]] @@ -805,9 +806,8 @@ dependencies = [ [[package]] name = "strict_types" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f16e8855a575633815f01482ac927ebaca3d2485aec8e17226c6826de29154e" +version = "2.7.1" +source = "git+https://github.com/strict-types/strict-types?branch=develop#729a4f86d25dfcea15ed15bbeb1e027473401c58" dependencies = [ "amplify", "ascii-armor", diff --git a/Cargo.toml b/Cargo.toml index 9244fd34..5920369c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ amplify = "~4.7.0" baid64 = "~0.2.2" base64 = "0.22.1" strict_encoding = "~2.7.0" -strict_types = { version = "~2.7.0", features = ["armor"] } +strict_types = { version = "~2.7.1", features = ["armor"] } aluvm = { version = "~0.11.0-beta.9", features = ["std", "ascii-armor"] } commit_verify = { version = "~0.11.0-beta.9", features = ["derive"] } single_use_seals = "~0.11.0-beta.9" @@ -62,6 +62,7 @@ features = ["all"] [patch.crates-io] commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" } single_use_seals = { git = "https://github.com/LNP-BP/client_side_validation", branch = "develop" } +strict_types = { git = "https://github.com/strict-types/strict-types", branch = "develop" } aluvm = { git = "https://github.com/AluVM/rust-aluvm", branch = "develop" } bp-consensus = { git = "https://github.com/BP-WG/bp-core", branch = "develop" } bp-dbc = { git = "https://github.com/BP-WG/bp-core", branch = "develop" } diff --git a/src/operation/mod.rs b/src/operation/mod.rs index 5de5f87b..c25915f7 100644 --- a/src/operation/mod.rs +++ b/src/operation/mod.rs @@ -46,7 +46,10 @@ pub use seal::{ ExposedSeal, GenesisSeal, GraphSeal, OutputSeal, SecretSeal, TxoSeal, XGenesisSeal, XGraphSeal, XOutputSeal, }; -pub use state::{AttachId, State, StateCommitment, StateData, StateParseError}; +pub use state::{ + AttachId, State, StateCommitment, StateData, StateParseError, STATE_DATA_BASE32_ALPHABET, + STATE_DATA_MAX_LEN, +}; pub use xchain::{ AltLayer1, AltLayer1Set, Impossible, Layer1, XChain, XChainParseError, XOutpoint, XCHAIN_BITCOIN_PREFIX, XCHAIN_LIQUID_PREFIX, diff --git a/src/operation/state.rs b/src/operation/state.rs index 3cebaab1..43c0f681 100644 --- a/src/operation/state.rs +++ b/src/operation/state.rs @@ -25,7 +25,7 @@ use std::fmt; use std::fmt::{Display, Formatter}; use std::str::FromStr; -use amplify::confinement::{SmallBlob, U16 as U16MAX}; +use amplify::confinement::SmallBlob; use amplify::{confinement, ByteArray, Bytes32, Wrapper}; use baid64::{Baid64ParseError, DisplayBaid64, FromBaid64Str}; use base64::alphabet::Alphabet; @@ -37,6 +37,8 @@ use strict_encoding::{SerializeError, StrictDeserialize, StrictSerialize, Strict use crate::{impl_serde_baid64, LIB_NAME_RGB_COMMIT}; +pub const STATE_DATA_MAX_LEN: usize = confinement::U16; + // We put in the middle the least desirable characters to occur in typical numbers. pub const STATE_DATA_BASE32_ALPHABET: &str = "-abcdefghijklmnopqrstuvwxyz!#@&$ABCDEFGHIJKLMNOPQRSTUVWXYZ*~;:.,"; @@ -102,7 +104,9 @@ impl StateData { /// /// If the size of the serialized value exceeds 0xFFFF bytes. pub fn from_serialized(typed_data: &impl StrictSerialize) -> Result { - typed_data.to_strict_serialized::().map(Self) + typed_data + .to_strict_serialized::() + .map(Self) } pub fn from_checked(vec: Vec) -> Self { Self(SmallBlob::from_checked(vec)) } @@ -310,4 +314,22 @@ mod test { StateData::from_str(&(int * 10).to_string()).unwrap_err(); } } + + #[test] + fn state_data_limits() { + #[derive(Clone, Eq, PartialEq, Hash)] + #[derive(StrictType, StrictEncode, StrictDecode)] + #[strict_type(lib = "Test")] + struct MaxData(Box<[u8; STATE_DATA_MAX_LEN]>); + impl Default for MaxData { + fn default() -> Self { Self(Box::new([0xACu8; STATE_DATA_MAX_LEN])) } + } + impl StrictSerialize for MaxData {} + + let data = StateData::from_serialized(&MaxData::default()).unwrap(); + assert_eq!(data.len(), STATE_DATA_MAX_LEN); + for byte in data.0 { + assert_eq!(byte, 0xAC) + } + } }