Skip to content

Commit

Permalink
Merge pull request #81 from L2-Technology/batch-node-create
Browse files Browse the repository at this point in the history
batch node create
  • Loading branch information
johncantrell97 authored Jun 7, 2022
2 parents 97a6f7a + 3dad03f commit cd74d6f
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 273 deletions.
7 changes: 1 addition & 6 deletions migration/src/m20220421_000001_create_nodes_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Node::Network).string().not_null())
.col(ColumnDef::new(Node::ListenAddr).string().not_null())
.col(ColumnDef::new(Node::ListenPort).integer().not_null())
.col(
ColumnDef::new(Node::Pubkey)
.string()
.unique_key()
.not_null(),
)
.col(ColumnDef::new(Node::Pubkey).string().not_null())
.col(ColumnDef::new(Node::CreatedAt).big_integer().not_null())
.col(ColumnDef::new(Node::UpdatedAt).big_integer().not_null())
.col(ColumnDef::new(Node::Status).small_integer().not_null())
Expand Down
2 changes: 1 addition & 1 deletion senseicore/src/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl ChannelOpener {
.collect::<Vec<EventFilter<_>>>();

// TODO: is this appropriate timeout? maybe should accept as param
let events = self.wait_for_events(filters, 15000, 500).await;
let events = self.wait_for_events(filters, 30000, 500).await;

// set error state for requests we didn't get an event for
let requests_with_results = requests_with_results
Expand Down
2 changes: 2 additions & 0 deletions senseicore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct SenseiConfig {
pub bitcoind_rpc_username: String,
pub bitcoind_rpc_password: String,
pub network: Network,
pub api_host: String,
pub api_port: u16,
pub port_range_min: u16,
pub port_range_max: u16,
Expand All @@ -39,6 +40,7 @@ impl Default for SenseiConfig {
bitcoind_rpc_username: String::from("bitcoin"),
bitcoind_rpc_password: String::from("bitcoin"),
network: Network::Bitcoin,
api_host: String::from("127.0.0.1"),
api_port: 5401,
port_range_min: 10000,
port_range_max: 65535,
Expand Down
48 changes: 47 additions & 1 deletion senseicore/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use entity::payment::Entity as Payment;
use entity::sea_orm;
use entity::sea_orm::ActiveValue;
use entity::sea_orm::QueryOrder;
use entity::seconds_since_epoch;
use migration::Condition;
use migration::Expr;
use rand::thread_rng;
Expand Down Expand Up @@ -427,6 +428,24 @@ impl SenseiDatabase {
})
}

pub async fn create_value(
&self,
node_id: String,
key: String,
value: Vec<u8>,
) -> Result<kv_store::Model, Error> {
let entry = kv_store::ActiveModel {
node_id: ActiveValue::Set(node_id),
k: ActiveValue::Set(key),
v: ActiveValue::Set(value),
..Default::default()
}
.insert(&self.connection)
.await?;

Ok(entry)
}

pub async fn set_value(
&self,
node_id: String,
Expand Down Expand Up @@ -474,10 +493,37 @@ impl SenseiDatabase {
self.set_value(node_id, String::from("seed"), seed).await
}

pub async fn create_seed(
&self,
node_id: String,
seed: Vec<u8>,
) -> Result<kv_store::Model, Error> {
self.create_value(node_id, String::from("seed"), seed).await
}

pub fn get_seed_active_model(&self, node_id: String, seed: Vec<u8>) -> kv_store::ActiveModel {
let now = seconds_since_epoch();
kv_store::ActiveModel {
node_id: ActiveValue::Set(node_id),
k: ActiveValue::Set(String::from("seed")),
v: ActiveValue::Set(seed),
created_at: ActiveValue::Set(now),
updated_at: ActiveValue::Set(now),
..Default::default()
}
}

pub async fn insert_kv_store(
&self,
entity: kv_store::ActiveModel,
) -> Result<kv_store::Model, Error> {
Ok(entity.insert(&self.connection).await?)
}

// Note: today we assume there's only ever one macaroon for a user
// once there's some `bakery` functionality exposed we need to define
// which macaroon we return when a user unlocks their node
pub async fn get_macaroon(&self, node_id: String) -> Result<Option<macaroon::Model>, Error> {
pub async fn get_macaroon(&self, node_id: &str) -> Result<Option<macaroon::Model>, Error> {
Ok(Macaroon::find()
.filter(macaroon::Column::NodeId.eq(node_id))
.one(&self.connection)
Expand Down
6 changes: 6 additions & 0 deletions senseicore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ pub enum Error {
LdkInvoiceParse(lightning_invoice::ParseOrSemanticError),
InvalidSeedLength,
FailedToWriteSeed,
SeedNotFound,
MacaroonNotFound,
Unauthenticated,
InvalidMacaroon,
AdminNodeNotStarted,
AdminNodeNotCreated,
FundingGenerationNeverHappened,
NodeBeingStartedAlready,
}

impl Display for Error {
Expand All @@ -51,11 +54,14 @@ impl Display for Error {
Error::LdkInvoiceSign(e) => e.to_string(),
Error::LdkInvoiceParse(e) => e.to_string(),
Error::InvalidSeedLength => String::from("invalid seed length"),
Error::SeedNotFound => String::from("seed not found for node"),
Error::MacaroonNotFound => String::from("macaroon not found for node"),
Error::FailedToWriteSeed => String::from("failed to write seed"),
Error::Unauthenticated => String::from("unauthenticated"),
Error::InvalidMacaroon => String::from("invalid macaroon"),
Error::AdminNodeNotCreated => String::from("admin node not created"),
Error::AdminNodeNotStarted => String::from("admin node not started"),
Error::NodeBeingStartedAlready => String::from("node already being started"),
Error::FundingGenerationNeverHappened => {
String::from("funding generation for request never happened")
}
Expand Down
25 changes: 25 additions & 0 deletions senseicore/src/network_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,37 @@
use bitcoin::secp256k1::PublicKey;
use lightning::{
ln::msgs::{self, Init, LightningError, RoutingMessageHandler},
routing::network_graph::NetworkGraph,
util::events::{MessageSendEvent, MessageSendEventsProvider},
};
use std::{ops::Deref, sync::Arc};

use crate::node::NetworkGraphMessageHandler;

#[derive(Clone)]
pub struct SenseiNetworkGraph {
pub graph: Option<Arc<NetworkGraph>>,
pub msg_handler: Option<Arc<NetworkGraphMessageHandler>>,
}

impl SenseiNetworkGraph {
pub fn set_graph(&mut self, graph: Arc<NetworkGraph>) {
self.graph = Some(graph);
}

pub fn get_graph(&self) -> Option<Arc<NetworkGraph>> {
self.graph.clone()
}

pub fn set_msg_handler(&mut self, msg_handler: Arc<NetworkGraphMessageHandler>) {
self.msg_handler = Some(msg_handler);
}

pub fn get_msg_handler(&self) -> Option<Arc<NetworkGraphMessageHandler>> {
self.msg_handler.clone()
}
}

pub struct OptionalNetworkGraphMsgHandler {
pub network_graph_msg_handler: Option<Arc<NetworkGraphMessageHandler>>,
}
Expand Down
Loading

0 comments on commit cd74d6f

Please sign in to comment.