This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Global State: versioned KV store with root signature calculation (#333)
- Loading branch information
Showing
13 changed files
with
1,817 additions
and
44 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ description = "Spacemesh Virtual Machine" | |
publish = false | ||
|
||
[dependencies] | ||
tiny-keccak = "1.4.2" | ||
blake3 = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,57 @@ | ||
//! A [`Hasher`] trait for wide-digest algorithms and its [`DefaultHasher`] | ||
//! implementation. | ||
use tiny_keccak::Keccak; | ||
#![deny(missing_docs)] | ||
#![deny(unused)] | ||
#![deny(dead_code)] | ||
#![deny(unreachable_code)] | ||
|
||
/// A low-level trait for defining a hasher. | ||
pub trait Hasher { | ||
pub trait Hasher: Default { | ||
/// `KeyHasher` produces hashes of type `Self::Hash` | ||
type Hash: AsRef<[u8]> + Copy + Clone + std::fmt::Debug + Sized; | ||
type Hash: AsRef<[u8]> + Copy + std::fmt::Debug + Sized; | ||
|
||
/// Receives an input `key` and returns its hash as `Self::Hash` | ||
fn hash(key: &[u8]) -> Self::Hash; | ||
/// Receives an input `key` and returns its hash as `Self::Hash`. | ||
fn hash(key: &[u8]) -> Self::Hash { | ||
let mut hasher = Self::default(); | ||
hasher.update(key); | ||
hasher.finalize() | ||
} | ||
|
||
/// Writes some arbitrary `bytes` into this [`Hasher`]. | ||
fn update(&mut self, bytes: &[u8]) -> &mut Self; | ||
|
||
/// Returns the final [`Hasher::Hash`] value for all data written to `self` | ||
/// via [`Hasher::update`] so far. | ||
fn finalize(self) -> Self::Hash; | ||
} | ||
|
||
/// Implements the `KeyHasher` trait using the `keccak256` hashing algorithm (output: 32 bytes) | ||
pub struct DefaultHasher; | ||
/// Implements the [`Hasher`] trait using the Blake3 hashing algorithm (output: | ||
/// 32 bytes). | ||
#[derive(Clone, Debug, Default)] | ||
pub struct Blake3Hasher(blake3::Hasher); | ||
|
||
impl std::hash::Hasher for Blake3Hasher { | ||
fn write(&mut self, bytes: &[u8]) { | ||
self.0.update(bytes); | ||
} | ||
|
||
impl Hasher for DefaultHasher { | ||
fn finish(&self) -> u64 { | ||
let mut hash = [0; 8]; | ||
self.0.finalize_xof().fill(&mut hash); | ||
u64::from_be_bytes(hash) | ||
} | ||
} | ||
|
||
impl Hasher for Blake3Hasher { | ||
type Hash = [u8; 32]; | ||
|
||
fn hash(key: &[u8]) -> Self::Hash { | ||
let mut out = [0; 32]; | ||
fn update(&mut self, bytes: &[u8]) -> &mut Self { | ||
self.0.update(bytes); | ||
self | ||
} | ||
|
||
Keccak::keccak256(key, &mut out); | ||
out | ||
fn finalize(self) -> Self::Hash { | ||
*self.0.finalize().as_bytes() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "svm-state" | ||
version = "0.0.0" | ||
authors = ["Spacemesh SVM Team"] | ||
license = "MIT" | ||
edition = "2018" | ||
readme = "README.md" | ||
repository = "https://github.com/spacemeshos/svm" | ||
homepage = "https://github.com/spacemeshos/svm" | ||
description = "Spacemesh Virtual Machine" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
blake3 = "1" | ||
futures = "0.3" | ||
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "sqlite", "macros"] } | ||
svm-hash = { path = "../hash" } | ||
thiserror = "1" | ||
|
||
[dev-dependencies] | ||
quickcheck = "1" | ||
quickcheck_async = "0.1" | ||
quickcheck_macros = "1" | ||
tokio = { version = "1", features = ["rt", "macros"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::fmt::Debug; | ||
|
||
use crate::Fingerprint; | ||
|
||
/// An alias for [`Result`](std::result::Result)'s with [`StorageError`]. | ||
pub type Result<T> = std::result::Result<T, StorageError>; | ||
|
||
/// A sum type for all error conditions that can arise in this crate. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum StorageError { | ||
/// The storage layer contains some dirty changes that must be either saved | ||
/// or rollbacked before attempting such operation. | ||
#[error("Please checkout dirty changes or rollback to avoid data loss.")] | ||
DirtyChanges, | ||
|
||
/// Two checkpoints have resulted in key collision, which prevents unordered | ||
/// transaction replaying. | ||
#[error("Key collision from two different checkpoints.")] | ||
KeyCollision { | ||
/// They Blake3 hash of the key that caused this collision. | ||
key_hash: Fingerprint, | ||
}, | ||
|
||
/// A SQLite error happened. | ||
#[error("SQLite error.")] | ||
Sqlite(#[from] sqlx::Error), | ||
} |
Oops, something went wrong.