runtime.rs 1.8 KB

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