From 1b65c83c6aea86fa97db66c23868b3603443ce2d Mon Sep 17 00:00:00 2001 From: librelois Date: Sun, 9 May 2021 01:25:53 +0200 Subject: [PATCH] feat(db): add cols blocks_chunk_hash & compressed_blocks_chunk --- db/src/lib.rs | 5 +++ db/src/values.rs | 40 ++++++++++++++++++++++++ db/src/values/gva_block_db.rs | 57 +++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 db/src/values/gva_block_db.rs diff --git a/db/src/lib.rs b/db/src/lib.rs index e704870..99cff28 100644 --- a/db/src/lib.rs +++ b/db/src/lib.rs @@ -27,9 +27,11 @@ mod values; pub use keys::gva_utxo_id::GvaUtxoIdDbV1; pub use keys::wallet_hash_with_bn::WalletHashWithBnV1Db; +pub use values::gva_block_db::GvaBlockDbV1; pub use values::gva_idty_db::GvaIdtyDbV1; pub use values::gva_tx::GvaTxDbV1; pub use values::wallet_script_array::WalletScriptArrayV2; +pub use values::HashDb; pub(crate) use dubp::common::prelude::*; pub(crate) use dubp::crypto::hashs::Hash; @@ -50,6 +52,9 @@ db_schema!( ["blocks_by_common_time", BlocksByCommonTime, U64BE, u32], ["blocks_with_ud", BlocksWithUd, U32BE, ()], ["blockchain_time", BlockchainTime, U32BE, u64], + ["blocks_chunk_hash", BlocksChunkHash, U32BE, HashDb], + ["compressed_blocks_chunk", CompressedBlocksChunk, U32BE, Vec], + ["current_blocks_chunk", CurrentBlocksChunk, U32BE, GvaBlockDbV1], ["txs", Txs, HashKeyV2, GvaTxDbV1], ["txs_by_block", TxsByBlock, U32BE, Vec], ["txs_by_issuer", TxsByIssuer, WalletHashWithBnV1Db, BTreeSet], diff --git a/db/src/values.rs b/db/src/values.rs index ed42095..8dbd3ba 100644 --- a/db/src/values.rs +++ b/db/src/values.rs @@ -13,6 +13,46 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +use crate::*; + +pub mod gva_block_db; pub mod gva_idty_db; pub mod gva_tx; pub mod wallet_script_array; + +#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct HashDb(pub Hash); + +impl AsBytes for HashDb { + fn as_bytes T>(&self, mut f: F) -> T { + f(self.0.as_ref()) + } +} + +impl kv_typed::prelude::FromBytes for HashDb { + type Err = bincode::Error; + + fn from_bytes(bytes: &[u8]) -> std::result::Result { + let mut hash_bytes = [0; 32]; + hash_bytes.copy_from_slice(bytes); + Ok(Self(Hash(hash_bytes))) + } +} + +impl ToDumpString for HashDb { + fn to_dump_string(&self) -> String { + todo!() + } +} + +#[cfg(feature = "explorer")] +impl ExplorableValue for HashDb { + fn from_explorer_str(source: &str) -> Result { + Ok(Self( + Hash::from_hex(source).map_err(|e| FromExplorerValueErr(e.into()))?, + )) + } + fn to_explorer_json(&self) -> KvResult { + Ok(serde_json::Value::String(self.0.to_hex())) + } +} diff --git a/db/src/values/gva_block_db.rs b/db/src/values/gva_block_db.rs new file mode 100644 index 0000000..1278dbd --- /dev/null +++ b/db/src/values/gva_block_db.rs @@ -0,0 +1,57 @@ +// Copyright (C) 2020 Éloïs SANCHEZ. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use crate::*; +use dubp::block::{DubpBlock, DubpBlockV10}; + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[repr(transparent)] +pub struct GvaBlockDbV1(pub DubpBlock); + +impl Default for GvaBlockDbV1 { + fn default() -> Self { + Self(DubpBlock::V10(DubpBlockV10::default())) + } +} + +impl AsBytes for GvaBlockDbV1 { + fn as_bytes T>(&self, mut f: F) -> T { + f(&bincode::serialize(&self).unwrap_or_else(|_| unreachable!())) + } +} + +impl kv_typed::prelude::FromBytes for GvaBlockDbV1 { + type Err = bincode::Error; + + fn from_bytes(bytes: &[u8]) -> std::result::Result { + bincode::deserialize(bytes) + } +} + +impl ToDumpString for GvaBlockDbV1 { + fn to_dump_string(&self) -> String { + todo!() + } +} + +#[cfg(feature = "explorer")] +impl ExplorableValue for GvaBlockDbV1 { + fn from_explorer_str(source: &str) -> Result { + serde_json::from_str(source).map_err(|e| FromExplorerValueErr(e.into())) + } + fn to_explorer_json(&self) -> KvResult { + serde_json::to_value(&self).map_err(|e| KvError::DeserError(e.into())) + } +}