Skip to content

Commit

Permalink
Add Fly Deploy workflow and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
10d9e committed Dec 15, 2023
1 parent d697528 commit 19a95f1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/fly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Fly Deploy
on:
push:
branches:
- master
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_cbor = "0.11"
serde_json = "1.0"
async-std = { version = "1.12", features = ["attributes"] }
clap = { version = "4.4.8", features = ["derive", "cargo"] }
clap = { version = "4.4.8", features = ["derive", "cargo", "env"] }
either = "1.9"
futures = "0.3.29"
libp2p = { version = "0.53.1", features = [ "async-std", "tokio", "identify", "gossipsub", "mdns", "cbor", "dns", "kad", "noise", "macros", "request-response", "tcp", "websocket", "yamux"] }
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ FROM debian:bookworm-slim AS runtime
WORKDIR /app
COPY --from=builder /app/target/release/shard /usr/local/bin/

EXPOSE 40837/tcp

# Define an environment variable for the custom command
ENV COMMAND=

Expand Down
21 changes: 17 additions & 4 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
# fly.toml app configuration file generated for shard-bootstrapper on 2023-12-14T18:20:57-04:00
# fly.toml app configuration file generated for shard-long-dew-9956 on 2023-12-15T00:40:56-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "shard-bootstrapper"
app = "shard"
primary_region = "yul"

[build]
dockerfile = "Dockerfile"

[env]
COMMAND = "shard --listen-address /ip4/0.0.0.0/tcp/40837 --secret-key-seed 1 provide --db-path .db --refresh-interval 300"
RUST_LOG = "info"

[processes]
boot = "shard --listen-address /ip4/0.0.0.0/tcp/40837 --secret-key-seed 1 provide --db-path .db --refresh-interval 1209600"
other = "shard --listen-address /ip4/0.0.0.0/tcp/42961 --peer /ip4/213.188.207.114/tcp/40837/p2p/12D3KooWPjceQrSwdWXPyLLeABRXmuqt69Rg3sBYbU1Nft9HyQ6X provide --db-path .db"

[[services]]
protocol = "tcp"
internal_port = 40837
processes = ["boot"]

[[services.ports]]
port = 40837

[[services]]
protocol = "tcp"
internal_port = 42961
processes = ["other"]

[[services.ports]]
port = "40837"
port = 42961

[[vm]]
cpu_kind = "shared"
cpus = 1
memory_mb = 1024
processes = ["boot", "other"]
22 changes: 21 additions & 1 deletion src/bin/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rand::RngCore;
use shard::config::ShardConfig;
use std::collections::HashMap;
use std::error::Error;
use std::net::IpAddr;
use std::sync::Arc;
use tokio::time::Duration;
use tokio::{spawn, time};
Expand Down Expand Up @@ -121,6 +122,10 @@ struct Opt {
#[clap(long, short)]
listen_address: Option<Multiaddr>,

/// If known, the external address of this node. Will be used to correctly advertise our external address across all transports.
#[clap(long, env)]
external_address: Option<IpAddr>,

/// Subcommand to run.
#[clap(subcommand)]
argument: CliArgument,
Expand All @@ -142,7 +147,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
network::new(opt.secret_key_seed).await?;

// Spawn the network task for it to run in the background.
spawn(network_event_loop.run());
spawn(network_event_loop.run(opt.external_address));

// In case a listen address was provided use it, otherwise listen on any
// address.
Expand All @@ -157,6 +162,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.expect("Listening not to fail."),
};

/*
if let Some(addr) = config.bootstrapper {
let Some(Protocol::P2p(peer_id)) = addr.iter().last() else {
return Err("Expect peer multiaddr to contain peer ID.".into());
Expand All @@ -171,6 +177,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.expect("Dial to succeed");
}
}
*/

// In case the user provided an address of a peer on the CLI, dial it.
if let Some(addr) = opt.peer {
Expand All @@ -182,6 +189,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
.dial(peer_id, addr)
.await
.expect("Dial to succeed");
} else if let Some(addr) = config.bootstrapper {
let Some(Protocol::P2p(peer_id)) = addr.iter().last() else {
return Err("Expect peer multiaddr to contain peer ID.".into());
};

// if the peer is the same as the local peer, don't dial
if peer_id != local_peer_id {
debug!("👢 Bootstrapping to peer at {}.", addr);
network_client
.dial(peer_id, addr)
.await
.expect("Dial to succeed");
}
}

debug!("Waiting for network to be ready...");
Expand Down
17 changes: 13 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use libp2p::{

use std::collections::{HashMap, HashSet};
use std::error::Error;
use std::net::IpAddr;
use tracing::debug;

use crate::command::command_handler;
Expand Down Expand Up @@ -129,14 +130,14 @@ impl EventLoop {
/// ```ignore
/// event_loop.run().await;
/// ```
pub async fn run(mut self) {
pub async fn run(mut self, external_address: Option<IpAddr>) {
loop {
futures::select! {
event = self.swarm.next() => self.handle_event(event.expect("Swarm stream to be infinite.")).await ,
event = self.swarm.next() => self.handle_event(event.expect("Swarm stream to be infinite."), external_address).await,
command = self.command_receiver.next() => match command {
Some(c) => self.handle_command(c).await,
// Command channel closed, thus shutting down the network event loop.
None=> return,
None => return,
},
}
}
Expand All @@ -147,7 +148,7 @@ impl EventLoop {
/// # Arguments
///
/// * `event` - The event to handle.
async fn handle_event(&mut self, event: SwarmEvent<BehaviourEvent>) {
async fn handle_event(&mut self, event: SwarmEvent<BehaviourEvent>, external_address: Option<IpAddr>) {
match event {
SwarmEvent::Behaviour(BehaviourEvent::Kademlia(
kad::Event::OutboundQueryProgressed {
Expand Down Expand Up @@ -319,6 +320,14 @@ impl EventLoop {
)) => {}

SwarmEvent::NewListenAddr { address, .. } => {
if let Some(external_ip) = external_address {
let external_address = address
.replace(0, |_| Some(external_ip.into()))
.expect("address.len > 1 and we always return `Some`");

self.swarm.add_external_address(external_address);
}

let local_peer_id = *self.swarm.local_peer_id();
debug!(
"Local node is listening on {:?}",
Expand Down

0 comments on commit 19a95f1

Please sign in to comment.