Skip to content

Commit

Permalink
Merge pull request #244 from RGB-WG/contract-state
Browse files Browse the repository at this point in the history
Support contract state computation during validation
  • Loading branch information
dr-orlovsky authored Aug 9, 2024
2 parents b530b30 + b561262 commit c211727
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 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.

12 changes: 6 additions & 6 deletions src/contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod merge_reveal;
pub use assignments::{KnownState, OutputAssignment, TypedAssignsExt};
pub use bundle::{BundleExt, RevealError};
pub use merge_reveal::{MergeReveal, MergeRevealError};
use rgb::vm::AnchoredOpRef;
use rgb::vm::OrdOpRef;
use rgb::{OpId, XWitnessId};

use crate::LIB_NAME_RGB_STD;
Expand All @@ -46,12 +46,12 @@ pub enum OpWitness {
Extension(XWitnessId),
}

impl From<AnchoredOpRef<'_>> for OpWitness {
fn from(aor: AnchoredOpRef) -> Self {
impl From<OrdOpRef<'_>> for OpWitness {
fn from(aor: OrdOpRef) -> Self {
match aor {
AnchoredOpRef::Genesis(_) => OpWitness::Genesis,
AnchoredOpRef::Transition(_, witness_id) => OpWitness::Transition(witness_id),
AnchoredOpRef::Extension(_, witness_id) => OpWitness::Transition(witness_id),
OrdOpRef::Genesis(_) => OpWitness::Genesis,
OrdOpRef::Transition(_, witness_id, ..) => OpWitness::Transition(witness_id),
OrdOpRef::Extension(_, witness_id, ..) => OpWitness::Transition(witness_id),
}
}
}
Expand Down
28 changes: 10 additions & 18 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use bp::dbc::tapret::TapretCommitment;
use commit_verify::{CommitId, Conceal};
use rgb::validation::ResolveWitness;
use rgb::vm::{
AnchoredOpRef, ContractStateAccess, ContractStateEvolve, GlobalContractState, GlobalOrd,
GlobalStateIter, UnknownGlobalStateType, WitnessOrd,
ContractStateAccess, ContractStateEvolve, GlobalContractState, GlobalOrd, GlobalStateIter,
OrdOpRef, UnknownGlobalStateType, WitnessOrd,
};
use rgb::{
Assign, AssignmentType, Assignments, AssignmentsRef, AttachId, AttachState, BundleId,
Expand Down Expand Up @@ -675,7 +675,7 @@ impl MemContractState {
}
}

fn add_operation(&mut self, op: AnchoredOpRef) {
fn add_operation(&mut self, op: OrdOpRef) {
let opid = op.id();

for (ty, state) in op.globals() {
Expand Down Expand Up @@ -964,13 +964,7 @@ impl ContractStateEvolve for MemContract<MemContractState> {
}
}

fn evolve_state(&mut self, op: AnchoredOpRef) -> Result<(), confinement::Error> {
fn ordering(
filter: &HashMap<XWitnessId, WitnessOrd>,
witness_id: XWitnessId,
) -> WitnessOrd {
*filter.get(&witness_id).expect("unknown witness id")
}
fn evolve_state(&mut self, op: OrdOpRef) -> Result<(), confinement::Error> {
(move || -> Result<(), SerializeError> {
fn writer(me: &mut MemContract<MemContractState>) -> MemContractWriter {
MemContractWriter {
Expand All @@ -987,17 +981,15 @@ impl ContractStateEvolve for MemContract<MemContractState> {
}
}
match op {
AnchoredOpRef::Genesis(genesis) => {
OrdOpRef::Genesis(genesis) => {
let mut writer = writer(self);
writer.add_genesis(genesis)
}
AnchoredOpRef::Transition(transition, witness_id) => {
let ord = ordering(&self.filter, witness_id);
OrdOpRef::Transition(transition, witness_id, ord) => {
let mut writer = writer(self);
writer.add_transition(transition, witness_id, ord)
}
AnchoredOpRef::Extension(extension, witness_id) => {
let ord = ordering(&self.filter, witness_id);
OrdOpRef::Extension(extension, witness_id, ord) => {
let mut writer = writer(self);
writer.add_extension(extension, witness_id, ord)
}
Expand Down Expand Up @@ -1070,7 +1062,7 @@ impl<'mem> ContractStateWrite for MemContractWriter<'mem> {
/// If genesis violates RGB consensus rules and wasn't checked against the
/// schema before adding to the history.
fn add_genesis(&mut self, genesis: &Genesis) -> Result<(), Self::Error> {
self.contract.add_operation(AnchoredOpRef::Genesis(genesis));
self.contract.add_operation(OrdOpRef::Genesis(genesis));
Ok(())
}

Expand All @@ -1086,7 +1078,7 @@ impl<'mem> ContractStateWrite for MemContractWriter<'mem> {
) -> Result<(), Self::Error> {
(self.writer)(witness_id, ord)?;
self.contract
.add_operation(AnchoredOpRef::Transition(transition, witness_id));
.add_operation(OrdOpRef::Transition(transition, witness_id, ord));
Ok(())
}

Expand All @@ -1102,7 +1094,7 @@ impl<'mem> ContractStateWrite for MemContractWriter<'mem> {
) -> Result<(), Self::Error> {
(self.writer)(witness_id, ord)?;
self.contract
.add_operation(AnchoredOpRef::Extension(extension, witness_id));
.add_operation(OrdOpRef::Extension(extension, witness_id, ord));
Ok(())
}
}
Expand Down

0 comments on commit c211727

Please sign in to comment.