Skip to content

Commit

Permalink
Merge pull request #260 from RGB-WG/nonce
Browse files Browse the repository at this point in the history
Further improvements to consensus ordering
  • Loading branch information
dr-orlovsky committed Aug 28, 2024
2 parents 8440518 + 1deaa56 commit d6f22dc
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 74 deletions.
11 changes: 5 additions & 6 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@ wasm-bindgen-test = "0.3"

[package.metadata.docs.rs]
features = ["all"]

[patch.crates-io]
rgb-core = { git = "https://github.com/RGB-WG/rgb-core", branch = "nonce" }
4 changes: 2 additions & 2 deletions asset/armored_contract.default
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:Kej4ueIS-4VLOUQe-SpFUOCe-BzEZ$Lt-C4PNmJC-J0Um7WM#cigar-pretend-mayor
Id: rgb:csg:mK5KaBwW-ht!iR1o-5qi3fe7-gIHhBiL-t80Tud5-Wi2Jo4A#bikini-binary-table
Version: 2
Type: contract
Contract: rgb:T24t0N1D-eiInTgb-BXlrrXz-$7OgV6n-WJWHPUD-BWNuqZw
Contract: rgb:5M7hTCP5-or5y2Bp-xPPIYez-WEsey5D-e2GhCpV-HlsK7jI
Schema: rgb:sch:CyqM42yAdM1moWyNZPQedAYt73BM$k9z$dKLUXY1voA#cello-global-deluxe
Check-SHA256: 181748dae0c83cbb44f6ccfdaddf6faca0bc4122a9f35fef47bab9aea023e4a1

Expand Down
4 changes: 2 additions & 2 deletions asset/armored_transfer.default
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:9jMKgkmP-alPghZC-bu65ctP-GT5tKgM-cAbaTLT-rhu8xQo#urban-athena-adam
Id: rgb:csg:H58AX9qS-4IFd5aQ-tx9Exr$-DzCvDDW-luMVTFJ-GqEnZ3k#avenue-educate-finland
Version: 2
Type: transfer
Contract: rgb:T24t0N1D-eiInTgb-BXlrrXz-$7OgV6n-WJWHPUD-BWNuqZw
Contract: rgb:5M7hTCP5-or5y2Bp-xPPIYez-WEsey5D-e2GhCpV-HlsK7jI
Schema: rgb:sch:CyqM42yAdM1moWyNZPQedAYt73BM$k9z$dKLUXY1voA#cello-global-deluxe
Check-SHA256: 562a944631243e23a8de1d2aa2a5621be13351fc6f4d9aa8127c12ac4fb54d97

Expand Down
73 changes: 29 additions & 44 deletions src/containers/consignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,25 +462,20 @@ mod test {

#[test]
fn contract_str_round_trip() {
let mut contract = Contract::from_str(include_str!("../../asset/armored_contract.default"))
.expect("contract from str should work");
assert_eq!(
contract.to_string(),
include_str!("../../asset/armored_contract.default").replace('\r', ""),
"contract string round trip fails"
);
let s = include_str!("../../asset/armored_contract.default");
let mut contract = Contract::from_str(s).unwrap();
assert_eq!(contract.to_string(), s.replace('\r', ""), "contract string round trip fails");
contract.transfer = true;
eprintln!("{contract}");
}

#[test]
fn error_contract_strs() {
assert!(Contract::from_str(include_str!("../../asset/armored_contract.default")).is_ok());
Contract::from_str(include_str!("../../asset/armored_contract.default")).unwrap();

// Wrong Id
assert!(
Contract::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Contract::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:aaaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa#guide-campus-arctic
Version: 2
Type: contract
Expand All @@ -492,15 +487,13 @@ Check-SHA256: 181748dae0c83cbb44f6ccfdaddf6faca0bc4122a9f35fef47bab9aea023e4a1
0000000000000000000000d59ZDjxe00000000dDb8~4rVQz13d2MfXa{vGU00000000000000000000
0000000000000
-----END RGB CONSIGNMENT-----"#
)
.is_err()
);
-----END RGB CONSIGNMENT-----"#,
)
.unwrap_err();

// Wrong checksum
assert!(
Contract::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Contract::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:poAMvm9j-NdapxqA-MJ!5dwP-d!IIt2A-T!5OiXE-Tl54Yew#guide-campus-arctic
Version: 2
Type: contract
Expand All @@ -512,32 +505,26 @@ Check-SHA256: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0000000000000000000000d59ZDjxe00000000dDb8~4rVQz13d2MfXa{vGU00000000000000000000
0000000000000
-----END RGB CONSIGNMENT-----"#
)
.is_err()
);
-----END RGB CONSIGNMENT-----"#,
)
.unwrap_err();
}

