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
130
131
132
133
#[macro_use]
extern crate serde_derive;

use hypermachines_sys::{host_request, read_arguments, rpc_response};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_bytes::ByteBuf;
use std::collections::HashMap;

// ID Types
pub type CoreId = u32;
pub type DatabaseId = u32;

// Hypercore Types
pub type CoreKey = Vec<u8>;
pub type Seq = u64;
pub type ByteLength = u64;

pub type Block = ByteBuf;

// Hyperbee Types
pub type DatabaseKey = String;
pub type DatabaseValue = Block;

#[derive(Serialize, Deserialize)]
pub struct DatabaseRecord {
    pub key: DatabaseKey,
    #[serde(with = "serde_bytes")]
    pub value: Option<DatabaseValue>,
}

// Data Structs

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CoreSpec {
    #[serde(with = "serde_bytes")]
    key: CoreKey,
    length: Option<Seq>,
    writable: Option<bool>,
    is_static: Option<bool>,
}

#[derive(Serialize, Deserialize)]
pub struct DatabaseSpec {
    core: CoreSpec,
    sub: String,
}

#[derive(Serialize, Deserialize)]
pub struct MachineState {
    cores: HashMap<CoreId, CoreSpec>,
    dbs: HashMap<DatabaseId, CoreSpec>,
    output: DatabaseId,
}

// Request Structs

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Request {
    // Info Requests
    GetMachineState,

    // Hypercore Requests
    GetCore {
        spec: Option<CoreSpec>,
    },
    GetCoreSpec {
        id: CoreId,
    },
    GetCoreBlocks {
        id: CoreId,
        seqs: Vec<Seq>,
    },
    GetCoreLength {
        id: CoreId,
    },
    AppendCoreBlocks {
        id: CoreId,
        blocks: Vec<Block>,
    },

    // Hyperbee Requests
    GetPrimaryDatabase {
        sub: Option<String>,
    },
    GetDatabase {
        spec: Option<DatabaseSpec>,
    },
    GetDbRecord {
        id: DatabaseId,
        key: DatabaseKey,
    },
    PutDbRecord {
        id: DatabaseId,
        record: DatabaseRecord,
    },
}

// Public Functions

pub fn get_core(spec: Option<CoreSpec>) -> CoreId {
    host_request(Request::GetCore { spec })
}

pub fn get_core_blocks(id: CoreId, seqs: Vec<Seq>) -> Vec<Block> {
    host_request(Request::GetCoreBlocks { id, seqs })
}

pub fn get_core_length(id: CoreId) -> Seq {
    host_request(Request::GetCoreLength { id })
}

pub fn append_core_blocks(id: CoreId, blocks: Vec<Block>) -> u32 {
    host_request(Request::AppendCoreBlocks { id, blocks })
}

pub fn get_db(spec: Option<DatabaseSpec>) -> DatabaseId {
    host_request(Request::GetDatabase { spec })
}

pub fn get_primary_db(sub: Option<String>) -> DatabaseId {
    host_request(Request::GetPrimaryDatabase { sub })
}

pub fn get_db_record(id: DatabaseId, key: DatabaseKey) -> DatabaseRecord {
    host_request(Request::GetDbRecord { id, key })
}

pub fn put_db_record(id: DatabaseId, record: DatabaseRecord) -> u32 {
    host_request(Request::PutDbRecord { id, record })
}