runtime.rs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{
  19. node::{MemoryState, State},
  20. runtime::{util::serialize_payload, vm_runtime::Runtime},
  21. Result,
  22. };
  23. use darkfi_sdk::{crypto::nullifier::Nullifier, pasta::pallas};
  24. use darkfi_serial::serialize;
  25. use smart_contract::Args;
  26. #[test]
  27. fn run_contract() -> Result<()> {
  28. // Debug log configuration
  29. let mut cfg = simplelog::ConfigBuilder::new();
  30. cfg.add_filter_ignore("sled".to_string());
  31. simplelog::TermLogger::init(
  32. simplelog::LevelFilter::Debug,
  33. cfg.build(),
  34. simplelog::TerminalMode::Mixed,
  35. simplelog::ColorChoice::Auto,
  36. )?;
  37. // =============================================================
  38. // Build a ledger state so the runtime has something to work on
  39. // =============================================================
  40. let state_machine = State::dummy()?;
  41. // Add a nullifier to the nullifier set. (This is checked by the contract)
  42. state_machine.nullifiers.insert(&[Nullifier::from(pallas::Base::from(0x10))])?;
  43. // ================================================================
  44. // Load the wasm binary into memory and create an execution runtime
  45. // ================================================================
  46. let wasm_bytes = std::fs::read("contract.wasm")?;
  47. let mut runtime = Runtime::new(&wasm_bytes, MemoryState::new(state_machine))?;
  48. // =============================================
  49. // Build some kind of payload to show an example
  50. // =============================================
  51. let args = Args { a: 777, b: 666 };
  52. let payload = serialize(&args);
  53. // ============================================================
  54. // Serialize the payload into the runtime format and execute it
  55. // ============================================================
  56. runtime.run(&serialize_payload(&payload))
  57. }