Skip to content

Commit 903b9c5

Browse files
committed
Bump up some deps
Needed to bump the lightning version used and had to bump 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 903b9c5

File tree

12 files changed

+86
-51
lines changed

12 files changed

+86
-51
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"
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 16 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,7 @@ 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+
self.store_data(query, params![sk.secret_bytes().to_vec()])
537536
}
538537

539538
/// Loads the last known tower secret key from the database.
@@ -549,8 +548,8 @@ impl DBM {
549548
.unwrap();
550549

551550
stmt.query_row(["keys"], |row| {
552-
let sk: String = row.get(0).unwrap();
553-
Ok(SecretKey::from_str(&sk).unwrap())
551+
let sk: Vec<u8> = row.get(0).unwrap();
552+
Ok(SecretKey::from_slice(&sk).unwrap())
554553
})
555554
.map_err(|_| Error::NotFound)
556555
}
@@ -561,7 +560,7 @@ mod tests {
561560
use super::*;
562561
use std::iter::FromIterator;
563562

564-
use teos_common::cryptography::get_random_bytes;
563+
use teos_common::cryptography::{get_random_bytes, get_random_keypair};
565564
use teos_common::test_utils::get_random_user_id;
566565

567566
use crate::test_utils::{
@@ -1201,4 +1200,16 @@ mod tests {
12011200

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

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: 19 additions & 13 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,18 @@ 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());
8588
self.blocks.push(Block {
8689
header: BlockHeader {
8790
version: 0,
8891
prev_blockhash,
89-
merkle_root: Default::default(),
92+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
9093
time,
9194
bits,
9295
nonce: 0,
9396
},
94-
txdata: vec![],
97+
txdata,
9598
});
9699
}
97100
self
@@ -196,14 +199,20 @@ impl Blockchain {
196199
let prev_blockhash = prev_block.block_hash();
197200
let time = prev_block.header.time + (self.blocks.len() + 1) as u32;
198201
let txdata = match txs {
199-
Some(t) => t,
200-
None => vec![],
202+
Some(v) => {
203+
if v.is_empty() {
204+
vec![get_random_tx()]
205+
} else {
206+
v
207+
}
208+
}
209+
None => vec![get_random_tx()],
201210
};
202211
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
203212
let mut header = BlockHeader {
204213
version: 0,
205214
prev_blockhash,
206-
merkle_root: bitcoin_merkle_root(hashes).into(),
215+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
207216
time,
208217
bits,
209218
nonce: 0,
@@ -222,7 +231,7 @@ impl Blockchain {
222231

223232
impl BlockSource for Blockchain {
224233
fn get_header<'a>(
225-
&'a mut self,
234+
&'a self,
226235
header_hash: &'a BlockHash,
227236
_height_hint: Option<u32>,
228237
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
@@ -245,10 +254,7 @@ impl BlockSource for Blockchain {
245254
})
246255
}
247256

248-
fn get_block<'a>(
249-
&'a mut self,
250-
header_hash: &'a BlockHash,
251-
) -> AsyncBlockSourceResult<'a, Block> {
257+
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
252258
Box::pin(async move {
253259
for (height, block) in self.blocks.iter().enumerate() {
254260
if block.header.block_hash() == *header_hash {
@@ -265,7 +271,7 @@ impl BlockSource for Blockchain {
265271
})
266272
}
267273

268-
fn get_best_block(&mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
274+
fn get_best_block(&self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
269275
Box::pin(async move {
270276
if *self.unreachable.lock().unwrap() {
271277
return Err(BlockSourceError::transient("Connection refused"));
@@ -300,7 +306,7 @@ pub(crate) fn get_random_tx() -> Transaction {
300306
rng.gen_range(0..200),
301307
),
302308
script_sig: Script::new(),
303-
witness: Vec::new(),
309+
witness: Witness::new(),
304310
sequence: 0,
305311
}],
306312
output: vec![TxOut {
@@ -356,7 +362,7 @@ pub(crate) fn store_appointment_and_fks_to_db(
356362

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

361367
let mut last_n_blocks = Vec::new();
362368
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)]

watchtower-plugin/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ tonic = { version = "^0.5", features = [ "tls", "transport" ] }
2525
tokio = { version = "1.5", features = [ "rt-multi-thread", "fs" ] }
2626

2727
# Bitcoin and Lightning
28-
bitcoin = "0.27"
28+
bitcoin = "0.28.0"
2929
cln-plugin = "0.1.0"
3030

3131
# Local

watchtower-plugin/src/dbm.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::{HashMap, HashSet};
22
use std::iter::FromIterator;
33
use std::path::PathBuf;
4-
use std::str::FromStr;
54

65
use rusqlite::{params, Connection, Error as SqliteError};
76

@@ -117,7 +116,7 @@ impl DBM {
117116
/// When a new key is generated, old keys are not overwritten but are not retrievable from the API either.
118117
pub fn store_client_key(&self, sk: &SecretKey) -> Result<(), Error> {
119118
let query = "INSERT INTO keys (key) VALUES (?)";
120-
self.store_data(query, params![sk.to_string()])
119+
self.store_data(query, params![sk.secret_bytes().to_vec()])
121120
}
122121

123122
/// Loads the last known client secret key from the database.
@@ -131,10 +130,9 @@ impl DBM {
131130
"SELECT key FROM keys WHERE id = (SELECT seq FROM sqlite_sequence WHERE name=(?))",
132131
)
133132
.unwrap();
134-
135133
stmt.query_row(["keys"], |row| {
136-
let sk: String = row.get(0).unwrap();
137-
Ok(SecretKey::from_str(&sk).unwrap())
134+
let sk: Vec<u8> = row.get(0).unwrap();
135+
Ok(SecretKey::from_slice(&sk).unwrap())
138136
})
139137
.map_err(|_| Error::NotFound)
140138
}
@@ -535,6 +533,7 @@ impl DBM {
535533
mod tests {
536534
use super::*;
537535

536+
use teos_common::cryptography::get_random_keypair;
538537
use teos_common::test_utils::{
539538
generate_random_appointment, get_random_registration_receipt, get_random_user_id,
540539
};
@@ -1031,4 +1030,16 @@ mod tests {
10311030
let dbm = DBM::in_memory().unwrap();
10321031
assert!(!dbm.exists_misbehaving_proof(get_random_user_id()));
10331032
}
1033+
1034+
#[test]
1035+
fn test_store_load_client_key() {
1036+
let dbm = DBM::in_memory().unwrap();
1037+
1038+
assert!(matches!(dbm.load_client_key(), Err(Error::NotFound)));
1039+
for _ in 0..7 {
1040+
let sk = get_random_keypair().0;
1041+
dbm.store_client_key(&sk).unwrap();
1042+
assert_eq!(dbm.load_client_key().unwrap(), sk);
1043+
}
1044+
}
10341045
}

0 commit comments

Comments
 (0)