2021-03-11 12:47:21 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
use async_trait::async_trait;
|
2021-03-11 12:47:21 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-12-14 10:58:06 +00:00
|
|
|
use tokio::select;
|
2021-03-16 10:43:58 +00:00
|
|
|
use tokio::sync::watch;
|
2021-03-11 12:47:21 +00:00
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
use garage_db as db;
|
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
use garage_util::background::*;
|
2021-03-11 12:47:21 +00:00
|
|
|
use garage_util::data::*;
|
2023-01-03 14:27:36 +00:00
|
|
|
use garage_util::encode::{nonversioned_decode, nonversioned_encode};
|
2021-03-11 12:47:21 +00:00
|
|
|
use garage_util::error::Error;
|
|
|
|
|
2021-03-16 11:18:03 +00:00
|
|
|
use garage_rpc::ring::*;
|
|
|
|
|
2021-03-16 10:43:58 +00:00
|
|
|
use crate::data::*;
|
|
|
|
use crate::replication::*;
|
|
|
|
use crate::schema::*;
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
// This modules partitions the data in 2**16 partitions, based on the top
|
|
|
|
// 16 bits (two bytes) of item's partition keys' hashes.
|
|
|
|
// It builds one Merkle tree for each of these 2**16 partitions.
|
|
|
|
|
2021-03-16 10:43:58 +00:00
|
|
|
pub struct MerkleUpdater<F: TableSchema, R: TableReplication> {
|
|
|
|
data: Arc<TableData<F, R>>,
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
// Content of the todo tree: items where
|
|
|
|
// - key = the key of an item in the main table, ie hash(partition_key)+sort_key
|
|
|
|
// - value = the hash of the full serialized item, if present,
|
|
|
|
// or an empty vec if item is absent (deleted)
|
2021-03-16 10:43:58 +00:00
|
|
|
// Fields in data:
|
|
|
|
// pub(crate) merkle_todo: sled::Tree,
|
|
|
|
// pub(crate) merkle_todo_notify: Notify,
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
// Content of the merkle tree: items where
|
|
|
|
// - key = .bytes() for MerkleNodeKey
|
|
|
|
// - value = serialization of a MerkleNode, assumed to be MerkleNode::empty if not found
|
2021-03-16 10:43:58 +00:00
|
|
|
// Field in data:
|
|
|
|
// pub(crate) merkle_tree: sled::Tree,
|
2021-03-11 12:47:21 +00:00
|
|
|
empty_node_hash: Hash,
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:30:24 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-03-11 12:47:21 +00:00
|
|
|
pub struct MerkleNodeKey {
|
2021-03-16 11:18:03 +00:00
|
|
|
// partition number
|
|
|
|
pub partition: Partition,
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
// prefix: a prefix for the hash of full keys, i.e. hash(hash(partition_key)+sort_key)
|
2021-03-15 17:40:27 +00:00
|
|
|
#[serde(with = "serde_bytes")]
|
2021-03-11 12:47:21 +00:00
|
|
|
pub prefix: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum MerkleNode {
|
|
|
|
// The empty Merkle node
|
|
|
|
Empty,
|
|
|
|
|
|
|
|
// An intermediate Merkle tree node for a prefix
|
|
|
|
// Contains the hashes of the 256 possible next prefixes
|
|
|
|
Intermediate(Vec<(u8, Hash)>),
|
|
|
|
|
|
|
|
// A final node for an item
|
|
|
|
// Contains the full key of the item and the hash of the value
|
|
|
|
Leaf(Vec<u8>, Hash),
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:08:37 +00:00
|
|
|
impl<F: TableSchema, R: TableReplication> MerkleUpdater<F, R> {
|
2022-12-14 11:28:07 +00:00
|
|
|
pub(crate) fn new(data: Arc<TableData<F, R>>) -> Arc<Self> {
|
2023-01-03 14:27:36 +00:00
|
|
|
let empty_node_hash = blake2sum(&nonversioned_encode(&MerkleNode::Empty).unwrap()[..]);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
2022-12-14 11:28:07 +00:00
|
|
|
Arc::new(Self {
|
2021-03-16 10:43:58 +00:00
|
|
|
data,
|
2021-03-11 12:47:21 +00:00
|
|
|
empty_node_hash,
|
2022-12-14 11:28:07 +00:00
|
|
|
})
|
|
|
|
}
|
2021-03-11 15:54:15 +00:00
|
|
|
|
2022-12-14 11:28:07 +00:00
|
|
|
pub(crate) fn spawn_workers(self: &Arc<Self>, background: &BackgroundRunner) {
|
|
|
|
background.spawn_worker(MerkleWorker(self.clone()));
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 11:30:26 +00:00
|
|
|
fn updater_loop_iter(&self) -> Result<WorkerState, Error> {
|
2022-06-08 08:01:44 +00:00
|
|
|
if let Some((key, valhash)) = self.data.merkle_todo.first()? {
|
|
|
|
self.update_item(&key, &valhash)?;
|
2022-07-08 11:30:26 +00:00
|
|
|
Ok(WorkerState::Busy)
|
2022-06-08 08:01:44 +00:00
|
|
|
} else {
|
2022-07-08 11:30:26 +00:00
|
|
|
Ok(WorkerState::Idle)
|
2022-06-08 08:01:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
fn update_item(&self, k: &[u8], vhash_by: &[u8]) -> Result<(), Error> {
|
|
|
|
let khash = blake2sum(k);
|
|
|
|
|
2021-04-23 19:42:52 +00:00
|
|
|
let new_vhash = if vhash_by.is_empty() {
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
} else {
|
2021-04-23 19:42:52 +00:00
|
|
|
Some(Hash::try_from(vhash_by).unwrap())
|
2021-03-11 12:47:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let key = MerkleNodeKey {
|
2021-03-16 14:58:40 +00:00
|
|
|
partition: self
|
|
|
|
.data
|
|
|
|
.replication
|
|
|
|
.partition_of(&Hash::try_from(&k[0..32]).unwrap()),
|
2021-03-11 12:47:21 +00:00
|
|
|
prefix: vec![],
|
|
|
|
};
|
2021-03-16 10:43:58 +00:00
|
|
|
self.data
|
|
|
|
.merkle_tree
|
2022-06-08 08:01:44 +00:00
|
|
|
.db()
|
2023-09-21 13:32:25 +00:00
|
|
|
.transaction(|tx| self.update_item_rec(tx, k, &khash, &key, new_vhash))?;
|
2021-03-11 12:47:21 +00:00
|
|
|
|
2023-09-21 13:32:25 +00:00
|
|
|
let deleted = self.data.merkle_todo.db().transaction(|tx| {
|
2022-06-08 08:01:44 +00:00
|
|
|
let remove = matches!(tx.get(&self.data.merkle_todo, k)?, Some(ov) if ov == vhash_by);
|
|
|
|
if remove {
|
|
|
|
tx.remove(&self.data.merkle_todo, k)?;
|
|
|
|
}
|
|
|
|
Ok(remove)
|
|
|
|
})?;
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
if !deleted {
|
2021-03-12 13:37:46 +00:00
|
|
|
debug!(
|
|
|
|
"({}) Item not deleted from Merkle todo because it changed: {:?}",
|
2021-12-14 11:34:01 +00:00
|
|
|
F::TABLE_NAME,
|
|
|
|
k
|
2021-03-11 12:47:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_item_rec(
|
|
|
|
&self,
|
2022-06-08 08:01:44 +00:00
|
|
|
tx: &mut db::Transaction<'_>,
|
2021-03-11 12:47:21 +00:00
|
|
|
k: &[u8],
|
2021-03-16 19:10:41 +00:00
|
|
|
khash: &Hash,
|
2021-03-11 12:47:21 +00:00
|
|
|
key: &MerkleNodeKey,
|
|
|
|
new_vhash: Option<Hash>,
|
2022-06-08 08:01:44 +00:00
|
|
|
) -> db::TxResult<Option<Hash>, Error> {
|
2021-03-11 12:47:21 +00:00
|
|
|
let i = key.prefix.len();
|
2021-03-11 15:54:15 +00:00
|
|
|
|
|
|
|
// Read node at current position (defined by the prefix stored in key)
|
|
|
|
// Calculate an update to apply to this node
|
|
|
|
// This update is an Option<_>, so that it is None if the update is a no-op
|
|
|
|
// and we can thus skip recalculating and re-storing everything
|
2021-10-26 08:20:05 +00:00
|
|
|
let mutate = match self.read_node_txn(tx, key)? {
|
2021-04-23 19:42:52 +00:00
|
|
|
MerkleNode::Empty => new_vhash.map(|vhv| MerkleNode::Leaf(k.to_vec(), vhv)),
|
2021-03-11 12:47:21 +00:00
|
|
|
MerkleNode::Intermediate(mut children) => {
|
|
|
|
let key2 = key.next_key(khash);
|
|
|
|
if let Some(subhash) = self.update_item_rec(tx, k, khash, &key2, new_vhash)? {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Subtree changed, update this node as well
|
2021-03-11 12:47:21 +00:00
|
|
|
if subhash == self.empty_node_hash {
|
|
|
|
intermediate_rm_child(&mut children, key2.prefix[i]);
|
|
|
|
} else {
|
|
|
|
intermediate_set_child(&mut children, key2.prefix[i], subhash);
|
|
|
|
}
|
2021-03-11 15:54:15 +00:00
|
|
|
|
2021-04-23 19:42:52 +00:00
|
|
|
if children.is_empty() {
|
2021-03-11 12:47:21 +00:00
|
|
|
// should not happen
|
2021-03-12 14:05:26 +00:00
|
|
|
warn!(
|
|
|
|
"({}) Replacing intermediate node with empty node, should not happen.",
|
2021-12-14 11:34:01 +00:00
|
|
|
F::TABLE_NAME
|
2021-03-12 14:05:26 +00:00
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
Some(MerkleNode::Empty)
|
|
|
|
} else if children.len() == 1 {
|
2021-03-11 15:54:15 +00:00
|
|
|
// We now have a single node (case when the update deleted one of only two
|
2021-03-16 19:10:41 +00:00
|
|
|
// children). If that node is a leaf, move it to this level.
|
2021-03-11 12:47:21 +00:00
|
|
|
let key_sub = key.add_byte(children[0].0);
|
|
|
|
let subnode = self.read_node_txn(tx, &key_sub)?;
|
2021-03-16 19:10:41 +00:00
|
|
|
match subnode {
|
|
|
|
MerkleNode::Empty => {
|
2021-04-05 17:55:53 +00:00
|
|
|
warn!(
|
|
|
|
"({}) Single subnode in tree is empty Merkle node",
|
2021-12-14 11:34:01 +00:00
|
|
|
F::TABLE_NAME
|
2021-04-05 17:55:53 +00:00
|
|
|
);
|
2021-03-16 19:10:41 +00:00
|
|
|
Some(MerkleNode::Empty)
|
|
|
|
}
|
2021-04-05 17:55:53 +00:00
|
|
|
MerkleNode::Intermediate(_) => Some(MerkleNode::Intermediate(children)),
|
2021-03-16 19:10:41 +00:00
|
|
|
x @ MerkleNode::Leaf(_, _) => {
|
2022-06-08 08:01:44 +00:00
|
|
|
tx.remove(&self.data.merkle_tree, key_sub.encode())?;
|
2021-03-16 19:10:41 +00:00
|
|
|
Some(x)
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 12:47:21 +00:00
|
|
|
} else {
|
|
|
|
Some(MerkleNode::Intermediate(children))
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Subtree not changed, nothing to do
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 19:10:41 +00:00
|
|
|
MerkleNode::Leaf(exlf_k, exlf_vhash) => {
|
|
|
|
if exlf_k == k {
|
2021-03-11 15:54:15 +00:00
|
|
|
// This leaf is for the same key that the one we are updating
|
2021-03-11 12:47:21 +00:00
|
|
|
match new_vhash {
|
2021-03-16 19:10:41 +00:00
|
|
|
Some(vhv) if vhv == exlf_vhash => None,
|
2021-03-11 12:47:21 +00:00
|
|
|
Some(vhv) => Some(MerkleNode::Leaf(k.to_vec(), vhv)),
|
|
|
|
None => Some(MerkleNode::Empty),
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// This is an only leaf for another key
|
2021-03-16 19:10:41 +00:00
|
|
|
if new_vhash.is_some() {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Move that other key to a subnode, create another subnode for our
|
|
|
|
// insertion and replace current node by an intermediary node
|
2021-03-11 12:47:21 +00:00
|
|
|
let mut int = vec![];
|
2021-03-16 19:10:41 +00:00
|
|
|
|
|
|
|
let exlf_khash = blake2sum(&exlf_k[..]);
|
|
|
|
assert_eq!(khash.as_slice()[..i], exlf_khash.as_slice()[..i]);
|
|
|
|
|
|
|
|
{
|
|
|
|
let exlf_subkey = key.next_key(&exlf_khash);
|
2021-04-05 17:55:53 +00:00
|
|
|
let exlf_sub_hash = self
|
|
|
|
.update_item_rec(
|
|
|
|
tx,
|
|
|
|
&exlf_k[..],
|
|
|
|
&exlf_khash,
|
|
|
|
&exlf_subkey,
|
|
|
|
Some(exlf_vhash),
|
|
|
|
)?
|
|
|
|
.unwrap();
|
2021-03-16 19:10:41 +00:00
|
|
|
intermediate_set_child(&mut int, exlf_subkey.prefix[i], exlf_sub_hash);
|
|
|
|
assert_eq!(int.len(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let key2 = key.next_key(khash);
|
2021-04-05 17:55:53 +00:00
|
|
|
let subhash = self
|
|
|
|
.update_item_rec(tx, k, khash, &key2, new_vhash)?
|
|
|
|
.unwrap();
|
2021-03-16 19:10:41 +00:00
|
|
|
intermediate_set_child(&mut int, key2.prefix[i], subhash);
|
|
|
|
if exlf_khash.as_slice()[i] == khash.as_slice()[i] {
|
|
|
|
assert_eq!(int.len(), 1);
|
|
|
|
} else {
|
|
|
|
assert_eq!(int.len(), 2);
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 12:47:21 +00:00
|
|
|
Some(MerkleNode::Intermediate(int))
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Nothing to do, we don't want to insert this value because it is None,
|
|
|
|
// and we don't want to change the other value because it's for something
|
|
|
|
// else
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(new_node) = mutate {
|
2021-10-26 08:20:05 +00:00
|
|
|
let hash = self.put_node_txn(tx, key, &new_node)?;
|
2021-03-11 12:47:21 +00:00
|
|
|
Ok(Some(hash))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merkle tree node manipulation
|
|
|
|
|
|
|
|
fn read_node_txn(
|
|
|
|
&self,
|
2022-06-08 08:01:44 +00:00
|
|
|
tx: &mut db::Transaction<'_>,
|
2021-03-11 12:47:21 +00:00
|
|
|
k: &MerkleNodeKey,
|
2022-06-08 08:01:44 +00:00
|
|
|
) -> db::TxResult<MerkleNode, Error> {
|
|
|
|
let ent = tx.get(&self.data.merkle_tree, k.encode())?;
|
|
|
|
MerkleNode::decode_opt(&ent).map_err(db::TxError::Abort)
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn put_node_txn(
|
|
|
|
&self,
|
2022-06-08 08:01:44 +00:00
|
|
|
tx: &mut db::Transaction<'_>,
|
2021-03-11 12:47:21 +00:00
|
|
|
k: &MerkleNodeKey,
|
|
|
|
v: &MerkleNode,
|
2022-06-08 08:01:44 +00:00
|
|
|
) -> db::TxResult<Hash, Error> {
|
2021-03-11 18:30:24 +00:00
|
|
|
trace!("Put Merkle node: {:?} => {:?}", k, v);
|
2021-03-11 12:47:21 +00:00
|
|
|
if *v == MerkleNode::Empty {
|
2022-06-08 08:01:44 +00:00
|
|
|
tx.remove(&self.data.merkle_tree, k.encode())?;
|
2021-03-11 12:47:21 +00:00
|
|
|
Ok(self.empty_node_hash)
|
|
|
|
} else {
|
2023-01-03 14:27:36 +00:00
|
|
|
let vby = nonversioned_encode(v).map_err(|e| db::TxError::Abort(e.into()))?;
|
2021-03-11 12:47:21 +00:00
|
|
|
let rethash = blake2sum(&vby[..]);
|
2022-06-08 08:01:44 +00:00
|
|
|
tx.insert(&self.data.merkle_tree, k.encode(), vby)?;
|
2021-03-11 12:47:21 +00:00
|
|
|
Ok(rethash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:54:15 +00:00
|
|
|
// Access a node in the Merkle tree, used by the sync protocol
|
2021-03-11 17:28:03 +00:00
|
|
|
pub(crate) fn read_node(&self, k: &MerkleNodeKey) -> Result<MerkleNode, Error> {
|
2021-03-16 10:43:58 +00:00
|
|
|
let ent = self.data.merkle_tree.get(k.encode())?;
|
2022-06-08 08:01:44 +00:00
|
|
|
MerkleNode::decode_opt(&ent)
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
2021-03-15 18:51:16 +00:00
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
pub fn merkle_tree_len(&self) -> Result<usize, Error> {
|
|
|
|
Ok(self.data.merkle_tree.len()?)
|
2021-03-15 18:51:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-13 14:43:22 +00:00
|
|
|
pub fn merkle_tree_fast_len(&self) -> Result<Option<usize>, Error> {
|
|
|
|
Ok(self.data.merkle_tree.fast_len()?)
|
|
|
|
}
|
|
|
|
|
2022-06-08 08:01:44 +00:00
|
|
|
pub fn todo_len(&self) -> Result<usize, Error> {
|
|
|
|
Ok(self.data.merkle_todo.len()?)
|
2021-03-15 18:51:16 +00:00
|
|
|
}
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 14:08:37 +00:00
|
|
|
struct MerkleWorker<F: TableSchema, R: TableReplication>(Arc<MerkleUpdater<F, R>>);
|
2022-07-08 11:30:26 +00:00
|
|
|
|
|
|
|
#[async_trait]
|
2023-01-03 14:08:37 +00:00
|
|
|
impl<F: TableSchema, R: TableReplication> Worker for MerkleWorker<F, R> {
|
2022-07-08 11:30:26 +00:00
|
|
|
fn name(&self) -> String {
|
2022-12-12 16:16:49 +00:00
|
|
|
format!("{} Merkle", F::TABLE_NAME)
|
2022-07-08 11:30:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 16:16:49 +00:00
|
|
|
fn status(&self) -> WorkerStatus {
|
|
|
|
WorkerStatus {
|
|
|
|
queue_length: Some(self.0.todo_len().unwrap_or(0) as u64),
|
|
|
|
..Default::default()
|
2022-07-08 11:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn work(&mut self, _must_exit: &mut watch::Receiver<bool>) -> Result<WorkerState, Error> {
|
|
|
|
let updater = self.0.clone();
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
for _i in 0..100 {
|
|
|
|
let s = updater.updater_loop_iter();
|
|
|
|
if !matches!(s, Ok(WorkerState::Busy)) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(WorkerState::Busy)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:25:29 +00:00
|
|
|
async fn wait_for_work(&mut self) -> WorkerState {
|
2022-12-14 10:58:06 +00:00
|
|
|
select! {
|
|
|
|
_ = tokio::time::sleep(Duration::from_secs(60)) => (),
|
|
|
|
_ = self.0.data.merkle_todo_notify.notified() => (),
|
|
|
|
}
|
2022-07-08 11:30:26 +00:00
|
|
|
WorkerState::Busy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
impl MerkleNodeKey {
|
|
|
|
fn encode(&self) -> Vec<u8> {
|
|
|
|
let mut ret = Vec::with_capacity(2 + self.prefix.len());
|
2021-03-16 11:18:03 +00:00
|
|
|
ret.extend(&u16::to_be_bytes(self.partition)[..]);
|
2021-03-11 12:47:21 +00:00
|
|
|
ret.extend(&self.prefix[..]);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:10:41 +00:00
|
|
|
pub fn next_key(&self, h: &Hash) -> Self {
|
|
|
|
assert_eq!(h.as_slice()[0..self.prefix.len()], self.prefix[..]);
|
2021-03-11 12:47:21 +00:00
|
|
|
let mut s2 = self.clone();
|
|
|
|
s2.prefix.push(h.as_slice()[self.prefix.len()]);
|
|
|
|
s2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_byte(&self, b: u8) -> Self {
|
|
|
|
let mut s2 = self.clone();
|
|
|
|
s2.prefix.push(b);
|
|
|
|
s2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:10:41 +00:00
|
|
|
impl MerkleNode {
|
2022-06-08 08:01:44 +00:00
|
|
|
fn decode_opt(ent: &Option<db::Value>) -> Result<Self, Error> {
|
2021-03-16 19:10:41 +00:00
|
|
|
match ent {
|
|
|
|
None => Ok(MerkleNode::Empty),
|
2023-01-03 14:27:36 +00:00
|
|
|
Some(v) => Ok(nonversioned_decode::<MerkleNode>(&v[..])?),
|
2021-03-16 19:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
*self == MerkleNode::Empty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
fn intermediate_set_child(ch: &mut Vec<(u8, Hash)>, pos: u8, v: Hash) {
|
|
|
|
for i in 0..ch.len() {
|
|
|
|
if ch[i].0 == pos {
|
|
|
|
ch[i].1 = v;
|
|
|
|
return;
|
|
|
|
} else if ch[i].0 > pos {
|
|
|
|
ch.insert(i, (pos, v));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 19:10:41 +00:00
|
|
|
ch.push((pos, v));
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn intermediate_rm_child(ch: &mut Vec<(u8, Hash)>, pos: u8) {
|
|
|
|
for i in 0..ch.len() {
|
|
|
|
if ch[i].0 == pos {
|
|
|
|
ch.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intermediate_aux() {
|
|
|
|
let mut v = vec![];
|
2021-03-11 17:28:03 +00:00
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 12u8, [12u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(v, vec![(12u8, [12u8; 32].into())]);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 42u8, [42u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![(12u8, [12u8; 32].into()), (42u8, [42u8; 32].into())]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 4u8, [4u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(12u8, [12u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 12u8, [8u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 6u8, [6u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 42u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 11u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 6u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(v, vec![(4u8, [4u8; 32].into()), (12u8, [8u8; 32].into())]);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 6u8, [7u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [7u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|