runtime.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. crypto::contract_id::ContractId,
  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::FooArgs;
  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 contract_id = ContractId::new(pallas::Base::from(1));
  48. let mut runtime = Runtime::new(&wasm_bytes, contract_id)?;
  49. runtime.deploy()?;
  50. // =============================================
  51. // Build some kind of payload to show an example
  52. // =============================================
  53. let args = FooArgs { a: 777, b: 666 };
  54. // Prepend the func id
  55. let mut payload = vec![0x00];
  56. payload.extend_from_slice(&serialize(&args));
  57. // ============================================================
  58. // Serialize the payload into the runtime format and execute it
  59. // ============================================================
  60. runtime.exec(&serialize_payload(&payload))?;
  61. runtime.apply()?;
  62. //Ok(())
  63. //runtime.exec(&serialize_payload(&payload))?;
  64. //runtime.apply()?;
  65. Ok(())
  66. }