Skip to content

Commit

Permalink
validation: refactor scripting system
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Mar 27, 2024
1 parent 3b089b8 commit 86a2980
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 113 deletions.
6 changes: 3 additions & 3 deletions src/contract/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ impl Genesis {
redeemed: Redeemed::default().commit_id(),
valencies: self.valencies.commit_id(),
witness: MerkleHash::void(0, u256::ZERO),
script: self.script.commit_id(),
script: self.validator.commit_id(),

Check warning on line 303 in src/contract/commit.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/commit.rs#L303

Added line #L303 was not covered by tests
}
}

Expand All @@ -319,7 +319,7 @@ impl Transition {
redeemed: Redeemed::default().commit_id(),
valencies: self.valencies.commit_id(),
witness: MerkleHash::void(0, u256::ZERO),
script: self.script.commit_id(),
script: self.validator.commit_id(),

Check warning on line 322 in src/contract/commit.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/commit.rs#L322

Added line #L322 was not covered by tests
}
}
}
Expand All @@ -336,7 +336,7 @@ impl Extension {
redeemed: self.redeemed.commit_id(),
valencies: self.valencies.commit_id(),
witness: MerkleHash::void(0, u256::ZERO),
script: self.script.commit_id(),
script: self.validator.commit_id(),

Check warning on line 339 in src/contract/commit.rs

View check run for this annotation

Codecov / codecov/patch

src/contract/commit.rs#L339

Added line #L339 was not covered by tests
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/contract/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub struct Genesis {
pub assignments: Assignments<GenesisSeal>,
pub valencies: Valencies,
pub issuer: Issuer,
pub script: ReservedBytes<1, 0>,
pub validator: ReservedBytes<1, 0>,
}

impl StrictSerialize for Genesis {}
Expand All @@ -345,8 +345,8 @@ pub struct Extension {
pub assignments: Assignments<GenesisSeal>,
pub redeemed: Redeemed,
pub valencies: Valencies,
pub witness: ReservedBytes<1, 0>,
pub script: ReservedBytes<1, 0>,
pub validator: ReservedBytes<1, 0>,
pub witness: ReservedBytes<2, 0>,
}

impl StrictSerialize for Extension {}
Expand Down Expand Up @@ -377,8 +377,8 @@ pub struct Transition {
pub inputs: Inputs,
pub assignments: Assignments<GraphSeal>,
pub valencies: Valencies,
pub witness: ReservedBytes<1, 0>,
pub script: ReservedBytes<1, 0>,
pub validator: ReservedBytes<1, 0>,
pub witness: ReservedBytes<2, 0>,
}

impl StrictSerialize for Transition {}
Expand Down
4 changes: 2 additions & 2 deletions src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ pub use operations::{
OpSchema, OpType, TransitionSchema, ValencySchema, ValencyType,
};
pub use schema::{ExtensionType, GlobalStateType, Schema, SchemaId, TransitionType};
pub use script::{Script, VmType};
pub use state::{FungibleType, GlobalStateSchema, MediaType, StateSchema};
pub use script::ScriptRef;
pub use state::{FungibleType, GlobalStateSchema, MediaType, OwnedStateSchema};
5 changes: 4 additions & 1 deletion src/schema/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use amplify::confinement::{TinyOrdMap, TinyOrdSet};
use amplify::Wrapper;
use strict_types::SemId;

use super::{ExtensionType, GlobalStateType, Occurrences, TransitionType};
use super::{ExtensionType, GlobalStateType, Occurrences, ScriptRef, TransitionType};
use crate::LIB_NAME_RGB;

#[derive(Wrapper, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From, Display)]
Expand Down Expand Up @@ -152,6 +152,7 @@ pub struct GenesisSchema {
pub globals: GlobalSchema,
pub assignments: AssignmentsSchema,
pub valencies: ValencySchema,
pub validator: ScriptRef,
}

#[derive(Clone, PartialEq, Eq, Debug, Default)]
Expand All @@ -168,6 +169,7 @@ pub struct ExtensionSchema {
pub redeems: ValencySchema,
pub assignments: AssignmentsSchema,
pub valencies: ValencySchema,
pub validator: ScriptRef,
}

#[derive(Clone, PartialEq, Eq, Debug, Default)]
Expand All @@ -184,6 +186,7 @@ pub struct TransitionSchema {
pub inputs: InputsSchema,
pub assignments: AssignmentsSchema,
pub valencies: ValencySchema,
pub validator: ScriptRef,
}

impl OpSchema for GenesisSchema {
Expand Down
10 changes: 2 additions & 8 deletions src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ use commit_verify::{
use strict_encoding::{StrictDecode, StrictDeserialize, StrictEncode, StrictSerialize, StrictType};

use super::{
AssignmentType, ExtensionSchema, GenesisSchema, Script, StateSchema, TransitionSchema,
ValencyType,
AssignmentType, ExtensionSchema, GenesisSchema, OwnedStateSchema, TransitionSchema, ValencyType,
};
use crate::{Ffv, GlobalStateSchema, Occurrences, LIB_NAME_RGB};

Expand Down Expand Up @@ -156,14 +155,11 @@ pub struct Schema {
pub flags: ReservedBytes<1, 0>,

pub global_types: TinyOrdMap<GlobalStateType, GlobalStateSchema>,
pub owned_types: TinyOrdMap<AssignmentType, StateSchema>,
pub owned_types: TinyOrdMap<AssignmentType, OwnedStateSchema>,
pub valency_types: TinyOrdSet<ValencyType>,
pub genesis: GenesisSchema,
pub extensions: TinyOrdMap<ExtensionType, ExtensionSchema>,
pub transitions: TinyOrdMap<TransitionType, TransitionSchema>,

/// Validation code.
pub script: Script,
}

impl CommitEncode for Schema {
Expand All @@ -179,8 +175,6 @@ impl CommitEncode for Schema {
e.commit_to_serialized(&self.genesis);
e.commit_to_map(&self.extensions);
e.commit_to_map(&self.transitions);

e.commit_to_serialized(&self.script);
}
}

Expand Down
54 changes: 13 additions & 41 deletions src/schema/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,26 @@
//! Components related to the scripting system used by schema or applied at the
//! specific contract operation level
use crate::vm::AluScript;
use crate::LIB_NAME_RGB;
use aluvm::library::LibSite;

/// Virtual machine types.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display(Debug)]
pub enum VmType {
/// AluVM: pure functional register-based virtual machine designed for RGB
/// and multiparty computing.
AluVM,
}
use crate::LIB_NAME_RGB;

/// Virtual machine and machine-specific script data.
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
/// Virtual machine-specific script calling information.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Default, Display)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
serde(crate = "serde_crate", rename_all = "camelCase", tag = "type")
)]
pub enum Script {
/// AluVM: pure functional register-based virtual machine designed for RGB
/// and multiparty computing.
///
/// The inner data contains actual executable code in form of complete set
/// of AliVM libraries, which must be holistic and not dependent on any
/// external libraries (i.e. must contain all libraries embedded).
///
/// Its routines can be accessed only through well-typed ABI entrance
/// pointers, defined as a part of the schema.
#[strict_type(tag = 0x01)]
AluVM(AluScript),
}
pub enum ScriptRef {
#[default]
#[display("~")]
#[strict_type(tag = 0x00)]
None,

impl Default for Script {
fn default() -> Self { Script::AluVM(none!()) }
}

impl Script {
pub fn vm_type(&self) -> VmType {
match self {
Script::AluVM(_) => VmType::AluVM,
}
}

pub fn as_alu_script(&self) -> &AluScript {
let Script::AluVM(alu) = self;
alu
}
#[display("alu({0})")]
#[strict_type(tag = 0x01)]
AluVM(LibSite),
}
19 changes: 13 additions & 6 deletions src/schema/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use commit_verify::ReservedBytes;
use strict_encoding::Primitive;
use strict_types::SemId;

Expand Down Expand Up @@ -58,21 +59,22 @@ impl MediaType {
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum StateSchema {
pub enum OwnedStateSchema {
#[strict_type(dumb)]
Declarative,
Fungible(FungibleType),
Structured(SemId),
Attachment(MediaType),
// TODO: Computed state (RCP240327A) will be added here
}

impl StateSchema {
impl OwnedStateSchema {
pub fn state_type(&self) -> StateType {
match self {
StateSchema::Declarative => StateType::Void,
StateSchema::Fungible(_) => StateType::Fungible,
StateSchema::Structured(_) => StateType::Structured,
StateSchema::Attachment(_) => StateType::Attachment,
OwnedStateSchema::Declarative => StateType::Void,
OwnedStateSchema::Fungible(_) => StateType::Fungible,
OwnedStateSchema::Structured(_) => StateType::Structured,
OwnedStateSchema::Attachment(_) => StateType::Attachment,

Check warning on line 77 in src/schema/state.rs

View check run for this annotation

Codecov / codecov/patch

src/schema/state.rs#L74-L77

Added lines #L74 - L77 were not covered by tests
}
}
}
Expand Down Expand Up @@ -108,20 +110,25 @@ pub enum FungibleType {
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct GlobalStateSchema {
// TODO: Reserved for computed state (RCP240327A): will be used as an enum tag with computed
// state having value 1.
pub reserved: ReservedBytes<1>,
pub sem_id: SemId,
pub max_items: u16,
}

impl GlobalStateSchema {
pub fn once(sem_id: SemId) -> Self {
GlobalStateSchema {
reserved: default!(),

Check warning on line 123 in src/schema/state.rs

View check run for this annotation

Codecov / codecov/patch

src/schema/state.rs#L123

Added line #L123 was not covered by tests
sem_id,
max_items: 1,
}
}

pub fn many(sem_id: SemId) -> Self {
GlobalStateSchema {
reserved: default!(),

Check warning on line 131 in src/schema/state.rs

View check run for this annotation

Codecov / codecov/patch

src/schema/state.rs#L131

Added line #L131 was not covered by tests
sem_id,
max_items: u16::MAX,
}
Expand Down
2 changes: 1 addition & 1 deletion src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{

/// Strict types id for the library providing data types for RGB consensus.
pub const LIB_ID_RGB: &str =
"urn:ubideco:stl:8c6sWAHuApYB1te5zqKLZK2JcqNHNdBn1p2VtvQBv4y8#suzuki-soda-talent";
"urn:ubideco:stl:m7rpPY187B71Uz3NiHsJy2adfRtVfwczpVx4ijKcjnh#dollar-prize-first";

fn _rgb_core_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB), tiny_bset! {
Expand Down
5 changes: 5 additions & 0 deletions src/validation/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use std::rc::Rc;

use strict_types::TypeSystem;

use crate::validation::VirtualMachine;
use crate::{
AnchoredBundle, AssetTag, AssignmentType, BundleId, Genesis, OpId, OpRef, Operation, Schema,
SecretSeal, WitnessId, XChain,
Expand All @@ -47,6 +48,8 @@ impl<'consignment, C: ConsignmentApi> ConsignmentApi for CheckedConsignment<'con

fn types(&self) -> &TypeSystem { self.0.types() }

Check warning on line 49 in src/validation/consignment.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/consignment.rs#L49

Added line #L49 was not covered by tests

fn vm(&self) -> Box<dyn VirtualMachine> { self.0.vm() }

Check warning on line 51 in src/validation/consignment.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/consignment.rs#L51

Added line #L51 was not covered by tests

fn asset_tags(&self) -> &BTreeMap<AssignmentType, AssetTag> { self.0.asset_tags() }

fn operation(&self, opid: OpId) -> Option<OpRef> {
Expand Down Expand Up @@ -85,6 +88,8 @@ pub trait ConsignmentApi {
/// Returns reference to the type system.
fn types(&self) -> &TypeSystem;

fn vm(&self) -> Box<dyn VirtualMachine>;

/// Asset tags uses in the confidential asset validation.
fn asset_tags(&self) -> &BTreeMap<AssignmentType, AssetTag>;

Expand Down
17 changes: 13 additions & 4 deletions src/validation/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::validation::{CheckedConsignment, ConsignmentApi, VirtualMachine};
use crate::{
validation, AssetTag, AssignmentType, Assignments, AssignmentsRef, ContractId, ExposedSeal,
GlobalState, GlobalStateSchema, GlobalValues, GraphSeal, Inputs, OpFullType, OpId, OpRef,
Operation, Opout, Schema, TransitionType, TypedAssigns, Valencies,
Operation, Opout, Schema, ScriptRef, TransitionType, TypedAssigns, Valencies,
};

impl Schema {
Expand All @@ -53,6 +53,7 @@ impl Schema {
redeem_schema,
assign_schema,
valency_schema,
validator,

Check warning on line 56 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L56

Added line #L56 was not covered by tests
) = match (op.transition_type(), op.extension_type()) {
(None, None) => {
// Right now we do not have actions to implement; but later
Expand All @@ -71,6 +72,7 @@ impl Schema {
&empty_valency_schema,
&self.genesis.assignments,
&self.genesis.valencies,
self.genesis.validator,

Check warning on line 75 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L75

Added line #L75 was not covered by tests
)
}
(Some(transition_type), None) => {
Expand Down Expand Up @@ -100,6 +102,7 @@ impl Schema {
&empty_valency_schema,
&transition_schema.assignments,
&transition_schema.valencies,
transition_schema.validator,

Check warning on line 105 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L105

Added line #L105 was not covered by tests
)
}
(None, Some(extension_type)) => {
Expand Down Expand Up @@ -128,6 +131,7 @@ impl Schema {
&extension_schema.redeems,
&extension_schema.assignments,
&extension_schema.redeems,
extension_schema.validator,

Check warning on line 134 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L134

Added line #L134 was not covered by tests
)
}
_ => unreachable!("Node can't be extension and state transition at the same time"),
Expand Down Expand Up @@ -176,7 +180,7 @@ impl Schema {
// We need to run scripts as the very last step, since before that
// we need to make sure that the operation data match the schema, so
// scripts are not required to validate the structure of the state
status += self.validate_state_evolution(op_info, vm);
status += self.validate_state_evolution(validator, op_info, vm);

Check warning on line 183 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L183

Added line #L183 was not covered by tests
status
}

Expand Down Expand Up @@ -238,7 +242,11 @@ impl Schema {
.map(Confined::unbox)
.unwrap_or_default();

let GlobalStateSchema { sem_id, max_items } = self.global_types.get(type_id).expect(
let GlobalStateSchema {
sem_id,
max_items,
reserved: _,
} = self.global_types.get(type_id).expect(

Check warning on line 249 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L245-L249

Added lines #L245 - L249 were not covered by tests
"if the field were absent, the schema would not be able to pass the internal \
validation and we would not reach this point",
);
Expand Down Expand Up @@ -410,6 +418,7 @@ impl Schema {

fn validate_state_evolution(
&self,
entry_point: ScriptRef,

Check warning on line 421 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L421

Added line #L421 was not covered by tests
op_info: OpInfo,
vm: &dyn VirtualMachine,
) -> validation::Status {
Expand All @@ -418,7 +427,7 @@ impl Schema {
// We do not validate public rights, since they do not have an
// associated state and there is nothing to validate beyond schema

if let Err(err) = vm.validate(op_info) {
if let Err(err) = vm.validate(entry_point, op_info) {

Check warning on line 430 in src/validation/logic.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L430

Added line #L430 was not covered by tests
status.add_failure(err);
}

Expand Down
4 changes: 2 additions & 2 deletions src/validation/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use strict_types::TypeSystem;

use crate::{validation, OpFullType, OpSchema, Schema, StateSchema, TransitionType};
use crate::{validation, OpFullType, OpSchema, OwnedStateSchema, Schema, TransitionType};

impl Schema {
pub fn verify(&self, types: &TypeSystem) -> validation::Status {

Check warning on line 28 in src/validation/schema.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/schema.rs#L28

Added line #L28 was not covered by tests
Expand Down Expand Up @@ -50,7 +50,7 @@ impl Schema {
}

for (type_id, schema) in &self.owned_types {
if let StateSchema::Structured(sem_id) = schema {
if let OwnedStateSchema::Structured(sem_id) = schema {
if !types.contains_key(sem_id) {

Check warning on line 54 in src/validation/schema.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/schema.rs#L53-L54

Added lines #L53 - L54 were not covered by tests
status.add_failure(validation::Failure::SchemaOwnedSemIdUnknown(
*type_id, *sem_id,
Expand Down
Loading

0 comments on commit 86a2980

Please sign in to comment.