Skip to content

Commit 2986251

Browse files
committed
Pump up some deps
Needed to pump the lightning version used and had to pump bitcoin, bitcoincore-rpc aswell to match dep verions in lightining merkle root computation changed in `bitcoin`, thus some methods in the test_utils needed to adapt (basically by adding a tx if there is none in a block). See: rust-bitcoin/rust-bitcoin@b454cf8 Also SecretKey had it's `to_string` method removed, so TEOS now encodes its tower key using `secret_bytes()` and `from_slice()`. See: rust-bitcoin/rust-secp256k1#312
1 parent 3175aca commit 2986251

File tree

12 files changed

+85
-52
lines changed

12 files changed

+85
-52
lines changed

teos-common/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ rand = "0.8.4"
2020
chacha20poly1305 = "0.8.0"
2121

2222
# Bitcoin and Lightning
23-
bitcoin = { version = "0.27", features = [ "use-serde" ] }
24-
lightning = "0.0.105"
23+
bitcoin = { version = "0.28.0", features = [ "use-serde" ] }
24+
lightning = "0.0.108"
2525

2626
[build-dependencies]
27-
tonic-build = "0.6"
27+
tonic-build = "0.6"

teos-common/src/appointment.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Logic related to appointments shared between users and the towers.
22
3-
use hex;
43
use serde::{Deserialize, Serialize};
5-
64
use std::array::TryFromSliceError;
75
use std::{convert::TryInto, fmt};
86

teos/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ warp = "0.3.2"
3434
torut = "0.2.1"
3535

3636
# Bitcoin and Lightning
37-
bitcoin = { version = "0.27", features = [ "base64" ] }
38-
bitcoincore-rpc = "0.14.0"
39-
lightning = "0.0.105"
40-
lightning-net-tokio = "0.0.105"
41-
lightning-block-sync = { version = "0.0.105", features = [ "rpc-client" ] }
37+
bitcoin = { version = "0.28.0", features = [ "base64" ] }
38+
bitcoincore-rpc = "0.15.0"
39+
lightning = "0.0.108"#{ git = "https://github.com/meryacine/rust-lightning", branch = "expose-tlv-macros" }
40+
lightning-net-tokio = "0.0.108"
41+
lightning-block-sync = { version = "0.0.108", features = [ "rpc-client" ] }
4242

4343
# Local
4444
teos-common = { path = "../teos-common" }

teos/src/bitcoin_cli.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,28 @@ pub struct BitcoindClient<'a> {
3838
impl BlockSource for &BitcoindClient<'_> {
3939
/// Gets a block header given its hash.
4040
fn get_header<'a>(
41-
&'a mut self,
41+
&'a self,
4242
header_hash: &'a BlockHash,
4343
height_hint: Option<u32>,
4444
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
4545
Box::pin(async move {
46-
let mut rpc = self.bitcoind_rpc_client.lock().await;
46+
let rpc = self.bitcoind_rpc_client.lock().await;
4747
rpc.get_header(header_hash, height_hint).await
4848
})
4949
}
5050

5151
/// Gets a block given its hash.
52-
fn get_block<'a>(
53-
&'a mut self,
54-
header_hash: &'a BlockHash,
55-
) -> AsyncBlockSourceResult<'a, Block> {
52+
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
5653
Box::pin(async move {
57-
let mut rpc = self.bitcoind_rpc_client.lock().await;
54+
let rpc = self.bitcoind_rpc_client.lock().await;
5855
rpc.get_block(header_hash).await
5956
})
6057
}
6158

