utils.rs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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_sdk::crypto::{PublicKey, CONSENSUS_CONTRACT_ID, DAO_CONTRACT_ID, MONEY_CONTRACT_ID};
  19. use darkfi_serial::serialize;
  20. use log::info;
  21. use crate::{
  22. blockchain::BlockchainOverlayPtr, runtime::vm_runtime::Runtime, util::time::TimeKeeper, Result,
  23. };
  24. /// Deploy DarkFi native wasm contracts to provided blockchain overlay.
  25. /// If overlay already contains the contracts, it will just open the
  26. /// necessary db and trees, and give back what it has. This means that
  27. /// on subsequent runs, our native contracts will already be in a deployed
  28. /// state, so what we actually do here is a redeployment. This kind of
  29. /// operation should only modify the contract's state in case it wasn't
  30. /// deployed before (meaning the initial run). Otherwise, it shouldn't
  31. /// touch anything, or just potentially update the db schemas or whatever
  32. /// is necessary. This logic should be handled in the init function of
  33. /// the actual contract, so make sure the native contracts handle this well.
  34. pub fn deploy_native_contracts(
  35. overlay: &BlockchainOverlayPtr,
  36. time_keeper: &TimeKeeper,
  37. faucet_pubkeys: &Vec<PublicKey>,
  38. ) -> Result<()> {
  39. info!(target: "validator::utils::deploy_native_contracts", "Deploying native WASM contracts");
  40. // The faucet pubkeys are pubkeys which are allowed to create clear inputs
  41. // in the Money contract.
  42. let money_contract_deploy_payload = serialize(faucet_pubkeys);
  43. // The DAO contract uses an empty payload to deploy itself.
  44. let dao_contract_deploy_payload = vec![];
  45. // The Consensus contract uses an empty payload to deploy itself.
  46. let consensus_contract_deploy_payload = vec![];
  47. let native_contracts = vec![
  48. (
  49. "Money Contract",
  50. *MONEY_CONTRACT_ID,
  51. include_bytes!("../contract/money/money_contract.wasm").to_vec(),
  52. money_contract_deploy_payload,
  53. ),
  54. (
  55. "DAO Contract",
  56. *DAO_CONTRACT_ID,
  57. include_bytes!("../contract/dao/dao_contract.wasm").to_vec(),
  58. dao_contract_deploy_payload,
  59. ),
  60. (
  61. "Consensus Contract",
  62. *CONSENSUS_CONTRACT_ID,
  63. include_bytes!("../contract/consensus/consensus_contract.wasm").to_vec(),
  64. consensus_contract_deploy_payload,
  65. ),
  66. ];
  67. for nc in native_contracts {
  68. info!(target: "validator::utils::deploy_native_contracts", "Deploying {} with ContractID {}", nc.0, nc.1);
  69. let mut runtime = Runtime::new(&nc.2[..], overlay.clone(), nc.1, time_keeper.clone())?;
  70. runtime.deploy(&nc.3)?;
  71. info!(target: "validator::utils::deploy_native_contracts", "Successfully deployed {}", nc.0);
  72. }
  73. info!(target: "validator::utils::deploy_native_contracts", "Finished deployment of native WASM contracts");
  74. Ok(())
  75. }