feat: initialize Kurdistan SDK - independent fork of Polkadot SDK

This commit is contained in:
2025-12-13 15:44:15 +03:00
commit e4778b4576
6838 changed files with 1847450 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "sp-database"
version = "10.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage.workspace = true
repository.workspace = true
description = "Substrate database trait."
documentation = "https://docs.rs/sp-database"
readme = "README.md"
[lints]
workspace = true
[dependencies]
kvdb = { workspace = true }
parking_lot = { workspace = true, default-features = true }
+3
View File
@@ -0,0 +1,3 @@
The main database trait, allowing Substrate to store data persistently.
License: Apache-2.0
@@ -0,0 +1,35 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// The error type for database operations.
#[derive(Debug)]
pub struct DatabaseError(pub Box<dyn std::error::Error + Send + Sync + 'static>);
impl std::fmt::Display for DatabaseError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for DatabaseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
/// A specialized `Result` type for database operations.
pub type Result<T> = std::result::Result<T, DatabaseError>;
+118
View File
@@ -0,0 +1,118 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// A wrapper around `kvdb::Database` that implements `sp_database::Database` trait
use ::kvdb::{DBTransaction, KeyValueDB};
use crate::{error, Change, ColumnId, Database, Transaction};
struct DbAdapter<D: KeyValueDB + 'static>(D);
fn handle_err<T>(result: std::io::Result<T>) -> T {
match result {
Ok(r) => r,
Err(e) => {
panic!("Critical database error: {:?}", e);
},
}
}
/// Wrap RocksDb database into a trait object that implements `sp_database::Database`
pub fn as_database<D, H>(db: D) -> std::sync::Arc<dyn Database<H>>
where
D: KeyValueDB + 'static,
H: Clone + AsRef<[u8]>,
{
std::sync::Arc::new(DbAdapter(db))
}
impl<D: KeyValueDB> DbAdapter<D> {
// Returns counter key and counter value if it exists.
fn read_counter(&self, col: ColumnId, key: &[u8]) -> error::Result<(Vec<u8>, Option<u32>)> {
// Add a key suffix for the counter
let mut counter_key = key.to_vec();
counter_key.push(0);
Ok(match self.0.get(col, &counter_key).map_err(|e| error::DatabaseError(Box::new(e)))? {
Some(data) => {
let mut counter_data = [0; 4];
if data.len() != 4 {
return Err(error::DatabaseError(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Unexpected counter len {}", data.len()),
))));
}
counter_data.copy_from_slice(&data);
let counter = u32::from_le_bytes(counter_data);
(counter_key, Some(counter))
},
None => (counter_key, None),
})
}
}
impl<D: KeyValueDB, H: Clone + AsRef<[u8]>> Database<H> for DbAdapter<D> {
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
let mut tx = DBTransaction::new();
for change in transaction.0.into_iter() {
match change {
Change::Set(col, key, value) => tx.put_vec(col, &key, value),
Change::Remove(col, key) => tx.delete(col, &key),
Change::Store(col, key, value) => match self.read_counter(col, key.as_ref())? {
(counter_key, Some(mut counter)) => {
counter += 1;
tx.put(col, &counter_key, &counter.to_le_bytes());
},
(counter_key, None) => {
let d = 1u32.to_le_bytes();
tx.put(col, &counter_key, &d);
tx.put_vec(col, key.as_ref(), value);
},
},
Change::Reference(col, key) => {
if let (counter_key, Some(mut counter)) =
self.read_counter(col, key.as_ref())?
{
counter += 1;
tx.put(col, &counter_key, &counter.to_le_bytes());
}
},
Change::Release(col, key) => {
if let (counter_key, Some(mut counter)) =
self.read_counter(col, key.as_ref())?
{
counter -= 1;
if counter == 0 {
tx.delete(col, &counter_key);
tx.delete(col, key.as_ref());
} else {
tx.put(col, &counter_key, &counter.to_le_bytes());
}
}
},
}
}
self.0.write(tx).map_err(|e| error::DatabaseError(Box::new(e)))
}
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
handle_err(self.0.get(col, key))
}
fn contains(&self, col: ColumnId, key: &[u8]) -> bool {
handle_err(self.0.has_key(col, key))
}
}
+144
View File
@@ -0,0 +1,144 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! The main database trait, allowing Substrate to store data persistently.
pub mod error;
mod kvdb;
mod mem;
pub use crate::kvdb::as_database;
pub use mem::MemDb;
/// An identifier for a column.
pub type ColumnId = u32;
/// An alteration to the database.
#[derive(Clone)]
pub enum Change<H> {
Set(ColumnId, Vec<u8>, Vec<u8>),
Remove(ColumnId, Vec<u8>),
Store(ColumnId, H, Vec<u8>),
Reference(ColumnId, H),
Release(ColumnId, H),
}
/// A series of changes to the database that can be committed atomically. They do not take effect
/// until passed into `Database::commit`.
#[derive(Default, Clone)]
pub struct Transaction<H>(pub Vec<Change<H>>);
impl<H> Transaction<H> {
/// Create a new transaction to be prepared and committed atomically.
pub fn new() -> Self {
Transaction(Vec::new())
}
/// Set the value of `key` in `col` to `value`, replacing anything that is there currently.
pub fn set(&mut self, col: ColumnId, key: &[u8], value: &[u8]) {
self.0.push(Change::Set(col, key.to_vec(), value.to_vec()))
}
/// Set the value of `key` in `col` to `value`, replacing anything that is there currently.
pub fn set_from_vec(&mut self, col: ColumnId, key: &[u8], value: Vec<u8>) {
self.0.push(Change::Set(col, key.to_vec(), value))
}
/// Remove the value of `key` in `col`.
pub fn remove(&mut self, col: ColumnId, key: &[u8]) {
self.0.push(Change::Remove(col, key.to_vec()))
}
/// Store the `preimage` of `hash` into the database, so that it may be looked up later with
/// `Database::get`. This may be called multiple times, but subsequent
/// calls will ignore `preimage` and simply increase the number of references on `hash`.
pub fn store(&mut self, col: ColumnId, hash: H, preimage: Vec<u8>) {
self.0.push(Change::Store(col, hash, preimage))
}
/// Increase the number of references for `hash` in the database.
pub fn reference(&mut self, col: ColumnId, hash: H) {
self.0.push(Change::Reference(col, hash))
}
/// Release the preimage of `hash` from the database. An equal number of these to the number of
/// corresponding `store`s must have been given before it is legal for `Database::get` to
/// be unable to provide the preimage.
pub fn release(&mut self, col: ColumnId, hash: H) {
self.0.push(Change::Release(col, hash))
}
}
pub trait Database<H: Clone + AsRef<[u8]>>: Send + Sync {
/// Commit the `transaction` to the database atomically. Any further calls to `get` or `lookup`
/// will reflect the new state.
fn commit(&self, transaction: Transaction<H>) -> error::Result<()>;
/// Retrieve the value previously stored against `key` or `None` if
/// `key` is not currently in the database.
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>>;
/// Check if the value exists in the database without retrieving it.
fn contains(&self, col: ColumnId, key: &[u8]) -> bool {
self.get(col, key).is_some()
}
/// Check value size in the database possibly without retrieving it.
fn value_size(&self, col: ColumnId, key: &[u8]) -> Option<usize> {
self.get(col, key).map(|v| v.len())
}
/// Call `f` with the value previously stored against `key`.
///
/// This may be faster than `get` since it doesn't allocate.
/// Use `with_get` helper function if you need `f` to return a value from `f`
fn with_get(&self, col: ColumnId, key: &[u8], f: &mut dyn FnMut(&[u8])) {
if let Some(v) = self.get(col, key) {
f(&v)
}
}
/// Check if database supports internal ref counting for state data.
///
/// For backwards compatibility returns `false` by default.
fn supports_ref_counting(&self) -> bool {
false
}
/// Remove a possible path-prefix from the key.
///
/// Not all database implementations use a prefix for keys, so this function may be a noop.
fn sanitize_key(&self, _key: &mut Vec<u8>) {}
}
impl<H> std::fmt::Debug for dyn Database<H> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Database")
}
}
/// Call `f` with the value previously stored against `key` and return the result, or `None` if
/// `key` is not currently in the database.
///
/// This may be faster than `get` since it doesn't allocate.
pub fn with_get<R, H: Clone + AsRef<[u8]>>(
db: &dyn Database<H>,
col: ColumnId,
key: &[u8],
mut f: impl FnMut(&[u8]) -> R,
) -> Option<R> {
let mut result: Option<R> = None;
let mut adapter = |k: &_| {
result = Some(f(k));
};
db.with_get(col, key, &mut adapter);
result
}
+89
View File
@@ -0,0 +1,89 @@
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! In-memory implementation of `Database`
use crate::{error, Change, ColumnId, Database, Transaction};
use parking_lot::RwLock;
use std::collections::{hash_map::Entry, HashMap};
#[derive(Default)]
/// This implements `Database` as an in-memory hash map. `commit` is not atomic.
pub struct MemDb(RwLock<HashMap<ColumnId, HashMap<Vec<u8>, (u32, Vec<u8>)>>>);
impl<H> Database<H> for MemDb
where
H: Clone + AsRef<[u8]>,
{
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
let mut s = self.0.write();
for change in transaction.0.into_iter() {
match change {
Change::Set(col, key, value) => {
s.entry(col).or_default().insert(key, (1, value));
},
Change::Remove(col, key) => {
s.entry(col).or_default().remove(&key);
},
Change::Store(col, hash, value) => {
s.entry(col)
.or_default()
.entry(hash.as_ref().to_vec())
.and_modify(|(c, _)| *c += 1)
.or_insert_with(|| (1, value));
},
Change::Reference(col, hash) => {
if let Entry::Occupied(mut entry) =
s.entry(col).or_default().entry(hash.as_ref().to_vec())
{
entry.get_mut().0 += 1;
}
},
Change::Release(col, hash) => {
if let Entry::Occupied(mut entry) =
s.entry(col).or_default().entry(hash.as_ref().to_vec())
{
entry.get_mut().0 -= 1;
if entry.get().0 == 0 {
entry.remove();
}
}
},
}
}
Ok(())
}
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
let s = self.0.read();
s.get(&col).and_then(|c| c.get(key).map(|(_, v)| v.clone()))
}
}
impl MemDb {
/// Create a new instance
pub fn new() -> Self {
MemDb::default()
}
/// Count number of values in a column
pub fn count(&self, col: ColumnId) -> usize {
let s = self.0.read();
s.get(&col).map(|c| c.len()).unwrap_or(0)
}
}