Prepare db

This commit is contained in:
Janus
2021-07-13 14:04:02 -04:00
parent b6fb3a486a
commit 516f1f3be6
6 changed files with 119 additions and 15 deletions

View File

@@ -1,10 +1,11 @@
use std::net::SocketAddr;
use std::sync::Arc;
use drk::blockchain::{rocks::columns, Rocks, RocksColumn};
use drk::cli::ServiceCli;
use drk::service::CashierService;
// Testing only
use drk::service::CashierKeys;
use drk::util::join_config_path;
use drk::Result;
use async_executor::Executor;
@@ -20,15 +21,15 @@ fn setup_addr(address: Option<SocketAddr>, default: SocketAddr) -> SocketAddr {
async fn start(executor: Arc<Executor<'_>>, options: ServiceCli) -> Result<()> {
let accept_addr: SocketAddr = setup_addr(options.accept_addr, "127.0.0.1:7777".parse()?);
//let pub_addr: SocketAddr = setup_addr(options.pub_addr, "127.0.0.1:8888".parse()?);
//let database_path = options.database_path.clone();
let database_path = options.database_path.clone();
//let database_path = join_config_path(&(*database_path))?;
//let rocks = Rocks::new(&database_path)?;
//let rocks_slabstore_column = RocksColumn::<columns::Slabs>::new(rocks);
let database_path = join_config_path(&(*database_path))?;
let rocks = Rocks::new(&database_path)?;
let rocks_cashierstore_column = RocksColumn::<columns::CashierKeys>::new(rocks);
// Use pw: PASSWORD for now
let cashier_wallet = Arc::new(WalletDB::new("cashier.db", "PASSWORD")?);
//let cashier_wallet = Arc::new(WalletDB::new("cashier.db", "PASSWORD")?);
let cashier = CashierService::new(accept_addr, cashier_wallet)?;
let cashier = CashierService::new(accept_addr, rocks_cashierstore_column)?;
cashier.start(executor.clone()).await?;
Ok(())
@@ -36,11 +37,6 @@ async fn start(executor: Arc<Executor<'_>>, options: ServiceCli) -> Result<()> {
fn main() -> Result<()> {
let btc = CashierKeys::new().unwrap();
let deposit = btc.get_deposit_address();
println!("{:?}", deposit);
use simplelog::*;
let ex = Arc::new(Executor::new());

View File

@@ -0,0 +1,44 @@
use crate::serial::{Decodable, Encodable};
use crate::Result;
#[derive(Clone, Debug)]
pub struct CashierKeypair {
zk_public: jubjub::SubgroupPoint,
payload: Vec<u8>,
}
impl CashierKeypair {
pub fn new(zk_public: jubjub::SubgroupPoint, payload: Vec<u8>) -> Self {
CashierKeypair { zk_public, payload }
}
pub fn set_index(&mut self, index: jubjub::SubgroupPoint) {
self.zk_public = index;
}
pub fn get_index(&self) -> jubjub::SubgroupPoint {
self.zk_public
}
pub fn get_payload(&self) -> Vec<u8> {
self.payload.clone()
}
}
impl Encodable for CashierKeypair {
fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
let mut len = 0;
len += self.zk_public.encode(&mut s)?;
len += self.payload.encode(&mut s)?;
Ok(len)
}
}
impl Decodable for CashierKeypair {
fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
Ok(Self {
zk_public: Decodable::decode(&mut d)?,
payload: Decodable::decode(&mut d)?,
})
}
}

View File

@@ -0,0 +1,55 @@
use std::sync::Arc;
use crate::serial::{deserialize, serialize};
use crate::Result;
use super::rocks::{columns, IteratorMode, RocksColumn};
use super::cashier_keypair::CashierKeypair;
pub struct CashierStore {
rocks: RocksColumn<columns::CashierKeys>,
}
impl CashierStore {
pub fn new(rocks: RocksColumn<columns::CashierKeys>) -> Result<Arc<Self>> {
Ok(Arc::new(CashierStore { rocks }))
}
pub fn get(&self, key: jubjub::SubgroupPoint) -> Result<Option<Vec<u8>>> {
let value = self.rocks.get(key)?;
Ok(value)
}
pub fn put(&self, keypair: CashierKeypair) -> Result<Option<jubjub::SubgroupPoint>> {
let index = keypair.get_index();
let check = self.get(index);
match self.get(index) {
Ok(_v) => Ok(None),
Err(_e) => {
self.rocks.put(index.clone(), keypair)?;
Ok(Some(index))
},
}
}
pub fn get_value_deserialized(&self, key: Vec<u8>) -> Result<Option<CashierKeypair>> {
self.rocks.get_value_deserialized::<CashierKeypair>(key)
}
// Fix this
// pub fn get_last_index(&self) -> Result<jubjub::SubgroupPoint> {
// let last_index = self.rocks.iterator(IteratorMode::End)?.next();
// match last_index {
// Some((index, _)) => Ok(deserialize(&index)?),
// None => Ok()
// }
// }
pub fn get_last_index_as_bytes(&self) -> Result<Vec<u8>> {
let last_index = self.rocks.iterator(IteratorMode::End)?.next();
match last_index {
Some((index, _)) => Ok(index.to_vec()),
None => Ok(serialize::<u64>(&0)),
}
}
}

View File

@@ -1,7 +1,11 @@
pub mod rocks;
pub mod slab;
pub mod slabstore;
pub mod cashier_keypair;
pub mod cashierstore;
pub use rocks::{Rocks, RocksColumn};
pub use slab::Slab;
pub use slabstore::SlabStore;
pub use cashier_keypair::CashierKeypair;
pub use cashierstore::CashierStore;

View File

@@ -54,7 +54,7 @@ impl Rocks {
// nullifiers column family
let nullifiers_cf = ColumnFamilyDescriptor::new(columns::Nullifiers::NAME, cf_opts.clone());
// merkleroots column family
let merkleroots_cf = ColumnFamilyDescriptor::new(columns::MerkleRoots::NAME, cf_opts);
let merkleroots_cf = ColumnFamilyDescriptor::new(columns::MerkleRoots::NAME, cf_opts.clone());
// cashierkeypair column family
let cashierkeys_cf = ColumnFamilyDescriptor::new(columns::CashierKeys::NAME, cf_opts);

View File

@@ -7,15 +7,14 @@ use bitcoin::util::{address::Payload, address::Address};
use bitcoin::hash_types::PubkeyHash;
use bitcoin::network::constants::Network;
use super::reqrep::{PeerId, RepProtocol, Reply, ReqProtocol, Request};
use crate::blockchain::{rocks::columns, RocksColumn, CashierKeypair, CashierStore};
use crate::{serial::deserialize, serial::serialize, Error, Result};
use std::net::SocketAddr;
use async_std::sync::Arc;
use async_executor::Executor;
// Struct still needs to attach to drk key stored in db
pub struct CashierKeys {
zk_pubkey: jubjub::SubgroupPoint,
secret_key: SecretKey,
bitcoin_private_key: PrivateKey,
pub bitcoin_public_key: BitcoinPubKey,
@@ -71,9 +70,12 @@ pub struct CashierService {
impl CashierService {
pub fn new(
addr: SocketAddr,
rocks: RocksColumn<columns::CashierKeys>,
)-> Result<Arc<CashierService>> {
let cashierstore = CashierStore::new(rocks)?;
Ok(Arc::new(CashierService {
cashierstore,
addr,
}))
}
@@ -105,9 +107,11 @@ impl CashierService {
loop {
match recv_queue.recv().await {
Ok(msg) => {
let cashierstore = self.cashierstore.clone();
let _ = executor
.spawn(Self::handle_request(
msg,
cashierstore,
send_queue.clone(),
))
.detach();
@@ -121,6 +125,7 @@ impl CashierService {
}
async fn handle_request(
msg: (PeerId, Request),
cashierstore: Arc<CashierStore>,
send_queue: async_channel::Sender<(PeerId, Reply)>,
) -> Result<()> {
let request = msg.1;