Skip to content

Commit

Permalink
vm: improve contract VM apis
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 27, 2024
1 parent 137ef74 commit aef42cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
52 changes: 43 additions & 9 deletions src/vm/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,43 @@ impl Ord for GlobalOrd {
}

impl GlobalOrd {
#[inline]
pub fn with_anchor(ord_txid: WitnessAnchor, idx: u16) -> Self {
GlobalOrd {
witness_anchor: Some(ord_txid),
idx,
}
}
#[inline]
pub fn genesis(idx: u16) -> Self {
GlobalOrd {
witness_anchor: None,
idx,
}
}
#[inline]
pub fn witness_id(&self) -> Option<XWitnessId> { self.witness_anchor.map(|a| a.witness_id) }
}

pub trait GlobalStateIter {
fn size(&self) -> u24;
fn prev(&mut self) -> Option<(GlobalOrd, impl Borrow<DataState>)>;
fn last(&mut self) -> Option<(GlobalOrd, impl Borrow<DataState>)>;
type Data: Borrow<DataState>;
fn size(&mut self) -> u24;
fn prev(&mut self) -> Option<(GlobalOrd, Self::Data)>;
fn last(&mut self) -> Option<(GlobalOrd, Self::Data)>;
fn reset(&mut self, depth: u24);
}

impl<I: GlobalStateIter> GlobalStateIter for &mut I {
type Data = I::Data;

#[inline]
fn size(&self) -> u24 { GlobalStateIter::size(*self) }
fn size(&mut self) -> u24 { GlobalStateIter::size(*self) }

#[inline]
fn prev(&mut self) -> Option<(GlobalOrd, impl Borrow<DataState>)> { (*self).prev() }
fn prev(&mut self) -> Option<(GlobalOrd, Self::Data)> { (*self).prev() }

#[inline]
fn last(&mut self) -> Option<(GlobalOrd, impl Borrow<DataState>)> { (*self).last() }
fn last(&mut self) -> Option<(GlobalOrd, Self::Data)> { (*self).last() }

#[inline]
fn reset(&mut self, depth: u24) { (*self).reset(depth) }
Expand Down Expand Up @@ -191,7 +198,7 @@ impl<I: GlobalStateIter> GlobalContractState<I> {
}

#[inline]
pub fn size(&self) -> u24 { self.iter.size() }
pub fn size(&mut self) -> u24 { self.iter.size() }

/// Retrieves global state data located `depth` items back from the most
/// recent global state value. Ensures that the global state ordering is
Expand Down Expand Up @@ -227,8 +234,35 @@ impl<I: GlobalStateIter> GlobalContractState<I> {
}
}

pub trait ContractState {
fn global(&self, ty: GlobalStateType) -> GlobalContractState<impl GlobalStateIter>;
impl<I: GlobalStateIter> Iterator for GlobalContractState<I> {
type Item = I::Data;

#[inline]
fn next(&mut self) -> Option<Self::Item> {
let Some((ord, item)) = self.iter.prev() else {
return None;
};
if ord >= self.last_ord {
panic!(
"global contract state iterator has invalid implementation: it fails to order \
global state according to the consensus ordering"
);
}
self.checked_depth += u24::ONE;
self.last_ord = ord;
Some(item)
}
}

#[derive(Copy, Clone, Debug, Display, Error)]
#[display("unknown global state type {0} requested from the contract")]
pub struct UnknownGlobalStateType(pub GlobalStateType);

pub trait ContractState<'c> {
fn global(
&'c self,
ty: GlobalStateType,
) -> Result<GlobalContractState<impl GlobalStateIter + 'c>, UnknownGlobalStateType>;

fn rights(&self, outpoint: XOutpoint, ty: AssignmentType) -> u32;

Expand Down
2 changes: 1 addition & 1 deletion src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod contract;
pub use aluvm::aluasm_isa;
pub use contract::{
AssignmentWitness, ContractState, GlobalContractState, GlobalOrd, GlobalStateIter,
WitnessAnchor,
UnknownGlobalStateType, WitnessAnchor,
};
pub use isa::RgbIsa;
pub use op_contract::ContractOp;
Expand Down

0 comments on commit aef42cb

Please sign in to comment.