runtime.rs 3.9 KB

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