Skip to content

Root Hash is always [0; 32], but per-shard root hashed are non-zero and change as expected #44

@NikhilSharmaWe

Description

@NikhilSharmaWe

Description

  • Changed the v2_demo.rs to print the root hashe but it only outputs [0; 32], but shard root hashes are non-zero and change as expected.

  • If it was an issue of not committing then shard root hashes should be also have been not updated. What could be the issue?

Reproduce

Change v2_demo.rs with

use parking_lot::RwLock;
use qmdb::config::Config;
use qmdb::def::{DEFAULT_ENTRY_SIZE, IN_BLOCK_IDX_BITS, OP_CREATE, SHARD_COUNT};
use qmdb::entryfile::EntryBz;
use qmdb::tasks::TasksManager;
use qmdb::test_helper::SimpleTask;
use qmdb::utils::byte0_to_shard_id;
use qmdb::utils::changeset::ChangeSet;
use qmdb::utils::hasher;
use qmdb::{AdsCore, AdsWrap, ADS};
use std::sync::Arc;

fn print_root_hashes(shared_ads: &qmdb::SharedAdsWrap, heights: &[i64]) {
    for &h in heights {
        let root = shared_ads.get_root_hash_of_height(h);
        println!("ROOT HASH at height {}: {:?}", h, root);
    }
    for h in heights {
        for shard in 0..SHARD_COUNT {
            let root = shared_ads.ads.get_metadb().read().get_root_hash(shard);
            println!("  Shard {} root at height {}: {:?}", shard, h, root);
        }
    }
}

fn main() {
    let ads_dir = "ADS";
    let config = Config::from_dir(ads_dir);
    AdsCore::init_dir(&config);
    let mut ads = AdsWrap::new(&config);

    // Block 1
    let mut task_list = Vec::with_capacity(10);
    for i in 0..10 {
        let mut cset_list = Vec::with_capacity(2);
        for j in 0..2 {
            let mut cset = ChangeSet::new();
            let mut k = [0u8; 32];
            let mut v = [1u8; 32];
            k[0] = i as u8;
            k[1] = j as u8;
            for n in 0..5 {
                k[2] = n as u8;
                v[0] = n as u8;
                let kh = hasher::hash(&k[..]);
                let shard_id = byte0_to_shard_id(kh[0]) as u8;
                cset.add_op(OP_CREATE, shard_id, &kh, &k[..], &v[..], None);
            }
            cset.sort();
            cset_list.push(cset);
        }
        let task = SimpleTask::new(cset_list);
        task_list.push(RwLock::new(Some(task)));
    }

    let height1 = 1;
    let task_count = task_list.len() as i64;
    let last_task_id = (height1 << IN_BLOCK_IDX_BITS) | (task_count - 1);
    ads.start_block(height1, Arc::new(TasksManager::new(task_list, last_task_id)));
    let shared_ads = ads.get_shared();
    shared_ads.insert_extra_data(height1, "".to_owned());
    for idx in 0..task_count {
        let task_id = (height1 << IN_BLOCK_IDX_BITS) | idx;
        shared_ads.add_task(task_id);
    }
    println!("Flushing block 1...");
    ads.flush();
    let shared_ads = ads.get_shared();
    let curr_height = ads.get_metadb().read().get_curr_height();
    println!("After flush block 1: Last committed height: {}", curr_height);
    print_root_hashes(&shared_ads, &[1, 2]);

    // Read an entry from block 1
    let mut buf = [0; DEFAULT_ENTRY_SIZE];
    let mut k = [0u8; 32];
    k[0] = 1;
    k[1] = 1;
    k[2] = 3;
    let kh = hasher::hash(&k[..]);
    let (n, ok) = shared_ads.read_entry(-1, &kh[..], &[], &mut buf);
    let e = EntryBz { bz: &buf[..n] };
    println!("entry={:?} value={:?} ok={}", &buf[..n], e.value(), ok);

    // Block 2
    let mut cset = ChangeSet::new();
    let mut k = [0u8; 32];
    let mut v = [2u8; 32];
    k[0] = 10;
    k[1] = 0;
    k[2] = 0;
    v[0] = 42;
    let kh = hasher::hash(&k[..]);
    let shard_id = byte0_to_shard_id(kh[0]) as u8;
    cset.add_op(OP_CREATE, shard_id, &kh, &k[..], &v[..], None);
    cset.sort();

    let task = SimpleTask::new(vec![cset]);
    let task_list = vec![RwLock::new(Some(task))];
    let height2 = 2;
    let last_task_id = (height2 << IN_BLOCK_IDX_BITS) | 0;

    ads.start_block(height2, Arc::new(TasksManager::new(task_list, last_task_id)));
    let shared_ads = ads.get_shared();
    shared_ads.insert_extra_data(height2, "".to_owned());
    shared_ads.add_task(last_task_id);
    println!("Flushing block 2...");
    ads.flush();

    let shared_ads = ads.get_shared();
    let curr_height = ads.get_metadb().read().get_curr_height();
    println!("After flush block 2: Last committed height: {}", curr_height);
    print_root_hashes(&shared_ads, &[1, 2]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions