From f2ab3f88dea83ee4fb2cc8812e5f00876ffb4fca Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 3 Feb 2024 16:33:54 +0000 Subject: [PATCH] feat: regtest nix flake --- config.toml | 5 +++-- flake.nix | 56 +++++++++++++++++++++++++++++++++++++++++---------- src/config.rs | 5 ++--- src/main.rs | 29 +++++++++++++++++--------- 4 files changed, 69 insertions(+), 26 deletions(-) diff --git a/config.toml b/config.toml index d8d4cbc..1f06825 100644 --- a/config.toml +++ b/config.toml @@ -4,7 +4,8 @@ listen_host = "127.0.0.1" listen_port = 8085 mnemonic = "inner strategy nasty verify camp bind avoid cause glue holiday dress bounce exotic husband hungry winter diet town spray team number hen upper seek" max_order = 32 -db_path = "./mint.redb" +db_path = "/tmp/cashu-rs-mint/mint.redb" +last_pay_path = "/tmp/cashu-rs-mint/last_pay.txt" #[mint_info] #name = "test mint" @@ -21,7 +22,7 @@ email = "me@example.com" ln_backend = "Cln" # CLN -cln_path = "/home/thesimplekid/Documents/Development/cashu-rs-mint/tmp/ln_1/regtest/lightning-rpc" +cln_path = "/tmp/cashu-rs-mint/lighting/ln_1/regtest/lightning-rpc" # Required to start greenlight for the first time # greenlight_invite_code = "" diff --git a/flake.nix b/flake.nix index 24acb7c..4765c4b 100644 --- a/flake.nix +++ b/flake.nix @@ -17,6 +17,10 @@ let flakeboxLib = flakebox.lib.${system} { }; pkgs = import nixpkgs { inherit system; }; + + cashu_rs_mint_dir = "/tmp/cashu-rs-mint"; + bitcoin_dir = cashu_rs_mint_dir + "/bitcoin"; + lightning_dir = cashu_rs_mint_dir + "/lighting"; in { devShells = flakeboxLib.mkShells { @@ -26,19 +30,49 @@ ]; shellHook = '' - bitcoind -daemon -regtest - bitcoin-cli -regtest -rpcport=4000 -rpcpassword=bitcoin -rpcuser=bitcoin createwallet "testwallet" - address=`bitcoin-cli -regtest -rpcport=4000 -rpcpassword=bitcoin -rpcuser=bitcoin getnewaddress` - bitcoin-cli -regtest -rpcport=4000 -rpcpassword=bitcoin -rpcuser=bitcoin generatetoaddress 50 $address - bitcoin-cli -regtest -rpcport=4000 -rpcpassword=bitcoin -rpcuser=bitcoin getblockcount + mkdir -p ${cashu_rs_mint_dir} + mkdir -p ${bitcoin_dir} + mkdir -p ${lightning_dir} + + alias btc="bitcoin-cli -regtest -datadir=${bitcoin_dir}" + alias ln1="lightning-cli --lightning-dir=${lightning_dir}/ln_1 --regtest" + alias ln2="lightning-cli --lightning-dir=${lightning_dir}/ln_2 --regtest" + + + blockcount=$(btc getblockcount) || { blockcount=-1; } + if [[ $blockcount == "-1" ]]; then + echo "Starting bitcoind" + bitcoind -regtest -datadir=${bitcoin_dir} -daemon + sleep 1 + else + echo "bitcoind already started" + fi + + btc loadwallet "test" || btc createwallet "test" || echo "Wallet already loaded" + + address=`btc getnewaddress` + btc generatetoaddress 50 $address + + ln_1_info=$(ln1 getinfo) || { ln_1_info=-1; } + + if [[ $ln_1_info == "-1" ]]; then + echo "Starting ln1" + lightningd --bitcoin-datadir=${bitcoin_dir} --network=regtest --lightning-dir=${lightning_dir}/ln_1 --addr=127.0.0.1:19846 --autolisten=true --log-level=debug --log-file=./debug.log --daemon + sleep 1 + else + echo "ln1 already started" + fi + + ln_2_info=$(ln2 getinfo) || { ln_2_info=-1; } + if [[ $ln_2_info == "-1" ]]; then + echo "Starting ln2" + lightningd --bitcoin-datadir=${bitcoin_dir} --network=regtest --lightning-dir=${lightning_dir}/ln_2 --addr=127.0.0.1:80888 --autolisten=true --log-level=debug --log-file=./debug.log --daemon + sleep 1 + else + echo "ln2 already started" + fi - lightningd --daemon --network=regtest --lightning-dir=tmp/ln_1 --addr=127.0.0.1:19846 --autolisten=true --log-level=debug --log-file=./lig.log - echo "Started first" - lightningd --daemon --network=regtest --lightning-dir=tmp/ln_2 --addr=127.0.0.1:80888 --autolisten=true --log-level=debug --log-file=./lig_2.log - alias btc="bitcoin-cli -regtest -rpcport=4000 -rpcpassword=bitcoin -rpcuser=bitcoin" - alias ln1="lightning-cli --lightning-dir=tmp/ln_1 --regtest" - alias ln2="lightning-cli --lightning-dir=tmp/ln_2 --regtest" ''; }; diff --git a/src/config.rs b/src/config.rs index 538aba5..cc0b172 100644 --- a/src/config.rs +++ b/src/config.rs @@ -29,7 +29,6 @@ THE SOFTWARE. use std::path::PathBuf; use std::str::FromStr; -use cashu_sdk::nuts::MintInfo; use cashu_sdk::Amount; use config::{Config, ConfigError, File}; use serde::{Deserialize, Serialize}; @@ -54,7 +53,7 @@ pub struct Info { } fn path_default() -> PathBuf { - PathBuf::from_str(".").unwrap() + PathBuf::from_str("/tmp/config-rs-mint/cashu-rs-mint.redb").unwrap() } fn derivation_path_default() -> String { @@ -66,7 +65,7 @@ fn max_order_default() -> u8 { } fn last_pay_path() -> String { - "./last_path".to_string() + "/tmp/config-rs-mint/last_path".to_string() } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] diff --git a/src/main.rs b/src/main.rs index ee56333..dee4e2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::collections::HashSet; -use std::fs; +use std::fs::{self, File}; +use std::io::Write; use std::net::{Ipv4Addr, SocketAddr}; use std::path::PathBuf; use std::str::FromStr; @@ -93,19 +94,27 @@ async fn main() -> anyhow::Result<()> { ) .ok_or(anyhow!("cln socket not defined"))?; - let last_pay_path = settings.info.last_pay_path.clone(); + let last_pay_path = PathBuf::from_str(&settings.info.last_pay_path.clone())?; - let last_pay_path = "./last_pay"; - println!("last pay path: {}", last_pay_path); + match fs::metadata(&last_pay_path) { + Ok(_) => (), + Err(_e) => { + // Create the parent directory if it doesn't exist + fs::create_dir_all(&last_pay_path.parent().unwrap())?; - let last_pay = fs::read(last_pay_path)?; + // Attempt to create the file + let mut fs = File::create(&last_pay_path).unwrap(); + fs.write_all(&0_u64.to_be_bytes()).unwrap(); + } + } - let last_pay_index = - u64::from_be_bytes(last_pay.try_into().unwrap_or([0, 0, 0, 0, 0, 0, 0, 0])); + let last_pay = fs::read(&last_pay_path).unwrap(); - println!("last pay index: {}", last_pay_index); + println!("last pay {:?}", last_pay); - println!("cln: {}", cln_socket.display()); + let last_pay_index = + u64::from_be_bytes(last_pay.try_into().unwrap_or([0, 0, 0, 0, 0, 0, 0, 0])); + println!("last pay index {:?}", last_pay_index); let cln = ln_rs::Cln::new(cln_socket, Some(last_pay_index)).await?; @@ -127,7 +136,7 @@ async fn main() -> anyhow::Result<()> { warn!("{:?}", err); } if let Some(pay_index) = pay_index { - if let Err(err) = fs::write(last_pay_path.clone(), pay_index.to_be_bytes()) { + if let Err(err) = fs::write(&last_pay_path, pay_index.to_be_bytes()) { warn!("Could not write last pay index {:?}", err); } }