Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! [WIP] New Move object runtime. New test s…
Browse files Browse the repository at this point in the history
…cenario implementation
  • Loading branch information
tnowacki committed Sep 14, 2022
1 parent b3bfdc4 commit 5a06390
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
18 changes: 9 additions & 9 deletions crates/sui-framework/sources/test_scenario.move.WIP
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ module sui::test_scenario_wip {
}

/// Returns true if the object with `ID` id was in the inventory for `account`
public native fun was_taken_from_address(account: address, id: ID): bool;
native fun was_taken_from_address(account: address, id: ID): bool;

// == from sender ==

Expand All @@ -267,7 +267,7 @@ module sui::test_scenario_wip {
}

/// Returns true if the object with `ID` id was in the inventory for the sender
public fun was_taken_from_sender(scenario: &Scenario, id: ID): bool {
fun was_taken_from_sender(scenario: &Scenario, id: ID): bool {
was_taken_from_address(sender(scenario), id)
}

Expand All @@ -288,15 +288,15 @@ module sui::test_scenario_wip {
take_immutable_by_id(option::destroy_some(id_opt))
}

/// Return `t` to the global invetory
/// Return `t` to the global inventory
public fun return_immutable<T: key>(t: T) {
let id = object::id(&t);
assert!(was_taken_immutable(id), ECantReturnObject);
sui::transfer::freeze_object(t)
}

/// Returns true if the object with `ID` id was an immutable object in the global invetory
public native fun was_taken_immutable(id: ID): bool;
/// Returns true if the object with `ID` id was an immutable object in the global inventory
native fun was_taken_immutable(id: ID): bool;

// == shared ==

Expand All @@ -315,15 +315,15 @@ module sui::test_scenario_wip {
take_shared_by_id(option::destroy_some(id_opt))
}

/// Return `t` to the global invetory
/// Return `t` to the global inventory
public fun return_shared<T: key>(t: T) {
let id = object::id(&t);
assert!(was_taken_shared(id), ECantReturnObject);
sui::transfer::share_object(t)
}

/// Returns true if the object with `ID` id was an shared object in the global invetory
public native fun was_taken_shared(id: ID): bool;
/// Returns true if the object with `ID` id was an shared object in the global inventory
native fun was_taken_shared(id: ID): bool;

// // == child objects ==

Expand All @@ -341,7 +341,7 @@ module sui::test_scenario_wip {
// take_shared_by_id(id)
// }

// /// Return `t` to the global invetory
// /// Return `t` to the global inventory
// /// Note that this is object will now be returned from `most_recent_id_shared`
// public fun return_shared<T: key>(t: T) {
// sui::transfer::share_object(t)
Expand Down
23 changes: 18 additions & 5 deletions crates/sui-framework/src/natives/object_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};

use better_any::{Tid, TidAble};
use linked_hash_map::LinkedHashMap;
Expand Down Expand Up @@ -47,10 +47,11 @@ pub struct ObjectRuntime {
pub(crate) test_inventories: TestInventories,
// will eventually need a reference to the state view to access child objects
// pub(crate) state_view: &'a mut dyn ____,
pub(crate) input_objects: BTreeSet<ObjectID>,
// new ids from object::new
pub(crate) new_ids: Set<ObjectID>,
new_ids: Set<ObjectID>,
// ids passed to object::delete
pub(crate) deleted_ids: Set<ObjectID>,
deleted_ids: Set<ObjectID>,
// transfers to a new owner (shared, immutable, object, or account address)
pub(crate) transfers: Vec<(Owner, StructTag, Value)>,
pub(crate) events: Vec<(StructTag, Value)>,
Expand All @@ -63,9 +64,10 @@ impl TestInventories {
}

impl ObjectRuntime {
pub fn new() -> Self {
pub fn new(input_objects: BTreeSet<ObjectID>) -> Self {
Self {
test_inventories: TestInventories::new(),
input_objects,
new_ids: Set::new(),
deleted_ids: Set::new(),
transfers: vec![],
Expand Down Expand Up @@ -122,7 +124,18 @@ impl ObjectRuntime {
self.events.push((ty, event))
}

pub fn finish(&mut self) -> Result<RuntimeResults, ExecutionError> {
pub(crate) fn take(&mut self) -> Self {
// take fields for empty version
let test_inventories = std::mem::take(&mut self.test_inventories);
let input_objects = std::mem::take(&mut self.input_objects);
let taken = std::mem::take(self);
// restore fields
self.test_inventories = test_inventories;
self.input_objects = input_objects;
taken
}

pub fn finish(self) -> Result<RuntimeResults, ExecutionError> {
// TODO bring in the adapter rules here for deleting children
todo!()
}
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-framework/src/natives/test_scenario_wip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::collections::{BTreeMap, VecDeque};
use sui_types::{
base_types::{ObjectID, SuiAddress},
object::Owner,
storage::ObjectChange,
storage::{ObjectChange, WriteKind},
};

const E_OBJECT_NOT_FOUND_CODE: u64 = 5;
Expand All @@ -47,11 +47,10 @@ pub fn end_transaction(
assert!(cfg!(feature = "testing"));
assert!(ty_args.is_empty());
assert!(args.is_empty());
let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut();
let new_ids = object_runtime.new_ids.clone();
let object_runtime_ref: &mut ObjectRuntime = context.extensions_mut().get_mut();
let mut new_object_values = LinkedHashMap::new();
let mut transferred = vec![];
for (owner, tag, value) in &object_runtime.transfers {
for (owner, tag, value) in &object_runtime_ref.transfers {
let id: ObjectID = get_object_id(value.copy_value().unwrap())
.unwrap()
.value_as::<AccountAddress>()
Expand All @@ -60,6 +59,8 @@ pub fn end_transaction(
new_object_values.insert(id, (*owner, tag.clone(), value.copy_value().unwrap()));
transferred.push((id, *owner));
}
assert!(object_runtime_ref.input_objects.is_empty());
let object_runtime = object_runtime_ref.take();
let results = object_runtime.finish();
let RuntimeResults {
changes,
Expand All @@ -73,21 +74,20 @@ pub fn end_transaction(
));
}
};
let inventories = &mut object_runtime.test_inventories;
let inventories = &mut object_runtime_ref.test_inventories;
let mut created = vec![];
let mut written = vec![];
let mut deleted = vec![];
// handle transfers
for (id, change) in changes {
match change {
ObjectChange::Delete(_, _) => deleted.push(id),
ObjectChange::Write(_) => {
ObjectChange::Write(_, kind) => {
let (owner, tag, value) = new_object_values.remove(&id).unwrap();
inventories.objects.insert(id, value);
if new_ids.contains_key(&id) {
created.push(id)
} else {
written.push(id)
match kind {
WriteKind::Create => created.push(id),
WriteKind::Mutate | WriteKind::Unwrap => written.push(id),
}
match owner {
Owner::AddressOwner(a) => {
Expand Down

0 comments on commit 5a06390

Please sign in to comment.