Skip to content

Commit

Permalink
Basic data type validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jun 13, 2022
1 parent 154b351 commit 0d3beb4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rgb-core"
version = "0.7.0-beta.1"
version = "0.7.0-rc.1"
license = "MIT"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
description = "RGB Core Library: private & scalable client-validated smart contracts for Bitcoin & Lightning"
Expand Down
33 changes: 32 additions & 1 deletion src/contract/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use amplify::AsAny;
use bitcoin::hashes::{sha256, sha256t};
use commit_verify::{commit_encode, CommitConceal, CommitEncode, TaggedHash};
use half::bf16;
use stens::AsciiString;
use stens::{AsciiString, PrimitiveType, TypeRef};
use strict_encoding::strict_serialize;

use super::{ConfidentialState, RevealedState};
Expand Down Expand Up @@ -360,6 +360,37 @@ impl Revealed {
_ => None,
}
}

pub fn schema_type(&self) -> TypeRef {
match self {
Revealed::U8(_) => TypeRef::u8(),
Revealed::U16(_) => TypeRef::u16(),
Revealed::U32(_) => TypeRef::u32(),
Revealed::U64(_) => TypeRef::u64(),
Revealed::U128(_) => TypeRef::u128(),
Revealed::U256(_) => TypeRef::Primitive(PrimitiveType::U256.into()),
Revealed::U512(_) => TypeRef::Primitive(PrimitiveType::U512.into()),
Revealed::U1024(_) => TypeRef::Primitive(PrimitiveType::U1024.into()),
Revealed::I8(_) => TypeRef::i8(),
Revealed::I16(_) => TypeRef::i16(),
Revealed::I32(_) => TypeRef::i32(),
Revealed::I64(_) => TypeRef::i64(),
Revealed::I128(_) => TypeRef::i128(),
Revealed::I256(_) => TypeRef::Primitive(PrimitiveType::I256.into()),
Revealed::I512(_) => TypeRef::Primitive(PrimitiveType::I512.into()),
Revealed::I1024(_) => TypeRef::Primitive(PrimitiveType::I1024.into()),
Revealed::F16B(_) => TypeRef::Primitive(PrimitiveType::F16b.into()),
Revealed::F16(_) => TypeRef::Primitive(PrimitiveType::F16.into()),
Revealed::F32(_) => TypeRef::f32(),
Revealed::F64(_) => TypeRef::f64(),
Revealed::F80(_) => TypeRef::Primitive(PrimitiveType::F80.into()),
Revealed::F128(_) => TypeRef::Primitive(PrimitiveType::F128.into()),
Revealed::F256(_) => TypeRef::Primitive(PrimitiveType::F256.into()),
Revealed::Bytes(_) => TypeRef::bytes(),
Revealed::AsciiString(_) => TypeRef::ascii_string(),
Revealed::UnicodeString(_) => TypeRef::unicode_string(),
}
}
}

#[cfg(test)]
Expand Down
16 changes: 12 additions & 4 deletions src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ mod _validation {
use crate::script::{OverrideRules, ValidationScript};
use crate::vm::Validate;
use crate::{
validation, Assignment, AssignmentVec, Metadata, Node, NodeId, NodeSubtype, OwnedRights,
ParentOwnedRights, ParentPublicRights, PublicRights, State,
data, validation, Assignment, AssignmentVec, Metadata, Node, NodeId, NodeSubtype,
OwnedRights, ParentOwnedRights, ParentPublicRights, PublicRights, State,
};

impl SchemaVerify for Schema {
Expand Down Expand Up @@ -387,8 +387,16 @@ mod _validation {
let field = self.field_types.get(field_type_id)
.expect("If the field were absent, the schema would not be able to pass the internal validation and we would not reach this point");
for data in set {
// TODO: [validation] validate type schema
// status += field.validate(*field_type_id, &data);
let schema_type = data.schema_type();
if &schema_type != field
&& !matches!((data, field), (data::Revealed::Bytes(_), TypeRef::Named(_)))
{
status.add_failure(validation::Failure::SchemaMismatchedDataType(
*field_type_id,
));
}
// TODO: [validation] validate type serialization for structured types
// status += field.validate(&data);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stash/disclosure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl Disclosure {
self.signatures = empty!();
self.extensions
.entry(contract_id)
.or_insert_with(BTreeMap::new)
.or_insert_with(Vec::new)
.extend(extensions);
}

Expand Down

0 comments on commit 0d3beb4

Please sign in to comment.