aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/bin/adenosine-pds-dump-mst.rs
blob: 1f1b9471bffc08f1ff12bf61f9a7d1af910d4ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/// Helper program to print MST keys/docs from a sqlite repo
use anyhow::{anyhow, Result};
use ipfs_sqlite_block_store::BlockStore;
use libipld::cbor::DagCborCodec;
use libipld::prelude::Codec;
use libipld::{Cid, DagCbor};

use std::env;

#[derive(Debug, DagCbor, PartialEq, Eq)]
struct CommitNode {
    root: Cid,
    sig: Box<[u8]>,
}

#[derive(Debug, DagCbor, PartialEq, Eq)]
struct RootNode {
    auth_token: Option<String>,
    prev: Option<Cid>,
    // TODO: not 'metadata'?
    meta: Cid,
    data: Cid,
}

#[derive(Debug, DagCbor, PartialEq, Eq)]
struct MetadataNode {
    datastore: String, // "mst"
    did: String,
    version: u8, // 1
}

#[derive(Debug, DagCbor, PartialEq, Eq)]
struct MstEntry {
    k: String,
    p: u32,
    v: Cid,
    t: Option<Cid>,
}

#[derive(Debug, DagCbor, PartialEq)]
struct MstNode {
    l: Option<Cid>,
    e: Vec<MstEntry>,
}

fn get_mst_node(db: &mut BlockStore<libipld::DefaultParams>, cid: &Cid) -> Result<MstNode> {
    let mst_node: MstNode = DagCborCodec.decode(
        &db.get_block(cid)?
            .ok_or(anyhow!("expected block in store"))?,
    )?;
    Ok(mst_node)
}

fn print_mst_keys(db: &mut BlockStore<libipld::DefaultParams>, cid: &Cid) -> Result<()> {
    let node = get_mst_node(db, cid)?;
    if let Some(ref left) = node.l {
        print_mst_keys(db, left)?;
    }
    let mut key: String = "".to_string();
    for entry in node.e.iter() {
        key = format!("{}{}", &key[0..entry.p as usize], entry.k);
        println!("{}\t-> {}", key, entry.v);
        if let Some(ref right) = entry.t {
            print_mst_keys(db, right)?;
        }
    }
    Ok(())
}

async fn dump_mst_keys(db_path: &str) -> Result<()> {
    let mut db: BlockStore<libipld::DefaultParams> = {
        let path = std::path::PathBuf::from(db_path);
        let path = ipfs_sqlite_block_store::DbPath::File(path);
        BlockStore::open_path(path, Default::default())?
    };

    let all_aliases: Vec<(Vec<u8>, Cid)> = db.aliases()?;
    if all_aliases.is_empty() {
        println!("expected at least one alias in block store");
        std::process::exit(-1);
    }
    let (alias, commit_cid) = all_aliases[0].clone();
    println!(
        "starting from {} [{}]",
        commit_cid,
        String::from_utf8_lossy(&alias)
    );

    // NOTE: the faster way to develop would have been to decode to libipld::ipld::Ipld first? meh

    //println!("raw commit: {:?}", &db.get_block(&commit_cid)?.ok_or(anyhow!("expected commit block in store"))?);
    let commit: CommitNode = DagCborCodec.decode(
        &db.get_block(&commit_cid)?
            .ok_or(anyhow!("expected commit block in store"))?,
    )?;
    println!("Commit: {:?}", commit);
    //println!("raw root: {:?}", &db.get_block(&commit.root)?.ok_or(anyhow!("expected commit block in store"))?);
    let root: RootNode = DagCborCodec.decode(
        &db.get_block(&commit.root)?
            .ok_or(anyhow!("expected root block in store"))?,
    )?;
    println!("Root: {:?}", root);
    let metadata: MetadataNode = DagCborCodec.decode(
        &db.get_block(&root.meta)?
            .ok_or(anyhow!("expected metadata block in store"))?,
    )?;
    println!("Metadata: {:?}", metadata);
    let mst_node: MstNode = DagCborCodec.decode(
        &db.get_block(&root.data)?
            .ok_or(anyhow!("expected block in store"))?,
    )?;
    println!("MST root node: {:?}", mst_node);

    println!("============");

    print_mst_keys(&mut db, &root.data)?;
    Ok(())
}

#[tokio::main]
async fn main() -> Result<()> {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        println!("expected 1 args: <db_path>");
        std::process::exit(-1);
    }
    let db_path = &args[1];
    dump_mst_keys(db_path).await
}