Skip to content

Commit 12583b6

Browse files
authored
Merge pull request #73 from TheBlueMatt/2018-07-no-uint
Migrate all Uint256s used for channel_ids to [u8; 32]
2 parents 4563238 + e93c9fb commit 12583b6

File tree

7 files changed

+130
-80
lines changed

7 files changed

+130
-80
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use bitcoin::blockdata::script::Script;
99
use bitcoin::network::constants::Network;
1010
use bitcoin::network::serialize::{serialize, BitcoinHash};
1111
use bitcoin::util::hash::Sha256dHash;
12-
use bitcoin::util::uint::Uint256;
1312

1413
use crypto::sha2::Sha256;
1514
use crypto::digest::Digest;
@@ -168,7 +167,7 @@ pub fn do_test(data: &[u8]) {
168167
let mut should_forward = false;
169168
let mut payments_received = Vec::new();
170169
let mut payments_sent = 0;
171-
let mut pending_funding_generation: Vec<(Uint256, u64, Script)> = Vec::new();
170+
let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
172171
let mut pending_funding_signatures = HashMap::new();
173172
let mut pending_funding_relay = Vec::new();
174173

src/chain/transaction.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use bitcoin::util::hash::Sha256dHash;
2-
use bitcoin::util::uint::Uint256;
32

43
/// A reference to a transaction output.
54
/// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize
@@ -19,10 +18,33 @@ impl OutPoint {
1918
}
2019

2120
/// Convert an `OutPoint` to a lightning channel id.
22-
pub fn to_channel_id(&self) -> Uint256 {
23-
let mut index = [0; 32];
24-
index[30] = ((self.index >> 8) & 0xff) as u8;
25-
index[31] = ((self.index >> 0) & 0xff) as u8;
26-
self.txid.into_le() ^ Sha256dHash::from(&index[..]).into_le()
21+
pub fn to_channel_id(&self) -> [u8; 32] {
22+
let mut res = [0; 32];
23+
res[..].copy_from_slice(&self.txid[..]);
24+
res[30] ^= ((self.index >> 8) & 0xff) as u8;
25+
res[31] ^= ((self.index >> 0) & 0xff) as u8;
26+
res
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use chain::transaction::OutPoint;
33+
34+
use bitcoin::blockdata::transaction::Transaction;
35+
use bitcoin::network::serialize;
36+
use bitcoin::util::misc::hex_bytes;
37+
38+
#[test]
39+
fn test_channel_id_calculation() {
40+
let tx: Transaction = serialize::deserialize(&hex_bytes("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
41+
assert_eq!(&OutPoint {
42+
txid: tx.txid(),
43+
index: 0
44+
}.to_channel_id(), &hex_bytes("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
45+
assert_eq!(&OutPoint {
46+
txid: tx.txid(),
47+
index: 1
48+
}.to_channel_id(), &hex_bytes("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
2749
}
2850
}

src/ln/channel.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use bitcoin::blockdata::block::BlockHeader;
22
use bitcoin::blockdata::script::{Script,Builder};
33
use bitcoin::blockdata::transaction::{TxIn, TxOut, Transaction, SigHashType};
44
use bitcoin::blockdata::opcodes;
5-
use bitcoin::util::uint::Uint256;
65
use bitcoin::util::hash::{Sha256dHash, Hash160};
76
use bitcoin::util::bip143;
87
use bitcoin::network::serialize::BitcoinHash;
@@ -236,7 +235,7 @@ const BOTH_SIDES_SHUTDOWN_MASK: u32 = (ChannelState::LocalShutdownSent as u32 |
236235
pub struct Channel {
237236
user_id: u64,
238237

239-
channel_id: Uint256,
238+
channel_id: [u8; 32],
240239
channel_state: u32,
241240
channel_outbound: bool,
242241
secp_ctx: Secp256k1,
@@ -380,7 +379,7 @@ impl Channel {
380379
Channel {
381380
user_id: user_id,
382381

383-
channel_id: rng::rand_uint256(),
382+
channel_id: rng::rand_u832(),
384383
channel_state: ChannelState::OurInitSent as u32,
385384
channel_outbound: true,
386385
secp_ctx: secp_ctx,
@@ -1798,7 +1797,7 @@ impl Channel {
17981797

17991798
// Public utilities:
18001799

1801-
pub fn channel_id(&self) -> Uint256 {
1800+
pub fn channel_id(&self) -> [u8; 32] {
18021801
self.channel_id
18031802
}
18041803

src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use bitcoin::blockdata::constants::genesis_block;
44
use bitcoin::network::constants::Network;
55
use bitcoin::network::serialize::BitcoinHash;
66
use bitcoin::util::hash::Sha256dHash;
7-
use bitcoin::util::uint::Uint256;
87

98
use secp256k1::key::{SecretKey,PublicKey};
109
use secp256k1::{Secp256k1,Message};
@@ -111,16 +110,16 @@ enum PendingOutboundHTLC {
111110
const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u32 = 50;
112111

113112
struct ChannelHolder {
114-
by_id: HashMap<Uint256, Channel>,
115-
short_to_id: HashMap<u64, Uint256>,
113+
by_id: HashMap<[u8; 32], Channel>,
114+
short_to_id: HashMap<u64, [u8; 32]>,
116115
next_forward: Instant,
117116
/// short channel id -> forward infos. Key of 0 means payments received
118117
forward_htlcs: HashMap<u64, Vec<PendingForwardHTLCInfo>>,
119118
claimable_htlcs: HashMap<[u8; 32], PendingOutboundHTLC>,
120119
}
121120
struct MutChannelHolder<'a> {
122-
by_id: &'a mut HashMap<Uint256, Channel>,
123-
short_to_id: &'a mut HashMap<u64, Uint256>,
121+
by_id: &'a mut HashMap<[u8; 32], Channel>,
122+
short_to_id: &'a mut HashMap<u64, [u8; 32]>,
124123
next_forward: &'a mut Instant,
125124
/// short channel id -> forward infos. Key of 0 means payments received
126125
forward_htlcs: &'a mut HashMap<u64, Vec<PendingForwardHTLCInfo>>,
@@ -187,7 +186,7 @@ pub struct ChannelDetails {
187186
/// thereafter this is the txid of the funding transaction xor the funding transaction output).
188187
/// Note that this means this value is *not* persistent - it can change once during the
189188
/// lifetime of the channel.
190-
pub channel_id: Uint256,
189+
pub channel_id: [u8; 32],
191190
/// The position of the funding transaction in the chain. None if the funding transaction has
192191
/// not yet been confirmed and the channel fully opened.
193192
pub short_channel_id: Option<u64>,
@@ -297,7 +296,7 @@ impl ChannelManager {
297296
/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
298297
/// will be accepted on the given channel, and after additional timeout/the closing of all
299298
/// pending HTLCs, the channel will be closed on chain.
300-
pub fn close_channel(&self, channel_id: &Uint256) -> Result<msgs::Shutdown, HandleError> {
299+
pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<msgs::Shutdown, HandleError> {
301300
let (res, chan_option) = {
302301
let mut channel_state_lock = self.channel_state.lock().unwrap();
303302
let channel_state = channel_state_lock.borrow_parts();
@@ -676,10 +675,10 @@ impl ChannelManager {
676675

677676
/// Call this upon creation of a funding transaction for the given channel.
678677
/// Panics if a funding transaction has already been provided for this channel.
679-
pub fn funding_transaction_generated(&self, temporary_channel_id: &Uint256, funding_txo: OutPoint) {
678+
pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
680679
let (chan, msg, chan_monitor) = {
681680
let mut channel_state = self.channel_state.lock().unwrap();
682-
match channel_state.by_id.remove(&temporary_channel_id) {
681+
match channel_state.by_id.remove(temporary_channel_id) {
683682
Some(mut chan) => {
684683
match chan.get_outbound_funding_created(funding_txo) {
685684
Ok(funding_msg) => {
@@ -1838,7 +1837,6 @@ mod tests {
18381837

18391838
use bitcoin::util::misc::hex_bytes;
18401839
use bitcoin::util::hash::Sha256dHash;
1841-
use bitcoin::util::uint::Uint256;
18421840
use bitcoin::blockdata::block::{Block, BlockHeader};
18431841
use bitcoin::blockdata::transaction::{Transaction, TxOut};
18441842
use bitcoin::network::constants::Network;
@@ -2030,7 +2028,7 @@ mod tests {
20302028
}
20312029

20322030
static mut CHAN_COUNT: u32 = 0;
2033-
fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) {
2031+
fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
20342032
node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 42).unwrap();
20352033

20362034
let events_1 = node_a.node.get_and_clear_pending_events();
@@ -2158,7 +2156,7 @@ mod tests {
21582156
((*announcement).clone(), (*as_update).clone(), (*bs_update).clone(), channel_id, tx)
21592157
}
21602158

2161-
fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) {
2159+
fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
21622160
let chan_announcement = create_chan_between_nodes(&nodes[a], &nodes[b]);
21632161
for node in nodes {
21642162
assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
@@ -2168,7 +2166,7 @@ mod tests {
21682166
(chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
21692167
}
21702168

2171-
fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &Uint256, funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) {
2169+
fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) {
21722170
let (node_a, broadcaster_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster) } else { (&outbound_node.node, &outbound_node.tx_broadcaster) };
21732171
let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
21742172
let (tx_a, tx_b);
@@ -2679,7 +2677,7 @@ mod tests {
26792677

26802678
#[derive(PartialEq)]
26812679
enum HTLCType { NONE, TIMEOUT, SUCCESS }
2682-
fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
2680+
fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
26832681
let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
26842682
assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
26852683

0 commit comments

Comments
 (0)