use bitcoin::network::constants::Network;
use bitcoin::network::serialize::BitcoinHash;
use bitcoin::util::hash::Sha256dHash;
-use bitcoin::util::uint::Uint256;
use secp256k1::key::{SecretKey,PublicKey};
use secp256k1::{Secp256k1,Message};
const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u32 = 50;
struct ChannelHolder {
- by_id: HashMap<Uint256, Channel>,
- short_to_id: HashMap<u64, Uint256>,
+ by_id: HashMap<[u8; 32], Channel>,
+ short_to_id: HashMap<u64, [u8; 32]>,
next_forward: Instant,
/// short channel id -> forward infos. Key of 0 means payments received
forward_htlcs: HashMap<u64, Vec<PendingForwardHTLCInfo>>,
claimable_htlcs: HashMap<[u8; 32], PendingOutboundHTLC>,
}
struct MutChannelHolder<'a> {
- by_id: &'a mut HashMap<Uint256, Channel>,
- short_to_id: &'a mut HashMap<u64, Uint256>,
+ by_id: &'a mut HashMap<[u8; 32], Channel>,
+ short_to_id: &'a mut HashMap<u64, [u8; 32]>,
next_forward: &'a mut Instant,
/// short channel id -> forward infos. Key of 0 means payments received
forward_htlcs: &'a mut HashMap<u64, Vec<PendingForwardHTLCInfo>>,
/// thereafter this is the txid of the funding transaction xor the funding transaction output).
/// Note that this means this value is *not* persistent - it can change once during the
/// lifetime of the channel.
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
/// The position of the funding transaction in the chain. None if the funding transaction has
/// not yet been confirmed and the channel fully opened.
pub short_channel_id: Option<u64>,
/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
/// will be accepted on the given channel, and after additional timeout/the closing of all
/// pending HTLCs, the channel will be closed on chain.
- pub fn close_channel(&self, channel_id: &Uint256) -> Result<msgs::Shutdown, HandleError> {
+ pub fn close_channel(&self, channel_id: &[u8; 32]) -> Result<msgs::Shutdown, HandleError> {
let (res, chan_option) = {
let mut channel_state_lock = self.channel_state.lock().unwrap();
let channel_state = channel_state_lock.borrow_parts();
/// Call this upon creation of a funding transaction for the given channel.
/// Panics if a funding transaction has already been provided for this channel.
- pub fn funding_transaction_generated(&self, temporary_channel_id: &Uint256, funding_txo: OutPoint) {
+ pub fn funding_transaction_generated(&self, temporary_channel_id: &[u8; 32], funding_txo: OutPoint) {
let (chan, msg, chan_monitor) = {
let mut channel_state = self.channel_state.lock().unwrap();
- match channel_state.by_id.remove(&temporary_channel_id) {
+ match channel_state.by_id.remove(temporary_channel_id) {
Some(mut chan) => {
match chan.get_outbound_funding_created(funding_txo) {
Ok(funding_msg) => {
use bitcoin::util::misc::hex_bytes;
use bitcoin::util::hash::Sha256dHash;
- use bitcoin::util::uint::Uint256;
use bitcoin::blockdata::block::{Block, BlockHeader};
use bitcoin::blockdata::transaction::{Transaction, TxOut};
use bitcoin::network::constants::Network;
}
static mut CHAN_COUNT: u32 = 0;
- fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) {
+ fn create_chan_between_nodes(node_a: &Node, node_b: &Node) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
node_a.node.create_channel(node_b.node.get_our_node_id(), 100000, 42).unwrap();
let events_1 = node_a.node.get_and_clear_pending_events();
((*announcement).clone(), (*as_update).clone(), (*bs_update).clone(), channel_id, tx)
}
- fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction) {
+ fn create_announced_chan_between_nodes(nodes: &Vec<Node>, a: usize, b: usize) -> (msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction) {
let chan_announcement = create_chan_between_nodes(&nodes[a], &nodes[b]);
for node in nodes {
assert!(node.router.handle_channel_announcement(&chan_announcement.0).unwrap());
(chan_announcement.1, chan_announcement.2, chan_announcement.3, chan_announcement.4)
}
- fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &Uint256, funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) {
+ fn close_channel(outbound_node: &Node, inbound_node: &Node, channel_id: &[u8; 32], funding_tx: Transaction, close_inbound_first: bool) -> (msgs::ChannelUpdate, msgs::ChannelUpdate) {
let (node_a, broadcaster_a) = if close_inbound_first { (&inbound_node.node, &inbound_node.tx_broadcaster) } else { (&outbound_node.node, &outbound_node.tx_broadcaster) };
let (node_b, broadcaster_b) = if close_inbound_first { (&outbound_node.node, &outbound_node.tx_broadcaster) } else { (&inbound_node.node, &inbound_node.tx_broadcaster) };
let (tx_a, tx_b);
#[derive(PartialEq)]
enum HTLCType { NONE, TIMEOUT, SUCCESS }
- fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, Uint256, Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
+ fn test_txn_broadcast(node: &Node, chan: &(msgs::ChannelUpdate, msgs::ChannelUpdate, [u8; 32], Transaction), commitment_tx: Option<Transaction>, has_htlc_tx: HTLCType) -> Vec<Transaction> {
let mut node_txn = node.tx_broadcaster.txn_broadcasted.lock().unwrap();
assert!(node_txn.len() >= if commitment_tx.is_some() { 0 } else { 1 } + if has_htlc_tx == HTLCType::NONE { 0 } else { 1 });
use secp256k1::key::PublicKey;
use secp256k1::{Secp256k1, Signature};
-use bitcoin::util::uint::Uint256;
use bitcoin::util::hash::Sha256dHash;
use bitcoin::network::serialize::{deserialize,serialize};
use bitcoin::blockdata::script::Script;
pub struct OpenChannel {
pub chain_hash: Sha256dHash,
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub funding_satoshis: u64,
pub push_msat: u64,
pub dust_limit_satoshis: u64,
}
pub struct AcceptChannel {
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub dust_limit_satoshis: u64,
pub max_htlc_value_in_flight_msat: u64,
pub channel_reserve_satoshis: u64,
}
pub struct FundingCreated {
- pub temporary_channel_id: Uint256,
+ pub temporary_channel_id: [u8; 32],
pub funding_txid: Sha256dHash,
pub funding_output_index: u16,
pub signature: Signature,
}
pub struct FundingSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub signature: Signature,
}
pub struct FundingLocked {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub next_per_commitment_point: PublicKey,
}
pub struct Shutdown {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub scriptpubkey: Script,
}
pub struct ClosingSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub fee_satoshis: u64,
pub signature: Signature,
}
#[derive(Clone)]
pub struct UpdateAddHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub amount_msat: u64,
pub payment_hash: [u8; 32],
#[derive(Clone)]
pub struct UpdateFulfillHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub payment_preimage: [u8; 32],
}
#[derive(Clone)]
pub struct UpdateFailHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub reason: OnionErrorPacket,
}
#[derive(Clone)]
pub struct UpdateFailMalformedHTLC {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub htlc_id: u64,
pub sha256_of_onion: [u8; 32],
pub failure_code: u16,
#[derive(Clone)]
pub struct CommitmentSigned {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub signature: Signature,
pub htlc_signatures: Vec<Signature>,
}
pub struct RevokeAndACK {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub per_commitment_secret: [u8; 32],
pub next_per_commitment_point: PublicKey,
}
pub struct UpdateFee {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub feerate_per_kw: u32,
}
pub struct ChannelReestablish {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub next_local_commitment_number: u64,
pub next_remote_commitment_number: u64,
pub your_last_per_commitment_secret: Option<[u8; 32]>,
#[derive(Clone)]
pub struct AnnouncementSignatures {
- pub channel_id: Uint256,
+ pub channel_id: [u8; 32],
pub short_channel_id: u64,
pub node_signature: Signature,
pub bitcoin_signature: Signature,
return Err(DecodeError::WrongLength);
}
+ let mut temporary_channel_id = [0; 32];
+ temporary_channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ temporary_channel_id,
dust_limit_satoshis: byte_utils::slice_to_be64(&v[32..40]),
max_htlc_value_in_flight_msat: byte_utils::slice_to_be64(&v[40..48]),
channel_reserve_satoshis: byte_utils::slice_to_be64(&v[48..56]),
&Some(ref script) => Vec::with_capacity(270 + 2 + script.len()),
&None => Vec::with_capacity(270),
};
- res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
+ res.extend_from_slice(&self.temporary_channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.dust_limit_satoshis));
res.extend_from_slice(&byte_utils::be64_to_array(self.max_htlc_value_in_flight_msat));
res.extend_from_slice(&byte_utils::be64_to_array(self.channel_reserve_satoshis));
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut temporary_channel_id = [0; 32];
+ temporary_channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- temporary_channel_id: deserialize(&v[0..32]).unwrap(),
+ temporary_channel_id,
funding_txid: deserialize(&v[32..64]).unwrap(),
funding_output_index: byte_utils::slice_to_be16(&v[64..66]),
signature: secp_signature!(&ctx, &v[66..130]),
impl MsgEncodable for FundingCreated {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+32+2+64);
- res.extend_from_slice(&serialize(&self.temporary_channel_id).unwrap()[..]);
+ res.extend_from_slice(&self.temporary_channel_id);
res.extend_from_slice(&serialize(&self.funding_txid).unwrap()[..]);
res.extend_from_slice(&byte_utils::be16_to_array(self.funding_output_index));
let secp_ctx = Secp256k1::without_caps();
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
signature: secp_signature!(&ctx, &v[32..96]),
})
}
impl MsgEncodable for FundingSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.signature.serialize_compact(&Secp256k1::without_caps()));
res
}
return Err(DecodeError::WrongLength);
}
let ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
next_per_commitment_point: secp_pubkey!(&ctx, &v[32..65]),
})
}
impl MsgEncodable for FundingLocked {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+33);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.next_per_commitment_point.serialize());
res
}
if v.len() < 32 + 2 + scriptlen {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
scriptpubkey: Script::from(v[34..34 + scriptlen].to_vec()),
})
}
impl MsgEncodable for Shutdown {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32 + 2 + self.scriptpubkey.len());
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be16_to_array(self.scriptpubkey.len() as u16));
res.extend_from_slice(&self.scriptpubkey[..]);
res
return Err(DecodeError::WrongLength);
}
let secp_ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
fee_satoshis: byte_utils::slice_to_be64(&v[32..40]),
signature: secp_signature!(&secp_ctx, &v[40..104]),
})
impl MsgEncodable for ClosingSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.fee_satoshis));
let secp_ctx = Secp256k1::without_caps();
res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
if v.len() < 32+8+8+32+4+1+33+20*65+32 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut payment_hash = [0; 32];
payment_hash.copy_from_slice(&v[48..80]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
amount_msat: byte_utils::slice_to_be64(&v[40..48]),
payment_hash,
impl MsgEncodable for UpdateAddHTLC {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+8+32+4+1+1366);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&byte_utils::be64_to_array(self.amount_msat));
res.extend_from_slice(&self.payment_hash);
if v.len() < 32+8+32 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut payment_preimage = [0; 32];
payment_preimage.copy_from_slice(&v[40..72]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
payment_preimage,
})
impl MsgEncodable for UpdateFulfillHTLC {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+32);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&self.payment_preimage);
res
if v.len() < 32+8 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
reason: OnionErrorPacket::decode(&v[40..])?,
})
fn encode(&self) -> Vec<u8> {
let reason = self.reason.encode();
let mut res = Vec::with_capacity(32+8+reason.len());
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&reason[..]);
res
if v.len() < 32+8+32+2 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut sha256_of_onion = [0; 32];
sha256_of_onion.copy_from_slice(&v[40..72]);
Ok(Self{
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
htlc_id: byte_utils::slice_to_be64(&v[32..40]),
sha256_of_onion,
failure_code: byte_utils::slice_to_be16(&v[72..74]),
impl MsgEncodable for UpdateFailMalformedHTLC {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+32+2);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.htlc_id));
res.extend_from_slice(&self.sha256_of_onion);
res.extend_from_slice(&byte_utils::be16_to_array(self.failure_code));
if v.len() < 32+64+2 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
+
let htlcs = byte_utils::slice_to_be16(&v[96..98]) as usize;
if v.len() < 32+64+2+htlcs*64 {
return Err(DecodeError::WrongLength);
htlc_signatures.push(secp_signature!(&secp_ctx, &v[98+i*64..98+(i+1)*64]));
}
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
signature: secp_signature!(&secp_ctx, &v[32..96]),
htlc_signatures,
})
impl MsgEncodable for CommitmentSigned {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+64+2+self.htlc_signatures.len()*64);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
let secp_ctx = Secp256k1::without_caps();
res.extend_from_slice(&self.signature.serialize_compact(&secp_ctx));
res.extend_from_slice(&byte_utils::be16_to_array(self.htlc_signatures.len() as u16));
if v.len() < 32+32+33 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
let mut per_commitment_secret = [0; 32];
per_commitment_secret.copy_from_slice(&v[32..64]);
let secp_ctx = Secp256k1::without_caps();
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
per_commitment_secret,
next_per_commitment_point: secp_pubkey!(&secp_ctx, &v[64..97]),
})
impl MsgEncodable for RevokeAndACK {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+32+33);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&self.per_commitment_secret);
res.extend_from_slice(&self.next_per_commitment_point.serialize());
res
if v.len() < 32+4 {
return Err(DecodeError::WrongLength);
}
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
feerate_per_kw: byte_utils::slice_to_be32(&v[32..36]),
})
}
impl MsgEncodable for UpdateFee {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+4);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be32_to_array(self.feerate_per_kw));
res
}
return Err(DecodeError::WrongLength);
}
let secp_ctx = Secp256k1::without_caps();
+ let mut channel_id = [0; 32];
+ channel_id[..].copy_from_slice(&v[0..32]);
Ok(Self {
- channel_id: deserialize(&v[0..32]).unwrap(),
+ channel_id,
short_channel_id: byte_utils::slice_to_be64(&v[32..40]),
node_signature: secp_signature!(&secp_ctx, &v[40..104]),
bitcoin_signature: secp_signature!(&secp_ctx, &v[104..168]),
impl MsgEncodable for AnnouncementSignatures {
fn encode(&self) -> Vec<u8> {
let mut res = Vec::with_capacity(32+8+64*2);
- res.extend_from_slice(&serialize(&self.channel_id).unwrap());
+ res.extend_from_slice(&self.channel_id);
res.extend_from_slice(&byte_utils::be64_to_array(self.short_channel_id));
let secp_ctx = Secp256k1::without_caps();
res.extend_from_slice(&self.node_signature.serialize_compact(&secp_ctx));