mod.rs 3.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

Denis_P's avatar
Denis_P committed
14
//! A `RocksDB` instance for storing parachain data; availability data, and approvals.
15

Squirrel's avatar
Squirrel committed
16
17
use kvdb::KeyValueDB;
use std::{io, path::PathBuf, sync::Arc};
18

19
mod upgrade;
20

Andronik Ordian's avatar
Andronik Ordian committed
21
22
23
24
25
pub(crate) mod columns {
	pub mod v0 {
		pub const NUM_COLUMNS: u32 = 3;
	}
	pub const NUM_COLUMNS: u32 = 5;
26
27
28
29

	pub const COL_AVAILABILITY_DATA: u32 = 0;
	pub const COL_AVAILABILITY_META: u32 = 1;
	pub const COL_APPROVAL_DATA: u32 = 2;
Andronik Ordian's avatar
Andronik Ordian committed
30
31
	pub const COL_CHAIN_SELECTION_DATA: u32 = 3;
	pub const COL_DISPUTE_COORDINATOR_DATA: u32 = 4;
32
33
34
35
36
37
38
39
40
41
42
}

/// Columns used by different subsystems.
#[derive(Debug, Clone)]
pub struct ColumnsConfig {
	/// The column used by the av-store for data.
	pub col_availability_data: u32,
	/// The column used by the av-store for meta information.
	pub col_availability_meta: u32,
	/// The column used by approval voting for data.
	pub col_approval_data: u32,
Andronik Ordian's avatar
Andronik Ordian committed
43
44
45
46
	/// The column used by chain selection for data.
	pub col_chain_selection_data: u32,
	/// The column used by dispute coordinator for data.
	pub col_dispute_coordinator_data: u32,
47
48
49
50
51
52
53
}

/// The real columns used by the parachains DB.
pub const REAL_COLUMNS: ColumnsConfig = ColumnsConfig {
	col_availability_data: columns::COL_AVAILABILITY_DATA,
	col_availability_meta: columns::COL_AVAILABILITY_META,
	col_approval_data: columns::COL_APPROVAL_DATA,
Andronik Ordian's avatar
Andronik Ordian committed
54
55
	col_chain_selection_data: columns::COL_CHAIN_SELECTION_DATA,
	col_dispute_coordinator_data: columns::COL_DISPUTE_COORDINATOR_DATA,
56
57
};

58
/// The cache size for each column, in megabytes.
59
60
61
62
63
64
65
66
67
68
69
70
#[derive(Debug, Clone)]
pub struct CacheSizes {
	/// Cache used by availability data.
	pub availability_data: usize,
	/// Cache used by availability meta.
	pub availability_meta: usize,
	/// Cache used by approval data.
	pub approval_data: usize,
}

impl Default for CacheSizes {
	fn default() -> Self {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
71
		CacheSizes { availability_data: 25, availability_meta: 1, approval_data: 5 }
72
73
74
	}
}

Andronik Ordian's avatar
Andronik Ordian committed
75
pub(crate) fn other_io_error(err: String) -> io::Error {
76
77
78
	io::Error::new(io::ErrorKind::Other, err)
}

79
/// Open the database on disk, creating it if it doesn't exist.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
80
81
pub fn open_creating(root: PathBuf, cache_sizes: CacheSizes) -> io::Result<Arc<dyn KeyValueDB>> {
	use kvdb_rocksdb::{Database, DatabaseConfig};
82
83
84
85
86

	let path = root.join("parachains").join("db");

	let mut db_config = DatabaseConfig::with_columns(columns::NUM_COLUMNS);

Shawn Tabrizi's avatar
Shawn Tabrizi committed
87
88
	let _ = db_config
		.memory_budget
89
		.insert(columns::COL_AVAILABILITY_DATA, cache_sizes.availability_data);
Shawn Tabrizi's avatar
Shawn Tabrizi committed
90
91
	let _ = db_config
		.memory_budget
92
		.insert(columns::COL_AVAILABILITY_META, cache_sizes.availability_meta);
Shawn Tabrizi's avatar
Shawn Tabrizi committed
93
94
	let _ = db_config
		.memory_budget
95
96
		.insert(columns::COL_APPROVAL_DATA, cache_sizes.approval_data);

Shawn Tabrizi's avatar
Shawn Tabrizi committed
97
98
99
	let path_str = path
		.to_str()
		.ok_or_else(|| other_io_error(format!("Bad database path: {:?}", path)))?;
100

101
102
103
	std::fs::create_dir_all(&path_str)?;
	upgrade::try_upgrade_db(&path)?;
	let db = Database::open(&db_config, &path_str)?;
104
105
106

	Ok(Arc::new(db))
}