Skip to content

Commit b5fe75a

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 `display_secret().to_string()`. See: rust-bitcoin/rust-secp256k1#312
1 parent 66a1eef commit b5fe75a

File tree

12 files changed

+85
-40
lines changed

12 files changed

+85
-40
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ impl DBM {
542542
/// When a new key is generated, old keys are not overwritten but are not retrievable from the API either.
543543
pub fn store_tower_key(&self, sk: &SecretKey) -> Result<(), Error> {
544544
let query = "INSERT INTO keys (key) VALUES (?)";
545-
self.store_data(query, params![sk.to_string()])
545+
self.store_data(query, params![sk.display_secret().to_string()])
546546
}
547547

548548
/// Loads the last known tower secret key from the database.
@@ -570,7 +570,7 @@ mod tests {
570570
use super::*;
571571
use std::iter::FromIterator;
572572

573-
use teos_common::cryptography::get_random_bytes;
573+
use teos_common::cryptography::{get_random_bytes, get_random_keypair};
574574
use teos_common::test_utils::get_random_user_id;
575575

576576
use crate::test_utils::{
@@ -1237,4 +1237,16 @@ mod tests {
12371237

12381238
assert!(matches!(dbm.load_last_known_block(), Err(Error::NotFound)));
12391239
}
1240+
1241+
#[test]
1242+
fn test_store_load_tower_key() {
1243+
let dbm = DBM::in_memory().unwrap();
1244+
1245+
assert!(matches!(dbm.load_tower_key(), Err(Error::NotFound)));
1246+
for _ in 0..7 {
1247+
let sk = get_random_keypair().0;
1248+
dbm.store_tower_key(&sk).unwrap();
1249+
assert_eq!(dbm.load_tower_key().unwrap(), sk);
1250+
}
1251+
}
12401252
}

teos/src/gatekeeper.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ impl chain::Listen for Gatekeeper {
335335
self.last_known_block_height
336336
.store(height - 1, Ordering::Release);
337337
}
338+
339+
fn filtered_block_connected(&self, header: &bitcoin::BlockHeader, txdata: &chain::transaction::TransactionData, height: u32) {
340+
341+
}
338342
}
339343

340344
#[cfg(test)]

teos/src/responder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,14 @@ impl chain::Listen for Responder {
560560
}
561561
}
562562
}
563+
564+
fn filtered_block_connected(
565+
&self,
566+
header: &BlockHeader,
567+
txdata: &chain::transaction::TransactionData,
568+
height: u32,
569+
) {
570+
}
563571
}
564572

