如何在单元测试期间更改分类帐的纪元?

How to change ledger's epoch during unit test?

我正在尝试在单个单元测试中更改纪元:

  1. 我运行一个方法
  2. 我切换到下一个纪元
  3. 我运行另一种方法(在同一个子状态存储上)

如果我在其上使用 TransactionExecutor,我似乎无法改变子状态存储的纪元:

use radix_engine::ledger::*;
use radix_engine::transaction::*;
use scrypto::prelude::*;

#[test]
fn can_tick_from_scratch() {
    let mut ledger = InMemorySubstateStore::with_bootstrap();

    ledger.set_epoch(15);

    let mut executor = TransactionExecutor::new(&mut ledger, false);

    // Run transactions
    // ...

    ledger.set_epoch(16);

    // Run other transactions
    // ...
}
error[E0499]: cannot borrow `ledger` as mutable more than once at a time
  --> tests/lib.rs:16:5
   |
11 |     let mut executor = TransactionExecutor::new(&mut ledger, false);
   |                                                 ----------- first mutable borrow occurs here ...
16 |     ledger.set_epoch(16);
   |     ^^^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
17 | 
18 |     let (pk, sk, oracle_owner) = executor.new_account();
   |                                  ---------------------- first borrow later used here

如何更改传递给 TransactionExecutorInMemorySubstateStore 的纪元?

确实不能在将 InMemorySubstateStore 变量传递给 TransactionExecutor 后直接对其进行变异。 你要做的是改变执行者的substate_store状态:

use radix_engine::ledger::*;
use radix_engine::transaction::*;
use scrypto::prelude::*;

#[test]
fn can_tick_from_scratch() {
    let mut ledger = InMemorySubstateStore::with_bootstrap();

    ledger.set_epoch(15);

    let mut executor = TransactionExecutor::new(&mut ledger, false);

    // Run transactions
    // ...

    executor.substate_store_mut().set_epoch(16);

    // Run other transactions
    // ...
}