@@ -63,14 +63,15 @@ use std::time::{Instant,Duration};
63
63
mod channel_held_info {
64
64
use ln:: msgs;
65
65
use ln:: router:: Route ;
66
+ use ln:: channelmanager:: PaymentHash ;
66
67
use secp256k1:: key:: SecretKey ;
67
68
68
69
/// Stores the info we will need to send when we want to forward an HTLC onwards
69
70
#[ derive( Clone ) ] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
70
71
pub struct PendingForwardHTLCInfo {
71
72
pub ( super ) onion_packet : Option < msgs:: OnionPacket > ,
72
73
pub ( super ) incoming_shared_secret : [ u8 ; 32 ] ,
73
- pub ( super ) payment_hash : [ u8 ; 32 ] ,
74
+ pub ( super ) payment_hash : PaymentHash ,
74
75
pub ( super ) short_channel_id : u64 ,
75
76
pub ( super ) amt_to_forward : u64 ,
76
77
pub ( super ) outgoing_cltv_value : u32 ,
@@ -133,13 +134,21 @@ mod channel_held_info {
133
134
}
134
135
pub ( super ) use self :: channel_held_info:: * ;
135
136
136
- type ShutdownResult = ( Vec < Transaction > , Vec < ( HTLCSource , [ u8 ; 32 ] ) > ) ;
137
+ /// payment_hash type, use to cross-lock hop
138
+ #[ derive( Hash , Copy , Clone , PartialEq , Eq , Debug ) ]
139
+ pub struct PaymentHash ( pub [ u8 ; 32 ] ) ;
140
+ /// payment_preimage type, use to route payment between hop
141
+ #[ derive( Hash , Copy , Clone , PartialEq , Eq , Debug ) ]
142
+ pub struct PaymentPreimage ( pub [ u8 ; 32 ] ) ;
143
+
144
+ type ShutdownResult = ( Vec < Transaction > , Vec < ( HTLCSource , PaymentHash ) > ) ;
137
145
138
146
/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
139
147
/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
140
148
/// immediately (ie with no further calls on it made). Thus, this step happens inside a
141
149
/// channel_state lock. We then return the set of things that need to be done outside the lock in
142
150
/// this struct and call handle_error!() on it.
151
+
143
152
struct MsgHandleErrInternal {
144
153
err : msgs:: HandleError ,
145
154
shutdown_finish : Option < ( ShutdownResult , Option < msgs:: ChannelUpdate > ) > ,
@@ -248,7 +257,7 @@ struct ChannelHolder {
248
257
/// Note that while this is held in the same mutex as the channels themselves, no consistency
249
258
/// guarantees are made about the channels given here actually existing anymore by the time you
250
259
/// go to read them!
251
- claimable_htlcs : HashMap < [ u8 ; 32 ] , Vec < HTLCPreviousHopData > > ,
260
+ claimable_htlcs : HashMap < PaymentHash , Vec < HTLCPreviousHopData > > ,
252
261
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
253
262
/// for broadcast messages, where ordering isn't as strict).
254
263
pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -258,7 +267,7 @@ struct MutChannelHolder<'a> {
258
267
short_to_id : & ' a mut HashMap < u64 , [ u8 ; 32 ] > ,
259
268
next_forward : & ' a mut Instant ,
260
269
forward_htlcs : & ' a mut HashMap < u64 , Vec < HTLCForwardInfo > > ,
261
- claimable_htlcs : & ' a mut HashMap < [ u8 ; 32 ] , Vec < HTLCPreviousHopData > > ,
270
+ claimable_htlcs : & ' a mut HashMap < PaymentHash , Vec < HTLCPreviousHopData > > ,
262
271
pending_msg_events : & ' a mut Vec < events:: MessageSendEvent > ,
263
272
}
264
273
impl ChannelHolder {
@@ -858,7 +867,7 @@ impl ChannelManager {
858
867
}
859
868
860
869
const ZERO : [ u8 ; 21 * 65 ] = [ 0 ; 21 * 65 ] ;
861
- fn construct_onion_packet ( mut payloads : Vec < msgs:: OnionHopData > , onion_keys : Vec < OnionKeys > , associated_data : & [ u8 ; 32 ] ) -> msgs:: OnionPacket {
870
+ fn construct_onion_packet ( mut payloads : Vec < msgs:: OnionHopData > , onion_keys : Vec < OnionKeys > , associated_data : & PaymentHash ) -> msgs:: OnionPacket {
862
871
let mut buf = Vec :: with_capacity ( 21 * 65 ) ;
863
872
buf. resize ( 21 * 65 , 0 ) ;
864
873
@@ -895,7 +904,7 @@ impl ChannelManager {
895
904
896
905
let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & keys. mu ) ;
897
906
hmac. input ( & packet_data) ;
898
- hmac. input ( & associated_data[ ..] ) ;
907
+ hmac. input ( & associated_data. 0 [ ..] ) ;
899
908
hmac. raw_result ( & mut hmac_res) ;
900
909
}
901
910
@@ -1017,7 +1026,7 @@ impl ChannelManager {
1017
1026
1018
1027
let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & mu) ;
1019
1028
hmac. input ( & msg. onion_routing_packet . hop_data ) ;
1020
- hmac. input ( & msg. payment_hash ) ;
1029
+ hmac. input ( & msg. payment_hash . 0 [ .. ] ) ;
1021
1030
if hmac. result ( ) != MacResult :: new ( & msg. onion_routing_packet . hmac ) {
1022
1031
return_err ! ( "HMAC Check failed" , 0x8000 | 0x4000 | 5 , & get_onion_hash!( ) ) ;
1023
1032
}
@@ -1225,7 +1234,7 @@ impl ChannelManager {
1225
1234
/// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1226
1235
/// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1227
1236
/// the payment via a different route unless you intend to pay twice!
1228
- pub fn send_payment ( & self , route : Route , payment_hash : [ u8 ; 32 ] ) -> Result < ( ) , APIError > {
1237
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash ) -> Result < ( ) , APIError > {
1229
1238
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1230
1239
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1231
1240
}
@@ -1520,7 +1529,7 @@ impl ChannelManager {
1520
1529
}
1521
1530
1522
1531
/// Indicates that the preimage for payment_hash is unknown or the received amount is incorrect after a PaymentReceived event.
1523
- pub fn fail_htlc_backwards ( & self , payment_hash : & [ u8 ; 32 ] , reason : PaymentFailReason ) -> bool {
1532
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , reason : PaymentFailReason ) -> bool {
1524
1533
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1525
1534
1526
1535
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
@@ -1540,7 +1549,7 @@ impl ChannelManager {
1540
1549
/// to fail and take the channel_state lock for each iteration (as we take ownership and may
1541
1550
/// drop it). In other words, no assumptions are made that entries in claimable_htlcs point to
1542
1551
/// still-available channels.
1543
- fn fail_htlc_backwards_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , source : HTLCSource , payment_hash : & [ u8 ; 32 ] , onion_error : HTLCFailReason ) {
1552
+ fn fail_htlc_backwards_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , source : HTLCSource , payment_hash : & PaymentHash , onion_error : HTLCFailReason ) {
1544
1553
match source {
1545
1554
HTLCSource :: OutboundRoute { .. } => {
1546
1555
mem:: drop ( channel_state_lock) ;
@@ -1616,11 +1625,11 @@ impl ChannelManager {
1616
1625
/// should probably kick the net layer to go send messages if this returns true!
1617
1626
///
1618
1627
/// May panic if called except in response to a PaymentReceived event.
1619
- pub fn claim_funds ( & self , payment_preimage : [ u8 ; 32 ] ) -> bool {
1628
+ pub fn claim_funds ( & self , payment_preimage : PaymentPreimage ) -> bool {
1620
1629
let mut sha = Sha256 :: new ( ) ;
1621
- sha. input ( & payment_preimage) ;
1622
- let mut payment_hash = [ 0 ; 32 ] ;
1623
- sha. result ( & mut payment_hash) ;
1630
+ sha. input ( & payment_preimage. 0 [ .. ] ) ;
1631
+ let mut payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
1632
+ sha. result ( & mut payment_hash. 0 [ .. ] ) ;
1624
1633
1625
1634
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1626
1635
@@ -1634,7 +1643,7 @@ impl ChannelManager {
1634
1643
true
1635
1644
} else { false }
1636
1645
}
1637
- fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , source : HTLCSource , payment_preimage : [ u8 ; 32 ] ) {
1646
+ fn claim_funds_internal ( & self , mut channel_state_lock : MutexGuard < ChannelHolder > , source : HTLCSource , payment_preimage : PaymentPreimage ) {
1638
1647
match source {
1639
1648
HTLCSource :: OutboundRoute { .. } => {
1640
1649
mem:: drop ( channel_state_lock) ;
@@ -3353,7 +3362,7 @@ mod tests {
3353
3362
use chain:: keysinterface:: { KeysInterface , SpendableOutputDescriptor } ;
3354
3363
use chain:: keysinterface;
3355
3364
use ln:: channel:: { COMMITMENT_TX_BASE_WEIGHT , COMMITMENT_TX_WEIGHT_PER_HTLC } ;
3356
- use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder } ;
3365
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , OnionKeys , PaymentFailReason , RAACommitmentOrder , PaymentPreimage , PaymentHash } ;
3357
3366
use ln:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS , ManyChannelMonitor } ;
3358
3367
use ln:: channel:: { ACCEPTED_HTLC_SCRIPT_WEIGHT , OFFERED_HTLC_SCRIPT_WEIGHT } ;
3359
3368
use ln:: router:: { Route , RouteHop , Router } ;
@@ -3516,7 +3525,7 @@ mod tests {
3516
3525
} ,
3517
3526
) ;
3518
3527
3519
- let packet = ChannelManager :: construct_onion_packet ( payloads, onion_keys, & [ 0x42 ; 32 ] ) ;
3528
+ let packet = ChannelManager :: construct_onion_packet ( payloads, onion_keys, & PaymentHash ( [ 0x42 ; 32 ] ) ) ;
3520
3529
// Just check the final packet encoding, as it includes all the per-hop vectors in it
3521
3530
// anyway...
3522
3531
assert_eq ! ( packet. encode( ) , hex:: decode( "0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a716a996c7845c93d90e4ecbb9bde4ece2f69425c99e4bc820e44485455f135edc0d10f7d61ab590531cf08000179a333a347f8b4072f216400406bdf3bf038659793d4a1fd7b246979e3150a0a4cb052c9ec69acf0f48c3d39cd55675fe717cb7d80ce721caad69320c3a469a202f1e468c67eaf7a7cd8226d0fd32f7b48084dca885d56047694762b67021713ca673929c163ec36e04e40ca8e1c6d17569419d3039d9a1ec866abe044a9ad635778b961fc0776dc832b3a451bd5d35072d2269cf9b040f6b7a7dad84fb114ed413b1426cb96ceaf83825665ed5a1d002c1687f92465b49ed4c7f0218ff8c6c7dd7221d589c65b3b9aaa71a41484b122846c7c7b57e02e679ea8469b70e14fe4f70fee4d87b910cf144be6fe48eef24da475c0b0bcc6565ae82cd3f4e3b24c76eaa5616c6111343306ab35c1fe5ca4a77c0e314ed7dba39d6f1e0de791719c241a939cc493bea2bae1c1e932679ea94d29084278513c77b899cc98059d06a27d171b0dbdf6bee13ddc4fc17a0c4d2827d488436b57baa167544138ca2e64a11b43ac8a06cd0c2fba2d4d900ed2d9205305e2d7383cc98dacb078133de5f6fb6bed2ef26ba92cea28aafc3b9948dd9ae5559e8bd6920b8cea462aa445ca6a95e0e7ba52961b181c79e73bd581821df2b10173727a810c92b83b5ba4a0403eb710d2ca10689a35bec6c3a708e9e92f7d78ff3c5d9989574b00c6736f84c199256e76e19e78f0c98a9d580b4a658c84fc8f2096c2fbea8f5f8c59d0fdacb3be2802ef802abbecb3aba4acaac69a0e965abd8981e9896b1f6ef9d60f7a164b371af869fd0e48073742825e9434fc54da837e120266d53302954843538ea7c6c3dbfb4ff3b2fdbe244437f2a153ccf7bdb4c92aa08102d4f3cff2ae5ef86fab4653595e6a5837fa2f3e29f27a9cde5966843fb847a4a61f1e76c281fe8bb2b0a181d096100db5a1a5ce7a910238251a43ca556712eaadea167fb4d7d75825e440f3ecd782036d7574df8bceacb397abefc5f5254d2722215c53ff54af8299aaaad642c6d72a14d27882d9bbd539e1cc7a527526ba89b8c037ad09120e98ab042d3e8652b31ae0e478516bfaf88efca9f3676ffe99d2819dcaeb7610a626695f53117665d267d3f7abebd6bbd6733f645c72c389f03855bdf1e4b8075b516569b118233a0f0971d24b83113c0b096f5216a207ca99a7cddc81c130923fe3d91e7508c9ac5f2e914ff5dccab9e558566fa14efb34ac98d878580814b94b73acbfde9072f30b881f7f0fff42d4045d1ace6322d86a97d164aa84d93a60498065cc7c20e636f5862dc81531a88c60305a2e59a985be327a6902e4bed986dbf4a0b50c217af0ea7fdf9ab37f9ea1a1aaa72f54cf40154ea9b269f1a7c09f9f43245109431a175d50e2db0132337baa0ef97eed0fcf20489da36b79a1172faccc2f7ded7c60e00694282d93359c4682135642bc81f433574aa8ef0c97b4ade7ca372c5ffc23c7eddd839bab4e0f14d6df15c9dbeab176bec8b5701cf054eb3072f6dadc98f88819042bf10c407516ee58bce33fbe3b3d86a54255e577db4598e30a135361528c101683a5fcde7e8ba53f3456254be8f45fe3a56120ae96ea3773631fcb3873aa3abd91bcff00bd38bd43697a2e789e00da6077482e7b1b1a677b5afae4c54e6cbdf7377b694eb7d7a5b913476a5be923322d3de06060fd5e819635232a2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780df54bfdb0dd5cd9855179f602f9172307c7268724c3618e6817abd793adc214a0dc0bc616816632f27ea336fb56dfd" ) . unwrap( ) ) ;
@@ -4020,18 +4029,18 @@ mod tests {
4020
4029
macro_rules! get_payment_preimage_hash {
4021
4030
( $node: expr) => {
4022
4031
{
4023
- let payment_preimage = [ * $node. network_payment_count. borrow( ) ; 32 ] ;
4032
+ let payment_preimage = PaymentPreimage ( [ * $node. network_payment_count. borrow( ) ; 32 ] ) ;
4024
4033
* $node. network_payment_count. borrow_mut( ) += 1 ;
4025
- let mut payment_hash = [ 0 ; 32 ] ;
4034
+ let mut payment_hash = PaymentHash ( [ 0 ; 32 ] ) ;
4026
4035
let mut sha = Sha256 :: new( ) ;
4027
- sha. input( & payment_preimage[ ..] ) ;
4028
- sha. result( & mut payment_hash) ;
4036
+ sha. input( & payment_preimage. 0 [ ..] ) ;
4037
+ sha. result( & mut payment_hash. 0 [ .. ] ) ;
4029
4038
( payment_preimage, payment_hash)
4030
4039
}
4031
4040
}
4032
4041
}
4033
4042
4034
- fn send_along_route ( origin_node : & Node , route : Route , expected_route : & [ & Node ] , recv_value : u64 ) -> ( [ u8 ; 32 ] , [ u8 ; 32 ] ) {
4043
+ fn send_along_route ( origin_node : & Node , route : Route , expected_route : & [ & Node ] , recv_value : u64 ) -> ( PaymentPreimage , PaymentHash ) {
4035
4044
let ( our_payment_preimage, our_payment_hash) = get_payment_preimage_hash ! ( origin_node) ;
4036
4045
4037
4046
let mut payment_event = {
@@ -4085,7 +4094,7 @@ mod tests {
4085
4094
( our_payment_preimage, our_payment_hash)
4086
4095
}
4087
4096
4088
- fn claim_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_preimage : [ u8 ; 32 ] ) {
4097
+ fn claim_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_preimage : PaymentPreimage ) {
4089
4098
assert ! ( expected_route. last( ) . unwrap( ) . node. claim_funds( our_payment_preimage) ) ;
4090
4099
check_added_monitors ! ( expected_route. last( ) . unwrap( ) , 1 ) ;
4091
4100
@@ -4170,13 +4179,13 @@ mod tests {
4170
4179
}
4171
4180
}
4172
4181
4173
- fn claim_payment ( origin_node : & Node , expected_route : & [ & Node ] , our_payment_preimage : [ u8 ; 32 ] ) {
4182
+ fn claim_payment ( origin_node : & Node , expected_route : & [ & Node ] , our_payment_preimage : PaymentPreimage ) {
4174
4183
claim_payment_along_route ( origin_node, expected_route, false , our_payment_preimage) ;
4175
4184
}
4176
4185
4177
4186
const TEST_FINAL_CLTV : u32 = 32 ;
4178
4187
4179
- fn route_payment ( origin_node : & Node , expected_route : & [ & Node ] , recv_value : u64 ) -> ( [ u8 ; 32 ] , [ u8 ; 32 ] ) {
4188
+ fn route_payment ( origin_node : & Node , expected_route : & [ & Node ] , recv_value : u64 ) -> ( PaymentPreimage , PaymentHash ) {
4180
4189
let route = origin_node. router . get_route ( & expected_route. last ( ) . unwrap ( ) . node . get_our_node_id ( ) , None , & Vec :: new ( ) , recv_value, TEST_FINAL_CLTV ) . unwrap ( ) ;
4181
4190
assert_eq ! ( route. hops. len( ) , expected_route. len( ) ) ;
4182
4191
for ( node, hop) in expected_route. iter ( ) . zip ( route. hops . iter ( ) ) {
@@ -4207,7 +4216,7 @@ mod tests {
4207
4216
claim_payment ( & origin, expected_route, our_payment_preimage) ;
4208
4217
}
4209
4218
4210
- fn fail_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_hash : [ u8 ; 32 ] ) {
4219
+ fn fail_payment_along_route ( origin_node : & Node , expected_route : & [ & Node ] , skip_last : bool , our_payment_hash : PaymentHash ) {
4211
4220
assert ! ( expected_route. last( ) . unwrap( ) . node. fail_htlc_backwards( & our_payment_hash, PaymentFailReason :: PreimageUnknown ) ) ;
4212
4221
check_added_monitors ! ( expected_route. last( ) . unwrap( ) , 1 ) ;
4213
4222
@@ -4272,7 +4281,7 @@ mod tests {
4272
4281
}
4273
4282
}
4274
4283
4275
- fn fail_payment ( origin_node : & Node , expected_route : & [ & Node ] , our_payment_hash : [ u8 ; 32 ] ) {
4284
+ fn fail_payment ( origin_node : & Node , expected_route : & [ & Node ] , our_payment_hash : PaymentHash ) {
4276
4285
fail_payment_along_route ( origin_node, expected_route, false , our_payment_hash) ;
4277
4286
}
4278
4287
@@ -6641,7 +6650,7 @@ mod tests {
6641
6650
assert_eq ! ( events. len( ) , 1 ) ;
6642
6651
match events[ 0 ] {
6643
6652
Event :: PaymentFailed { ref payment_hash, .. } => {
6644
- assert ! ( failed_htlcs. insert( * payment_hash) ) ;
6653
+ assert ! ( failed_htlcs. insert( payment_hash. 0 ) ) ;
6645
6654
} ,
6646
6655
_ => panic ! ( "Unexpected event" ) ,
6647
6656
}
@@ -6672,20 +6681,20 @@ mod tests {
6672
6681
assert_eq ! ( events. len( ) , 2 ) ;
6673
6682
match events[ 0 ] {
6674
6683
Event :: PaymentFailed { ref payment_hash, .. } => {
6675
- assert ! ( failed_htlcs. insert( * payment_hash) ) ;
6684
+ assert ! ( failed_htlcs. insert( payment_hash. 0 ) ) ;
6676
6685
} ,
6677
6686
_ => panic ! ( "Unexpected event" ) ,
6678
6687
}
6679
6688
match events[ 1 ] {
6680
6689
Event :: PaymentFailed { ref payment_hash, .. } => {
6681
- assert ! ( failed_htlcs. insert( * payment_hash) ) ;
6690
+ assert ! ( failed_htlcs. insert( payment_hash. 0 ) ) ;
6682
6691
} ,
6683
6692
_ => panic ! ( "Unexpected event" ) ,
6684
6693
}
6685
6694
6686
- assert ! ( failed_htlcs. contains( & first_payment_hash) ) ;
6687
- assert ! ( failed_htlcs. contains( & second_payment_hash) ) ;
6688
- assert ! ( failed_htlcs. contains( & third_payment_hash) ) ;
6695
+ assert ! ( failed_htlcs. contains( & first_payment_hash. 0 ) ) ;
6696
+ assert ! ( failed_htlcs. contains( & second_payment_hash. 0 ) ) ;
6697
+ assert ! ( failed_htlcs. contains( & third_payment_hash. 0 ) ) ;
6689
6698
}
6690
6699
6691
6700
#[ test]
0 commit comments