runtime.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use darkfi::{
  2. crypto::nullifier::Nullifier,
  3. node::{MemoryState, State},
  4. runtime::{util::serialize_payload, vm_runtime::Runtime},
  5. serial::serialize,
  6. Result,
  7. };
  8. use darkfi_sdk::pasta::pallas;
  9. use smart_contract::Args;
  10. #[test]
  11. fn run_contract() -> Result<()> {
  12. // Debug log configuration
  13. let mut cfg = simplelog::ConfigBuilder::new();
  14. cfg.add_filter_ignore("sled".to_string());
  15. simplelog::TermLogger::init(
  16. simplelog::LevelFilter::Debug,
  17. cfg.build(),
  18. simplelog::TerminalMode::Mixed,
  19. simplelog::ColorChoice::Auto,
  20. )?;
  21. // =============================================================
  22. // Build a ledger state so the runtime has something to work on
  23. // =============================================================
  24. let state_machine = State::dummy()?;
  25. // Add a nullifier to the nullifier set. (This is checked by the contract)
  26. state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
  27. // ================================================================
  28. // Load the wasm binary into memory and create an execution runtime
  29. // ================================================================
  30. let wasm_bytes = std::fs::read("contract.wasm")?;
  31. let mut runtime = Runtime::new(&wasm_bytes, MemoryState::new(state_machine))?;
  32. // =============================================
  33. // Build some kind of payload to show an example
  34. // =============================================
  35. let args = Args { a: 777, b: 666 };
  36. let payload = serialize(&args);
  37. // ============================================================
  38. // Serialize the payload into the runtime format and execute it
  39. // ============================================================
  40. runtime.run(&serialize_payload(&payload))
  41. }