Skip to content

Commit

Permalink
operation: test state serialization limits
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 17, 2024
1 parent b59f1ca commit e92e5b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down
5 changes: 4 additions & 1 deletion src/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 24 additions & 2 deletions src/operation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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*~;:.,";
Expand Down Expand Up @@ -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<Self, SerializeError> {
typed_data.to_strict_serialized::<U16MAX>().map(Self)
typed_data
.to_strict_serialized::<STATE_DATA_MAX_LEN>()
.map(Self)
}

pub fn from_checked(vec: Vec<u8>) -> Self { Self(SmallBlob::from_checked(vec)) }

Check warning on line 112 in src/operation/state.rs

View check run for this annotation

Codecov / codecov/patch

src/operation/state.rs#L112

Added line #L112 was not covered by tests
Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit e92e5b1

Please sign in to comment.