runtime.rs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use incrementalmerkletree::bridgetree::BridgeTree;
  2. use lazy_init::Lazy;
  3. use pasta_curves::pallas;
  4. use darkfi::{
  5. blockchain::Blockchain,
  6. consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
  7. crypto::{merkle_node::MerkleNode, nullifier::Nullifier},
  8. node::{MemoryState, State},
  9. runtime::{util::serialize_payload, vm_runtime::Runtime},
  10. serial::serialize,
  11. Result,
  12. };
  13. use smart_contract::Args;
  14. #[test]
  15. fn run_contract() -> Result<()> {
  16. let mut logcfg = simplelog::ConfigBuilder::new();
  17. logcfg.add_filter_ignore("sled".to_string());
  18. simplelog::TermLogger::init(
  19. simplelog::LevelFilter::Debug,
  20. logcfg.build(),
  21. simplelog::TerminalMode::Mixed,
  22. simplelog::ColorChoice::Auto,
  23. )?;
  24. // ============================================================
  25. // Build a ledger state so the runtime has something to work on
  26. // ============================================================
  27. let state_machine = MemoryState::new(State::dummy())?;
  28. // We check if this nullifier is in the set from the contract
  29. state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
  30. // ================================================================
  31. // Load the wasm binary into memory and create an execution runtime
  32. // ================================================================
  33. let wasm_bytes = std::fs::read("smart_contract.wasm")?;
  34. let mut runtime = Runtime::new(&wasm_bytes, MemoryState::new(state_machine))?;
  35. // ===========================================================
  36. // Build some kind of payload for the wasm entrypoint function
  37. // ===========================================================
  38. let args = Args { a: 777, b: 666 };
  39. let payload = serialize(&args);
  40. // ============================================================
  41. // Serialize the payload into the runtime format and execute it
  42. // ============================================================
  43. runtime.run(&serialize_payload(&payload))
  44. }