Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
feat: narwhal (#369)
Browse files Browse the repository at this point in the history
* narwhal init
  • Loading branch information
b0xtch authored Feb 13, 2023
1 parent e20ab1f commit 5b02e51
Show file tree
Hide file tree
Showing 9 changed files with 466 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/ursa",
"crates/ursa-consensus",
"crates/ursa-gateway",
"crates/ursa-index-provider",
"crates/ursa-metrics",
Expand Down Expand Up @@ -47,7 +48,7 @@ futures-util = "0.3.25"
fvm_ipld_blockstore = { git = "https://github.com/filecoin-project/ref-fvm/" }
fvm_ipld_car = { git = "https://github.com/filecoin-project/ref-fvm/" }
fvm_ipld_encoding = "=0.3.2"
graphsync = { git = "https://github.com/kckeiks/rs-graphsync.git", branch = "downgrade-cid" }
graphsync = { git = "https://github.com/kckeiks/rs-graphsync.git", branch = "downgrade-cid" }
hyper = { version = "0.14.23", features = ["full"] }
hyper-tls = "0.5.0"
imara-diff = "0.1.5"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![rust-ci](https://img.shields.io/github/actions/workflow/status/fleek-network/ursa/rust.yml?branch=main&label=Tests&style=for-the-badge)](https://github.com/fleek-network/ursa/actions/workflows/rust.yml) 
[![docker-build](https://img.shields.io/github/actions/workflow/status/fleek-network/ursa/docker-publish.yml?branch=main&label=Docker%20Build&style=for-the-badge)](https://github.com/fleek-network/ursa/pkgs/container/ursa) 

Ursa, a decentralized content delivery network.
> Ursa, a decentralized content delivery network.
## Run a node

Expand Down
38 changes: 38 additions & 0 deletions crates/ursa-consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "ursa-consensus"
version = "0.1.0"
edition = "2021"
authors = ["b0xtch <[email protected]>"]

[dependencies]
anyhow = "1.0"
arc-swap = { version = "1.6.0", features = ["serde"] }
async-trait = "0.1"
bytes = "1.3.0"

fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto", rev = "235211dc8195590f5353d38135f5ee51a267521e" }
fastcrypto-zkp = { git = "https://github.com/MystenLabs/fastcrypto", rev = "235211dc8195590f5353d38135f5ee51a267521e", package = "fastcrypto-zkp" }
fastcrypto-tbls = { git = "https://github.com/MystenLabs/fastcrypto", rev = "235211dc8195590f5353d38135f5ee51a267521e", package = "fastcrypto-tbls" }

futures = "0.3.23"
multiaddr = "0.17.0"
mysten-metrics = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "mysten-metrics" }
mysten-network = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "mysten-network" }

narwhal-config = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-config" }
narwhal-consensus = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-consensus" }
narwhal-crypto = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-crypto" }
narwhal-executor = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-executor" }
narwhal-node = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-node" }
narwhal-primary = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-primary" }
narwhal-types = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-types" }
narwhal-worker = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "narwhal-worker" }
prometheus = "0.13.3"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
tempfile = "3.3.0"
tokio = { version = "1.24.2", features = ["sync", "rt", "macros"] }
tokio-stream = { version = "0.1.11", features = ["net"] }
tracing = "0.1.37"

workspace-hack = { git = "https://github.com/MystenLabs/sui.git", branch = "main", package = "workspace-hack" }
6 changes: 6 additions & 0 deletions crates/ursa-consensus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Narwhal and Bullshark - Mempool and Consensus

> Narwhal and Bullshark (N/B) - Fleek Network's consensus and ordering algorithms. A DAG based consensus with total ordering and reliable broadcast.
This code has been adapted from the [MystenLabs Sui](https://github.com/MystenLabs/sui)

73 changes: 73 additions & 0 deletions crates/ursa-consensus/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022-2023 Fleek Network
// SPDX-License-Identifier: Apache-2.0, MIT

use std::{path::PathBuf, sync::Arc};

use fastcrypto::{bls12381::min_sig::BLS12381KeyPair, ed25519::Ed25519KeyPair};
use multiaddr::Multiaddr;
use mysten_metrics::RegistryService;
use narwhal_config::{Parameters, WorkerId};
use narwhal_crypto::NetworkKeyPair as NarwhalNetworkKeyPair;
use serde::{Deserialize, Serialize};
use tokio::sync::OnceCell;

pub type KeyPair = Ed25519KeyPair;
pub type AuthorityKeyPair = BLS12381KeyPair;

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct ValidatorKeyPair {
#[serde(skip)]
keypair: OnceCell<Arc<AuthorityKeyPair>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct NetworkKeyPair {
#[serde(skip)]
keypair: OnceCell<Arc<KeyPair>>,
}

impl ValidatorKeyPair {
pub fn new(keypair: AuthorityKeyPair) -> Self {
let cell = OnceCell::new();
cell.set(Arc::new(keypair))
.expect("Failed to set authority keypair");
Self { keypair: cell }
}

pub fn authority_keypair(&self) -> &AuthorityKeyPair {
self.keypair.get().as_ref().unwrap()
}
}

impl NetworkKeyPair {
pub fn new(keypair: KeyPair) -> Self {
let cell = OnceCell::new();
cell.set(Arc::new(keypair))
.expect("Failed to set authority keypair");
Self { keypair: cell }
}

pub fn keypair(&self) -> &KeyPair {
self.keypair.get().as_ref().unwrap()
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct NodeConfig {
pub keypair: ValidatorKeyPair,
pub worker_keypair: NetworkKeyPair,
pub account_keypair: NetworkKeyPair,
pub network_keypair: NetworkKeyPair,
pub network_address: Multiaddr,
pub db_path: PathBuf,
}

pub struct NarwhalConfig {
pub keypair: ValidatorKeyPair,
pub network_keypair: NetworkKeyPair,
pub registry_service: RegistryService,
pub ids_and_keypairs: Vec<(WorkerId, NarwhalNetworkKeyPair)>,
pub internal_consensus: bool,
pub parameters: Parameters,
pub storage_base_path: PathBuf,
}
50 changes: 50 additions & 0 deletions crates/ursa-consensus/src/execution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2022-2023 Fleek Network
// SPDX-License-Identifier: Apache-2.0, MIT

use async_trait::async_trait;
use bytes::Bytes;
use narwhal_executor::ExecutionState;
use narwhal_types::ConsensusOutput;
use tokio::sync::mpsc::Sender;
use tracing::error;

type Epoch = u64;

pub struct Execution<N> {
/// a store for epoch
_store: N,
/// current epoch store implementation
epoch: Epoch,
/// managing certificates generated by narwhal
transactions: Sender<Vec<u8>>,
}

impl<N> Execution<N> {
pub fn new(store: N, epoch: Epoch, transactions: Sender<Vec<u8>>) -> Self {
Self {
_store: store,
epoch,
transactions,
}
}
}

#[async_trait]
impl<N: Send + Sync> ExecutionState for Execution<N> {
async fn handle_consensus_output(&self, consensus_output: ConsensusOutput) {
for (_, batches) in consensus_output.batches {
for batch in batches {
for transaction in batch.transactions.into_iter() {
assert_eq!(transaction, Bytes::from(self.epoch.to_be_bytes().to_vec()));
if let Err(err) = self.transactions.send(transaction).await {
error!("Failed to send txn: {}", err);
}
}
}
}
}

async fn last_executed_sub_dag_index(&self) -> u64 {
0
}
}
Loading

0 comments on commit 5b02e51

Please sign in to comment.