Skip to content

Commit

Permalink
use linked hash map for object inventory to preserve insertion order
Browse files Browse the repository at this point in the history
  • Loading branch information
emmazzz committed Jul 29, 2022
1 parent 5e06c90 commit dafbaa2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/sui-framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false
[dependencies]
anyhow = { version = "1.0.58", features = ["backtrace"] }
bcs = "0.1.3"
linked-hash-map = "0.5.6"
smallvec = "1.9.0"
num_enum = "0.5.7"
once_cell = "1.11.0"
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-framework/src/natives/test_scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::EventType;
use core::panic;
use linked_hash_map::LinkedHashMap;
use move_binary_format::errors::PartialVMResult;
use move_core_types::{account_address::AccountAddress, value::MoveTypeLayout};
use move_vm_runtime::native_functions::NativeContext;
Expand Down Expand Up @@ -52,7 +53,7 @@ struct OwnedObj {
// This will require extending NativeContext with a function to map `Type` (which is just an index
// into the module's StructHandle table for structs) to something human-readable like `TypeTag`.
// TODO: add a native function that prints the log of transfers, deletes, wraps for debugging purposes
type Inventory = BTreeMap<ObjectID, OwnedObj>;
type Inventory = LinkedHashMap<ObjectID, OwnedObj>;

/// Return the object ID involved in an event.
/// This depends on the value format for each event type.
Expand Down
36 changes: 36 additions & 0 deletions crates/sui-framework/tests/test_scenario_tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,33 @@ module sui::test_scenarioTests {
};
}

#[test]
fun test_get_last_created_owned_object() {
let sender = @0x0;
let scenario = &mut test_scenario::begin(&sender);
create_and_transfer_object(scenario, 1);
test_scenario::next_tx(scenario, &sender);
{
let obj = test_scenario::take_last_created_owned<Object>(scenario);
assert!(obj.value == 1, VALUE_MISMATCH);
test_scenario::return_owned(scenario, obj);
};
create_and_transfer_object(scenario, 2);
test_scenario::next_tx(scenario, &sender);
{
let obj = test_scenario::take_last_created_owned<Object>(scenario);
assert!(obj.value == 2, VALUE_MISMATCH);
test_scenario::return_owned(scenario, obj);
};
create_and_transfer_object(scenario, 3);
test_scenario::next_tx(scenario, &sender);
{
let obj = test_scenario::take_last_created_owned<Object>(scenario);
assert!(obj.value == 3, VALUE_MISMATCH);
test_scenario::return_owned(scenario, obj);
};
}

#[test]
fun test_take_child_object() {
let sender = @0x0;
Expand Down Expand Up @@ -370,4 +397,13 @@ module sui::test_scenarioTests {
};
transfer::transfer(parent, test_scenario::sender(scenario));
}

/// Create an object and transfer it to the sender of `scenario`.
fun create_and_transfer_object(scenario: &mut Scenario, value: u64) {
let object = Object {
info: test_scenario::new_object(scenario),
value,
};
transfer::transfer(object, test_scenario::sender(scenario));
}
}

0 comments on commit dafbaa2

Please sign in to comment.