Skip to content

Commit

Permalink
Fix a bug with upstream and add compose.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidson-Souza committed Aug 27, 2023
1 parent e07e372 commit bbcc2fb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target
.env
.env
bridge/
backend/
bitcoind/
21 changes: 18 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bridge"
version = "0.1.0"
version = "0.1.2"
edition = "2021"
authors = ["Davidson Souza <[email protected]>"]
description = "A hiper specialized Utreexo bridge node"
Expand All @@ -16,7 +16,7 @@ categories = ["bitcoin", "tools"]
bitcoin = { version = "0.29" }
bitcoincore-rpc = "0.16.0"
bitcoin_hashes = "0.11"
rustreexo = { version = "0.1.0", features = ["with-serde"] }
rustreexo = { git = "https://github.com/mit-dci/rustreexo", features = ["with-serde"] }
sha2 = "0.10.6"
anyhow = "1.0.71"
kv = "0.24.0"
Expand All @@ -39,6 +39,7 @@ reqwest = { version = "0.11.18", features = [
"rustls-tls-webpki-roots",
], optional = true, default-features = false }
serde_json = { version = "1.0.104", optional = true }
actix-cors = "0.6.4"

[patch."https://github.com/rust-lang/crates.io-index"]
bitcoin = { git = "https://github.com/Davidson-Souza/rust-bitcoin", rev = "a320c6535567acd3771da37759a7644eea5c6eb2" }
Expand Down
45 changes: 45 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: "3"

services:
bitcoind:
image: ruimarinho/bitcoin-core:latest
restart: always
entrypoint: bitcoind -txindex=1 -signet -rpcuser=$RPC_USER -rpcpassword=$RPC_PASSWORD -rpcbind=0.0.0.0:38332 -rpcallowip=255.255.255.255/0
container_name: bitcoind
environment:
- RPC_USER=${RPC_USER}
- RPC_PASSWORD=${RPC_PASSWORD}
volumes:
- ./bitcoind/:$HOME/.bitcoin
ports:
- 38332:38332
networks:
- local-network

brige:
build:
context: .
dockerfile: Dockerfile
restart: always
entrypoint: ./bridge
container_name: bridge
environment:
- BITCOIN_CORE_RPC_URL=bitcoind:38332
- BITCOIN_CORE_RPC_USER=${RPC_USER}
- BITCOIN_CORE_RPC_PASSWORD=${RPC_PASSWORD}
- P2P_PORT=8333
- API_PORT=${API_PORT}
- DATA_DIR=$HOME/.bridge
volumes:
- ./bridge/:$HOME/.bridge
ports:
- $P2P_PORT:$P2P_PORT
- $API_PORT:$API_PORT
networks:
- local-network
depends_on:
- bitcoind

networks:
local-network:
driver: bridge
23 changes: 22 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use std::str::FromStr;

use crate::prover::{Requests, Responses};
use actix_cors::Cors;
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use bitcoin::{consensus::deserialize, network::utreexo::UtreexoBlock, Block, Txid};
use bitcoin_hashes::Hash;
use bitcoincore_rpc::jsonrpc::serde_json::json;
use futures::{channel::mpsc::Sender, lock::Mutex, SinkExt};
use rustreexo::accumulator::{node_hash::NodeHash, proof::Proof};
use serde::{Deserialize, Serialize};

/// This is the state of the actix-web server that will be passed as reference by each
/// callback function. It contains a sender that can be used to send requests to the prover.
struct AppState {
Expand Down Expand Up @@ -111,6 +111,24 @@ async fn get_block_by_height(height: web::Path<u32>, data: web::Data<AppState>)
})),
}
}
/// Same as `get_roots`, but returns the leaf number of the accumulator too.
async fn get_roots_with_leaf(data: web::Data<AppState>) -> Result<HttpResponse, actix_web::Error> {
let res = perform_request(&data, Requests::GetCSN).await;
match res {
Ok(Responses::CSN(acc)) => Ok(HttpResponse::Ok().json(json!({
"error": null,
"data": acc
}))),
Ok(_) => Ok(HttpResponse::InternalServerError().json(json!({
"error": "Invalid response",
"data": null
}))),
Err(e) => Ok(HttpResponse::NotAcceptable().json(json!({
"error": e,
"data": null
}))),
}
}
/// The handler for the `/roots` endpoint. It returns the roots of the accumulator.
async fn get_roots(data: web::Data<AppState>) -> HttpResponse {
let res = perform_request(&data, Requests::GetRoots).await;
Expand Down Expand Up @@ -145,12 +163,15 @@ pub async fn create_api(
sender: Mutex::new(request),
});
HttpServer::new(move || {
let cors = Cors::permissive();
App::new()
.wrap(cors)
.app_data(app_state.clone())
.route("/prove/{leaf}", web::get().to(get_proof))
.route("/roots", web::get().to(get_roots))
.route("/block/{height}", web::get().to(get_block_by_height))
.route("/tx/{hash}/outputs", web::get().to(get_transaction))
.route("/acc", web::get().to(get_roots_with_leaf))
})
.bind("0.0.0.0:8080")?
.run()
Expand Down
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Peer {
sender: version.receiver,
nonce: version.nonce + 100,
user_agent: "/rustreexo:0.1.0/bridge:0.1.0".to_string(),
start_height: 0,
start_height: 800_000,
relay: false,
},
),
Expand Down
2 changes: 1 addition & 1 deletion src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<LeafStorage: LeafCache> Prover<LeafStorage> {
fn check_tip(&mut self, last_tip_update: &mut std::time::Instant) -> anyhow::Result<()> {
let height = self.rpc.get_block_count()? as u32;
if height > self.height {
self.prove_range(self.height + 1, self.height + 10_000)?;
self.prove_range(self.height + 1, self.height + 100_000)?;

self.save_to_disk();
self.storage.update_height(height as usize);
Expand Down

0 comments on commit bbcc2fb

Please sign in to comment.