runtime.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. runtime::{util::serialize_payload, vm_runtime::Runtime},
  20. Result,
  21. };
  22. use darkfi_sdk::{crypto::nullifier::Nullifier, pasta::pallas};
  23. use darkfi_serial::serialize;
  24. use smart_contract::Args;
  25. #[test]
  26. fn run_contract() -> Result<()> {
  27. // Debug log configuration
  28. let mut cfg = simplelog::ConfigBuilder::new();
  29. cfg.add_filter_ignore("sled".to_string());
  30. simplelog::TermLogger::init(
  31. simplelog::LevelFilter::Debug,
  32. cfg.build(),
  33. simplelog::TerminalMode::Mixed,
  34. simplelog::ColorChoice::Auto,
  35. )?;
  36. // =============================================================
  37. // Build a ledger state so the runtime has something to work on
  38. // =============================================================
  39. //let state_machine = State::dummy()?;
  40. // Add a nullifier to the nullifier set. (This is checked by 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("contract.wasm")?;
  46. let mut runtime = Runtime::new(&wasm_bytes)?;
  47. // =============================================
  48. // Build some kind of payload to show an example
  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. }