Skip to content

Commit

Permalink
Choose v1 or v2 storage opening in ledger based on migration
Browse files Browse the repository at this point in the history
  • Loading branch information
MavenRain committed Mar 29, 2023
1 parent c5a75b8 commit 9cc2dcb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 96 deletions.
1 change: 1 addition & 0 deletions Cargo.Bazel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,7 @@ dependencies = [
name = "many-ledger"
version = "0.1.0"
dependencies = [
"anyhow",
"async-channel",
"async-trait",
"base64 0.20.0",
Expand Down
6 changes: 5 additions & 1 deletion cargo-bazel-lock.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "27d57dd505b230d5218e07f62416fbe3953e698597ffe5debace6f8691f6d7b3",
"checksum": "48036a60f6ffb186db4c3e1daff622f28ca75e52858140aaba059c989ac213f5",
"crates": {
"addr2line 0.19.0": {
"name": "addr2line",
Expand Down Expand Up @@ -13484,6 +13484,10 @@
],
"deps": {
"common": [
{
"id": "anyhow 1.0.70",
"target": "anyhow"
},
{
"id": "async-channel 1.8.0",
"target": "async_channel"
Expand Down
44 changes: 23 additions & 21 deletions src/many-kvstore/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use crate::module::{KvStoreMetadata, KvStoreMetadataWrapper};
use derive_more::{From, TryInto};
use many_error::{ManyError, ManyErrorCode};
use many_identity::Address;
use many_modules::abci_backend::AbciCommitInfo;
use many_modules::events::EventInfo;
use many_types::{Either, ProofOperation, Timestamp};
use merk_v1::rocksdb::{DBIterator, IteratorMode, ReadOptions};
use merk_v1::{
proofs::{
query::QueryItem,
Decoder,
Node::{Hash, KVHash, KV},
use {
crate::module::{KvStoreMetadata, KvStoreMetadataWrapper},
derive_more::{From, TryInto},
many_error::{ManyError, ManyErrorCode},
many_identity::Address,
many_modules::abci_backend::AbciCommitInfo,
many_modules::events::EventInfo,
many_types::{Either, ProofOperation, Timestamp},
merk_v1::rocksdb::{DBIterator, IteratorMode, ReadOptions},
merk_v1::{
proofs::{
query::QueryItem,
Decoder,
Node::{Hash, KVHash, KV},
},
Hash as MerkHash, Op,
},
Hash as MerkHash, Op,
serde::{Deserialize, Serialize},
std::collections::BTreeMap,
std::path::Path,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::Path;

mod account;
mod event;
Expand Down Expand Up @@ -264,16 +266,16 @@ impl KvStoreStorage {
let root_identity: Address = Address::from_bytes(
&persistent_store
.get(b"/config/identity")
.expect("Could not open storage.")
.expect("Could not find key '/config/identity' in storage."),
.map_err(|_| "Could not open storage.".to_string())?
.ok_or_else(|| "Could not find key '/config/identity' in storage.".to_string())?,
)
.map_err(|e| e.to_string())?;

let latest_event_id = minicbor::decode(
&persistent_store
.get(b"/latest_event_id")
.expect("Could not open storage.")
.expect("Could not find key '/latest_event_id'"),
.map_err(|_| "Could not open storage.".to_string())?
.ok_or_else(|| "Could not find key '/latest_event_id'".to_string())?,
)
.map_err(|e| e.to_string())?;

Expand Down
1 change: 1 addition & 0 deletions src/many-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "many-ledger"
doc = false

[dependencies]
anyhow = "1.0"
async-channel = "1.8"
async-trait = "0.1.51"
base64 = "0.20.0-alpha.1"
Expand Down
139 changes: 77 additions & 62 deletions src/many-ledger/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#![feature(used_with_arg)]

use clap::Parser;
use many_cli_helpers::CommonCliFlags;
use many_identity::verifiers::AnonymousVerifier;
use many_identity::{Address, Identity};
use many_identity_dsa::{CoseKeyIdentity, CoseKeyVerifier};
use many_identity_webauthn::WebAuthnVerifier;
use many_migration::MigrationConfig;
use many_modules::account::features::Feature;
use many_modules::{abci_backend, account, data, events, idstore, ledger};
use many_protocol::ManyUrl;
use many_server::transport::http::HttpServer;
use many_server::ManyServer;
use std::collections::BTreeSet;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tracing::{debug, info, warn};

use crate::allow_addrs::AllowAddrsModule;
use {
crate::allow_addrs::AllowAddrsModule,
clap::Parser,
derive_more::{From, TryInto},
many_cli_helpers::CommonCliFlags,
many_error::ManyError,
many_identity::verifiers::AnonymousVerifier,
many_identity::{Address, Identity},
many_identity_dsa::{CoseKeyIdentity, CoseKeyVerifier},
many_identity_webauthn::WebAuthnVerifier,
many_migration::MigrationConfig,
many_modules::account::features::Feature,
many_modules::{abci_backend, account, data, events, idstore, ledger},
many_protocol::ManyUrl,
many_server::transport::http::HttpServer,
many_server::ManyServer,
std::collections::BTreeSet,
std::net::SocketAddr,
std::path::PathBuf,
std::sync::{Arc, Mutex},
tracing::{debug, info, warn},
};

#[cfg(feature = "webauthn_testing")]
use crate::idstore_webauthn::IdStoreWebAuthnModule;
use crate::json::InitialStateJson;
use crate::migration::MIGRATIONS;
use crate::module::account::AccountFeatureModule;
use module::*;
use {
crate::idstore_webauthn::IdStoreWebAuthnModule, crate::json::InitialStateJson,
crate::migration::MIGRATIONS, crate::module::account::AccountFeatureModule, module::*,
};

mod error;
mod json;
Expand Down Expand Up @@ -106,7 +108,18 @@ struct Opts {
allow_addrs: Option<PathBuf>,
}

fn main() {
#[derive(Debug, From, TryInto)]
enum Error {
Anyhow(anyhow::Error),
Io(std::io::Error),
Json(json5::Error),
Many(ManyError),
Message(String),
ParseInt(std::num::ParseIntError),
Serde(serde_json::Error),
}

fn main() -> Result<(), Error> {
let Opts {
common_flags,
pem,
Expand All @@ -122,7 +135,7 @@ fn main() {
..
} = Opts::parse();

common_flags.init_logging().unwrap();
common_flags.init_logging()?;

debug!("{:?}", Opts::parse());
info!(
Expand All @@ -135,15 +148,12 @@ fn main() {
println!("Name: {}", migration.name());
println!("Description: {}", migration.description());
}
println!("Name: Hash Migration");
println!("Description: Migrate from old to new merk hash scheme");
return;
return Ok(());
}

// Safe unwrap.
// At this point the Options should contain a value.
let pem = pem.unwrap();
let persistent = persistent.unwrap();
let pem = pem.ok_or("Identity value should be present".to_string())?;
let persistent = persistent.ok_or("Persistent value should be present".to_string())?;

if clean {
// Delete the persistent storage.
Expand All @@ -160,20 +170,26 @@ fn main() {
state = None;
}

let pem = std::fs::read_to_string(pem).expect("Could not read PEM file.");
let key = CoseKeyIdentity::from_pem(pem).expect("Could not generate identity from PEM file.");
let pem = std::fs::read_to_string(pem)?;
let key = CoseKeyIdentity::from_pem(pem)?;
info!(address = key.address().to_string().as_str());

let state: Option<InitialStateJson> =
state.map(|p| InitialStateJson::read(p).expect("Could not read state file."));
let state: Option<InitialStateJson> = match state {
Some(p) => {
Some(InitialStateJson::read(p).map_err(|_| "Could not read state file.".to_string())?)
}
None => None,
};

info!("Loading migrations from {migrations_config:?}");
let maybe_migrations = migrations_config.map(|file| {
let content = std::fs::read_to_string(file)
.expect("Could not read file passed to --migrations_config");
let config: MigrationConfig = serde_json::from_str(&content).unwrap();
config.strict()
});
let maybe_migrations = match migrations_config {
Some(file) => {
let content = std::fs::read_to_string(file)?;
let config: MigrationConfig = serde_json::from_str(&content)?;
Some(config.strict())
}
None => None,
};

let module_impl = if persistent.exists() {
if state.is_some() {
Expand All @@ -197,12 +213,11 @@ fn main() {
}
}

LedgerModuleImpl::load(maybe_migrations, persistent, abci).unwrap()
LedgerModuleImpl::load(maybe_migrations, persistent, abci)?
} else if let Some(state) = state {
#[cfg(feature = "balance_testing")]
{
let mut module_impl =
LedgerModuleImpl::new(state, maybe_migrations, persistent, abci).unwrap();
let mut module_impl = LedgerModuleImpl::new(state, maybe_migrations, persistent, abci)?;

use std::str::FromStr;

Expand All @@ -214,26 +229,25 @@ fn main() {
for balance in balance_only_for_testing.unwrap_or_default() {
let args: Vec<&str> = balance.splitn(3, ':').collect();
let (identity, amount, symbol) = (
args.first().unwrap(),
args.get(1).expect("No amount."),
args.get(2).expect("No symbol."),
args.first()
.ok_or("Missing arguments for balance testing".to_string())?,
args.get(1).ok_or("No amount.".to_string())?,
args.get(2).ok_or("No symbol.".to_string())?,
);

module_impl
.set_balance_only_for_testing(
Address::from_str(identity).expect("Invalid identity."),
amount.parse::<u64>().expect("Invalid amount."),
Address::from_str(symbol).expect("Invalid symbol."),
)
.expect("Unable to set balance for testing.");
module_impl.set_balance_only_for_testing(
Address::from_str(identity)?,
amount.parse::<u64>()?,
Address::from_str(symbol)?,
)?;
}
module_impl
}

#[cfg(not(feature = "balance_testing"))]
LedgerModuleImpl::new(state, maybe_migrations, persistent, abci).unwrap()
LedgerModuleImpl::new(state, maybe_migrations, persistent, abci)?
} else {
panic!("Persistent store or staging file not found.")
Err("Persistent store or staging file not found.".to_string())?
};
let module_impl = Arc::new(Mutex::new(module_impl));

Expand All @@ -249,12 +263,13 @@ fn main() {
);

{
let mut s = many.lock().unwrap();
let mut s = many
.lock()
.map_err(|_| "Could not acquire server lock".to_string())?;
s.add_module(ledger::LedgerModule::new(module_impl.clone()));
let ledger_command_module = ledger::LedgerCommandsModule::new(module_impl.clone());
if let Some(path) = allow_addrs {
let allow_addrs: BTreeSet<Address> =
json5::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
let allow_addrs: BTreeSet<Address> = json5::from_str(&std::fs::read_to_string(path)?)?;
s.add_module(AllowAddrsModule {
inner: ledger_command_module,
allow_addrs,
Expand Down Expand Up @@ -309,6 +324,6 @@ fn main() {
signal_hook::flag::register(signal_hook::consts::SIGINT, many_server.term_signal())
.expect("Could not register signal handler");

let runtime = tokio::runtime::Runtime::new().unwrap();
runtime.block_on(many_server.bind(addr)).unwrap();
let runtime = tokio::runtime::Runtime::new()?;
runtime.block_on(many_server.bind(addr)).map_err(Into::into)
}
Loading

0 comments on commit 9cc2dcb

Please sign in to comment.