Skip to content

Commit

Permalink
Effort to port rusk-wallet to new
Browse files Browse the repository at this point in the history
Add missing features in
  • Loading branch information
Daksh14 committed Aug 28, 2024
1 parent 76698f2 commit 965b9ce
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 904 deletions.
19 changes: 5 additions & 14 deletions rusk-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,15 @@ aes = "0.7"
rocksdb = "0.22"
flume = "0.10.14"
reqwest = { version = "0.11", features = ["stream"] }

dusk-wallet-core = "0.24.0-plonk.0.16-rc.2"
dusk-bytes = "0.1"
dusk-pki = "0.13"
rusk-abi = { version = "0.12.0-rc", default-features = false }
phoenix-core = { version = "0.21", features = ["alloc"] }
dusk-schnorr = { version = "0.14", default-features = false }
dusk-poseidon = "0.31"
dusk-plonk = "0.16"
dusk-bls12_381-sign = { version = "0.5", default-features = false }
ff = { version = "0.13", default-features = false }
poseidon-merkle = "0.3"

wallet-core = { version = "0.1", path = "../wallet-core" }

tracing = "0.1"
tracing-subscriber = { version = "0.3.0", features = [
"fmt",
"env-filter",
"json",
"fmt",
"env-filter",
"json",
] }

rkyv = { version = "=0.7.39", default-features = false }
Expand Down
86 changes: 16 additions & 70 deletions rusk-wallet/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::path::Path;

use dusk_bytes::{DeserializableSlice, Serializable};
use dusk_pki::PublicSpendKey;
use dusk_plonk::prelude::BlsScalar;
use dusk_wallet_core::Store;
use phoenix_core::Note;
use rocksdb::{DBWithThreadMode, MultiThreaded, Options};
use wallet_core::prelude::*;

use crate::{error::Error, store::LocalStore, MAX_ADDRESSES};
use crate::error::Error;

type DB = DBWithThreadMode<MultiThreaded>;

Expand All @@ -30,21 +26,9 @@ impl Cache {
/// Returns a new cache instance.
pub(crate) fn new<T: AsRef<Path>>(
path: T,
store: &LocalStore,
cfs: Vec<String>,
status: fn(&str),
) -> Result<Self, Error> {
let cfs: Vec<_> = (0..MAX_ADDRESSES)
.flat_map(|i| {
let ssk =
store.retrieve_ssk(i as u64).expect("ssk to be available");
let psk = ssk.public_spend_key();

let live = format!("{:?}", psk);
let spent = format!("spent_{:?}", psk);
[live, spent]
})
.collect();

status("Opening notes database");

let mut opts = Options::default();
Expand All @@ -64,11 +48,11 @@ impl Cache {
// representation of the tuple (NoteHeight, Note)
pub(crate) fn insert(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
height: u64,
note_data: (Note, BlsScalar),
) -> Result<(), Error> {
let cf_name = format!("{:?}", psk);
let cf_name = format!("{:?}", pk);

let cf = self
.db
Expand All @@ -90,11 +74,11 @@ impl Cache {
// representation of the tuple (NoteHeight, Note)
pub(crate) fn insert_spent(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
height: u64,
note_data: (Note, BlsScalar),
) -> Result<(), Error> {
let cf_name = format!("spent_{:?}", psk);
let cf_name = format!("spent_{:?}", pk);

let cf = self
.db
Expand All @@ -113,14 +97,14 @@ impl Cache {

pub(crate) fn spend_notes(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
nullifiers: &[BlsScalar],
) -> Result<(), Error> {
if nullifiers.is_empty() {
return Ok(());
}
let cf_name = format!("{:?}", psk);
let spent_cf_name = format!("spent_{:?}", psk);
let cf_name = format!("{:?}", pk);
let spent_cf_name = format!("spent_{:?}", pk);

let cf = self
.db
Expand Down Expand Up @@ -163,9 +147,9 @@ impl Cache {
/// Returns an iterator over all unspent notes nullifier for the given PSK.
pub(crate) fn unspent_notes_id(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
) -> Result<Vec<BlsScalar>, Error> {
let cf_name = format!("{:?}", psk);
let cf_name = format!("{:?}", pk);
let mut notes = vec![];

if let Some(cf) = self.db.cf_handle(&cf_name) {
Expand All @@ -187,9 +171,9 @@ impl Cache {
/// in order of note position.
pub(crate) fn notes(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
) -> Result<BTreeSet<NoteData>, Error> {
let cf_name = format!("{:?}", psk);
let cf_name = format!("{:?}", pk);
let mut notes = BTreeSet::<NoteData>::new();

if let Some(cf) = self.db.cf_handle(&cf_name) {
Expand All @@ -212,9 +196,9 @@ impl Cache {
/// of block height.
pub(crate) fn spent_notes(
&self,
psk: &PublicSpendKey,
pk: &PhoenixPublicKey,
) -> Result<Vec<(BlsScalar, NoteData)>, Error> {
let cf_name = format!("spent_{:?}", psk);
let cf_name = format!("spent_{:?}", ps);
let mut notes = vec![];

if let Some(cf) = self.db.cf_handle(&cf_name) {
Expand All @@ -241,41 +225,3 @@ pub(crate) struct NoteData {
pub height: u64,
pub note: Note,
}

impl PartialOrd for NoteData {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for NoteData {
fn cmp(&self, other: &Self) -> Ordering {
self.note.pos().cmp(other.note.pos())
}
}

impl Serializable<{ u64::SIZE + Note::SIZE }> for NoteData {
type Error = dusk_bytes::Error;
/// Converts a Note into a byte representation
fn to_bytes(&self) -> [u8; Self::SIZE] {
let mut buf = [0u8; Self::SIZE];

buf[0..8].copy_from_slice(&self.height.to_bytes());

buf[8..].copy_from_slice(&self.note.to_bytes());

buf
}

/// Attempts to convert a byte representation of a note into a `Note`,
/// failing if the input is invalid
fn from_bytes(bytes: &[u8; Self::SIZE]) -> Result<Self, Self::Error> {
let mut one_u64 = [0u8; 8];
one_u64.copy_from_slice(&bytes[0..8]);
let height = u64::from_bytes(&one_u64)?;

let note = Note::from_slice(&bytes[8..])?;
Ok(Self { height, note })
}
}
Loading

0 comments on commit 965b9ce

Please sign in to comment.