Skip to content

Commit

Permalink
Fix networking, serialization, reorg issues, tag for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Apr 29, 2024
1 parent cf4f21f commit efad71b
Show file tree
Hide file tree
Showing 14 changed files with 310 additions and 170 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ version = "0.8.0"
git = "https://github.com/Ash-L2L/bip300301.git"
rev = "d1da609b4c77d2b53bea2c8e922891b83612a03b"

[workspace.dependencies.rustreexo]
git = "https://github.com/Ash-L2L/rustreexo.git"
rev = "a3ac7d3ebe9749ebd0bb34c709e7616f83d573b3"

[profile.release]
# lto = "fat"
2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ eframe = "0.27.1"
futures = "0.3.30"
human-size = "0.4.3"
jsonrpsee = { version = "0.20.0", features = ["server"] }
rustreexo = { version = "0.1.0" }
rustreexo = { workspace = true }
parking_lot = "0.12.1"
serde = { version = "1.0.179", features = ["derive"] }
shlex = "1.3.0"
Expand Down
3 changes: 2 additions & 1 deletion app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ impl App {
.await?;
let roots = {
let mut accumulator = self.node.get_accumulator()?;
body.modify_pollard(&mut accumulator)
body.modify_pollard(&mut accumulator.0)
.map_err(Error::Utreexo)?;
accumulator
.0
.get_roots()
.iter()
.map(|root| root.get_data())
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ quinn = "0.10.1"
rayon = "1.7.0"
rcgen = "0.11.1"
rustls = { version = "0.21.5", features = ["dangerous_configuration"] }
rustreexo = { version = "0.1.0", features = ["with-serde"] }
rustreexo = { workspace = true, features = ["with-serde"] }
serde = { version = "1.0.179", features = ["derive"] }
serde_json = "1.0.113"
sha256 = "1.2.2"
Expand Down
69 changes: 52 additions & 17 deletions lib/archive.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::cmp::Ordering;

use fallible_iterator::FallibleIterator;
use heed::{
types::{OwnedType, SerdeBincode},
Database, RoTxn, RwTxn,
};
use heed::{types::SerdeBincode, Database, RoTxn, RwTxn};

use crate::types::{BlockHash, Body, Header};
use crate::types::{Accumulator, BlockHash, Body, Header};

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand All @@ -16,6 +13,8 @@ pub enum Error {
InvalidPrevSideHash,
#[error("invalid merkle root")]
InvalidMerkleRoot,
#[error("no accumulator for block {0}")]
NoAccumulator(BlockHash),
#[error("no block with hash {0}")]
NoBlock(BlockHash),
#[error("no header with hash {0}")]
Expand All @@ -26,25 +25,50 @@ pub enum Error {

#[derive(Clone)]
pub struct Archive {
accumulators: Database<SerdeBincode<BlockHash>, SerdeBincode<Accumulator>>,
headers: Database<SerdeBincode<BlockHash>, SerdeBincode<Header>>,
bodies: Database<SerdeBincode<BlockHash>, SerdeBincode<Body>>,
hash_to_height: Database<SerdeBincode<BlockHash>, OwnedType<u32>>,
hash_to_height: Database<SerdeBincode<BlockHash>, SerdeBincode<u32>>,
}

impl Archive {
pub const NUM_DBS: u32 = 3;
pub const NUM_DBS: u32 = 4;

pub fn new(env: &heed::Env) -> Result<Self, Error> {
let accumulators = env.create_database(Some("accumulators"))?;
let headers = env.create_database(Some("headers"))?;
let bodies = env.create_database(Some("bodies"))?;
let hash_to_height = env.create_database(Some("hash_to_height"))?;
Ok(Self {
accumulators,
headers,
bodies,
hash_to_height,
})
}

pub fn try_get_accumulator(
&self,
rotxn: &RoTxn,
block_hash: BlockHash,
) -> Result<Option<Accumulator>, Error> {
if block_hash == BlockHash::default() {
Ok(Some(Accumulator::default()))
} else {
let accumulator = self.accumulators.get(rotxn, &block_hash)?;
Ok(accumulator)
}
}

pub fn get_accumulator(
&self,
rotxn: &RoTxn,
block_hash: BlockHash,
) -> Result<Accumulator, Error> {
self.try_get_accumulator(rotxn, block_hash)?
.ok_or(Error::NoAccumulator(block_hash))
}

pub fn try_get_header(
&self,
rotxn: &RoTxn,
Expand Down Expand Up @@ -104,6 +128,17 @@ impl Archive {
.ok_or(Error::NoHeight(block_hash))
}

/// Store a block body. The header must already exist.
pub fn put_accumulator(
&self,
rwtxn: &mut RwTxn,
block_hash: BlockHash,
accumulator: &Accumulator,
) -> Result<(), Error> {
self.accumulators.put(rwtxn, &block_hash, accumulator)?;
Ok(())
}

/// Store a block body. The header must already exist.
pub fn put_body(
&self,
Expand Down Expand Up @@ -159,30 +194,30 @@ impl Archive {
) -> Result<BlockHash, Error> {
let mut height0 = self.get_height(rotxn, block_hash0)?;
let mut height1 = self.get_height(rotxn, block_hash1)?;
let mut header0 = self.get_header(rotxn, block_hash0)?;
let mut header1 = self.get_header(rotxn, block_hash1)?;
let mut header0 = self.try_get_header(rotxn, block_hash0)?;
let mut header1 = self.try_get_header(rotxn, block_hash1)?;
// Find respective ancestors of block_hash0 and block_hash1 with height
// equal to min(height0, height1)
loop {
match height0.cmp(&height1) {
Ordering::Less => {
block_hash1 = header1.prev_side_hash;
header1 = self.get_header(rotxn, block_hash1)?;
block_hash1 = header1.unwrap().prev_side_hash;
header1 = self.try_get_header(rotxn, block_hash1)?;
height1 -= 1;
}
Ordering::Greater => {
block_hash0 = header0.prev_side_hash;
header0 = self.get_header(rotxn, block_hash0)?;
block_hash0 = header0.unwrap().prev_side_hash;
header0 = self.try_get_header(rotxn, block_hash0)?;
height0 -= 1;
}
Ordering::Equal => {
if block_hash0 == block_hash1 {
return Ok(block_hash0);
} else {
block_hash0 = header0.prev_side_hash;
block_hash1 = header1.prev_side_hash;
header0 = self.get_header(rotxn, block_hash0)?;
header1 = self.get_header(rotxn, block_hash1)?;
block_hash0 = header0.unwrap().prev_side_hash;
block_hash1 = header1.unwrap().prev_side_hash;
header0 = self.try_get_header(rotxn, block_hash0)?;
header1 = self.try_get_header(rotxn, block_hash1)?;
height0 -= 1;
height1 -= 1;
}
Expand Down
10 changes: 4 additions & 6 deletions lib/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::VecDeque;

use heed::{types::SerdeBincode, Database, RoTxn, RwTxn};
use rustreexo::accumulator::pollard::Pollard;

use crate::types::{AuthorizedTransaction, OutPoint, Txid};
use crate::types::{Accumulator, AuthorizedTransaction, OutPoint, Txid};

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand Down Expand Up @@ -104,7 +103,7 @@ impl MemPool {
pub fn regenerate_proofs(
&self,
rwtxn: &mut RwTxn,
accumulator: &Pollard,
accumulator: &Accumulator,
) -> Result<(), Error> {
let mut iter = self.transactions.iter_mut(rwtxn)?;
while let Some(tx) = iter.next() {
Expand All @@ -115,9 +114,8 @@ impl MemPool {
.iter()
.map(|(_, utxo_hash)| utxo_hash.into())
.collect();
let (proof, _) =
accumulator.prove(&targets).map_err(Error::Utreexo)?;
tx.transaction.proof = proof;
tx.transaction.proof =
accumulator.0.prove(&targets).map_err(Error::Utreexo)?;
unsafe { iter.put_current(&txid, &tx) }?;
}
Ok(())
Expand Down
7 changes: 4 additions & 3 deletions lib/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Miner {
height: u32,
header: Header,
body: Body,
) -> Result<(), Error> {
) -> Result<bitcoin::Txid, Error> {
let str_hash_prev = header.prev_main_hash.to_string();
let critical_hash: [u8; 32] = header.hash().into();
let critical_hash = bitcoin::BlockHash::from_byte_array(critical_hash);
Expand All @@ -77,11 +77,12 @@ impl Miner {
.as_str()
.map(|s| s.to_owned())
.ok_or(Error::InvalidJson { json: value })?;
let _ =
let txid =
bitcoin::Txid::from_str(&txid).map_err(bip300301::Error::from)?;
tracing::info!("created BMM tx: {txid}");
assert_eq!(header.merkle_root, body.compute_merkle_root());
self.block = Some((header, body));
Ok(())
Ok(txid)
}

pub async fn confirm_bmm(
Expand Down
2 changes: 1 addition & 1 deletion lib/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Net {
let (server, _) = make_server_endpoint(bind_addr)?;
let client = make_client_endpoint("0.0.0.0:0".parse()?)?;
let active_peers = Arc::new(RwLock::new(HashMap::new()));
let known_peers = env.create_database(Some("utxos"))?;
let known_peers = env.create_database(Some("known_peers"))?;
let (peer_info_tx, peer_info_rx) = mpsc::unbounded();
let net = Net {
server,
Expand Down
Loading

0 comments on commit efad71b

Please sign in to comment.