Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
feat: regtest nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Feb 3, 2024
1 parent 8257470 commit f2ab3f8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
5 changes: 3 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,7 +22,7 @@ email = "[email protected]"
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 = ""
Expand Down
56 changes: 45 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
'';
};
Expand Down
5 changes: 2 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand All @@ -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)]
Expand Down
29 changes: 19 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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?;

Expand All @@ -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);
}
}
Expand Down

0 comments on commit f2ab3f8

Please sign in to comment.