Skip to content

Commit

Permalink
wip on global state in VM
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 27, 2024
1 parent 52e9dcb commit 7472d06
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/validation/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<Root: SchemaRoot> Schema<Root> {
&'validator self,
consignment: &'validator CheckedConsignment<'consignment, C>,
op: OpRef,
globals: &'consignment GlobalState,

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

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L42

Added line #L42 was not covered by tests
vm: &'consignment dyn VirtualMachine,
) -> validation::Status {
let id = op.id();
Expand Down Expand Up @@ -168,6 +169,7 @@ impl<Root: SchemaRoot> Schema<Root> {
consignment.genesis().contract_id(),
id,
self.subset_of.is_some(),
globals,

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

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L172

Added line #L172 was not covered by tests
&op,
&prev_state,
&redeemed,
Expand Down Expand Up @@ -426,7 +428,7 @@ impl<Root: SchemaRoot> Schema<Root> {
}
}

pub struct OpInfo<'op> {
pub struct OpInfo<'contract, 'op> {
pub subschema: bool,
pub contract_id: ContractId,
pub id: OpId,
Expand All @@ -437,14 +439,16 @@ pub struct OpInfo<'op> {
pub owned_state: AssignmentsRef<'op>,
pub redeemed: &'op Valencies,
pub valencies: &'op Valencies,
pub global: &'op GlobalState,
pub op_global: &'op GlobalState,
pub contract_global: &'contract GlobalState,
}

impl<'op> OpInfo<'op> {
impl<'contract, 'op> OpInfo<'contract, 'op> {
pub fn with(
contract_id: ContractId,
id: OpId,
subschema: bool,
globals: &'contract GlobalState,

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

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L451

Added line #L451 was not covered by tests
op: &'op OpRef<'op>,
prev_state: &'op Assignments<GraphSeal>,
redeemed: &'op Valencies,
Expand All @@ -461,7 +465,8 @@ impl<'op> OpInfo<'op> {
owned_state: op.assignments(),
redeemed,
valencies: op.valencies(),
global: op.globals(),
op_global: op.globals(),
contract_global: globals,

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

View check run for this annotation

Codecov / codecov/patch

src/validation/logic.rs#L468-L469

Added lines #L468 - L469 were not covered by tests
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/validation/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ use super::status::{Failure, Warning};
use super::{CheckedConsignment, ConsignmentApi, Status, Validity, VirtualMachine};
use crate::vm::AluRuntime;
use crate::{
AltLayer1, BundleId, ContractId, Layer1, OpId, OpRef, OpType, Operation, Opout, Schema,
SchemaId, SchemaRoot, Script, SubSchema, Transition, TransitionBundle, TypedAssigns, WitnessId,
XAnchor, XChain, XOutpoint, XOutputSeal, XPubWitness, XWitness,
AltLayer1, BundleId, ContractId, GlobalState, Layer1, OpId, OpRef, OpType, Operation, Opout,
Schema, SchemaId, SchemaRoot, Script, SubSchema, Transition, TransitionBundle, TypedAssigns,
WitnessId, XAnchor, XChain, XOutpoint, XOutputSeal, XPubWitness, XWitness,
};

#[derive(Clone, Debug, Display, Error, From)]
Expand Down Expand Up @@ -66,6 +66,8 @@ pub struct Validator<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitne
validated_op_seals: BTreeSet<OpId>,
validated_op_state: BTreeSet<OpId>,

global_state: GlobalState,

vm: Box<dyn VirtualMachine + 'consignment>,
resolver: &'resolver R,
}
Expand Down Expand Up @@ -134,6 +136,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
validated_op_seals,
vm,
resolver,
global_state: none!(),

Check warning on line 139 in src/validation/validator.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/validator.rs#L139

Added line #L139 was not covered by tests
}
}

Expand Down Expand Up @@ -199,6 +202,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
self.status += schema.validate_state(
&self.consignment,
OpRef::Genesis(self.consignment.genesis()),
&self.global_state,

Check warning on line 205 in src/validation/validator.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/validator.rs#L205

Added line #L205 was not covered by tests
self.vm.as_ref(),
);
self.validated_op_state.insert(self.genesis_id);
Expand Down Expand Up @@ -231,7 +235,7 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
// utilize queue to keep the track of the upstream (ancestor) nodes and make
// sure that ve have validated each one of them up to genesis. The graph is
// valid when each of its nodes and each of its edges is valid, i.e. when all
// individual nodes has passed validation against the schema (we track
// individual nodes have passed validation against the schema (we track

Check warning on line 238 in src/validation/validator.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/validator.rs#L238

Added line #L238 was not covered by tests
// that fact with `validation_index`) and each of the operation ancestor state
// change to a given operation is valid against the schema + committed
// into bitcoin transaction graph with proper anchor. That is what we are
Expand All @@ -253,8 +257,12 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
}
// [VALIDATION]: Verify operation against the schema and scripts
if self.validated_op_state.insert(opid) {
self.status +=
schema.validate_state(&self.consignment, operation, self.vm.as_ref());
self.status += schema.validate_state(
&self.consignment,
operation,
&self.global_state,
self.vm.as_ref(),
);

Check warning on line 265 in src/validation/validator.rs

View check run for this annotation

Codecov / codecov/patch

src/validation/validator.rs#L260-L265

Added lines #L260 - L265 were not covered by tests
}

match operation {
Expand Down Expand Up @@ -530,8 +538,8 @@ impl<'consignment, 'resolver, C: ConsignmentApi, R: ResolveWitness>
/// generic type `Dbc`) and extra-transaction data, which are taken from
/// anchors DBC proof.
///
/// Additionally checks that the provided message contains commitment to the
/// bundle under the current contract.
/// Additionally, checks that the provided message contains commitment to
/// the bundle under the current contract.
fn validate_seal_closing<'seal, 'temp, Seal: 'seal, Dbc: dbc::Proof>(
&mut self,
seals: impl IntoIterator<Item = &'seal Seal>,
Expand Down
2 changes: 1 addition & 1 deletion src/vm/isa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum RgbIsa {
}

impl InstructionSet for RgbIsa {
type Context<'ctx> = OpInfo<'ctx>;
type Context<'ctx> = OpInfo<'ctx, 'ctx>;

fn isa_ids() -> BTreeSet<&'static str> {
bset! {"RGB"}
Expand Down
8 changes: 4 additions & 4 deletions src/vm/op_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub enum ContractOp {
}

impl InstructionSet for ContractOp {
type Context<'ctx> = OpInfo<'ctx>;
type Context<'ctx> = OpInfo<'ctx, 'ctx>;

fn isa_ids() -> BTreeSet<&'static str> { none!() }

Expand Down Expand Up @@ -246,7 +246,7 @@ impl InstructionSet for ContractOp {
);
}
ContractOp::CnG(state_type, reg) => {
regs.set_n(RegA::A16, *reg, context.global.get(state_type).map(|a| a.len_u16()));
regs.set_n(RegA::A16, *reg, context.op_global.get(state_type).map(|a| a.len_u16()));

Check warning on line 249 in src/vm/op_contract.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/op_contract.rs#L249

Added line #L249 was not covered by tests
}
ContractOp::CnC(_state_type, _reg) => {
// TODO: implement global contract state
Expand Down Expand Up @@ -286,7 +286,7 @@ impl InstructionSet for ContractOp {
}
ContractOp::LdG(state_type, index, reg) => {
let Some(state) = context
.global
.op_global

Check warning on line 289 in src/vm/op_contract.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/op_contract.rs#L289

Added line #L289 was not covered by tests
.get(state_type)
.and_then(|a| a.get(*index as usize))
else {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl InstructionSet for ContractOp {
}

ContractOp::PcCs(owned_state, global_state) => {
let Some(sum) = context.global.get(global_state) else {
let Some(sum) = context.op_global.get(global_state) else {

Check warning on line 318 in src/vm/op_contract.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/op_contract.rs#L318

Added line #L318 was not covered by tests
fail!()
};
if sum.len() != 1 {
Expand Down
2 changes: 1 addition & 1 deletion src/vm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'script> AluRuntime<'script> {
}
}

for ty in info.global.keys() {
for ty in info.op_global.keys() {

Check warning on line 63 in src/vm/runtime.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/runtime.rs#L63

Added line #L63 was not covered by tests
regs.nums
.insert((RegAFR::A(RegA::A16), Reg32::Reg1), ty.into_inner().into());
self.run(EntryPoint::ValidateGlobalState(*ty), &regs, info)?;
Expand Down

0 comments on commit 7472d06

Please sign in to comment.