channeldb: cache htlc attempt session key

This commit is contained in:
carla 2021-05-19 09:03:53 +02:00
parent 8071ebb16a
commit 64333c0303
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91
2 changed files with 23 additions and 8 deletions

@ -26,6 +26,11 @@ type HTLCAttemptInfo struct {
// EC operations used by btcec.PrivKeyFromBytes. // EC operations used by btcec.PrivKeyFromBytes.
sessionKey [btcec.PrivKeyBytesLen]byte sessionKey [btcec.PrivKeyBytesLen]byte
// cachedSessionKey is our fully deserialized sesionKey. This value
// may be nil if the attempt has just been read from disk and its
// session key has not been used yet.
cachedSessionKey *btcec.PrivateKey
// Route is the route attempted to send the HTLC. // Route is the route attempted to send the HTLC.
Route route.Route Route route.Route
@ -51,6 +56,7 @@ func NewHtlcAttemptInfo(attemptID uint64, sessionKey *btcec.PrivateKey,
return &HTLCAttemptInfo{ return &HTLCAttemptInfo{
AttemptID: attemptID, AttemptID: attemptID,
sessionKey: scratch, sessionKey: scratch,
cachedSessionKey: sessionKey,
Route: route, Route: route,
AttemptTime: attemptTime, AttemptTime: attemptTime,
Hash: hash, Hash: hash,
@ -58,10 +64,15 @@ func NewHtlcAttemptInfo(attemptID uint64, sessionKey *btcec.PrivateKey,
} }
// SessionKey returns the ephemeral key used for a htlc attempt. This function // SessionKey returns the ephemeral key used for a htlc attempt. This function
// performs expensive ec-ops to obtain the session key. // performs expensive ec-ops to obtain the session key if it is not cached.
func (h *HTLCAttemptInfo) SessionKey() *btcec.PrivateKey { func (h *HTLCAttemptInfo) SessionKey() *btcec.PrivateKey {
priv, _ := btcec.PrivKeyFromBytes(btcec.S256(), h.sessionKey[:]) if h.cachedSessionKey == nil {
return priv h.cachedSessionKey, _ = btcec.PrivKeyFromBytes(
btcec.S256(), h.sessionKey[:],
)
}
return h.cachedSessionKey
} }
// HTLCAttempt contains information about a specific HTLC attempt for a given // HTLCAttempt contains information about a specific HTLC attempt for a given

@ -120,6 +120,10 @@ func TestSentPaymentSerialization(t *testing.T) {
newWireInfo.Route = route.Route{} newWireInfo.Route = route.Route{}
s.Route = route.Route{} s.Route = route.Route{}
// Call session key method to set our cached session key so we can use
// DeepEqual, and assert that our key equals the original key.
require.Equal(t, s.cachedSessionKey, newWireInfo.SessionKey())
if !reflect.DeepEqual(s, newWireInfo) { if !reflect.DeepEqual(s, newWireInfo) {
t.Fatalf("Payments do not match after "+ t.Fatalf("Payments do not match after "+
"serialization/deserialization %v vs %v", "serialization/deserialization %v vs %v",