runtime.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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
  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 func_calls = vec![FuncCall {
  55. contract_id: pallas::Base::from(110),
  56. func_id: pallas::Base::from(4),
  57. call_data: serialize(&FooCallData { a: 777, b: 666 }),
  58. }];
  59. let func_call_index: u32 = 0;
  60. let mut payload = Vec::new();
  61. // Selects which path executes in the contract.
  62. payload.write_u8(Function::Foo as u8)?;
  63. // Write the actual payload data
  64. payload.write_u32(func_call_index)?;
  65. func_calls.encode(&mut payload)?;
  66. // ============================================================
  67. // Serialize the payload into the runtime format and execute it
  68. // ============================================================
  69. runtime.exec(&payload)?;
  70. // =====================================================
  71. // If exec was successful, try to apply the state change
  72. // =====================================================
  73. runtime.apply()?;
  74. Ok(())
  75. }