runtime.rs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, tx::{Transaction, FuncCall}};
  24. use darkfi_serial::{serialize, Encodable, WriteExt};
  25. use smart_contract::FooCallData;
  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 tx = Transaction {
  54. func_calls: vec![
  55. FuncCall {
  56. contract_id: pallas::Base::from(110),
  57. func_id: pallas::Base::from(4),
  58. call_data: serialize(&FooCallData { a: 777, b: 666 }),
  59. proofs: Vec::new()
  60. }
  61. ],
  62. signatures: Vec::new()
  63. };
  64. let func_call_index: u32 = 0;
  65. let mut payload = Vec::new();
  66. // Prepend the func id = 0x00
  67. // Selects which path executes in the contract.
  68. payload.write_u8(0x00);
  69. // Write the actual payload data
  70. payload.write_u32(func_call_index);
  71. tx.encode(&mut payload)?;
  72. // ============================================================
  73. // Serialize the payload into the runtime format and execute it
  74. // ============================================================
  75. runtime.exec(&serialize_payload(&payload))?;
  76. runtime.apply()?;
  77. //Ok(())
  78. //runtime.exec(&serialize_payload(&payload))?;
  79. //runtime.apply()?;
  80. Ok(())
  81. }