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 bc455d7
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 64 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
195 changes: 139 additions & 56 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 @@ -203,26 +202,44 @@ impl ContractBuilder {
self.builder.valency_name(type_id)
}

#[inline]
pub fn add_metadata(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: StrictVal,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_metadata(name, value)?;
Ok(self)
}

#[inline]
pub fn serialize_metadata(
mut self,
name: impl Into<FieldName>,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.serialize_metadata(name, value)?;
Ok(self)
}

pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: StrictVal,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_global_state(name, value)?;
Ok(self)
}

#[inline]
pub fn serialize_global_state(
mut self,
name: impl Into<FieldName>,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.serialize_global_state(name, value)?;
Ok(self)
}

pub fn add_owned_state_raw(
mut self,
type_id: AssignmentType,
Expand Down Expand Up @@ -400,39 +417,31 @@ impl TransitionBuilder {

pub fn transition_type(&self) -> TransitionType { self.transition_type }

pub fn set_nonce(mut self, nonce: u64) -> Self {
self.nonce = nonce;
self
#[inline]
pub fn global_type(&self, name: impl Into<FieldName>) -> Result<GlobalStateType, BuilderError> {
self.builder.global_type(name)
}

#[inline]
pub fn add_metadata(
mut self,
pub fn assignments_type(
&self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_metadata(name, value)?;
Ok(self)
) -> Result<AssignmentType, BuilderError> {
self.builder.assignments_type(name)
}

#[inline]
pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_global_state(name, value)?;
Ok(self)
pub fn valency_type(&self, name: impl Into<FieldName>) -> Result<ValencyType, BuilderError> {
self.builder.valency_type(name)
}

pub fn add_input(mut self, opout: Opout, state: State) -> Result<Self, BuilderError> {
if let Some(calc) = &mut self.calc {
calc.reg_input(opout.ty, &state)?;
}
self.inputs.insert(Input::with(opout), state)?;
Ok(self)
#[inline]
pub fn valency_name(&self, type_id: ValencyType) -> &FieldName {
self.builder.valency_name(type_id)
}

pub fn meta_name(&self, type_id: MetaType) -> &FieldName { self.builder.meta_name(type_id) }

pub fn default_assignment(&self) -> Result<&FieldName, BuilderError> {
self.builder
.transition_iface(self.transition_type)
Expand All @@ -441,31 +450,57 @@ impl TransitionBuilder {
.ok_or(BuilderError::NoDefaultAssignment)
}

#[inline]
pub fn assignments_type(
&self,
pub fn set_nonce(mut self, nonce: u64) -> Self {
self.nonce = nonce;
self
}

pub fn add_input(mut self, opout: Opout, state: State) -> Result<Self, BuilderError> {
if let Some(calc) = &mut self.calc {
calc.reg_input(opout.ty, &state)?;
}
self.inputs.insert(Input::with(opout), state)?;
Ok(self)
}

pub fn add_metadata(
mut self,
name: impl Into<FieldName>,
) -> Result<AssignmentType, BuilderError> {
self.builder.assignments_type(name)
value: StrictVal,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_metadata(name, value)?;
Ok(self)
}

#[inline]
pub fn global_type(&self, name: impl Into<FieldName>) -> Result<GlobalStateType, BuilderError> {
self.builder.global_type(name)
pub fn serialize_metadata(
mut self,
name: impl Into<FieldName>,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.serialize_metadata(name, value)?;
Ok(self)
}

#[inline]
pub fn valency_type(&self, name: impl Into<FieldName>) -> Result<ValencyType, BuilderError> {
self.builder.valency_type(name)
pub fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: StrictVal,
) -> Result<Self, BuilderError> {
self.builder = self.builder.add_global_state(name, value)?;
Ok(self)
}

#[inline]
pub fn valency_name(&self, type_id: ValencyType) -> &FieldName {
self.builder.valency_name(type_id)
pub fn serialize_global_state(
mut self,
name: impl Into<FieldName>,
value: &impl StrictSerialize,
) -> Result<Self, BuilderError> {
self.builder = self.builder.serialize_global_state(name, value)?;
Ok(self)
}

pub fn meta_name(&self, type_id: MetaType) -> &FieldName { self.builder.meta_name(type_id) }

// TODO: We won't need this once we will have Blank Transition builder
/// NB: This does not process the state with VM
pub fn add_owned_state_blank(
Expand Down Expand Up @@ -610,8 +645,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 @@ -625,29 +661,78 @@ impl<Seal: ExposedSeal> OperationBuilder<Seal> {
.expect("schema should match interface: must be checked by the constructor")
}

pub fn add_metadata(
fn add_metadata(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: StrictVal,
) -> Result<Self, BuilderError> {
let type_id = self.meta_type(name)?;

let types = self.type_system();
let sem_id = *self
.schema
.meta_types
.get(&type_id)
.expect("schema-interface inconsistency");
let value = types.typify(value, sem_id)?;
let data = types.strict_serialize_value::<STATE_DATA_MAX_LEN>(&value)?;

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

fn serialize_metadata(
mut self,
name: impl Into<FieldName>,
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(
fn add_global_state(
mut self,
name: impl Into<FieldName>,
value: impl StrictSerialize,
value: StrictVal,
) -> Result<Self, BuilderError> {
let type_id = self.global_type(name)?;

let types = self.type_system();
let sem_id = self
.schema
.global_types
.get(&type_id)
.expect("schema-interface inconsistency")
.sem_id;
let value = types.typify(value, sem_id)?;
let data = types.strict_serialize_value::<STATE_DATA_MAX_LEN>(&value)?;

self.global.add_state(type_id, data.into())?;
Ok(self)
}

fn serialize_global_state(
mut self,
name: impl Into<FieldName>,
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 +772,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 bc455d7

Please sign in to comment.