contractstore.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_sdk::crypto::ContractId;
  19. use darkfi_serial::{deserialize, serialize};
  20. use crate::{
  21. Error::{ContractAlreadyInitialized, ContractNotFound, ContractStateNotFound},
  22. Result,
  23. };
  24. #[derive(Clone)]
  25. pub struct ContractStore(sled::Tree);
  26. const SLED_CONTRACTS_TREE: &[u8] = b"_contracts";
  27. impl ContractStore {
  28. pub fn new(db: &sled::Db) -> Result<Self> {
  29. let tree = db.open_tree(SLED_CONTRACTS_TREE)?;
  30. Ok(Self(tree))
  31. }
  32. pub fn init(
  33. &self,
  34. db: &sled::Db,
  35. contract_id: &ContractId,
  36. tree_name: &str,
  37. ) -> Result<sled::Tree> {
  38. let contract_id_bytes = serialize(contract_id);
  39. // If the db was never initialized, it should not be in here.
  40. if self.0.contains_key(&contract_id_bytes)? {
  41. return Err(ContractAlreadyInitialized)
  42. }
  43. let mut hasher = blake3::Hasher::new();
  44. hasher.update(&contract_id_bytes);
  45. hasher.update(&tree_name.as_bytes());
  46. let ptr = hasher.finalize();
  47. // Now we add it so it's marked as initialized
  48. self.0.insert(&contract_id_bytes, ptr.as_bytes())?;
  49. // We open the tree and return its handle
  50. let tree = db.open_tree(ptr.as_bytes())?;
  51. Ok(tree)
  52. }
  53. pub fn lookup(
  54. &self,
  55. db: &sled::Db,
  56. contract_id: &ContractId,
  57. tree_name: &str,
  58. ) -> Result<sled::Tree> {
  59. let contract_id_bytes = serialize(contract_id);
  60. // A guard to make sure we went through init()
  61. if !self.0.contains_key(&contract_id_bytes)? {
  62. return Err(ContractNotFound(contract_id.to_string()))
  63. }
  64. let state_pointers = self.0.get(&contract_id_bytes)?.unwrap();
  65. let state_pointers: Vec<[u8; 32]> = deserialize(&state_pointers)?;
  66. let mut hasher = blake3::Hasher::new();
  67. hasher.update(&contract_id_bytes);
  68. hasher.update(&tree_name.as_bytes());
  69. let ptr = hasher.finalize();
  70. // We assume the tree has been created already, so it should be listed in this array.
  71. // If not, that's an error.
  72. if !state_pointers.contains(ptr.as_bytes()) {
  73. return Err(ContractStateNotFound)
  74. }
  75. // We open the tree and return its handle
  76. let tree = db.open_tree(ptr.as_bytes())?;
  77. Ok(tree)
  78. }
  79. }