|
@@ -1,5 +1,5 @@
|
|
|
use crate::{
|
|
use crate::{
|
|
|
- consensus::Block,
|
|
|
|
|
|
|
+ consensus::{Block, Header},
|
|
|
util::{
|
|
util::{
|
|
|
serial::{deserialize, serialize},
|
|
serial::{deserialize, serialize},
|
|
|
time::Timestamp,
|
|
time::Timestamp,
|
|
@@ -7,11 +7,98 @@ use crate::{
|
|
|
Error, Result,
|
|
Error, Result,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+const SLED_HEADER_TREE: &[u8] = b"_headers";
|
|
|
const SLED_BLOCK_TREE: &[u8] = b"_blocks";
|
|
const SLED_BLOCK_TREE: &[u8] = b"_blocks";
|
|
|
const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
|
|
const SLED_BLOCK_ORDER_TREE: &[u8] = b"_block_order";
|
|
|
|
|
|
|
|
|
|
+/// The `HeaderStore` is a `sled` tree storing all the blockchain's blocks' headers
|
|
|
|
|
+/// where the key is the headers's hash, and value is the serialized header.
|
|
|
|
|
+#[derive(Clone)]
|
|
|
|
|
+pub struct HeaderStore(sled::Tree);
|
|
|
|
|
+
|
|
|
|
|
+impl HeaderStore {
|
|
|
|
|
+ /// Opens a new or existing `HeaderStore` on the given sled database.
|
|
|
|
|
+ pub fn new(db: &sled::Db, genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
|
|
|
|
|
+ let tree = db.open_tree(SLED_HEADER_TREE)?;
|
|
|
|
|
+ let store = Self(tree);
|
|
|
|
|
+
|
|
|
|
|
+ // In case the store is empty, initialize it with the genesis header.
|
|
|
|
|
+ if store.0.is_empty() {
|
|
|
|
|
+ let genesis_header = Header::genesis_header(genesis_ts, genesis_data);
|
|
|
|
|
+ store.insert(&[genesis_header])?;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Ok(store)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Insert a slice of [`Header`] into the blockstore. With sled, the
|
|
|
|
|
+ /// operation is done as a batch.
|
|
|
|
|
+ /// The headers are hashed with BLAKE3 and this headerhash is used as
|
|
|
|
|
+ /// the key, while value is the serialized [`Header`] itself.
|
|
|
|
|
+ /// On success, the function returns the header hashes in the same order.
|
|
|
|
|
+ pub fn insert(&self, headers: &[Header]) -> Result<Vec<blake3::Hash>> {
|
|
|
|
|
+ let mut ret = Vec::with_capacity(headers.len());
|
|
|
|
|
+ let mut batch = sled::Batch::default();
|
|
|
|
|
+
|
|
|
|
|
+ for header in headers {
|
|
|
|
|
+ let serialized = serialize(header);
|
|
|
|
|
+ let headerhash = blake3::hash(&serialized);
|
|
|
|
|
+ batch.insert(headerhash.as_bytes(), serialized);
|
|
|
|
|
+ ret.push(headerhash);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ self.0.apply_batch(batch)?;
|
|
|
|
|
+ Ok(ret)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Check if the headerstore contains a given headerhash.
|
|
|
|
|
+ pub fn contains(&self, headerhash: &blake3::Hash) -> Result<bool> {
|
|
|
|
|
+ Ok(self.0.contains_key(headerhash.as_bytes())?)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Fetch given headerhashes from the headerstore.
|
|
|
|
|
+ /// The resulting vector contains `Option`, which is `Some` if the header
|
|
|
|
|
+ /// was found in the headerstore, and otherwise it is `None`, if it has not.
|
|
|
|
|
+ /// The second parameter is a boolean which tells the function to fail in
|
|
|
|
|
+ /// case at least one header was not found.
|
|
|
|
|
+ pub fn get(&self, headerhashes: &[blake3::Hash], strict: bool) -> Result<Vec<Option<Header>>> {
|
|
|
|
|
+ let mut ret = Vec::with_capacity(headerhashes.len());
|
|
|
|
|
+
|
|
|
|
|
+ for hash in headerhashes {
|
|
|
|
|
+ if let Some(found) = self.0.get(hash.as_bytes())? {
|
|
|
|
|
+ let header = deserialize(&found)?;
|
|
|
|
|
+ ret.push(Some(header));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if strict {
|
|
|
|
|
+ let s = hash.to_hex().as_str().to_string();
|
|
|
|
|
+ return Err(Error::HeaderNotFound(s))
|
|
|
|
|
+ }
|
|
|
|
|
+ ret.push(None);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Ok(ret)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// Retrieve all headers from the headerstore in the form of a tuple
|
|
|
|
|
+ /// (`headerhash`, `header`).
|
|
|
|
|
+ /// Be careful as this will try to load everything in memory.
|
|
|
|
|
+ pub fn get_all(&self) -> Result<Vec<(blake3::Hash, Header)>> {
|
|
|
|
|
+ let mut headers = vec![];
|
|
|
|
|
+
|
|
|
|
|
+ for header in self.0.iter() {
|
|
|
|
|
+ let (key, value) = header.unwrap();
|
|
|
|
|
+ let hash_bytes: [u8; 32] = key.as_ref().try_into().unwrap();
|
|
|
|
|
+ let header = deserialize(&value)?;
|
|
|
|
|
+ headers.push((hash_bytes.into(), header));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Ok(headers)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/// The `BlockStore` is a `sled` tree storing all the blockchain's blocks
|
|
/// The `BlockStore` is a `sled` tree storing all the blockchain's blocks
|
|
|
-/// where the key is the block's hash, and value is the serialized block.
|
|
|
|
|
|
|
+/// where the key is the block's headers' hash, and value is the serialized block.
|
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
|
pub struct BlockStore(sled::Tree);
|
|
pub struct BlockStore(sled::Tree);
|
|
|
|
|
|
|
@@ -30,40 +117,34 @@ impl BlockStore {
|
|
|
Ok(store)
|
|
Ok(store)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Insert a slice of [`Block`] into the blockstore. With sled, the
|
|
|
|
|
|
|
+ /// Insert a slice of [`Block`] into the store. With sled, the
|
|
|
/// operation is done as a batch.
|
|
/// operation is done as a batch.
|
|
|
- /// The blocks are hashed with BLAKE3 and this blockhash is used as
|
|
|
|
|
- /// the key, while value is the serialized [`Block`] itself.
|
|
|
|
|
- /// On success, the function returns the block hashes in the same order.
|
|
|
|
|
- pub fn insert(&self, blocks: &[Block]) -> Result<Vec<blake3::Hash>> {
|
|
|
|
|
- let mut ret = Vec::with_capacity(blocks.len());
|
|
|
|
|
|
|
+ /// The block's header is used as the key, while value is the serialized [`Block`] itself.
|
|
|
|
|
+ pub fn insert(&self, blocks: &[Block]) -> Result<()> {
|
|
|
let mut batch = sled::Batch::default();
|
|
let mut batch = sled::Batch::default();
|
|
|
|
|
|
|
|
for block in blocks {
|
|
for block in blocks {
|
|
|
- let serialized = serialize(block);
|
|
|
|
|
- let blockhash = blake3::hash(&serialized);
|
|
|
|
|
- batch.insert(blockhash.as_bytes(), serialized);
|
|
|
|
|
- ret.push(blockhash);
|
|
|
|
|
|
|
+ batch.insert(block.header.as_bytes(), serialize(block));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
self.0.apply_batch(batch)?;
|
|
self.0.apply_batch(batch)?;
|
|
|
- Ok(ret)
|
|
|
|
|
|
|
+ Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Check if the blockstore contains a given blockhash.
|
|
|
|
|
- pub fn contains(&self, blockhash: &blake3::Hash) -> Result<bool> {
|
|
|
|
|
- Ok(self.0.contains_key(blockhash.as_bytes())?)
|
|
|
|
|
|
|
+ /// Check if the blockstore contains a given headerhash.
|
|
|
|
|
+ pub fn contains(&self, headerhash: &blake3::Hash) -> Result<bool> {
|
|
|
|
|
+ Ok(self.0.contains_key(headerhash.as_bytes())?)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Fetch given blockhashes from the blockstore.
|
|
|
|
|
|
|
+ /// Fetch given headerhashes from the blockstore.
|
|
|
/// The resulting vector contains `Option`, which is `Some` if the block
|
|
/// The resulting vector contains `Option`, which is `Some` if the block
|
|
|
/// was found in the blockstore, and otherwise it is `None`, if it has not.
|
|
/// was found in the blockstore, and otherwise it is `None`, if it has not.
|
|
|
/// The second parameter is a boolean which tells the function to fail in
|
|
/// The second parameter is a boolean which tells the function to fail in
|
|
|
/// case at least one block was not found.
|
|
/// case at least one block was not found.
|
|
|
- pub fn get(&self, blockhashes: &[blake3::Hash], strict: bool) -> Result<Vec<Option<Block>>> {
|
|
|
|
|
- let mut ret = Vec::with_capacity(blockhashes.len());
|
|
|
|
|
|
|
+ pub fn get(&self, headerhashes: &[blake3::Hash], strict: bool) -> Result<Vec<Option<Block>>> {
|
|
|
|
|
+ let mut ret = Vec::with_capacity(headerhashes.len());
|
|
|
|
|
|
|
|
- for hash in blockhashes {
|
|
|
|
|
|
|
+ for hash in headerhashes {
|
|
|
if let Some(found) = self.0.get(hash.as_bytes())? {
|
|
if let Some(found) = self.0.get(hash.as_bytes())? {
|
|
|
let block = deserialize(&found)?;
|
|
let block = deserialize(&found)?;
|
|
|
ret.push(Some(block));
|
|
ret.push(Some(block));
|
|
@@ -80,7 +161,7 @@ impl BlockStore {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve all blocks from the blockstore in the form of a tuple
|
|
/// Retrieve all blocks from the blockstore in the form of a tuple
|
|
|
- /// (`blockhash`, `block`).
|
|
|
|
|
|
|
+ /// (`headerhash`, `block`).
|
|
|
/// Be careful as this will try to load everything in memory.
|
|
/// Be careful as this will try to load everything in memory.
|
|
|
pub fn get_all(&self) -> Result<Vec<(blake3::Hash, Block)>> {
|
|
pub fn get_all(&self) -> Result<Vec<(blake3::Hash, Block)>> {
|
|
|
let mut blocks = vec![];
|
|
let mut blocks = vec![];
|
|
@@ -98,7 +179,7 @@ impl BlockStore {
|
|
|
|
|
|
|
|
/// The `BlockOrderStore` is a `sled` tree storing the order of the
|
|
/// The `BlockOrderStore` is a `sled` tree storing the order of the
|
|
|
/// blockchain's slots, where the key is the slot uid, and the value is
|
|
/// blockchain's slots, where the key is the slot uid, and the value is
|
|
|
-/// the block's hash. [`BlockStore`] can be queried with this hash.
|
|
|
|
|
|
|
+/// the block's headers' hash. [`BlockStore`] can be queried with this hash.
|
|
|
pub struct BlockOrderStore(sled::Tree);
|
|
pub struct BlockOrderStore(sled::Tree);
|
|
|
|
|
|
|
|
impl BlockOrderStore {
|
|
impl BlockOrderStore {
|
|
@@ -110,16 +191,15 @@ impl BlockOrderStore {
|
|
|
// In case the store is empty, initialize it with the genesis block.
|
|
// In case the store is empty, initialize it with the genesis block.
|
|
|
if store.0.is_empty() {
|
|
if store.0.is_empty() {
|
|
|
let genesis_block = Block::genesis_block(genesis_ts, genesis_data);
|
|
let genesis_block = Block::genesis_block(genesis_ts, genesis_data);
|
|
|
- let blockhash = blake3::hash(&serialize(&genesis_block));
|
|
|
|
|
- store.insert(&[genesis_block.sl], &[blockhash])?;
|
|
|
|
|
|
|
+ store.insert(&[0], &[genesis_block.header])?;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Ok(store)
|
|
Ok(store)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Insert a slice of slots and blockhashes into the store. With sled, the
|
|
|
|
|
|
|
+ /// Insert a slice of slots and headerhashes into the store. With sled, the
|
|
|
/// operation is done as a batch.
|
|
/// operation is done as a batch.
|
|
|
- /// The block slot is used as the key, and the blockhash is used as value.
|
|
|
|
|
|
|
+ /// The block slot is used as the key, and the headerhash is used as value.
|
|
|
pub fn insert(&self, slots: &[u64], hashes: &[blake3::Hash]) -> Result<()> {
|
|
pub fn insert(&self, slots: &[u64], hashes: &[blake3::Hash]) -> Result<()> {
|
|
|
assert_eq!(slots.len(), hashes.len());
|
|
assert_eq!(slots.len(), hashes.len());
|
|
|
let mut batch = sled::Batch::default();
|
|
let mut batch = sled::Batch::default();
|
|
@@ -162,7 +242,7 @@ impl BlockOrderStore {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Retrieve all slots from the blockorderstore in the form of a tuple
|
|
/// Retrieve all slots from the blockorderstore in the form of a tuple
|
|
|
- /// (`slot`, `blockhash`).
|
|
|
|
|
|
|
+ /// (`slot`, `headerhash`).
|
|
|
/// Be careful as this will try to load everything in memory.
|
|
/// Be careful as this will try to load everything in memory.
|
|
|
pub fn get_all(&self) -> Result<Vec<(u64, blake3::Hash)>> {
|
|
pub fn get_all(&self) -> Result<Vec<(u64, blake3::Hash)>> {
|
|
|
let mut slots = vec![];
|
|
let mut slots = vec![];
|
|
@@ -191,8 +271,8 @@ impl BlockOrderStore {
|
|
|
if let Some(found) = self.0.get_gt(key.to_be_bytes())? {
|
|
if let Some(found) = self.0.get_gt(key.to_be_bytes())? {
|
|
|
let key_bytes: [u8; 8] = found.0.as_ref().try_into().unwrap();
|
|
let key_bytes: [u8; 8] = found.0.as_ref().try_into().unwrap();
|
|
|
key = u64::from_be_bytes(key_bytes);
|
|
key = u64::from_be_bytes(key_bytes);
|
|
|
- let block_hash = deserialize(&found.1)?;
|
|
|
|
|
- ret.push(block_hash);
|
|
|
|
|
|
|
+ let header_hash = deserialize(&found.1)?;
|
|
|
|
|
+ ret.push(header_hash);
|
|
|
counter += 1;
|
|
counter += 1;
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
@@ -202,7 +282,7 @@ impl BlockOrderStore {
|
|
|
Ok(ret)
|
|
Ok(ret)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /// Fetch the last block hash in the tree, based on the `Ord`
|
|
|
|
|
|
|
+ /// Fetch the last block headerhash in the tree, based on the `Ord`
|
|
|
/// implementation for `Vec<u8>`. This should not be able to
|
|
/// implementation for `Vec<u8>`. This should not be able to
|
|
|
/// fail because we initialize the store with the genesis block.
|
|
/// fail because we initialize the store with the genesis block.
|
|
|
pub fn get_last(&self) -> Result<(u64, blake3::Hash)> {
|
|
pub fn get_last(&self) -> Result<(u64, blake3::Hash)> {
|