#[test]
fn transfer_str_round_trip() {
let transfer = Transfer::from_str(include_str!("../../asset/armored_transfer.default"))
.expect("transfer from str should work");
assert_eq!(
transfer.to_string(),
include_str!("../../asset/armored_transfer.default").replace('\r', ""),
"transfer string round trip fails"
);
let s = include_str!("../../asset/armored_transfer.default");
let transfer = Transfer::from_str(s).unwrap();
assert_eq!(transfer.to_string(), s.replace('\r', ""), "transfer string round trip fails");
}

#[test]
fn error_transfer_strs() {
let s = include_str!("../../asset/armored_transfer.default");
assert!(Transfer::from_str(s).is_ok());
Transfer::from_str(s).unwrap();

// Wrong Id
assert!(
Transfer::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Transfer::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:aaaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa-aaaaaaa#guide-campus-arctic
Version: 2
Type: transfer
Expand All @@ -549,15 +536,14 @@ Check-SHA256: 562a944631243e23a8de1d2aa2a5621be13351fc6f4d9aa8127c12ac4fb54d97
0000000000000000000000d59ZDjxe00000000dDb8~4rVQz13d2MfXa{vGU00000000000000000000
0000000000000
-----END RGB CONSIGNMENT-----"#
)
.is_err()
);
-----END RGB CONSIGNMENT-----"#,
)
.unwrap_err();

// Wrong checksum
assert!(
Transfer::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----

Transfer::from_str(
r#"-----BEGIN RGB CONSIGNMENT-----
Id: rgb:csg:9jMKgkmP-alPghZC-bu65ctP-GT5tKgM-cAbaTLT-rhu8xQo#urban-athena-adam
Version: 2
Type: transfer
Expand All @@ -569,10 +555,9 @@ Check-SHA256: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0000000000000000000000d59ZDjxe00000000dDb8~4rVQz13d2MfXa{vGU00000000000000000000
0000000000000
-----END RGB CONSIGNMENT-----"#
)
.is_err()
);
-----END RGB CONSIGNMENT-----"#,
)
.unwrap_err();

// Wrong type
// TODO: Uncomment once ASCII headers get checked
Expand Down
2 changes: 1 addition & 1 deletion src/containers/partials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl Batch {
methods
}