565573
#[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
};
@@ -86,16 +87,18 @@ impl Blockchain {
8687
let prev_block = &self.blocks[i - 1];
8788
let prev_blockhash = prev_block.block_hash();
8889
let time = prev_block.header.time + height as u32;
90+
let txdata = vec![get_random_tx()];
91+
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
8992
self.blocks.push(Block {
9093
header: BlockHeader {
9194
version: 0,
9295
prev_blockhash,
93-
merkle_root: Default::default(),
96+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
9497
time,
9598
bits,
9699
nonce: 0,
97100
},
98-
txdata: vec![],
101+
txdata,
99102
});
100103
}
101104
self
@@ -200,14 +203,20 @@ impl Blockchain {
200203
let prev_blockhash = prev_block.block_hash();
201204
let time = prev_block.header.time + (self.blocks.len() + 1) as u32;
202205
let txdata = match txs {
203-
Some(t) => t,
204-
None => vec![],
206+
Some(v) => {
207+
if v.is_empty() {
208+
vec![get_random_tx()]
209+
} else {
210+
v
211+
}
212+
}
213+
None => vec![get_random_tx()],
205214
};
206215
let hashes = txdata.iter().map(|obj| obj.txid().as_hash());
207216
let mut header = BlockHeader {
208217
version: 0,
209218
prev_blockhash,
210-
merkle_root: bitcoin_merkle_root(hashes).into(),
219+
merkle_root: bitcoin_merkle_root(hashes).unwrap().into(),
211220
time,
212221
bits,
213222
nonce: 0,
@@ -226,7 +235,7 @@ impl Blockchain {
226235

227236
impl BlockSource for Blockchain {
228237
fn get_header<'a>(
229-
&'a mut self,
238+
&'a self,
230239
header_hash: &'a BlockHash,
231240
_height_hint: Option<u32>,
232241
) -> AsyncBlockSourceResult<'a, BlockHeaderData> {
@@ -249,10 +258,7 @@ impl BlockSource for Blockchain {
249258
})
250259
}
251260

252-
fn get_block<'a>(
253-
&'a mut self,
254-
header_hash: &'a BlockHash,
255-
) -> AsyncBlockSourceResult<'a, Block> {
261+
fn get_block<'a>(&'a self, header_hash: &'a BlockHash) -> AsyncBlockSourceResult<'a, Block> {
256262
Box::pin(async move {
257263
for (height, block) in self.blocks.iter().enumerate() {
258264
if block.header.block_hash() == *header_hash {
@@ -269,7 +275,7 @@ impl BlockSource for Blockchain {
269275
})
270276
}
271277

272-
fn get_best_block(&mut self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
278+
fn get_best_block(&self) -> AsyncBlockSourceResult<(BlockHash, Option<u32>)> {
273279
Box::pin(async move {
274280
if *self.unreachable.lock().unwrap() {
275281
return Err(BlockSourceError::transient("Connection refused"));
@@ -304,7 +310,7 @@ pub(crate) fn get_random_tx() -> Transaction {
304310
rng.gen_range(0..200),
305311
),
306312
script_sig: Script::new(),
307-
witness: Vec::new(),
313+
witness: Witness::new(),
308314
sequence: 0,
309315
}],
310316
output: vec![TxOut {
@@ -363,7 +369,7 @@ pub(crate) fn store_appointment_and_fks_to_db(
363369

364370
pub(crate) async fn get_last_n_blocks(chain: &mut Blockchain, n: usize) -> Vec<ValidatedBlock> {
365371
let tip = chain.tip();
366-
let mut poller = ChainPoller::new(chain, Network::Bitcoin);
372+
let poller = ChainPoller::new(chain, Network::Bitcoin);
367373

368374
let mut last_n_blocks = Vec::new();
369375
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl DBM {
118118
/// When a new key is generated, old keys are not overwritten but are not retrievable from the API either.
119119
pub fn store_client_key(&self, sk: &SecretKey) -> Result<(), Error> {
120120
let query = "INSERT INTO keys (key) VALUES (?)";
121-
self.store_data(query, params![sk.to_string()])
121+
self.store_data(query, params![sk.display_secret().to_string()])
122122
}
123123

124124
/// Loads the last known client secret key from the database.
@@ -132,7 +132,6 @@ impl DBM {
132132
"SELECT key FROM keys WHERE id = (SELECT seq FROM sqlite_sequence WHERE name=(?))",
133133
)
134134
.unwrap();
135-
136135
stmt.query_row(["keys"], |row| {
137136
let sk: String = row.get(0).unwrap();
138137
Ok(SecretKey::from_str(&sk).unwrap())
@@ -600,6 +599,7 @@ impl DBM {
600599
mod tests {
601600
use super::*;
602601

602+
use teos_common::cryptography::get_random_keypair;
603603
use teos_common::test_utils::{
604604
generate_random_appointment, get_random_registration_receipt, get_random_user_id,
605605
get_registration_receipt_from_previous,
@@ -1177,4 +1177,16 @@ mod tests {
11771177
let dbm = DBM::in_memory().unwrap();
11781178
assert!(!dbm.exists_misbehaving_proof(get_random_user_id()));
11791179
}
1180+
1181+
#[test]
1182+
fn test_store_load_client_key() {
1183+
let dbm = DBM::in_memory().unwrap();
1184+
1185+
assert!(matches!(dbm.load_client_key(), Err(Error::NotFound)));
1186+
for _ in 0..7 {
1187+
let sk = get_random_keypair().0;
1188+
dbm.store_client_key(&sk).unwrap();
1189+
assert_eq!(dbm.load_client_key().unwrap(), sk);
1190+
}
1191+
}
11801192
}

0 commit comments

Comments
 (0)