Skip to content

Commit 8bfc0a5

Browse files
committed
Explicitly version storage
1 parent 8e06b01 commit 8bfc0a5

File tree

5 files changed

+84
-12
lines changed

5 files changed

+84
-12
lines changed

lib/archive.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use fallible_iterator::{FallibleIterator, IteratorExt};
88
use heed::{types::SerdeBincode, RoTxn};
99
use sneed::{
1010
db::error::Error as DbError, rwtxn::Error as RwTxnError, DatabaseUnique,
11-
EnvError, RwTxn,
11+
EnvError, RwTxn, UnitKey,
1212
};
1313

1414
use crate::types::{
1515
proto::mainchain::{self, Deposit},
16-
Accumulator, BlockHash, BmmResult, Body, Header, Tip,
16+
Accumulator, BlockHash, BmmResult, Body, Header, Tip, Version, VERSION,
1717
};
1818

1919
#[derive(Debug, thiserror::Error)]
@@ -136,10 +136,11 @@ pub struct Archive {
136136
SerdeBincode<bitcoin::BlockHash>,
137137
SerdeBincode<bitcoin::Work>,
138138
>,
139+
_version: DatabaseUnique<UnitKey, SerdeBincode<Version>>,
139140
}
140141

141142
impl Archive {
142-
pub const NUM_DBS: u32 = 14;
143+
pub const NUM_DBS: u32 = 15;
143144

144145
pub fn new(env: &sneed::Env) -> Result<Self, Error> {
145146
let mut rwtxn = env.write_txn().map_err(EnvError::from)?;
@@ -205,6 +206,18 @@ impl Archive {
205206
}
206207
let total_work = DatabaseUnique::create(env, &mut rwtxn, "total_work")
207208
.map_err(EnvError::from)?;
209+
let version =
210+
DatabaseUnique::create(env, &mut rwtxn, "archive_version")
211+
.map_err(EnvError::from)?;
212+
if version
213+
.try_get(&rwtxn, &())
214+
.map_err(DbError::from)?
215+
.is_none()
216+
{
217+
version
218+
.put(&mut rwtxn, &(), &*VERSION)
219+
.map_err(DbError::from)?;
220+
}
208221
rwtxn.commit().map_err(RwTxnError::from)?;
209222
Ok(Self {
210223
accumulators,
@@ -221,6 +234,7 @@ impl Archive {
221234
main_successors,
222235
successors,
223236
total_work,
237+
_version: version,
224238
})
225239
}
226240

lib/mempool.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ use fallible_iterator::FallibleIterator as _;
44
use heed::{types::SerdeBincode, RoTxn};
55
use sneed::{
66
db::error::Error as DbError, DatabaseUnique, EnvError, RwTxn, RwTxnError,
7+
UnitKey,
78
};
89

910
use crate::types::{
10-
Accumulator, AuthorizedTransaction, OutPoint, Txid, UtreexoError,
11+
Accumulator, AuthorizedTransaction, OutPoint, Txid, UtreexoError, Version,
12+
VERSION,
1113
};
1214

1315
#[derive(Debug, thiserror::Error)]
@@ -29,10 +31,11 @@ pub struct MemPool {
2931
pub transactions:
3032
DatabaseUnique<SerdeBincode<Txid>, SerdeBincode<AuthorizedTransaction>>,
3133
pub spent_utxos: DatabaseUnique<SerdeBincode<OutPoint>, SerdeBincode<Txid>>,
34+
_version: DatabaseUnique<UnitKey, SerdeBincode<Version>>,
3235
}
3336

3437
impl MemPool {
35-
pub const NUM_DBS: u32 = 2;
38+
pub const NUM_DBS: u32 = 3;
3639

3740
pub fn new(env: &sneed::Env) -> Result<Self, Error> {
3841
let mut rwtxn = env.write_txn().map_err(EnvError::from)?;
@@ -42,10 +45,23 @@ impl MemPool {
4245
let spent_utxos =
4346
DatabaseUnique::create(env, &mut rwtxn, "spent_utxos")
4447
.map_err(EnvError::from)?;
48+
let version =
49+
DatabaseUnique::create(env, &mut rwtxn, "mempool_version")
50+
.map_err(EnvError::from)?;
51+
if version
52+
.try_get(&rwtxn, &())
53+
.map_err(DbError::from)?
54+
.is_none()
55+
{
56+
version
57+
.put(&mut rwtxn, &(), &*VERSION)
58+
.map_err(DbError::from)?;
59+
}
4560
rwtxn.commit().map_err(RwTxnError::from)?;
4661
Ok(Self {
4762
transactions,
4863
spent_utxos,
64+
_version: version,
4965
})
5066
}
5167

lib/net/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ use heed::types::{SerdeBincode, Unit};
1010
use parking_lot::RwLock;
1111
use quinn::{ClientConfig, Endpoint, ServerConfig};
1212
use sneed::{
13-
db::error::Error as DbError, DatabaseUnique, EnvError, RwTxnError,
13+
db::error::Error as DbError, DatabaseUnique, EnvError, RwTxnError, UnitKey,
1414
};
1515
use tokio_stream::StreamNotifyClose;
1616
use tracing::instrument;
1717

1818
use crate::{
1919
archive::Archive,
2020
state::State,
21-
types::{AuthorizedTransaction, THIS_SIDECHAIN},
21+
types::{AuthorizedTransaction, Version, THIS_SIDECHAIN, VERSION},
2222
};
2323

2424
mod peer;
@@ -215,10 +215,11 @@ pub struct Net {
215215
peer_info_tx:
216216
mpsc::UnboundedSender<(SocketAddr, Option<PeerConnectionInfo>)>,
217217
known_peers: DatabaseUnique<SerdeBincode<SocketAddr>, Unit>,
218+
_version: DatabaseUnique<UnitKey, SerdeBincode<Version>>,
218219
}
219220

220221
impl Net {
221-
pub const NUM_DBS: u32 = 1;
222+
pub const NUM_DBS: u32 = 2;
222223

223224
fn add_active_peer(
224225
&self,
@@ -339,6 +340,17 @@ impl Net {
339340
known_peers
340341
}
341342
};
343+
let version = DatabaseUnique::create(env, &mut rwtxn, "net_version")
344+
.map_err(EnvError::from)?;
345+
if version
346+
.try_get(&rwtxn, &())
347+
.map_err(DbError::from)?
348+
.is_none()
349+
{
350+
version
351+
.put(&mut rwtxn, &(), &*VERSION)
352+
.map_err(DbError::from)?;
353+
}
342354
rwtxn.commit().map_err(RwTxnError::from)?;
343355
let (peer_info_tx, peer_info_rx) = mpsc::unbounded();
344356
let net = Net {
@@ -348,6 +360,7 @@ impl Net {
348360
active_peers,
349361
peer_info_tx,
350362
known_peers,
363+
_version: version,
351364
};
352365
#[allow(clippy::let_and_return)]
353366
let known_peers: Vec<_> = {

lib/state/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::{
1919
AmountOverflowError, AmountUnderflowError, AuthorizedTransaction,
2020
BlockHash, Body, FilledTransaction, GetAddress, GetValue, Header,
2121
InPoint, M6id, OutPoint, Output, PointedOutput, SpentOutput,
22-
Transaction, Verify, WithdrawalBundle, WithdrawalBundleStatus,
22+
Transaction, Verify, Version, WithdrawalBundle, WithdrawalBundleStatus,
23+
VERSION,
2324
},
2425
util::Watchable,
2526
};
@@ -90,10 +91,11 @@ pub struct State {
9091
SerdeBincode<(bitcoin::BlockHash, u32)>,
9192
>,
9293
pub utreexo_accumulator: DatabaseUnique<UnitKey, SerdeBincode<Accumulator>>,
94+
_version: DatabaseUnique<UnitKey, SerdeBincode<Version>>,
9395
}
9496

9597
impl State {
96-
pub const NUM_DBS: u32 = 10;
98+
pub const NUM_DBS: u32 = 11;
9799

98100
pub fn new(env: &sneed::Env) -> Result<Self, Error> {
99101
let mut rwtxn = env.write_txn().map_err(EnvError::from)?;
@@ -132,6 +134,17 @@ impl State {
132134
let utreexo_accumulator =
133135
DatabaseUnique::create(env, &mut rwtxn, "utreexo_accumulator")
134136
.map_err(EnvError::from)?;
137+
let version = DatabaseUnique::create(env, &mut rwtxn, "state_version")
138+
.map_err(EnvError::from)?;
139+
if version
140+
.try_get(&rwtxn, &())
141+
.map_err(DbError::from)?
142+
.is_none()
143+
{
144+
version
145+
.put(&mut rwtxn, &(), &*VERSION)
146+
.map_err(DbError::from)?;
147+
}
135148
rwtxn.commit().map_err(RwTxnError::from)?;
136149
Ok(Self {
137150
tip,
@@ -144,6 +157,7 @@ impl State {
144157
deposit_blocks,
145158
withdrawal_bundle_event_blocks,
146159
utreexo_accumulator,
160+
_version: version,
147161
})
148162
}
149163

lib/wallet.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustreexo::accumulator::node_hash::BitcoinNodeHash;
1616
use serde::{Deserialize, Serialize};
1717
use sneed::{
1818
db::error::Error as DbError, DatabaseUnique, Env, EnvError, RwTxnError,
19+
UnitKey,
1920
};
2021
use tokio_stream::{wrappers::WatchStream, StreamMap};
2122

@@ -29,7 +30,7 @@ pub use crate::{
2930
use crate::{
3031
types::{
3132
hash, Accumulator, AmountOverflowError, AmountUnderflowError,
32-
PointedOutput, UtreexoError,
33+
PointedOutput, UtreexoError, Version, VERSION,
3334
},
3435
util::Watchable,
3536
};
@@ -100,10 +101,11 @@ pub struct Wallet {
100101
DatabaseUnique<SerdeBincode<[u8; 4]>, SerdeBincode<Address>>,
101102
utxos: DatabaseUnique<SerdeBincode<OutPoint>, SerdeBincode<Output>>,
102103
stxos: DatabaseUnique<SerdeBincode<OutPoint>, SerdeBincode<SpentOutput>>,
104+
_version: DatabaseUnique<UnitKey, SerdeBincode<Version>>,
103105
}
104106

105107
impl Wallet {
106-
pub const NUM_DBS: u32 = 5;
108+
pub const NUM_DBS: u32 = 6;
107109

108110
pub fn new(path: &Path) -> Result<Self, Error> {
109111
std::fs::create_dir_all(path)?;
@@ -128,6 +130,17 @@ impl Wallet {
128130
.map_err(EnvError::from)?;
129131
let stxos = DatabaseUnique::create(&env, &mut rwtxn, "stxos")
130132
.map_err(EnvError::from)?;
133+
let version = DatabaseUnique::create(&env, &mut rwtxn, "version")
134+
.map_err(EnvError::from)?;
135+
if version
136+
.try_get(&rwtxn, &())
137+
.map_err(DbError::from)?
138+
.is_none()
139+
{
140+
version
141+
.put(&mut rwtxn, &(), &*VERSION)
142+
.map_err(DbError::from)?;
143+
}
131144
rwtxn.commit().map_err(RwTxnError::from)?;
132145
Ok(Self {
133146
env,
@@ -136,6 +149,7 @@ impl Wallet {
136149
index_to_address,
137150
utxos,
138151
stxos,
152+
_version: version,
139153
})
140154
}
141155

@@ -518,6 +532,7 @@ impl Watchable<()> for Wallet {
518532
index_to_address,
519533
utxos,
520534
stxos,
535+
_version: _,
521536
} = self;
522537
let watchables = [
523538
seed.watch().clone(),

0 commit comments

Comments
 (0)