runtime.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 sled_db = sled::Config::new().temporary(true).open()?;
  28. let blockchain =
  29. Blockchain::new(&sled_db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
  30. let merkle_tree = BridgeTree::<MerkleNode, 32>::new(100);
  31. let state_machine = State {
  32. tree: merkle_tree,
  33. merkle_roots: blockchain.merkle_roots,
  34. nullifiers: blockchain.nullifiers,
  35. cashier_pubkeys: vec![],
  36. faucet_pubkeys: vec![],
  37. mint_vk: Lazy::new(),
  38. burn_vk: Lazy::new(),
  39. };
  40. // We check if this nullifier is in the set from the contract
  41. state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
  42. // ================================================================
  43. // Load the wasm binary into memory and create an execution runtime
  44. // ================================================================
  45. let wasm_bytes = std::fs::read("smart_contract.wasm")?;
  46. let mut runtime = Runtime::new(&wasm_bytes, MemoryState::new(state_machine))?;
  47. // ===========================================================
  48. // Build some kind of payload for the wasm entrypoint function
  49. // ===========================================================
  50. let args = Args { a: 777, b: 666 };
  51. let payload = serialize(&args);
  52. // ============================================================
  53. // Serialize the payload into the runtime format and execute it
  54. // ============================================================
  55. runtime.run(&serialize_payload(&payload))
  56. }