blockchain: use sled-overlay record parsing functions

This commit is contained in:
skoupidi
2024-12-03 15:27:10 +02:00
parent 9f490a09d3
commit 7739d8a40e
16 changed files with 30 additions and 50 deletions

4
Cargo.lock generated
View File

@@ -6492,9 +6492,9 @@ dependencies = [
[[package]] [[package]]
name = "sled-overlay" name = "sled-overlay"
version = "0.1.5" version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad42f44feda8a0f1697d6608bd34999ec40a3894915ca826bb3df66ac9247e75" checksum = "4c33dea88a6fcef15045db58735976a75488268751fa4574ca6d95a3f612a0da"
dependencies = [ dependencies = [
"darkfi-serial", "darkfi-serial",
"sled", "sled",

View File

@@ -122,7 +122,7 @@ wasmer-compiler-singlepass = {version = "4.4.0", optional = true}
wasmer-middlewares = {version = "4.4.0", optional = true} wasmer-middlewares = {version = "4.4.0", optional = true}
# Blockchain store # Blockchain store
sled-overlay = {version = "0.1.5", optional = true} sled-overlay = {version = "0.1.6", optional = true}
# Miner # Miner
randomx = {git = "https://github.com/darkrenaissance/RandomX", optional = true} randomx = {git = "https://github.com/darkrenaissance/RandomX", optional = true}

View File

@@ -22,7 +22,7 @@ bs58 = "0.5.1"
log = "0.4.22" log = "0.4.22"
num-bigint = "0.4.6" num-bigint = "0.4.6"
rand = "0.8.5" rand = "0.8.5"
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
toml = "0.8.19" toml = "0.8.19"
# JSON-RPC # JSON-RPC

View File

@@ -24,7 +24,7 @@ darkfi-serial = {version = "0.4.2", features = ["async"]}
libc = "0.2.159" libc = "0.2.159"
# Event Graph DB # Event Graph DB
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
# TLS # TLS
async-trait = "0.1.83" async-trait = "0.1.83"

View File

@@ -42,7 +42,7 @@ futures = "0.3.31"
async-recursion = "1.1.1" async-recursion = "1.1.1"
colored = "2.1.0" colored = "2.1.0"
#rustpython-vm = "0.3.1" #rustpython-vm = "0.3.1"
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
url = "2.5.2" url = "2.5.2"
semver = "1.0.23" semver = "1.0.23"
chrono = "0.4.38" chrono = "0.4.38"

View File

@@ -28,7 +28,7 @@ darkfi-serial = "0.4.2"
blake3 = "1.5.4" blake3 = "1.5.4"
# Event Graph DB # Event Graph DB
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
# Misc # Misc
async-trait = "0.1.83" async-trait = "0.1.83"

View File

@@ -27,7 +27,7 @@ darkfi = { path = "../../../", features = [
darkfi-serial = "0.4.2" darkfi-serial = "0.4.2"
# Event Graph DB # Event Graph DB
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
# Crypto # Crypto
blake3 = "1.5.4" blake3 = "1.5.4"

View File

@@ -26,7 +26,7 @@ darkfi = {path = "../../", features = ["event-graph"]}
darkfi-serial = {version = "0.4.2", features = ["async"]} darkfi-serial = {version = "0.4.2", features = ["async"]}
# Event Graph DB # Event Graph DB
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
# Crypto # Crypto
blake3 = "1.5.4" blake3 = "1.5.4"

View File

@@ -18,7 +18,7 @@ drk = {path = "../../../bin/drk"}
# Misc # Misc
log = "0.4.22" log = "0.4.22"
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
# JSON-RPC # JSON-RPC
async-trait = "0.1.83" async-trait = "0.1.83"

View File

@@ -18,13 +18,13 @@ darkfi = {path = "../../../", features = ["bs58", "validator"]}
darkfi_money_contract = {path = "../../../src/contract/money", features = ["no-entrypoint", "client"]} darkfi_money_contract = {path = "../../../src/contract/money", features = ["no-entrypoint", "client"]}
darkfi-contract-test-harness = {path = "../../../src/contract/test-harness"} darkfi-contract-test-harness = {path = "../../../src/contract/test-harness"}
darkfi-sdk = {path = "../../../src/sdk"} darkfi-sdk = {path = "../../../src/sdk"}
darkfi-serial = {path = "../../../src/serial"} darkfi-serial = "0.4.2"
# Misc # Misc
async-std = {version = "1.13.0", features = ["attributes"]} async-std = {version = "1.13.0", features = ["attributes"]}
bs58 = "0.5.1" bs58 = "0.5.1"
clap = {version = "4.4.11", features = ["derive"]} clap = {version = "4.4.11", features = ["derive"]}
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
[patch.crates-io] [patch.crates-io]
halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"} halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"}

View File

@@ -28,11 +28,14 @@ use darkfi_sdk::{
use darkfi_serial::async_trait; use darkfi_serial::async_trait;
use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable}; use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
use num_bigint::BigUint; use num_bigint::BigUint;
use sled_overlay::{sled, SledDbOverlayStateDiff}; use sled_overlay::{
serial::{parse_record, parse_u32_key_record},
sled, SledDbOverlayStateDiff,
};
use crate::{tx::Transaction, util::time::Timestamp, Error, Result}; use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
use super::{parse_record, parse_u32_key_record, Header, HeaderHash, SledDbOverlayPtr}; use super::{Header, HeaderHash, SledDbOverlayPtr};
/// This struct represents a tuple of the form (`header`, `txs`, `signature`). /// This struct represents a tuple of the form (`header`, `txs`, `signature`).
/// ///

View File

@@ -21,7 +21,7 @@ use std::io::Cursor;
use darkfi_sdk::crypto::ContractId; use darkfi_sdk::crypto::ContractId;
use darkfi_serial::{deserialize, serialize}; use darkfi_serial::{deserialize, serialize};
use log::{debug, error}; use log::{debug, error};
use sled_overlay::sled; use sled_overlay::{serial::parse_record, sled};
use crate::{ use crate::{
zk::{empty_witnesses, VerifyingKey, ZkCircuit}, zk::{empty_witnesses, VerifyingKey, ZkCircuit},
@@ -29,7 +29,7 @@ use crate::{
Error, Result, Error, Result,
}; };
use super::{parse_record, SledDbOverlayPtr}; use super::SledDbOverlayPtr;
pub const SLED_CONTRACTS_TREE: &[u8] = b"_contracts"; pub const SLED_CONTRACTS_TREE: &[u8] = b"_contracts";
pub const SLED_BINCODE_TREE: &[u8] = b"_wasm_bincode"; pub const SLED_BINCODE_TREE: &[u8] = b"_wasm_bincode";

View File

@@ -27,11 +27,14 @@ use darkfi_sdk::{
#[cfg(feature = "async-serial")] #[cfg(feature = "async-serial")]
use darkfi_serial::async_trait; use darkfi_serial::async_trait;
use darkfi_serial::{deserialize, serialize, Encodable, SerialDecodable, SerialEncodable}; use darkfi_serial::{deserialize, serialize, Encodable, SerialDecodable, SerialEncodable};
use sled_overlay::sled; use sled_overlay::{
serial::{parse_record, parse_u32_key_record},
sled,
};
use crate::{util::time::Timestamp, Error, Result}; use crate::{util::time::Timestamp, Error, Result};
use super::{parse_record, parse_u32_key_record, SledDbOverlayPtr}; use super::SledDbOverlayPtr;
#[derive(Copy, Clone, Debug, Eq, PartialEq, SerialEncodable, SerialDecodable)] #[derive(Copy, Clone, Debug, Eq, PartialEq, SerialEncodable, SerialDecodable)]
// We have to introduce a type rather than using an alias so we can restrict API access // We have to introduce a type rather than using an alias so we can restrict API access

View File

@@ -19,7 +19,6 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use darkfi_sdk::tx::TransactionHash; use darkfi_sdk::tx::TransactionHash;
use darkfi_serial::{deserialize, Decodable};
use log::debug; use log::debug;
use sled_overlay::{sled, sled::Transactional}; use sled_overlay::{sled, sled::Transactional};
@@ -585,31 +584,3 @@ impl BlockchainOverlay {
Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts }))) Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
} }
} }
/// Parse a sled record with a u32 key in the form of a tuple (`key`, `value`).
pub fn parse_u32_key_record<T: Decodable>(record: (sled::IVec, sled::IVec)) -> Result<(u32, T)> {
let key_bytes: [u8; 4] = record.0.as_ref().try_into().unwrap();
let key = u32::from_be_bytes(key_bytes);
let value = deserialize(&record.1)?;
Ok((key, value))
}
/// Parse a sled record with a u64 key in the form of a tuple (`key`, `value`).
pub fn parse_u64_key_record<T: Decodable>(record: (sled::IVec, sled::IVec)) -> Result<(u64, T)> {
let key_bytes: [u8; 8] = record.0.as_ref().try_into().unwrap();
let key = u64::from_be_bytes(key_bytes);
let value = deserialize(&record.1)?;
Ok((key, value))
}
/// Parse a sled record in the form of a tuple (`key`, `value`).
pub fn parse_record<T1: Decodable, T2: Decodable>(
record: (sled::IVec, sled::IVec),
) -> Result<(T1, T2)> {
let key = deserialize(&record.0)?;
let value = deserialize(&record.1)?;
Ok((key, value))
}

