-
Notifications
You must be signed in to change notification settings - Fork 2.3k
keychain: cache derived priv key in BtcWalletKeyRing.ECDH to avoid per-call DB txn #10779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
erickcestari
wants to merge
2
commits into
lightningnetwork:master
Choose a base branch
from
erickcestari:improve-onion-processing-performance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+259
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package onionmessage | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/btcsuite/btcd/chaincfg" | ||
| "github.com/btcsuite/btcd/chaincfg/chainhash" | ||
| "github.com/btcsuite/btcwallet/snacl" | ||
| "github.com/btcsuite/btcwallet/waddrmgr" | ||
| "github.com/btcsuite/btcwallet/wallet" | ||
| "github.com/btcsuite/btcwallet/walletdb" | ||
| _ "github.com/btcsuite/btcwallet/walletdb/bdb" | ||
| sphinx "github.com/lightningnetwork/lightning-onion" | ||
| "github.com/lightningnetwork/lnd/keychain" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/record" | ||
| "github.com/lightningnetwork/lnd/routing/route" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // noopDispatcher is a zero-overhead OnionMessageUpdateDispatcher used for | ||
| // benchmarks so the dispatch path does not perturb timing. | ||
| type noopDispatcher struct{} | ||
|
|
||
| func (noopDispatcher) SendUpdate(any) error { return nil } | ||
|
|
||
| // noopSender is a zero-overhead PeerMessageSender used for benchmarks. | ||
| type noopSender struct{} | ||
|
|
||
| func (noopSender) SendToPeer([33]byte, *lnwire.OnionMessage) error { | ||
| return nil | ||
| } | ||
|
|
||
| // testHDSeed is the deterministic seed used to create the benchmark wallet. | ||
| var testHDSeed = chainhash.Hash{ | ||
| 0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab, | ||
| 0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4, | ||
| 0x4f, 0x2f, 0x6f, 0x25, 0x98, 0xa3, 0xef, 0xb9, | ||
| 0x69, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53, | ||
| } | ||
|
|
||
| // lightningAddrSchema mirrors keychain/btcwallet.go (unexported there). | ||
| var lightningAddrSchema = waddrmgr.ScopeAddrSchema{ | ||
| ExternalAddrType: waddrmgr.WitnessPubKey, | ||
| InternalAddrType: waddrmgr.WitnessPubKey, | ||
| } | ||
|
|
||
| // waddrmgrNamespaceKey mirrors keychain/btcwallet.go (unexported there). | ||
| var waddrmgrNamespaceKey = []byte("waddrmgr") | ||
|
|
||
| // createBenchBtcWallet builds a real on-disk btcwallet (simnet, fast scrypt) | ||
| // suitable for driving keychain.BtcWalletKeyRing in benchmarks. It mirrors | ||
| // the unexported createTestBtcWallet in keychain/interface_test.go so the | ||
| // onionmessage bench can construct keychain.PubKeyECDH against a real | ||
| // SecretKeyRing — exactly as server.go does in production. | ||
| func createBenchBtcWallet(b *testing.B) *wallet.Wallet { | ||
| b.Helper() | ||
|
|
||
| fast := waddrmgr.FastScryptOptions | ||
| waddrmgr.SetSecretKeyGen(func(passphrase *[]byte, | ||
| _ *waddrmgr.ScryptOptions) (*snacl.SecretKey, error) { | ||
|
|
||
| return snacl.NewSecretKey(passphrase, fast.N, fast.R, fast.P) | ||
| }) | ||
|
|
||
| loader := wallet.NewLoader( | ||
| &chaincfg.SimNetParams, b.TempDir(), true, time.Second*10, 0, | ||
| ) | ||
|
|
||
| pass := []byte("test") | ||
| w, err := loader.CreateNewWallet( | ||
| pass, pass, testHDSeed[:], time.Time{}, | ||
| ) | ||
| require.NoError(b, err) | ||
| require.NoError(b, w.Unlock(pass, nil)) | ||
|
|
||
| scope := waddrmgr.KeyScope{ | ||
| Purpose: keychain.BIP0043Purpose, | ||
| Coin: keychain.CoinTypeBitcoin, | ||
| } | ||
| if _, err := w.Manager.FetchScopedKeyManager(scope); err != nil { | ||
| require.NoError(b, walletdb.Update(w.Database(), | ||
| func(tx walletdb.ReadWriteTx) error { | ||
| ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) | ||
| _, err := w.Manager.NewScopedKeyManager( | ||
| ns, scope, lightningAddrSchema, | ||
| ) | ||
|
|
||
| return err | ||
| })) | ||
| } | ||
|
|
||
| b.Cleanup(func() { w.Lock() }) | ||
|
|
||
| return w | ||
| } | ||
|
|
||
| // BenchmarkOnionMessagePipeline measures OnionPeerActor.Receive end to end on | ||
| // the deliver path: decode -> sphinx process -> routing decision -> dispatch. | ||
| // It isolates the actor + pipeline overhead on top of sphinx. | ||
| // ProcessOnionPacket. | ||
| func BenchmarkOnionMessagePipeline(b *testing.B) { | ||
| // Mirror server.go: derive the node key from a real BtcWalletKeyRing | ||
| // and wrap it in keychain.NewPubKeyECDH. This is the exact onion-key | ||
| // type the production sphinx router uses. | ||
| w := createBenchBtcWallet(b) | ||
| keyRing := keychain.NewBtcWalletKeyRing(w, keychain.CoinTypeBitcoin) | ||
|
|
||
| nodeKeyDesc, err := keyRing.DeriveKey(keychain.KeyLocator{ | ||
| Family: keychain.KeyFamilyNodeKey, | ||
| Index: 0, | ||
| }) | ||
| require.NoError(b, err) | ||
|
|
||
| nodeKeyECDH := keychain.NewPubKeyECDH(nodeKeyDesc, keyRing) | ||
|
|
||
| router := sphinx.NewRouter(nodeKeyECDH, sphinx.NewNoOpReplayLog()) | ||
| require.NoError(b, router.Start()) | ||
| b.Cleanup(func() { router.Stop() }) | ||
|
|
||
| var peerPubKey [33]byte | ||
| copy(peerPubKey[:], nodeKeyDesc.PubKey.SerializeCompressed()) | ||
|
|
||
| a := &OnionPeerActor{ | ||
| peerPubKey: peerPubKey, | ||
| peerSender: noopSender{}, | ||
| router: router, | ||
| resolver: newMockNodeIDResolver(), | ||
| updateDispatcher: noopDispatcher{}, | ||
| } | ||
|
|
||
| // Build a single-hop "deliver" onion message addressed to nodeKey. | ||
| plain, err := record.EncodeBlindedRouteData(&record.BlindedRouteData{}) | ||
| require.NoError(b, err) | ||
|
|
||
| hops := []*sphinx.HopInfo{ | ||
| {NodePub: nodeKeyDesc.PubKey, PlainText: plain}, | ||
| } | ||
|
|
||
| sessionKey, err := btcec.NewPrivateKey() | ||
| require.NoError(b, err) | ||
|
|
||
| blindedPath, err := sphinx.BuildBlindedPath(sessionKey, hops) | ||
| require.NoError(b, err) | ||
|
|
||
| sphinxPath, err := route.OnionMessageBlindedPathToSphinxPath( | ||
| blindedPath.Path, nil, nil, | ||
| ) | ||
| require.NoError(b, err) | ||
|
|
||
| onionSessionKey, err := btcec.NewPrivateKey() | ||
| require.NoError(b, err) | ||
|
|
||
| pkt, err := sphinx.NewOnionPacket( | ||
| sphinxPath, onionSessionKey, nil, | ||
| sphinx.DeterministicPacketFiller, | ||
| sphinx.WithMaxPayloadSize(sphinx.MaxRoutingPayloadSize), | ||
| ) | ||
| require.NoError(b, err) | ||
|
|
||
| var buf bytes.Buffer | ||
| require.NoError(b, pkt.Encode(&buf)) | ||
|
|
||
| onionBlob := buf.Bytes() | ||
| pathKey := blindedPath.SessionKey.PubKey() | ||
|
|
||
| ctx := b.Context() | ||
|
|
||
| b.ReportAllocs() | ||
|
|
||
| for b.Loop() { | ||
| req := &Request{msg: lnwire.OnionMessage{ | ||
| PathKey: pathKey, | ||
| OnionBlob: onionBlob, | ||
| }} | ||
| _ = a.Receive(ctx, req) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.