Skip to content

Commit

Permalink
stl: move ContractState from consensus library to a dedicated lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 25, 2024
1 parent f57448b commit 304a191
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 357 deletions.
15 changes: 15 additions & 0 deletions src/bin/rgbcore-stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ fn main() {
)
.expect("unable to write to the file");

rgbcore::stl::rgb_state_stl()
.serialize(
format,
dir.as_ref(),
"0.1.0",
Some(
"
Description: Consensus contract state for RGB smart contracts
Author: Dr Maxim Orlovsky <[email protected]>
Copyright (C) 2023-2024 LNP/BP Standards Association. All rights reserved.
License: Apache-2.0",
),
)
.expect("unable to write to the file");

let std = std_stl();
let tx = bp_tx_stl();
let bp = bp_core_stl();
Expand Down
4 changes: 2 additions & 2 deletions src/contract/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use commit_verify::mpc::Commitment;
use commit_verify::{mpc, ConvolveVerifyError, EmbedVerifyError};
use strict_encoding::{StrictDeserialize, StrictDumb, StrictSerialize};

use crate::{WitnessOrd, XWitnessId, LIB_NAME_RGB};
use crate::{WitnessOrd, XWitnessId, LIB_NAME_RGB, LIB_NAME_RGB_STATE};

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
#[cfg_attr(
Expand Down Expand Up @@ -118,7 +118,7 @@ pub type EAnchor<P = mpc::MerkleProof> = dbc::Anchor<P, DbcProof>;
/// Txid and height information ordered according to the RGB consensus rules.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[strict_type(lib = LIB_NAME_RGB_STATE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down
8 changes: 3 additions & 5 deletions src/contract/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
Assign, AssignmentType, Assignments, AssignmentsRef, ContractId, DataState, ExposedSeal,
ExposedState, Extension, Genesis, GlobalStateType, OpId, Operation, RevealedAttach,
RevealedData, RevealedValue, Schema, SchemaId, Transition, TypedAssigns, VoidState,
WitnessAnchor, XChain, XOutputSeal, XWitnessId, LIB_NAME_RGB,
WitnessAnchor, XChain, XOutputSeal, XWitnessId, LIB_NAME_RGB, LIB_NAME_RGB_STATE,
};

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<State: KnownState> OutputAssignment<State> {

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[strict_type(lib = LIB_NAME_RGB_STATE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down Expand Up @@ -289,7 +289,7 @@ impl GlobalOrd {
/// To access the valid contract state use [`Contract`] APIs.
#[derive(Getters, Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[strict_type(lib = LIB_NAME_RGB_STATE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down Expand Up @@ -471,8 +471,6 @@ impl ContractHistory {
/// Contract state provides API to read consensus-valid data from the
/// [`ContractHistory`].
#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down
6 changes: 3 additions & 3 deletions src/contract/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use single_use_seals::SealWitness;
use strict_encoding::{StrictDecode, StrictDumb, StrictEncode, StrictType};

use crate::contract::xchain::Impossible;
use crate::{XChain, XOutpoint, LIB_NAME_RGB};
use crate::{XChain, XOutpoint, LIB_NAME_RGB_STATE};

pub type GenesisSeal = SingleBlindSeal<Method>;
pub type GraphSeal = ChainBlindSeal<Method>;
Expand Down Expand Up @@ -180,7 +180,7 @@ impl<U: ExposedSeal> XChain<U> {

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB)]
#[strict_type(lib = LIB_NAME_RGB_STATE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Ord for WitnessPos {
/// transaction defining the ordering of the contract state data.
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug, Display, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = order)]
#[strict_type(lib = LIB_NAME_RGB_STATE, tags = order)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub mod prelude {
pub use prelude::*;

pub const LIB_NAME_RGB: &str = "RGB";
pub const LIB_NAME_RGB_STATE: &str = "RGBState";

/// Fast-forward version code
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Default, Debug, Display)]
Expand Down
41 changes: 35 additions & 6 deletions src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ use strict_types::typelib::LibBuilder;
use strict_types::{CompileError, TypeLib};

use crate::{
ContractState, DbcProof, Extension, Genesis, OpCommitment, Schema, TransitionBundle,
XWitnessId, LIB_NAME_RGB,
ContractHistory, DbcProof, Extension, Genesis, OpCommitment, Schema, TransitionBundle,
XWitnessId, LIB_NAME_RGB, LIB_NAME_RGB_STATE,
};

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB: &str =
"stl:UL810YQe-Eux21B0-HyyLIsF-qPvkbnE-NeXaHtG-zn3sclI#poetic-tiger-scorpio";
"stl:DtU4CU7y-uIGqQFH-xs4K7Lw-dG5WL9E-RNK5oB5-IYQS$V4#diploma-montana-elastic";

/// Strict types id for the library providing data types for RGB contract state.
pub const LIB_ID_RGB_STATE: &str =
"stl:Xl48C7OQ-!diM0pe-di2Evap-FNClN8v-dwJlFVj-DDK!0Uo#icon-passage-bonanza";

fn _rgb_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB), tiny_bset! {
Expand All @@ -52,21 +56,46 @@ fn _rgb_core_stl() -> Result<TypeLib, CompileError> {
.transpile::<XWitnessId>()
.transpile::<TransitionBundle>()
.transpile::<Extension>()
.transpile::<ContractState>()
.transpile::<OpCommitment>()
.compile()
}

fn _rgb_state_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB_STATE), tiny_bset! {
std_stl().to_dependency(),
strict_types_stl().to_dependency(),
commit_verify_stl().to_dependency(),
bp_tx_stl().to_dependency(),
bp_core_stl().to_dependency(),
aluvm_stl().to_dependency(),
rgb_core_stl().to_dependency()
})
.transpile::<ContractHistory>()
.compile()
}

/// Generates strict type library providing data types for RGB consensus.
pub fn rgb_core_stl() -> TypeLib { _rgb_core_stl().expect("invalid strict type RGB library") }
pub fn rgb_core_stl() -> TypeLib { _rgb_core_stl().expect("invalid strict type RGB Core library") }

/// Generates strict type library providing data types for RGB contract
/// consensus state.
pub fn rgb_state_stl() -> TypeLib {
_rgb_state_stl().expect("invalid strict type RGB State library")
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn lib_id() {
fn core_lib_id() {
let lib = rgb_core_stl();
assert_eq!(lib.id().to_string(), LIB_ID_RGB);
}

#[test]
fn state_lib_id() {
let lib = rgb_state_stl();
assert_eq!(lib.id().to_string(), LIB_ID_RGB_STATE);
}
}
Loading

0 comments on commit 304a191

Please sign in to comment.