Skip to content

Commit efad71b

Browse files
committed
Fix networking, serialization, reorg issues, tag for release
1 parent cf4f21f commit efad71b

File tree

14 files changed

+310
-170
lines changed

14 files changed

+310
-170
lines changed

Cargo.lock

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ version = "0.8.0"
1919
git = "https://github.com/Ash-L2L/bip300301.git"
2020
rev = "d1da609b4c77d2b53bea2c8e922891b83612a03b"
2121

22+
[workspace.dependencies.rustreexo]
23+
git = "https://github.com/Ash-L2L/rustreexo.git"
24+
rev = "a3ac7d3ebe9749ebd0bb34c709e7616f83d573b3"
2225

2326
[profile.release]
2427
# lto = "fat"

app/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ eframe = "0.27.1"
2323
futures = "0.3.30"
2424
human-size = "0.4.3"
2525
jsonrpsee = { version = "0.20.0", features = ["server"] }
26-
rustreexo = { version = "0.1.0" }
26+
rustreexo = { workspace = true }
2727
parking_lot = "0.12.1"
2828
serde = { version = "1.0.179", features = ["derive"] }
2929
shlex = "1.3.0"

app/app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ impl App {
153153
.await?;
154154
let roots = {
155155
let mut accumulator = self.node.get_accumulator()?;
156-
body.modify_pollard(&mut accumulator)
156+
body.modify_pollard(&mut accumulator.0)
157157
.map_err(Error::Utreexo)?;
158158
accumulator
159+
.0
159160
.get_roots()
160161
.iter()
161162
.map(|root| root.get_data())

lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ quinn = "0.10.1"
2424
rayon = "1.7.0"
2525
rcgen = "0.11.1"
2626
rustls = { version = "0.21.5", features = ["dangerous_configuration"] }
27-
rustreexo = { version = "0.1.0", features = ["with-serde"] }
27+
rustreexo = { workspace = true, features = ["with-serde"] }
2828
serde = { version = "1.0.179", features = ["derive"] }
2929
serde_json = "1.0.113"
3030
sha256 = "1.2.2"

lib/archive.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::cmp::Ordering;
22

33
use fallible_iterator::FallibleIterator;
4-
use heed::{
5-
types::{OwnedType, SerdeBincode},
6-
Database, RoTxn, RwTxn,
7-
};
4+
use heed::{types::SerdeBincode, Database, RoTxn, RwTxn};
85

9-
use crate::types::{BlockHash, Body, Header};
6+
use crate::types::{Accumulator, BlockHash, Body, Header};
107

118
#[derive(Debug, thiserror::Error)]
129
pub enum Error {
@@ -16,6 +13,8 @@ pub enum Error {
1613
InvalidPrevSideHash,
1714
#[error("invalid merkle root")]
1815
InvalidMerkleRoot,
16+
#[error("no accumulator for block {0}")]
17+
NoAccumulator(BlockHash),
1918
#[error("no block with hash {0}")]
2019
NoBlock(BlockHash),
2120
#[error("no header with hash {0}")]
@@ -26,25 +25,50 @@ pub enum Error {
2625

2726
#[derive(Clone)]
2827
pub struct Archive {
28+
accumulators: Database<SerdeBincode<BlockHash>, SerdeBincode<Accumulator>>,
2929
headers: Database<SerdeBincode<BlockHash>, SerdeBincode<Header>>,
3030
bodies: Database<SerdeBincode<BlockHash>, SerdeBincode<Body>>,
31-
hash_to_height: Database<SerdeBincode<BlockHash>, OwnedType<u32>>,
31+
hash_to_height: Database<SerdeBincode<BlockHash>, SerdeBincode<u32>>,
3232
}
3333

3434
impl Archive {
35-
pub const NUM_DBS: u32 = 3;
35+
pub const NUM_DBS: u32 = 4;
3636

3737
pub fn new(env: &heed::Env) -> Result<Self, Error> {
38+
let accumulators = env.create_database(Some("accumulators"))?;
3839
let headers = env.create_database(Some("headers"))?;
3940
let bodies = env.create_database(Some("bodies"))?;
4041
let hash_to_height = env.create_database(Some("hash_to_height"))?;
4142
Ok(Self {
43+
accumulators,
4244
headers,
4345
bodies,
4446
hash_to_height,
4547
})
4648
}
4749

50+
pub fn try_get_accumulator(
51+
&self,
52+
rotxn: &RoTxn,
53+
block_hash: BlockHash,
54+
) -> Result<Option<Accumulator>, Error> {
55+
if block_hash == BlockHash::default() {
56+
Ok(Some(Accumulator::default()))
57+
} else {
58+
let accumulator = self.accumulators.get(rotxn, &block_hash)?;
59+
Ok(accumulator)
60+
}
61+
}
62+
63+
pub fn get_accumulator(
64+
&self,
65+
rotxn: &RoTxn,
66+
block_hash: BlockHash,
67+
) -> Result<Accumulator, Error> {
68+
self.try_get_accumulator(rotxn, block_hash)?
69+
.ok_or(Error::NoAccumulator(block_hash))
70+
}
71+
4872
pub fn try_get_header(
4973
&self,
5074
rotxn: &RoTxn,
@@ -104,6 +128,17 @@ impl Archive {
104128
.ok_or(Error::NoHeight(block_hash))
105129
}
106130

131+
/// Store a block body. The header must already exist.
132+
pub fn put_accumulator(
133+
&self,
134+
rwtxn: &mut RwTxn,
135+
block_hash: BlockHash,
136+
accumulator: &Accumulator,
137+
) -> Result<(), Error> {
138+
self.accumulators.put(rwtxn, &block_hash, accumulator)?;
139+
Ok(())
140+
}
141+
107142
/// Store a block body. The header must already exist.
108143
pub fn put_body(
109144
&self,
@@ -159,30 +194,30 @@ impl Archive {
159194
) -> Result<BlockHash, Error> {
160195
let mut height0 = self.get_height(rotxn, block_hash0)?;
161196
let mut height1 = self.get_height(rotxn, block_hash1)?;
162-
let mut header0 = self.get_header(rotxn, block_hash0)?;
163-
let mut header1 = self.get_header(rotxn, block_hash1)?;
197+
let mut header0 = self.try_get_header(rotxn, block_hash0)?;
198+
let mut header1 = self.try_get_header(rotxn, block_hash1)?;
164199
// Find respective ancestors of block_hash0 and block_hash1 with height
165200
// equal to min(height0, height1)
166201
loop {
167202
match height0.cmp(&height1) {
168203
Ordering::Less => {
169-
block_hash1 = header1.prev_side_hash;
170-
header1 = self.get_header(rotxn, block_hash1)?;
204+
block_hash1 = header1.unwrap().prev_side_hash;
205+
header1 = self.try_get_header(rotxn, block_hash1)?;
171206
height1 -= 1;
172207
}
173208
Ordering::Greater => {
174-
block_hash0 = header0.prev_side_hash;
175-
header0 = self.get_header(rotxn, block_hash0)?;
209+
block_hash0 = header0.unwrap().prev_side_hash;
210+
header0 = self.try_get_header(rotxn, block_hash0)?;
176211
height0 -= 1;
177212
}
178213
Ordering::Equal => {
179214
if block_hash0 == block_hash1 {
180215
return Ok(block_hash0);
181216
} else {
182-
block_hash0 = header0.prev_side_hash;
183-
block_hash1 = header1.prev_side_hash;
184-
header0 = self.get_header(rotxn, block_hash0)?;
185-
header1 = self.get_header(rotxn, block_hash1)?;
217+
block_hash0 = header0.unwrap().prev_side_hash;
218+
block_hash1 = header1.unwrap().prev_side_hash;
219+
header0 = self.try_get_header(rotxn, block_hash0)?;
220+
header1 = self.try_get_header(rotxn, block_hash1)?;
186221
height0 -= 1;
187222
height1 -= 1;
188223
}

lib/mempool.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::collections::VecDeque;
22

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

6-
use crate::types::{AuthorizedTransaction, OutPoint, Txid};
5+
use crate::types::{Accumulator, AuthorizedTransaction, OutPoint, Txid};
76

87
#[derive(Debug, thiserror::Error)]
98
pub enum Error {
@@ -104,7 +103,7 @@ impl MemPool {
104103
pub fn regenerate_proofs(
105104
&self,
106105
rwtxn: &mut RwTxn,
107-
accumulator: &Pollard,
106+
accumulator: &Accumulator,
108107
) -> Result<(), Error> {
109108
let mut iter = self.transactions.iter_mut(rwtxn)?;
110109
while let Some(tx) = iter.next() {
@@ -115,9 +114,8 @@ impl MemPool {
115114
.iter()
116115
.map(|(_, utxo_hash)| utxo_hash.into())
117116
.collect();
118-
let (proof, _) =
119-
accumulator.prove(&targets).map_err(Error::Utreexo)?;
120-
tx.transaction.proof = proof;
117+
tx.transaction.proof =
118+
accumulator.0.prove(&targets).map_err(Error::Utreexo)?;
121119
unsafe { iter.put_current(&txid, &tx) }?;
122120
}
123121
Ok(())

lib/miner.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Miner {
5555
height: u32,
5656
header: Header,
5757
body: Body,
58-
) -> Result<(), Error> {
58+
) -> Result<bitcoin::Txid, Error> {
5959
let str_hash_prev = header.prev_main_hash.to_string();
6060
let critical_hash: [u8; 32] = header.hash().into();
6161
let critical_hash = bitcoin::BlockHash::from_byte_array(critical_hash);
@@ -77,11 +77,12 @@ impl Miner {
7777
.as_str()
7878
.map(|s| s.to_owned())
7979
.ok_or(Error::InvalidJson { json: value })?;
80-
let _ =
80+
let txid =
8181
bitcoin::Txid::from_str(&txid).map_err(bip300301::Error::from)?;
82+
tracing::info!("created BMM tx: {txid}");
8283
assert_eq!(header.merkle_root, body.compute_merkle_root());
8384
self.block = Some((header, body));
84-
Ok(())
85+
Ok(txid)
8586
}
8687

8788
pub async fn confirm_bmm(

lib/net/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Net {
109109
let (server, _) = make_server_endpoint(bind_addr)?;
110110
let client = make_client_endpoint("0.0.0.0:0".parse()?)?;
111111
let active_peers = Arc::new(RwLock::new(HashMap::new()));
112-
let known_peers = env.create_database(Some("utxos"))?;
112+
let known_peers = env.create_database(Some("known_peers"))?;
113113
let (peer_info_tx, peer_info_rx) = mpsc::unbounded();
114114
let net = Net {
115115
server,

0 commit comments

Comments
 (0)