6259
/// Get the best block known by our node.
63-
fn get_best_block(&mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
60+
fn get_best_block(&self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
6461
Box::pin(async move {
65-
let mut rpc = self.bitcoind_rpc_client.lock().await;
62+
let rpc = self.bitcoind_rpc_client.lock().await;
6663
rpc.get_best_block().await
6764
})
6865
}
@@ -108,14 +105,14 @@ impl<'a> BitcoindClient<'a> {
108105
pub async fn get_best_block_hash_and_height(
109106
&self,
110107
) -> Result<(BlockHash, Option<u32>), std::io::Error> {
111-
let mut rpc = self.bitcoind_rpc_client.lock().await;
108+
let rpc = self.bitcoind_rpc_client.lock().await;
112109
rpc.call_method::<(BlockHash, Option<u32>)>("getblockchaininfo", &[])
113110
.await
114111
}
115112

116113
/// Sends a transaction to the network.
117114
pub async fn send_raw_transaction(&self, raw_tx: &Transaction) -> Result<Txid, std::io::Error> {
118-
let mut rpc = self.bitcoind_rpc_client.lock().await;
115+
let rpc = self.bitcoind_rpc_client.lock().await;
119116

120117
let raw_tx_json = serde_json::json!(raw_tx.encode().to_hex());
121118
rpc.call_method::<Txid>("sendrawtransaction", &[raw_tx_json])
@@ -124,7 +121,7 @@ impl<'a> BitcoindClient<'a> {
124121

125122
/// Gets a transaction given its id.
126123
pub async fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction, std::io::Error> {
127-
let mut rpc = self.bitcoind_rpc_client.lock().await;
124+
let rpc = self.bitcoind_rpc_client.lock().await;
128125

129126
let txid_hex = serde_json::json!(txid.encode().to_hex());
130127
rpc.call_method::<Transaction>("getrawtransaction", &[txid_hex])

teos/src/chain_monitor.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ where
2222
P: Poll,
2323
C: Cache,
2424
L: Deref,
25-
L::Target: chain::Listen,
25+
<L as Deref>::Target: chain::Listen,
2626
{
2727
/// A bitcoin client to poll best tips from.
2828
spv_client: SpvClient<'a, P, C, L>,
@@ -43,7 +43,7 @@ where
4343
P: Poll,
4444
C: Cache,
4545
L: Deref,
46-
L::Target: chain::Listen,
46+
<L as Deref>::Target: chain::Listen,
4747
{
4848
/// Creates a new [ChainMonitor] instance.
4949
pub async fn new(
@@ -167,6 +167,10 @@ mod tests {
167167
.borrow_mut()
168168
.insert(header.block_hash());
169169
}
170+
171+
fn filtered_block_connected(&self, header: &bitcoin::BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
172+
173+
}
170174
}
171175

172176
#[tokio::test]

teos/src/dbm.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use std::collections::{HashMap, HashSet};
55
use std::iter::FromIterator;
66
use std::path::PathBuf;
7-
use std::str::FromStr;
87

98
use rusqlite::limits::Limit;
109
use rusqlite::{params, params_from_iter, Connection, Error as SqliteError};
@@ -533,7 +532,8 @@ impl DBM {
533532
/// When a new key is generated, old keys are not overwritten but are not retrievable from the API either.
534533
pub fn store_tower_key(&self, sk: &SecretKey) -> Result<(), Error> {
535534
let query = "INSERT INTO keys (key) VALUES (?)";
536-
self.store_data(query, params![sk.to_string()])
535+
let sk_bytes = Vec::from(sk.secret_bytes());
536+
self.store_data(query, params![sk_bytes])
537537
}
538538

539539
/// Loads the last known tower secret key from the database.
@@ -549,8 +549,8 @@ impl DBM {
549549
.unwrap();
550550

551551
stmt.query_row(["keys"], |row| {
552-
let sk: String = row.get(0).unwrap();
553-
Ok(SecretKey::from_str(&sk).unwrap())
552+
let sk: Vec<u8> = row.get(0).unwrap();
553+
Ok(SecretKey::from_slice(&sk).unwrap())
554554
})
555555
.map_err(|_| Error::NotFound)
556556
}
@@ -561,7 +561,7 @@ mod tests {
561561
use super::*;
562562
use std::iter::FromIterator;
563563

564-
use teos_common::cryptography::get_random_bytes;
564+
use teos_common::cryptography::{get_random_bytes, get_random_keypair};
565565
use teos_common::test_utils::get_random_user_id;
566566

567567
use crate::test_utils::{
@@ -1201,4 +1201,16 @@ mod tests {
12011201

12021202
assert!(matches!(dbm.load_last_known_block(), Err(Error::NotFound)));
12031203
}
1204+
1205+
#[test]
1206+
fn test_store_load_tower_key() {
1207+
let dbm = DBM::in_memory().unwrap();
1208+
1209+
assert!(matches!(dbm.load_tower_key(), Err(Error::NotFound)));
1210+
for _ in 0..7 {
1211+
let sk = get_random_keypair().0;
1212+
dbm.store_tower_key(&sk).unwrap();
1213+
assert_eq!(dbm.load_tower_key().unwrap(), sk);
1214+
}
1215+
}
12041216
}

teos/src/gatekeeper.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ impl chain::Listen for Gatekeeper {
328328
self.last_known_block_height
329329
.store(height - 1, Ordering::Release);
330330
}
331+
332+
fn filtered_block_connected(&self, header: &bitcoin::BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
333+
334+
}
331335
}
332336

333337
#[cfg(test)]

teos/src/responder.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,7 @@ impl chain::Listen for Responder {
499499
if self.trackers.lock().unwrap().len() > 0 {
500500
// Complete those appointments that are due at this height
501501
let completed_trackers = self.check_confirmations(
502-
&block
503-
.txdata
504-
.iter()
505-
.map(|tx| tx.txid())
506-
.collect::<Vec<Txid>>(),
502+
&txdata.iter().map(|(_, tx)| tx.txid()).collect::<Vec<_>>(),
507503
height,
508504
);
509505
let trackers_to_delete_gk = completed_trackers
@@ -560,6 +556,10 @@ impl chain::Listen for Responder {
560556
}
561557
}
562558
}
559+
560+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
561+
562+
}
563563
}
564564

565565
#[cfg(test)]

teos/src/test_utils.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use bitcoin::hashes::Hash;
2828
use bitcoin::network::constants::Network;
2929
use bitcoin::util::hash::bitcoin_merkle_root;
3030
use bitcoin::util::uint::Uint256;
31+
use bitcoin::Witness;
3132
use lightning_block_sync::poll::{
3233
ChainPoller, Poll, Validate, ValidatedBlock, ValidatedBlockHeader,
3334
};
@@ -82,16 +83,19 @@ impl Blockchain {
8283
let prev_block = &self.blocks[i - 1];
8384
let prev_blockhash = prev_block.block_hash();
8485
let time = prev_block.header.time + height as u32;
86+
let txdata = vec![get_random_tx()];
87+
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
88+
8589
self.blocks.push(Block {
8690
header: BlockHeader {
8791
version: 0,
8892
prev_blockhash,
89-
merkle_root: Default::default(),
93+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
9094
time,
9195
bits,
9296
nonce: 0,
9397
},
94-
txdata: vec![],
98+
txdata,
9599
});
96100
}
97101
self
@@ -196,14 +200,15 @@ impl Blockchain {
196200
let prev_blockhash = prev_block.block_hash();
197201
let time = prev_block.header.time + (self.blocks.len() + 1) as u32;
198202
let txdata = match txs {
203+
None => vec![get_random_tx()],
204+
Some(v) if v.is_empty() => vec![get_random_tx()],
199205
Some(t) => t,
200-
None => vec![],
201206
};
202207
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
203208
let mut header = BlockHeader {
204209
version: 0,
205210
prev_blockhash,
206-
merkle_root: bitcoin_merkle_root(hashes).into(),
211+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
207212
time,
208213
bits,
209214
nonce: 0,
@@ -222,7 +227,7 @@ impl Blockchain {
222227

223228
impl BlockSource for Blockchain {
224229
fn get_header<'a>(
225-
&'a mut self,
230+
&'a self,
226231
header_hash: &'a BlockHash,
227232
_height_hint: Option<u32>,
228233
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
@@ -245,10 +250,7 @@ impl BlockSource for Blockchain {
245250
})
246251
}
247252

248-
fn get_block<'a>(
249-
&'a mut self,
250-
header_hash: &'a BlockHash,
251-
) -> AsyncBlockSourceResult<'a, Block> {
253+
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
252254
Box::pin(async move {
253255
for (height, block) in self.blocks.iter().enumerate() {
254256
if block.header.block_hash() == *header_hash {
@@ -265,7 +267,7 @@ impl BlockSource for Blockchain {
265267
})
266268
}
267269

268-
fn get_best_block(&mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
270+
fn get_best_block(&self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
269271
Box::pin(async move {
270272
if *self.unreachable.lock().unwrap() {
271273
return Err(BlockSourceError::transient("Connection refused"));
@@ -300,7 +302,7 @@ pub(crate) fn get_random_tx() -> Transaction {
300302
rng.gen_range(0..200),
301303
),
302304
script_sig: Script::new(),
303-
witness: Vec::new(),
305+
witness: Witness::new(),
304306
sequence: 0,
305307
}],
306308
output: vec![TxOut {
@@ -356,7 +358,7 @@ pub(crate) fn store_appointment_and_fks_to_db(
356358

357359
pub(crate) async fn get_last_n_blocks(chain: &mut Blockchain, n: usize) -> Vec<ValidatedBlock> {
358360
let tip = chain.tip();
359-
let mut poller = ChainPoller::new(chain, Network::Bitcoin);
361+
let poller = ChainPoller::new(chain, Network::Bitcoin);
360362

361363
let mut last_n_blocks = Vec::new();
362364
let mut last_known_block = tip;

teos/src/watcher.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,10 @@ impl chain::Listen for Watcher {
877877
self.last_known_block_height
878878
.store(height - 1, Ordering::Release);
879879
}
880+
881+
fn filtered_block_connected(&self, header: &BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
882+
883+
}
880884
}
881885

882886
#[cfg(test)]

0 commit comments

Comments
 (0)