Skip to content

Commit 2c9049f

Browse files
committed
feat(node): consensus
1 parent 04804dd commit 2c9049f

25 files changed

Lines changed: 1354 additions & 24 deletions

File tree

Cargo.lock

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["bin/*", "crates/storage/*", "crates/utilities/*"]
2+
members = ["bin/*", "crates/node/*", "crates/storage/*", "crates/utilities/*"]
33
default-members = ["bin/kora"]
44
resolver = "2"
55

@@ -47,8 +47,10 @@ codegen-units = 1
4747
[workspace.dependencies]
4848
# Local crates
4949
kora-cli = { path = "crates/utilities/cli" }
50+
kora-consensus = { path = "crates/node/consensus" }
5051
kora-handlers = { path = "crates/storage/handlers" }
5152
kora-qmdb = { path = "crates/storage/qmdb" }
53+
kora-traits = { path = "crates/storage/traits" }
5254

5355
# Commonware
5456
commonware-cryptography = { git = "https://github.com/commonwarexyz/monorepo.git", rev = "v0.0.27" }

crates/node/consensus/Cargo.toml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "kora-consensus"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
license.workspace = true
7+
repository.workspace = true
8+
description = "Consensus application layer for Kora"
9+
10+
[dependencies]
11+
# Local crates
12+
kora-traits = { path = "../../storage/traits" }
13+
kora-qmdb = { path = "../../storage/qmdb" }
14+
15+
# Alloy
16+
alloy-primitives.workspace = true
17+
alloy-consensus.workspace = true
18+
alloy-rlp.workspace = true
19+
20+
# Error handling
21+
thiserror.workspace = true
22+
23+
[dev-dependencies]
24+
rstest = "0.24"
25+
26+
[lints]
27+
workspace = true

crates/node/consensus/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# `kora-consensus`
2+
3+
<a href="https://github.com/refcell/kora/actions/workflows/ci.yml"><img src="https://github.com/refcell/kora/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
4+
<a href="https://github.com/refcell/kora/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-d1d1f6.svg" alt="License"></a>
5+
6+
Consensus application layer for Kora.
7+
8+
This crate provides the bridge between Commonware consensus and REVM execution,
9+
using trait-abstracted components for modularity.
10+
11+
## Key Types
12+
13+
- `ConsensusApplication` - Implements Commonware's Application trait
14+
- `KoraBlock` - Block type using alloy types
15+
- `ExecutionOutcome` - Result of block execution
16+
17+
## Traits
18+
19+
All components are trait-abstracted for swappability:
20+
21+
- `Mempool` - Pending transaction pool
22+
- `SnapshotStore` - Execution state caching
23+
- `SeedTracker` - VRF seed management
24+
- `BlockExecutor` - Transaction execution
25+
26+
## Architecture
27+
28+
```text
29+
+--------------------------------------------------+
30+
| kora-consensus |
31+
| |
32+
| ConsensusApplication<M, S, SS, ST, E> |
33+
| | | | | | |
34+
| v v v v v |
35+
| Mempool StateDb Snapshot Seed Block |
36+
| trait trait Store Tracker Executor |
37+
+--------------------------------------------------+
38+
| |
39+
v v
40+
kora-traits kora-handlers
41+
```
42+
43+
## Usage
44+
45+
Add to your `Cargo.toml`:
46+
47+
```toml
48+
[dependencies]
49+
kora-consensus = { path = "crates/node/consensus" }
50+
```
51+
52+
## License
53+
54+
[MIT License](https://github.com/refcell/kora/blob/main/LICENSE)

crates/node/consensus/src/block.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Kora block type using alloy types.
2+
3+
use alloy_consensus::Header;
4+
use alloy_primitives::{B256, keccak256};
5+
use alloy_rlp::Encodable;
6+
7+
/// Block type for Kora consensus.
8+
///
9+
/// Uses alloy types directly for Ethereum compatibility.
10+
#[derive(Clone, Debug)]
11+
pub struct KoraBlock {
12+
/// Block header.
13+
pub header: Header,
14+
/// Transactions in the block (encoded).
15+
pub transactions: Vec<Vec<u8>>,
16+
/// Computed state root.
17+
pub state_root: B256,
18+
}
19+
20+
impl KoraBlock {
21+
/// Create a new block.
22+
pub const fn new(header: Header, transactions: Vec<Vec<u8>>, state_root: B256) -> Self {
23+
Self { header, transactions, state_root }
24+
}
25+
26+
/// Compute the block's hash from the header.
27+
pub fn hash(&self) -> B256 {
28+
let mut buf = Vec::new();
29+
self.header.encode(&mut buf);
30+
keccak256(&buf)
31+
}
32+
33+
/// Get the parent block's hash.
34+
pub const fn parent_hash(&self) -> B256 {
35+
self.header.parent_hash
36+
}
37+
38+
/// Get the block height.
39+
pub const fn height(&self) -> u64 {
40+
self.header.number
41+
}
42+
43+
/// Get the block timestamp.
44+
pub const fn timestamp(&self) -> u64 {
45+
self.header.timestamp
46+
}
47+
48+
/// Get the number of transactions.
49+
pub const fn tx_count(&self) -> usize {
50+
self.transactions.len()
51+
}
52+
}
53+
54+
impl Default for KoraBlock {
55+
fn default() -> Self {
56+
Self { header: Header::default(), transactions: Vec::new(), state_root: B256::ZERO }
57+
}
58+
}
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use super::*;
63+
64+
#[test]
65+
fn block_default() {
66+
let block = KoraBlock::default();
67+
assert_eq!(block.height(), 0);
68+
assert_eq!(block.tx_count(), 0);
69+
assert_eq!(block.state_root, B256::ZERO);
70+
}
71+
72+
#[test]
73+
fn block_hash_deterministic() {
74+
let block = KoraBlock::default();
75+
let hash1 = block.hash();
76+
let hash2 = block.hash();
77+
assert_eq!(hash1, hash2);
78+
}
79+
}

0 commit comments

Comments
 (0)