-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
17 deletions.
There are no files selected for viewing
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
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,16 @@ | ||
[package] | ||
name = "rpc" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
bincode = "1.3" | ||
clap = { version = "4.0", features = ["env"] } | ||
rocket = { version = "0.5", features = ["json"] } | ||
rollup = { package = "rollup", path = "../rollup", version = "0.1.0" } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
sha2 = "0.10" | ||
env_logger = { workspace = true } | ||
tokio = { version = "1", features = ["full"] } | ||
p2p = { package = "p2p", path = "../p2p", version = "0.1.0" } |
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,67 @@ | ||
#[macro_use] | ||
extern crate rocket; | ||
|
||
use std::sync::Arc; | ||
|
||
use rocket::State; | ||
use rocket::{serde::json::Json, Config}; | ||
use rollup::{Blockchain, SignedTransaction, TransactionSubmitter}; | ||
use serde_json::{json, Value}; | ||
use tokio::sync::Mutex; | ||
|
||
/// Accepts a transaction and adds it to the respective transaction pools. | ||
#[post("/", data = "<payload>")] | ||
async fn submit( | ||
submitter: &State<TransactionSubmitter>, | ||
payload: Json<SignedTransaction>, | ||
) -> Value { | ||
// Extract the transaction from the payload. | ||
let transaction = payload.into_inner(); | ||
let tx_digest = transaction.transaction.hash(); | ||
|
||
// Add the transaction to the pool. | ||
submitter.submit(transaction).await; | ||
|
||
// Respond with the transaction digest. | ||
json!({ "tx_digest": tx_digest.to_string() }) | ||
} | ||
|
||
/// Returns the head block of the blockchain. | ||
#[get("/")] | ||
async fn head(chain: &State<Arc<Mutex<Blockchain>>>) -> Value { | ||
// Retrieve the head block from the sequencer and return it. | ||
let head = chain.lock().await.head(); | ||
json!(head) | ||
} | ||
|
||
#[launch] | ||
#[tokio::main] | ||
async fn rocket() -> _ { | ||
env_logger::init(); | ||
// Set up sequencer. | ||
let pool = Arc::new(tokio::sync::Mutex::new(vec![])); | ||
let chain = Arc::new(tokio::sync::Mutex::new(Blockchain::default())); | ||
let (tx_out, rx_out) = tokio::sync::mpsc::channel::<(Vec<u8>, String)>(32); | ||
let mut rx_in = p2p::Network::start(rx_out); | ||
let submitter = TransactionSubmitter::new(pool, tx_out); | ||
|
||
// Spawn block producing sequencer task. | ||
tokio::task::spawn(async move { | ||
loop { | ||
let msg = rx_in.recv().await.unwrap(); | ||
println!("RPC Received message: {:?}", msg); | ||
} | ||
}); | ||
|
||
// Launch the HTTP server. | ||
let mut config = Config { | ||
log_level: rocket::config::LogLevel::Critical, | ||
..Config::debug_default() | ||
}; | ||
config.port = 8001; | ||
rocket::build() | ||
.configure(config) | ||
.mount("/", routes![submit, head]) | ||
.manage(submitter) | ||
.manage(chain) | ||
} |
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