我需要更改 block_timestamp 来测试定时投票。
使用 VMContext,使用以下命令更改 block_timestamp:
context.block_timestamp = get_timestamp();
testing_env!(context.clone());
Run Code Online (Sandbox Code Playgroud)
如何在near_sdk_sim中做到这一点?
let (root, contract) = deploy_contract();
// Change the block_timestamp
Run Code Online (Sandbox Code Playgroud)
运行时有block_timestamp(),不知道如何使用它。
小智 0
// You should start with the master_account returned by init_simulator
// You can change the genesis config by giving
// near_sdk_sim::runtime::GenesisConfig to init_simulator instead of None
// FYI, the default value of block timestamp was 0 with None
let master_account = init_simulator(None);
// If you don't wrap this code block below in brackets
// or don't call it as a function from outside,
// you will encounter the error 'already borrowed: BorrowMutError'.
{
let mut runtime = master_account.borrow_runtime_mut();
// In near_sdk_sim, 1 block takes 1 second !
// So it changes the block timestamp 0 to 10000000000
runtime.produce_blocks(10u64);
}
Run Code Online (Sandbox Code Playgroud)