Unexpected error when using shared Random object test scenario

Hello everyone,

Iā€™m quite new around here so apologies if I am overlooking something.
While writing tests for a package that is using the random module I came across some very strange behavior when using the random::Random shared object. I wrote this minimal example to reproduce it:

#[test_only]
module random_example::random_example_tests;

use sui::random;
use sui::test_scenario;

public struct DummyStruct has key, store {
    id: UID
}

#[test]
fun test_random_example() {
    let mut scenario = test_scenario::begin(@0x0);
    {
        random::create_for_testing(scenario.ctx());
    };
    scenario.end();

    let addr = @0xa;
    let mut scenario = test_scenario::begin(addr);
    {
        let dummy_struct = DummyStruct {
            id: object::new(scenario.ctx())
        };
        transfer::public_transfer(dummy_struct, scenario.sender());
    };
    
    scenario.next_tx(addr);
    {
        let dummy = scenario.take_from_sender<DummyStruct>();
        // OK
        scenario.return_to_sender(dummy);
    };

    scenario.next_tx(addr);
    {
        let dummy = scenario.take_from_sender<DummyStruct>();
        let rand = test_scenario::take_shared<random::Random>(&scenario);
        // OK
        scenario.return_to_sender(dummy);
        test_scenario::return_shared(rand);
    };

    scenario.next_tx(addr);
    {
        let dummy = scenario.take_from_sender<DummyStruct>(); // Fails here
        // FAIL
        scenario.return_to_sender(dummy);
    };

    scenario.end();

    /*
    Test failures:

    Failures in random_example::random_example_tests:

    ā”Œā”€ā”€ test_random_example ā”€ā”€ā”€ā”€ā”€ā”€
    ā”‚ error[E11001]: test failure
    ā”‚     ā”Œā”€ ~/.move/https___github_com_MystenLabs_sui_git_framework__testnet/crates/sui-framework/packages/sui-framework/sources/test/test_scenario.move:231:19
    ā”‚     ā”‚
    ā”‚ 231 ā”‚ public native fun take_from_address_by_id<T: key>(scenario: &Scenario, account: address, id: ID): T;
    ā”‚     ā”‚                   ^^^^^^^^^^^^^^^^^^^^^^^
    ā”‚     ā”‚                   ā”‚
    ā”‚     ā”‚                   Test was not expected to error, but it aborted with code 4 originating in the module sui::test_scenario rooted here
    ā”‚     ā”‚                   In this function in sui::test_scenario
    ā”‚ 
    ā”‚ 
    ā”‚ stack trace
    ā”‚       test_scenario::take_from_sender(~/.move/https___github_com_MystenLabs_sui_git_framework__testnet/crates/sui-framework/packages/sui-framework/sources/test/test_scenario.move:284)
    ā”‚       random_example_tests::test_random_example(./tests/random_example_tests.move:46)
    ā”‚ 
    ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€

    Test result: FAILED. Total tests: 1; passed: 0; failed: 1
    */
}

As you can see, it throws an exception because it is unable to find the DummyStruct object in the sender inventory after borrow the shared object. The exact same behavior works as expected when using another shared object.

Am I missing something or is this a bug with the random module?

Kind regards,
06 87

1 Like