runtime.rs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. blockchain::Blockchain,
  20. consensus::{TESTNET_GENESIS_HASH_BYTES, TESTNET_GENESIS_TIMESTAMP},
  21. runtime::vm_runtime::Runtime,
  22. Result,
  23. };
  24. use darkfi_sdk::{crypto::ContractId, pasta::pallas, tx::FuncCall};
  25. use darkfi_serial::{serialize, Encodable, WriteExt};
  26. use smart_contract::{FooCallData, Function};
  27. #[test]
  28. fn run_contract() -> Result<()> {
  29. // Debug log configuration
  30. let mut cfg = simplelog::ConfigBuilder::new();
  31. cfg.add_filter_ignore("sled".to_string());
  32. simplelog::TermLogger::init(
  33. simplelog::LevelFilter::Debug,
  34. cfg.build(),
  35. simplelog::TerminalMode::Mixed,
  36. simplelog::ColorChoice::Auto,
  37. )?;
  38. // =============================
  39. // Initialize a dummy blockchain
  40. // =============================
  41. // TODO: This blockchain interface should perhaps be ValidatorState and Mutex/RwLock.
  42. let db = sled::Config::new().temporary(true).open()?;
  43. let blockchain = Blockchain::new(&db, *TESTNET_GENESIS_TIMESTAMP, *TESTNET_GENESIS_HASH_BYTES)?;
  44. // ================================================================
  45. // Load the wasm binary into memory and create an execution runtime
  46. // ================================================================
  47. let wasm_bytes = std::fs::read("contract.wasm")?;
  48. let contract_id = ContractId::new(pallas::Base::from(1));
  49. let mut runtime = Runtime::new(&wasm_bytes, blockchain, contract_id)?;
  50. // Deploy function to initialize the smart contract state.
  51. // Here we pass an empty payload, but it's possible to feed in arbitrary data.
  52. runtime.deploy(&[])?;
  53. // =============================================
  54. // Build some kind of payload to show an example
  55. // =============================================
  56. let func_calls = vec![FuncCall {
  57. contract_id: pallas::Base::from(110),
  58. func_id: pallas::Base::from(4),
  59. call_data: serialize(&FooCallData { a: 777, b: 666 }),
  60. }];
  61. let func_call_index: u32 = 0;
  62. let mut payload = Vec::new();
  63. // Selects which path executes in the contract.
  64. payload.write_u8(Function::Foo as u8)?;
  65. // Write the actual payload data
  66. payload.write_u32(func_call_index)?;
  67. func_calls.encode(&mut payload)?;
  68. // ============================================================
  69. // Serialize the payload into the runtime format and execute it
  70. // ============================================================
  71. let update = runtime.exec(&payload)?;
  72. // =====================================================
  73. // If exec was successful, try to apply the state change
  74. // =====================================================
  75. runtime.apply(&update)?;
  76. Ok(())
  77. }