runtime.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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::{crypto::contract_id::ContractId, runtime::vm_runtime::Runtime, Result};
  19. use darkfi_sdk::{
  20. pasta::pallas,
  21. tx::{FuncCall, Transaction},
  22. };
  23. use darkfi_serial::{serialize, Encodable, WriteExt};
  24. use smart_contract::{FooCallData, Function};
  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 contract_id = ContractId::new(pallas::Base::from(1));
  47. let mut runtime = Runtime::new(&wasm_bytes, contract_id)?;
  48. // Deploy function to initialize the smart contract state.
  49. // Here we pass an empty payload, but it's possible to feed in arbitrary data.
  50. runtime.deploy(&[])?;
  51. // =============================================
  52. // Build some kind of payload to show an example
  53. // =============================================
  54. let tx = Transaction {
  55. func_calls: vec![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. signatures: Vec::new(),
  62. };
  63. let func_call_index: u32 = 0;
  64. let mut payload = Vec::new();
  65. // Selects which path executes in the contract.
  66. payload.write_u8(Function::Foo as u8)?;
  67. // Write the actual payload data
  68. payload.write_u32(func_call_index)?;
  69. tx.encode(&mut payload)?;
  70. // ============================================================
  71. // Serialize the payload into the runtime format and execute it
  72. // ============================================================
  73. runtime.exec(&payload)?;
  74. // =====================================================
  75. // If exec was successful, try to apply the state change
  76. // =====================================================
  77. runtime.apply()?;
  78. Ok(())
  79. }