use std::sync::Arc;
use moonproof_ledger::base_crypto::hash::HashOutput;
use moonproof_ledger::base_crypto::time::Timestamp;
use moonproof_ledger::coin_structure::contract::ContractAddress;
use moonproof_ledger::ledger::structure::INITIAL_PARAMETERS;
use moonproof_runtime::executable::{CircuitCallContext, ConstructorContext, InitializeResult};
use moonproof_runtime::types::CoinPublicKey;
use moonproof_runtime::CompactZswapLocalState;
use moonproof_runtime::{IrExecutable, NoWitnesses};
use moonproof_runtime_ir::RuntimeValue as Value;
const BLOCK_SECS: u64 = 1_000;
fn block_time_ir_path() -> std::path::PathBuf {
let mut p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
p.pop();
p.push("runtime-ir/tests/fixtures/extracted_block_time.cbor");
p
}
async fn loaded() -> (IrExecutable<NoWitnesses>, InitializeResult) {
let exec = IrExecutable::from_path(block_time_ir_path(), Arc::new(NoWitnesses))
.expect("block_time IR loads");
let init = exec
.initialize(
ConstructorContext {
initial_private_state: Value::Null,
initial_zswap_local_state: CompactZswapLocalState::empty(CoinPublicKey([0u8; 32])),
coin_public_key: CoinPublicKey([0u8; 32]),
signing_key: moonproof_ledger::signatures::schnorr_signing_key(&[0x42u8; 32])
.expect("32 bytes parse"),
time: None,
},
Value::Null,
)
.await
.expect("block_time initialize");
(exec, init)
}
async fn call(circuit: &str, t: u64) -> Value {
let (exec, init) = loaded().await;
let ctx = CircuitCallContext {
contract_address: ContractAddress(HashOutput([0x77u8; 32])),
contract_state: init.initial_contract_state,
private_state: Value::Null,
ledger_parameters: INITIAL_PARAMETERS,
coin_public_key: CoinPublicKey([0u8; 32]),
tblock: Timestamp::from_secs(BLOCK_SECS),
};
exec.execute_circuit(circuit, ctx, Value::Int(i128::from(t)))
.await
.unwrap_or_else(|e| panic!("{circuit}({t}) executes: {e}"))
.result
}
#[tokio::test]
async fn block_time_gt_compares_against_the_supplied_tblock() {
assert_eq!(call("after", BLOCK_SECS - 1).await, Value::Bool(true));
assert_eq!(call("after", BLOCK_SECS + 1).await, Value::Bool(false));
}
#[tokio::test]
async fn block_time_lt_compares_against_the_supplied_tblock() {
assert_eq!(call("before", BLOCK_SECS + 1).await, Value::Bool(true));
assert_eq!(call("before", BLOCK_SECS - 1).await, Value::Bool(false));
}