runtime.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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::from(pallas::Base::from(1));
  50. let mut runtime = Runtime::new(&wasm_bytes, blockchain.clone(), 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. // This is another call so we instantiate a new runtime.
  55. let mut runtime = Runtime::new(&wasm_bytes, blockchain, contract_id)?;
  56. // =============================================
  57. // Build some kind of payload to show an example
  58. // =============================================
  59. let func_calls = vec![FuncCall {
  60. contract_id: pallas::Base::from(110),
  61. func_id: pallas::Base::from(4),
  62. call_data: serialize(&FooCallData { a: 777, b: 666 }),
  63. }];
  64. let func_call_index: u32 = 0;
  65. let mut payload = Vec::new();
  66. // Selects which path executes in the contract.
  67. payload.write_u8(Function::Foo as u8)?;
  68. // Write the actual payload data
  69. payload.write_u32(func_call_index)?;
  70. func_calls.encode(&mut payload)?;
  71. // ============================================================
  72. // Serialize the payload into the runtime format and execute it
  73. // ============================================================
  74. let update = runtime.exec(&payload)?;
  75. // =====================================================
  76. // If exec was successful, try to apply the state change
  77. // =====================================================
  78. runtime.apply(&update)?;
  79. // =====================================================
  80. // Verify ZK proofs and signatures
  81. // =====================================================
  82. let metadata = runtime.metadata(&payload)?;
  83. let mut decoder = Cursor::new(&metadata);
  84. let zk_public_values: Vec<(String, Vec<pallas::Base>)> = Decodable::decode(&mut decoder)?;
  85. let signature_public_keys: Vec<pallas::Point> = Decodable::decode(decoder)?;
  86. Ok(())
  87. }