View File

@@ -20,11 +20,14 @@ use std::collections::HashMap;
use darkfi_sdk::tx::TransactionHash; use darkfi_sdk::tx::TransactionHash;
use darkfi_serial::{deserialize, serialize}; use darkfi_serial::{deserialize, serialize};
use sled_overlay::sled; use sled_overlay::{
serial::{parse_record, parse_u64_key_record},
sled,
};
use crate::{tx::Transaction, Error, Result}; use crate::{tx::Transaction, Error, Result};
use super::{parse_record, parse_u64_key_record, SledDbOverlayPtr}; use super::SledDbOverlayPtr;
pub const SLED_TX_TREE: &[u8] = b"_transactions"; pub const SLED_TX_TREE: &[u8] = b"_transactions";
pub const SLED_TX_LOCATION_TREE: &[u8] = b"_transaction_location"; pub const SLED_TX_LOCATION_TREE: &[u8] = b"_transaction_location";

View File

@@ -19,7 +19,7 @@ blake3 = "1.5.4"
log = "0.4.22" log = "0.4.22"
rand = "0.8.5" rand = "0.8.5"
simplelog = "0.12.2" simplelog = "0.12.2"
sled-overlay = "0.1.5" sled-overlay = "0.1.6"
[lints] [lints]
workspace = true workspace = true