Skip to content

Commit 9dbce1c

Browse files
authored
Merge pull request #610 from ariard/2020-04-cache-in-monitor
Move back to ChannelMonitor RemoteTxCache
2 parents fd71f8b + 81e358c commit 9dbce1c

11 files changed

+631
-246
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ impl KeysInterface for KeyProvider {
164164
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
165165
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
166166
channel_value_satoshis,
167+
(0, 0),
167168
))
168169
}
169170

fuzz/src/full_stack.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ impl KeysInterface for KeyProvider {
262262
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
263263
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
264264
channel_value_satoshis,
265+
(0, 0),
265266
)
266267
} else {
267268
InMemoryChannelKeys::new(
@@ -273,6 +274,7 @@ impl KeysInterface for KeyProvider {
273274
SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
274275
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
275276
channel_value_satoshis,
277+
(0, 0),
276278
)
277279
})
278280
}

lightning/src/chain/keysinterface.rs

Lines changed: 216 additions & 62 deletions
Large diffs are not rendered by default.

lightning/src/ln/chan_utils.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ impl Readable for CounterpartyCommitmentSecrets {
171171
}
172172
}
173173

174-
/// Derives a per-commitment-transaction private key (eg an htlc key or payment key) from the base
175-
/// private key for that type of key and the per_commitment_point (available in TxCreationKeys)
174+
/// Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
175+
/// from the base secret and the per_commitment_point.
176+
///
177+
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
178+
/// generated (ie our own).
176179
pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
177180
let mut sha = Sha256::engine();
178181
sha.input(&per_commitment_point.serialize());
@@ -184,7 +187,13 @@ pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_co
184187
Ok(key)
185188
}
186189

187-
pub(super) fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
190+
/// Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
191+
/// from the base point and the per_commitment_key. This is the public equivalent of
192+
/// derive_private_key - using only public keys to derive a public key instead of private keys.
193+
///
194+
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
195+
/// generated (ie our own).
196+
pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
188197
let mut sha = Sha256::engine();
189198
sha.input(&per_commitment_point.serialize());
190199
sha.input(&base_point.serialize());
@@ -194,10 +203,11 @@ pub(super) fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>,
194203
base_point.combine(&hashkey)
195204
}
196205

197-
/// Derives a revocation key from its constituent parts.
206+
/// Derives a per-commitment-transaction revocation key from its constituent parts.
207+
///
198208
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
199209
/// generated (ie our own).
200-
pub(super) fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
210+
pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, revocation_base_secret: &SecretKey) -> Result<SecretKey, secp256k1::Error> {
201211
let revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &revocation_base_secret);
202212
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
203213

@@ -224,7 +234,13 @@ pub(super) fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Se
224234
Ok(part_a)
225235
}
226236

227-
pub(super) fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
237+
/// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
238+
/// the public equivalend of derive_private_revocation_key - using only public keys to derive a
239+
/// public key instead of private keys.
240+
///
241+
/// Note that this is infallible iff we trust that at least one of the two input keys are randomly
242+
/// generated (ie our own).
243+
pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, revocation_base_point: &PublicKey) -> Result<PublicKey, secp256k1::Error> {
228244
let rev_append_commit_hash_key = {
229245
let mut sha = Sha256::engine();
230246
sha.input(&revocation_base_point.serialize());
@@ -273,9 +289,9 @@ pub struct ChannelPublicKeys {
273289
/// on-chain channel lock-in 2-of-2 multisig output.
274290
pub funding_pubkey: PublicKey,
275291
/// The base point which is used (with derive_public_revocation_key) to derive per-commitment
276-
/// revocation keys. The per-commitment revocation private key is then revealed by the owner of
277-
/// a commitment transaction so that their counterparty can claim all available funds if they
278-
/// broadcast an old state.
292+
/// revocation keys. This is combined with the per-commitment-secret generated by the
293+
/// counterparty to create a secret which the counterparty can reveal to revoke previous
294+
/// states.
279295
pub revocation_basepoint: PublicKey,
280296
/// The public key which receives our immediately spendable primary channel balance in
281297
/// remote-broadcasted commitment transactions. This key is static across every commitment
@@ -311,9 +327,10 @@ impl TxCreationKeys {
311327
}
312328
}
313329

314-
/// Gets the "to_local" output redeemscript, ie the script which is time-locked or spendable by
315-
/// the revocation key
316-
pub(super) fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script {
330+
/// A script either spendable by the revocation
331+
/// key or the delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
332+
/// Encumbering a `to_local` output on a commitment transaction or 2nd-stage HTLC transactions.
333+
pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, to_self_delay: u16, delayed_payment_key: &PublicKey) -> Script {
317334
Builder::new().push_opcode(opcodes::all::OP_IF)
318335
.push_slice(&revocation_key.serialize())
319336
.push_opcode(opcodes::all::OP_ELSE)

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,6 +4439,7 @@ mod tests {
44394439
// These aren't set in the test vectors:
44404440
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
44414441
10_000_000,
4442+
(0, 0)
44424443
);
44434444

44444445
assert_eq!(PublicKey::from_secret_key(&secp_ctx, chan_keys.funding_key()).serialize()[..],

0 commit comments

Comments
 (0)