pub fn set_priority(&mut self, priority: u8) {
pub fn set_priority(&mut self, priority: u64) {
self.main.first.transition.nonce = priority;
if let Some(info) = &mut self.main.second {
info.transition.nonce = priority;
Expand Down
18 changes: 11 additions & 7 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use assignments::{KnownState, OutputAssignment, TypedAssignsExt};
pub use bundle::{BundleExt, RevealError};
pub use merge_reveal::{MergeReveal, MergeRevealError};
use rgb::vm::OrdOpRef;
use rgb::{OpId, XWitnessId};
use rgb::{ExtensionType, OpId, TransitionType, XWitnessId};

use crate::LIB_NAME_RGB_STD;

Expand All @@ -42,16 +42,20 @@ use crate::LIB_NAME_RGB_STD;
pub enum OpWitness {
#[strict_type(dumb)]
Genesis,
Transition(XWitnessId),
Extension(XWitnessId),
Transition(XWitnessId, TransitionType),
Extension(XWitnessId, ExtensionType),
}

impl From<OrdOpRef<'_>> for OpWitness {
fn from(aor: OrdOpRef) -> Self {
match aor {
OrdOpRef::Genesis(_) => OpWitness::Genesis,
OrdOpRef::Transition(_, witness_id, ..) => OpWitness::Transition(witness_id),
OrdOpRef::Extension(_, witness_id, ..) => OpWitness::Transition(witness_id),
OrdOpRef::Transition(op, witness_id, ..) => {
OpWitness::Transition(witness_id, op.transition_type)
}
OrdOpRef::Extension(op, witness_id, ..) => {
OpWitness::Extension(witness_id, op.extension_type)
}
}
}
}
Expand All @@ -61,7 +65,7 @@ impl OpWitness {
pub fn witness_id(&self) -> Option<XWitnessId> {
match self {
OpWitness::Genesis => None,
OpWitness::Transition(witness_id) | OpWitness::Extension(witness_id) => {
OpWitness::Transition(witness_id, _) | OpWitness::Extension(witness_id, _) => {
Some(*witness_id)
}
}
Expand All @@ -78,7 +82,7 @@ impl OpWitness {
)]
pub struct GlobalOut {
pub opid: OpId,
pub nonce: u8,
pub nonce: u64,
pub index: u16,
pub op_witness: OpWitness,
}
Expand Down
8 changes: 4 additions & 4 deletions src/interface/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl ContractBuilder {
pub struct TransitionBuilder {
contract_id: ContractId,
builder: OperationBuilder<GraphSeal>,
nonce: u8,
nonce: u64,
transition_type: TransitionType,
inputs: TinyOrdMap<Input, PersistedState>,
}
Expand Down Expand Up @@ -531,7 +531,7 @@ impl TransitionBuilder {
Self {
contract_id,
builder: OperationBuilder::with(iface, schema, iimpl, types),
nonce: u8::MAX,
nonce: u64::MAX,
transition_type,
inputs: none!(),
}
Expand All @@ -548,7 +548,7 @@ impl TransitionBuilder {
Self {
contract_id,
builder: OperationBuilder::deterministic(iface, schema, iimpl, types),
nonce: u8::MAX,
nonce: u64::MAX,
transition_type,
inputs: none!(),
}
Expand All @@ -558,7 +558,7 @@ impl TransitionBuilder {

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

pub fn set_nonce(mut self, nonce: u8) -> Self {
pub fn set_nonce(mut self, nonce: u64) -> Self {
self.nonce = nonce;
self
}
Expand Down
8 changes: 4 additions & 4 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,13 +904,13 @@ impl<M: Borrow<MemContractState>> ContractStateAccess for MemContract<M> {
.filter_map(|(out, data)| {
let ord = match out.op_witness {
OpWitness::Genesis => GlobalOrd::genesis(out.index),
OpWitness::Transition(id) => {
OpWitness::Transition(id, ty) => {
let ord = self.filter.get(&id)?;
GlobalOrd::transition(out.opid, out.index, out.nonce, *ord)
GlobalOrd::transition(out.opid, out.index, ty, out.nonce, *ord)
}
OpWitness::Extension(id) => {
OpWitness::Extension(id, ty) => {
let ord = self.filter.get(&id)?;
GlobalOrd::extension(out.opid, out.index, out.nonce, *ord)
GlobalOrd::extension(out.opid, out.index, ty, out.nonce, *ord)
}
};
Some((ord, data))
Expand Down
4 changes: 2 additions & 2 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
prev_outputs,
method,
beneficiary_vout,
u8::MAX,
u64::MAX,
allocator,
|_, _| BlindingFactor::random(),
|_, _| rand::random(),
Expand All @@ -929,7 +929,7 @@ impl<S: StashProvider, H: StateProvider, P: IndexProvider> Stock<S, H, P> {
prev_outputs: impl IntoIterator<Item = impl Into<XOutputSeal>>,
method: CloseMethod,
beneficiary_vout: Option<impl Into<Vout>>,
priority: u8,
priority: u64,
allocator: impl Fn(ContractId, AssignmentType, VelocityHint) -> Option<Vout>,
pedersen_blinder: impl Fn(ContractId, AssignmentType) -> BlindingFactor,
seal_blinder: impl Fn(ContractId, AssignmentType) -> u64,
Expand Down
4 changes: 2 additions & 2 deletions src/stl/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::LIB_NAME_RGB_STD;
/// Strict types id for the library providing standard data types which may be
/// used in RGB smart contracts.
pub const LIB_ID_RGB_STORAGE: &str =
"stl:WDzyU4h6-vYKDkSa-2BfqXDg-d$LZbmr-oQgDi!z-rINtGDY#nerve-patrol-special";
"stl:ieoE!c74-z5Us1Cc-j6$RdoB-BWAdWBF-jwE9Vu7-sFCnYdI#lopez-alert-slow";

/// Strict types id for the library providing standard data types which may be
/// used in RGB smart contracts.
Expand All @@ -50,7 +50,7 @@ pub const LIB_ID_RGB_CONTRACT: &str =

/// Strict types id for the library representing of RGB StdLib data types.
pub const LIB_ID_RGB_STD: &str =
"stl:WfNRJuO!-BI3uve1-vVFxCsg-YP!o40Q-RuomKHn-SNz$6u0#paper-sunset-modular";
"stl:MRRNRLeI-muTBg22-pcaBEA2-MSllDDH-4RCDD6n-8ku826s#picnic-reverse-convert";

fn _rgb_std_stl() -> Result<TypeLib, CompileError> {
LibBuilder::new(libname!(LIB_NAME_RGB_STD), tiny_bset! {
Expand Down

0 comments on commit d6f22dc

Please sign in to comment.