Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support historical logging for delegated, bonded, and unbonding` mappings. #2493

Merged
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions synthesizer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ snark = [ "synthesizer-snark" ]
aleo-cli = [ ]
async = [ "ledger-query/async", "synthesizer-process/async" ]
cuda = [ "algorithms/cuda" ]
history = [ "serde_json" ]
history = [ "serde", "serde_json" ]
rocks = [ "ledger-store/rocks" ]
serial = [
"console/serial",
@@ -163,8 +163,13 @@ version = "0.8"
version = "1"
optional = true

[dependencies.serde]
version = "1.0"
optional = true

[dependencies.serde_json]
version = "1.0.117"
version = "1.0"
features = [ "preserve_order" ]
optional = true

[dependencies.tracing]
6 changes: 3 additions & 3 deletions synthesizer/src/vm/finalize.rs
Original file line number Diff line number Diff line change
@@ -1267,14 +1267,14 @@ impl<N: Network, C: ConsensusStorage<N>> VM<N, C> {
let history = History::new(N::ID, store.storage_mode().clone());

// Write the delegated mapping as JSON.
history.store_entry(state.block_height(), HistoryVariant::Delegated, &next_delegated_map)?;
history.store_entry(state.block_height(), MappingName::Delegated, &next_delegated_map)?;
// Write the bonded mapping as JSON.
history.store_entry(state.block_height(), HistoryVariant::Bonded, &next_bonded_map)?;
history.store_entry(state.block_height(), MappingName::Bonded, &next_bonded_map)?;

// TODO: Write the unbonding mapping.
let unbonding_mapping = Identifier::from_str("unbonding")?;
let unbonding_map = store.get_mapping_speculative(program_id, unbonding_mapping)?;
history.store_entry(state.block_height(), HistoryVariant::Unbonding, &unbonding_map)?;
history.store_entry(state.block_height(), MappingName::Unbonding, &unbonding_map)?;
}

// Store the finalize operations for updating the committee and bonded mapping.
48 changes: 25 additions & 23 deletions synthesizer/src/vm/helpers/history.rs
Original file line number Diff line number Diff line change
@@ -12,22 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use console::prelude::Serialize;
use console::prelude::{Deserialize, Serialize};

use aleo_std::{aleo_ledger_dir, StorageMode};

use anyhow::Result;
use serde_json;
use std::path::PathBuf;
use std::{
fmt::{Display, Formatter},
path::PathBuf,
};

/// Returns the path where a history directory may be stored.
/// Returns the path where a `history` directory may be stored.
pub fn history_directory_path(network: u16, storage_mode: StorageMode) -> PathBuf {
const HISTORY_DIRECTORY_NAME: &str = "history";

// Create the name of the history directory.
let directory_name = match &storage_mode {
StorageMode::Development(id) => format!(".{HISTORY_DIRECTORY_NAME}-{}-{}", network, id),
StorageMode::Production | StorageMode::Custom(_) => format!("{HISTORY_DIRECTORY_NAME}-{}", network),
StorageMode::Development(id) => format!(".{HISTORY_DIRECTORY_NAME}-{network}-{id}"),
StorageMode::Production | StorageMode::Custom(_) => format!("{HISTORY_DIRECTORY_NAME}-{network}"),
};

// Obtain the path to the ledger.
@@ -40,23 +43,22 @@ pub fn history_directory_path(network: u16, storage_mode: StorageMode) -> PathBu
path
}

#[derive(Copy, Clone)]
pub enum HistoryVariant {
/// A `bonded` mapping.
#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum MappingName {
/// The `bonded` mapping.
Bonded,
/// A `delegated` mapping.
/// The `delegated` mapping.
Delegated,
/// An `unbonding` mapping.
/// The `unbonding` mapping.
Unbonding,
}

impl HistoryVariant {
/// Returns the name of the variant.
pub fn name(&self) -> &'static str {
impl Display for MappingName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Bonded => "bonded",
Self::Delegated => "delegated",
Self::Unbonding => "unbonding",
Self::Bonded => write!(f, "bonded"),
Self::Delegated => write!(f, "delegated"),
Self::Unbonding => write!(f, "unbonding"),
}
}
}
@@ -67,13 +69,13 @@ pub struct History {
}

impl History {
/// Initializes a new instance of the history.
/// Initializes a new instance of `History`.
pub fn new(network: u16, storage_mode: StorageMode) -> Self {
Self { path: history_directory_path(network, storage_mode) }
}

/// Stores an entry into the history.
pub fn store_entry<T>(&self, height: u32, variant: HistoryVariant, data: &T) -> Result<()>
/// Stores a mapping from a given block in the history directory as JSON.
pub fn store_entry<T>(&self, height: u32, mapping: MappingName, data: &T) -> Result<()>
where
T: Serialize + ?Sized,
{
@@ -85,18 +87,18 @@ impl History {
}

// Write the entry to the block directory.
let entry_path = block_path.join(format!("block-{height}-{}.json", variant.name()));
let entry_path = block_path.join(format!("block-{height}-{mapping}.json"));
vicsn marked this conversation as resolved.
Show resolved Hide resolved
std::fs::write(entry_path, serde_json::to_string_pretty(data)?)?;

Ok(())
}

/// Loads an entry from the history.
pub fn load_entry(&self, height: u32, variant: HistoryVariant) -> Result<String> {
/// Loads the JSON string for a mapping from a given block from the history directory.
pub fn load_entry(&self, height: u32, mapping: MappingName) -> Result<String> {
// Get the path to the block directory.
let block_path = self.path.join(format!("block-{height}"));
// Get the path to the entry.
let entry_path = block_path.join(format!("block-{height}-{}.json", variant.name()));
let entry_path = block_path.join(format!("block-{height}-{mapping}.json"));
// Load the entry.
let result = std::fs::read_to_string(entry_path)?;