mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-05 00:03:40 -07:00
global: snapshot
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use std::ops::Add;
|
||||
|
||||
use vecdb::{CheckedSub, Printable};
|
||||
use derive_deref::Deref;
|
||||
use serde::Serialize;
|
||||
use vecdb::{CheckedSub, Printable, StoredCompressed};
|
||||
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::TypeIndex;
|
||||
@@ -22,6 +22,7 @@ use crate::TypeIndex;
|
||||
IntoBytes,
|
||||
KnownLayout,
|
||||
Serialize,
|
||||
StoredCompressed,
|
||||
)]
|
||||
pub struct EmptyAddressIndex(TypeIndex);
|
||||
|
||||
|
||||
@@ -227,13 +227,19 @@ impl TryFrom<&std::path::Path> for Height {
|
||||
}
|
||||
|
||||
impl From<ByteView> for Height {
|
||||
fn from(value: byteview::ByteView) -> Self {
|
||||
fn from(value: ByteView) -> Self {
|
||||
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Height> for ByteView {
|
||||
fn from(value: Height) -> Self {
|
||||
Self::from(&value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Height> for ByteView {
|
||||
fn from(value: &Height) -> Self {
|
||||
Self::new(&value.0.to_be_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@ pub struct LoadedAddressData {
|
||||
pub sent: Sats,
|
||||
pub received: Sats,
|
||||
pub realized_cap: Dollars,
|
||||
pub outputs_len: u32,
|
||||
pub utxos: u32,
|
||||
#[serde(skip)]
|
||||
padding: u32,
|
||||
}
|
||||
|
||||
@@ -41,12 +42,12 @@ impl LoadedAddressData {
|
||||
|
||||
#[inline]
|
||||
pub fn has_0_utxos(&self) -> bool {
|
||||
self.outputs_len == 0
|
||||
self.utxos == 0
|
||||
}
|
||||
|
||||
pub fn receive(&mut self, amount: Sats, price: Option<Dollars>) {
|
||||
self.received += amount;
|
||||
self.outputs_len += 1;
|
||||
self.utxos += 1;
|
||||
if let Some(price) = price {
|
||||
let added = price * amount;
|
||||
self.realized_cap += added;
|
||||
@@ -62,7 +63,7 @@ impl LoadedAddressData {
|
||||
return Err(Error::Str("Previous_amount smaller than sent amount"));
|
||||
}
|
||||
self.sent += amount;
|
||||
self.outputs_len -= 1;
|
||||
self.utxos -= 1;
|
||||
if let Some(previous_price) = previous_price {
|
||||
let subtracted = previous_price * amount;
|
||||
let realized_cap = self.realized_cap.checked_sub(subtracted).unwrap();
|
||||
@@ -94,7 +95,7 @@ impl From<&EmptyAddressData> for LoadedAddressData {
|
||||
sent: value.transfered,
|
||||
received: value.transfered,
|
||||
realized_cap: Dollars::ZERO,
|
||||
outputs_len: 0,
|
||||
utxos: 0,
|
||||
padding: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::ops::Add;
|
||||
|
||||
use derive_deref::Deref;
|
||||
use serde::Serialize;
|
||||
use vecdb::{CheckedSub, Printable};
|
||||
use vecdb::{CheckedSub, Printable, StoredCompressed};
|
||||
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::TypeIndex;
|
||||
@@ -22,6 +22,7 @@ use crate::TypeIndex;
|
||||
IntoBytes,
|
||||
KnownLayout,
|
||||
Serialize,
|
||||
StoredCompressed,
|
||||
)]
|
||||
pub struct LoadedAddressIndex(TypeIndex);
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ mod stored_bool;
|
||||
mod stored_f32;
|
||||
mod stored_f64;
|
||||
mod stored_i16;
|
||||
mod stored_string;
|
||||
mod stored_u16;
|
||||
mod stored_u32;
|
||||
mod stored_u64;
|
||||
@@ -105,6 +106,7 @@ pub use stored_bool::*;
|
||||
pub use stored_f32::*;
|
||||
pub use stored_f64::*;
|
||||
pub use stored_i16::*;
|
||||
pub use stored_string::*;
|
||||
pub use stored_u8::*;
|
||||
pub use stored_u16::*;
|
||||
pub use stored_u32::*;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
use byteview::ByteView;
|
||||
use derive_deref::Deref;
|
||||
use serde::Serialize;
|
||||
use vecdb::Printable;
|
||||
|
||||
#[derive(Default, Debug, Deref, Clone, Serialize)]
|
||||
pub struct StoredString(String);
|
||||
|
||||
impl StoredString {
|
||||
pub fn new(string: String) -> Self {
|
||||
Self(string)
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn as_string(&self) -> &String {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for StoredString {
|
||||
fn from(value: String) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ByteView> for StoredString {
|
||||
fn from(value: ByteView) -> Self {
|
||||
let bytes = &*value;
|
||||
Self(String::from_utf8_lossy(bytes).into_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoredString> for ByteView {
|
||||
fn from(value: StoredString) -> Self {
|
||||
Self::from(&value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&StoredString> for ByteView {
|
||||
fn from(value: &StoredString) -> Self {
|
||||
Self::new(value.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl Printable for StoredString {
|
||||
fn to_string() -> &'static str {
|
||||
"string"
|
||||
}
|
||||
|
||||
fn to_possible_strings() -> &'static [&'static str] {
|
||||
&["string"]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user