memorystate.rs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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::{constants::MERKLE_DEPTH, MerkleNode, Nullifier};
  19. use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
  20. use log::debug;
  21. use super::state::{ProgramState, State, StateUpdate};
  22. use crate::crypto::{keypair::PublicKey, proof::VerifyingKey};
  23. /// In-memory state extension for state transition validations
  24. #[derive(Clone)]
  25. pub struct MemoryState {
  26. /// Canonical state
  27. pub canon: State,
  28. /// The entire Merkle tree state (copied from `canon`)
  29. pub tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  30. /// List of all previous and the current merkle roots.
  31. pub merkle_roots: Vec<MerkleNode>,
  32. /// Nullifiers prevent double-spending
  33. pub nullifiers: Vec<Nullifier>,
  34. }
  35. impl ProgramState for MemoryState {
  36. fn is_valid_cashier_public_key(&self, public: &PublicKey) -> bool {
  37. self.canon.is_valid_cashier_public_key(public)
  38. }
  39. fn is_valid_faucet_public_key(&self, public: &PublicKey) -> bool {
  40. self.canon.is_valid_faucet_public_key(public)
  41. }
  42. fn is_valid_merkle(&self, merkle_root: &MerkleNode) -> bool {
  43. self.merkle_roots.contains(merkle_root) || self.canon.is_valid_merkle(merkle_root)
  44. }
  45. fn nullifier_exists(&self, nullifier: &Nullifier) -> bool {
  46. self.nullifiers.contains(nullifier) || self.canon.nullifier_exists(nullifier)
  47. }
  48. fn mint_vk(&self) -> &VerifyingKey {
  49. self.canon.mint_vk()
  50. }
  51. fn burn_vk(&self) -> &VerifyingKey {
  52. self.canon.burn_vk()
  53. }
  54. }
  55. impl MemoryState {
  56. pub fn new(canon_state: State) -> Self {
  57. Self {
  58. canon: canon_state.clone(),
  59. tree: canon_state.tree,
  60. merkle_roots: vec![],
  61. nullifiers: vec![],
  62. }
  63. }
  64. pub fn apply(&mut self, update: StateUpdate) {
  65. debug!(target: "state_apply", "(in-memory) Extend nullifier set");
  66. let mut nfs = update.nullifiers.clone();
  67. self.nullifiers.append(&mut nfs);
  68. debug!(target: "state_apply", "(in-memory) Update Merkle tree and witnesses");
  69. for coin in update.coins {
  70. let node = MerkleNode::from(coin.0);
  71. self.tree.append(&node);
  72. self.merkle_roots.push(self.tree.root(0).unwrap());
  73. }
  74. debug!(target: "state_apply", "(in-memory) Finished apply() successfully.");
  75. }
  76. }