Commit 5bb93ee
committed
keychain: bound BtcWalletKeyRing ECDH derived private key cache
Each call to BtcWalletKeyRing.ECDH went through DerivePrivKey, which
opens a read-write wallet DB transaction and forces a bbolt meta-page
write plus fdatasync per call. Memoize the derived private key on the
keyring, keyed by the descriptor's compressed public key, so repeated
ECDH operations against the same key (every brontide handshake against
the node identity key, every onion message decrypt, every watchtower
session ECDH) stay entirely in memory after the first call.
The cache is a neutrino LRU bounded at 1000 entries. ECDH callers in
practice use a small set of keys (the node identity key on the onion
hot path plus per-channel revocation roots, base-encryption keys, and
signrpc-supplied descriptors), so 1000 comfortably covers the working
set while keeping memory bounded against any caller that drives an
unusually wide range of descriptors. A wrapper type carries the
private key and reports Size 1 so the LRU bounds entry count rather
than bytes.
The cache lives on BtcWalletKeyRing rather than on PubKeyECDH so the
private material stays behind the type that already owns it and every
ECDHRing.ECDH caller benefits, not just those that go through the
PubKeyECDH wrapper. Keying by the serialized compressed public key
keeps lookups correct regardless of whether DerivePrivKey takes the
path-based or the PubKey-scan branch: the same priv.PubKey() always
maps to the same cache slot, with no cross-key collision risk.
Descriptors without a PubKey (uncommon on the ECDH hot path) bypass
the cache and forward to DerivePrivKey unchanged. Remote-signer
deployments use RPCKeyRing instead and are not affected.1 parent 4bd29bc commit 5bb93ee
1 file changed
Lines changed: 78 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
25 | 35 | | |
26 | 36 | | |
27 | 37 | | |
| |||
57 | 67 | | |
58 | 68 | | |
59 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
60 | 92 | | |
61 | 93 | | |
62 | 94 | | |
| |||
76 | 108 | | |
77 | 109 | | |
78 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
79 | 114 | | |
80 | 115 | | |
81 | 116 | | |
| |||
385 | 420 | | |
386 | 421 | | |
387 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
388 | 427 | | |
389 | 428 | | |
390 | 429 | | |
391 | 430 | | |
392 | | - | |
| 431 | + | |
393 | 432 | | |
394 | 433 | | |
395 | 434 | | |
| |||
408 | 447 | | |
409 | 448 | | |
410 | 449 | | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
411 | 488 | | |
412 | 489 | | |
413 | 490 | | |
| |||
0 commit comments