channeldb: cache htlc attempt session key
This commit is contained in:
parent
8071ebb16a
commit
64333c0303
@ -26,6 +26,11 @@ type HTLCAttemptInfo struct {
|
||||
// EC operations used by btcec.PrivKeyFromBytes.
|
||||
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 route.Route
|
||||
|
||||
@ -51,6 +56,7 @@ func NewHtlcAttemptInfo(attemptID uint64, sessionKey *btcec.PrivateKey,
|
||||
return &HTLCAttemptInfo{
|
||||
AttemptID: attemptID,
|
||||
sessionKey: scratch,
|
||||
cachedSessionKey: sessionKey,
|
||||
Route: route,
|
||||
AttemptTime: attemptTime,
|
||||
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
|
||||
// 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 {
|
||||
priv, _ := btcec.PrivKeyFromBytes(btcec.S256(), h.sessionKey[:])
|
||||
return priv
|
||||
if h.cachedSessionKey == nil {
|
||||
h.cachedSessionKey, _ = btcec.PrivKeyFromBytes(
|
||||
btcec.S256(), h.sessionKey[:],
|
||||
)
|
||||
}
|
||||
|
||||
return h.cachedSessionKey
|
||||
}
|
||||
|
||||
// HTLCAttempt contains information about a specific HTLC attempt for a given
|
||||
|
@ -120,6 +120,10 @@ func TestSentPaymentSerialization(t *testing.T) {
|
||||
newWireInfo.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) {
|
||||
t.Fatalf("Payments do not match after "+
|
||||
"serialization/deserialization %v vs %v",
|
||||
|
Loading…
Reference in New Issue
Block a user