Skip to content

Commit

Permalink
iface: use updated state serialization APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 17, 2024
1 parent 1a1ad23 commit 2d0c5a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
5 changes: 2 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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
44 changes: 25 additions & 19 deletions src/interface/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ use chrono::Utc;
use rgb::validation::Scripts;
use rgb::{
validation, AltLayer1, AltLayer1Set, AssignmentType, Assignments, AttachId, ContractId,
ExposedSeal, Genesis, GenesisSeal, GlobalState, GraphSeal, Identity, Input, Layer1,
MetadataError, Opout, Schema, State, StateData, Transition, TransitionType, TypedAssigns,
XChain, XOutpoint,
ExposedSeal, Genesis, GenesisSeal, GlobalState, GlobalStateSchema, GlobalStateType, GraphSeal,
Identity, Input, Layer1, MetaType, Metadata, MetadataError, Opout, Schema, State, StateData,
Transition, TransitionType, TypedAssigns, ValencyType, XChain, XOutpoint, STATE_DATA_MAX_LEN,
};
use rgbcore::{GlobalStateSchema, GlobalStateType, MetaType, Metadata, ValencyType};
use strict_encoding::{FieldName, SerializeError, StrictSerialize};
use strict_types::{decode, typify, SemId, StrictVal, TypeSystem};

Expand Down Expand Up @@ -207,7 +206,7 @@ impl ContractBuilder {
pub fn add_metadata(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_metadata(name, value)?;
Ok(self)
Expand All @@ -217,7 +216,7 @@ impl ContractBuilder {
pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_global_state(name, value)?;
Ok(self)
Expand Down Expand Up @@ -409,7 +408,7 @@ impl TransitionBuilder {
pub fn add_metadata(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_metadata(name, value)?;
Ok(self)
Expand All @@ -419,7 +418,7 @@ impl TransitionBuilder {
pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_global_state(name, value)?;
Ok(self)
Expand Down Expand Up @@ -610,8 +609,9 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
}

#[inline]
fn meta_schema(&self, type_id: MetaType) -> &SemId {
self.schema
fn meta_schema(&self, type_id: MetaType) -> SemId {
*self
.schema
.meta_types
.get(&type_id)
.expect("schema should match interface: must be checked by the constructor")
Expand All @@ -628,26 +628,34 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
pub fn add_metadata(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
let type_id = self.meta_type(name)?;
let serialized = value.to_strict_serialized::<{ u16::MAX as usize }>()?;
let sem_id = self.meta_schema(type_id);
self.types.strict_deserialize_type(*sem_id, &serialized)?;

#[cfg(debug_assertions)]
self.types
.strict_deserialize_type(sem_id, &serialized)
.expect("failed deserialization");

self.meta.add_value(type_id, serialized.into())?;
Ok(self)
}

pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
let type_id = self.global_type(name)?;
let serialized = value.to_strict_serialized::<{ u16::MAX as usize }>()?;
// Check value matches type requirements
let sem_id = self.global_schema(type_id).sem_id;
self.types.strict_deserialize_type(sem_id, &serialized)?;

#[cfg(debug_assertions)]
self.types
.strict_deserialize_type(sem_id, &serialized)
.expect("failed deserialization");

self.global.add_state(type_id, serialized.into())?;

Expand Down Expand Up @@ -687,12 +695,10 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
.schema
.owned_types
.get(&type_id)
.expect("schema-interface inconsistence")
.expect("schema-interface inconsistency")
.sem_id;
let value = types.typify(value, sem_id)?;
let data = types
.strict_serialize_type::<{ confinement::U16 }>(&value)?
.to_strict_serialized()?;
let data = types.strict_serialize_value::<STATE_DATA_MAX_LEN>(&value)?;

let mut state = State::from(StateData::from(data));
state.attach = attach;
Expand Down
8 changes: 3 additions & 5 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
use std::borrow::Borrow;
use std::collections::{BTreeSet, HashMap, HashSet};

use amplify::confinement;
use rgb::validation::Scripts;
use rgb::{
AssignmentType, AttachId, ContractId, OpId, Opout, Schema, State, StateData, XOutputSeal,
XWitnessId,
XWitnessId, STATE_DATA_MAX_LEN,
};
use strict_encoding::{FieldName, SerializeError, StrictSerialize};
use strict_encoding::{FieldName, SerializeError};
use strict_types::{typify, SemId, StrictVal, TypeSystem};

use crate::contract::{Allocation, WitnessInfo};
Expand Down Expand Up @@ -189,8 +188,7 @@ impl<S: ContractStateRead> ContractIface<S> {
let t = self.types.typify(value, self.assignment_sem_id(ty))?;
let value = self
.types
.strict_serialize_type::<{ confinement::U16 }>(&t)?
.to_strict_serialized()?;
.strict_serialize_value::<STATE_DATA_MAX_LEN>(&t)?;
Ok(value.into())
}

Expand Down

0 comments on commit 2d0c5a1

Please sign in to comment.