Skip to content

Commit

Permalink
Adds ProverState
Browse files Browse the repository at this point in the history
  • Loading branch information
howardwu committed Nov 30, 2021
1 parent b2d1107 commit 598a81c
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
9 changes: 8 additions & 1 deletion storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ mod helpers;
pub use helpers::BlockLocators;

pub(crate) mod state;
pub use state::{LedgerState, Metadata, MAXIMUM_BLOCK_LOCATORS, MAXIMUM_LINEAR_BLOCK_LOCATORS, MAXIMUM_QUADRATIC_BLOCK_LOCATORS};
pub use state::{
LedgerState,
Metadata,
ProverState,
MAXIMUM_BLOCK_LOCATORS,
MAXIMUM_LINEAR_BLOCK_LOCATORS,
MAXIMUM_QUADRATIC_BLOCK_LOCATORS,
};

pub mod storage;
3 changes: 3 additions & 0 deletions storage/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
pub(crate) mod ledger;
pub use ledger::{LedgerState, Metadata, MAXIMUM_BLOCK_LOCATORS, MAXIMUM_LINEAR_BLOCK_LOCATORS, MAXIMUM_QUADRATIC_BLOCK_LOCATORS};

pub(crate) mod prover;
pub use prover::ProverState;

#[cfg(test)]
mod tests;
120 changes: 120 additions & 0 deletions storage/src/state/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the snarkOS library.

// The snarkOS library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The snarkOS library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.

use crate::storage::{DataMap, Map, Storage};
use snarkvm::dpc::prelude::*;

use anyhow::{anyhow, Result};
use std::path::Path;

#[derive(Debug)]
pub struct ProverState<N: Network> {
/// The coinbase records of the prover in storage.
coinbase: CoinbaseState<N>,
}

impl<N: Network> ProverState<N> {
///
/// Opens a new writable instance of `ProverState` from the given storage path.
///
pub fn open_writer<S: Storage, P: AsRef<Path>>(path: P) -> Result<Self> {
// Open storage.
let context = N::NETWORK_ID;
let is_read_only = false;
let storage = S::open(path, context, is_read_only)?;

// Initialize the prover.
let prover = Self {
coinbase: CoinbaseState::open(storage)?,
};

// let value = storage.export()?;
// println!("{}", value);
// let storage_2 = S::open(".ledger_2", context)?;
// storage_2.import(value)?;

info!("Prover successfully initialized");
Ok(prover)
}

/// Returns `true` if the given commitment exists in storage.
pub fn contains_coinbase_record(&self, commitment: &N::Commitment) -> Result<bool> {
self.coinbase.contains_record(commitment)
}

/// Returns the coinbase record for a given commitment.
pub fn get_coinbase_record(&self, commitment: &N::Commitment) -> Result<Record<N>> {
self.coinbase.get_record(commitment)
}

/// Adds the given coinbase record to storage.
pub fn add_coinbase_record(&self, record: &Record<N>) -> Result<()> {
self.coinbase.add_record(record)
}

/// Removes the given record from storage.
pub fn remove_coinbase_record(&self, commitment: &N::Commitment) -> Result<()> {
self.coinbase.remove_record(commitment)
}
}

#[derive(Clone, Debug)]
#[allow(clippy::type_complexity)]
struct CoinbaseState<N: Network> {
records: DataMap<N::Commitment, Record<N>>,
}

impl<N: Network> CoinbaseState<N> {
/// Initializes a new instance of `CoinbaseState`.
fn open<S: Storage>(storage: S) -> Result<Self> {
Ok(Self {
records: storage.open_map("records")?,
})
}

/// Returns `true` if the given commitment exists in storage.
fn contains_record(&self, commitment: &N::Commitment) -> Result<bool> {
self.records.contains_key(commitment)
}

/// Returns the record for a given commitment.
fn get_record(&self, commitment: &N::Commitment) -> Result<Record<N>> {
match self.records.get(commitment)? {
Some(record) => Ok(record),
None => return Err(anyhow!("Record with commitment {} does not exist in storage", commitment)),
}
}

/// Adds the given record to storage.
fn add_record(&self, record: &Record<N>) -> Result<()> {
// Ensure the record does not exist.
let commitment = record.commitment();
if self.records.contains_key(&commitment)? {
Err(anyhow!("Record with commitment {} already exists in storage", commitment))
} else {
// Insert the record.
self.records.insert(&commitment, &record)?;
Ok(())
}
}

/// Removes the given record from storage.
fn remove_record(&self, commitment: &N::Commitment) -> Result<()> {
// Remove the record entry.
self.records.remove(commitment)?;
Ok(())
}
}

0 comments on commit 598a81c

Please sign in to comment.