Skip to content

Commit 51b56b0

Browse files
authored
cuprated/database: fix error mappings + msg (#419)
* add * dbi * err * log and panic
1 parent 550d859 commit 51b56b0

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

binaries/cuprated/src/main.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ use std::{mem, sync::Arc};
2020

2121
use tokio::sync::mpsc;
2222
use tower::{Service, ServiceExt};
23-
use tracing::{info, level_filters::LevelFilter};
23+
use tracing::{error, info, level_filters::LevelFilter};
2424
use tracing_subscriber::{layer::SubscriberExt, reload::Handle, util::SubscriberInitExt, Registry};
2525

2626
use cuprate_consensus_context::{
2727
BlockChainContextRequest, BlockChainContextResponse, BlockchainContextService,
2828
};
29+
use cuprate_database::{InitError, DATABASE_CORRUPT_MSG};
2930
use cuprate_helper::time::secs_to_hms;
3031
use cuprate_types::blockchain::BlockchainWriteRequest;
3132

@@ -77,9 +78,13 @@ fn main() {
7778
config.blockchain_config(),
7879
Arc::clone(&db_thread_pool),
7980
)
80-
.unwrap();
81+
.inspect_err(|e| error!("Blockchain database error: {e}"))
82+
.expect(DATABASE_CORRUPT_MSG);
83+
8184
let (txpool_read_handle, txpool_write_handle, _) =
82-
cuprate_txpool::service::init_with_pool(config.txpool_config(), db_thread_pool).unwrap();
85+
cuprate_txpool::service::init_with_pool(config.txpool_config(), db_thread_pool)
86+
.inspect_err(|e| error!("Txpool database error: {e}"))
87+
.expect(DATABASE_CORRUPT_MSG);
8388

8489
// Initialize async tasks.
8590

storage/database/src/backend/heed/error.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl From<heed::Error> for crate::InitError {
2626
//
2727
// "Requested page not found - this usually indicates corruption."
2828
// <https://docs.rs/heed/latest/heed/enum.MdbError.html#variant.PageNotFound>
29-
E2::Corrupted | E2::PageNotFound => Self::Corrupt,
29+
E2::BadDbi | E2::Corrupted | E2::PageNotFound => Self::Corrupt,
3030

3131
// These errors shouldn't be returned on database init.
3232
E2::Incompatible
@@ -45,7 +45,6 @@ impl From<heed::Error> for crate::InitError {
4545
| E2::MapResized
4646
| E2::BadRslot
4747
| E2::BadValSize
48-
| E2::BadDbi
4948
| E2::Panic => Self::Unknown(Box::new(mdb_error)),
5049
},
5150

@@ -85,7 +84,7 @@ impl From<heed::Error> for crate::RuntimeError {
8584
//
8685
// "Requested page not found - this usually indicates corruption."
8786
// <https://docs.rs/heed/latest/heed/enum.MdbError.html#variant.PageNotFound>
88-
E2::Corrupted | E2::PageNotFound => panic!("{mdb_error:#?}\n{DATABASE_CORRUPT_MSG}"),
87+
E2::BadDbi | E2::Corrupted | E2::PageNotFound => panic!("{mdb_error:#?}\n{DATABASE_CORRUPT_MSG}"),
8988

9089
// These errors should not occur, and if they do,
9190
// the best thing `cuprate_database` can do for
@@ -99,8 +98,7 @@ impl From<heed::Error> for crate::RuntimeError {
9998
| E2::TlsFull
10099
| E2::TxnFull
101100
| E2::BadRslot
102-
| E2::VersionMismatch
103-
| E2::BadDbi => panic!("{mdb_error:#?}"),
101+
| E2::VersionMismatch => panic!("{mdb_error:#?}"),
104102

105103
// These errors are the same as above, but instead
106104
// of being errors we can't control, these are errors

storage/database/src/constants.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ use cfg_if::cfg_if;
1010
/// messages if we think the database is corrupted.
1111
///
1212
/// This is meant to be user-friendly.
13-
pub const DATABASE_CORRUPT_MSG: &str = r"Cuprate has encountered a fatal error. The database may be corrupted.
13+
pub const DATABASE_CORRUPT_MSG: &str = r"`cuprated` has encountered a fatal error. The database may be corrupted.
1414
15-
TODO: instructions on:
16-
1. What to do
17-
2. How to fix (re-sync, recover, etc)
18-
3. General advice for preventing corruption
19-
4. etc";
15+
If `cuprated` continues to crash with the current database,
16+
you may have to delete the database file and re-sync from scratch.
17+
18+
See <https://user.cuprate.org/resources/disk.html>
19+
for more information on where database files are.
20+
21+
If this happens frequently, consider using the `Safe` sync mode.";
2022

2123
//---------------------------------------------------------------------------------------------------- Misc
2224
/// Static string of the `crate` being used as the database backend.

storage/database/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub type DbResult<T> = Result<T, RuntimeError>;
2424
pub enum InitError {
2525
/// The given `Path/File` existed and was accessible,
2626
/// but was not a valid database file.
27+
///
28+
/// This error can sometimes be returned after an
29+
/// initial corruption error over [`InitError::Corrupt`].
2730
#[error("database file exists but is not valid")]
2831
Invalid,
2932

0 commit comments

Comments
 (0)