multi: make AddPreimage variadic, optimistically compute key

In this commit, we modify the WitnessCache's
AddPreimage method to accept a variadic number
of preimages. This enables callers to batch
preimage writes in performance critical areas
of the codebase, e.g. the htlcswitch.

Additionally, we lift the computation of the
witnesses' keys outside of the db transaction.
This saves us from having to do hashing inside
and blocking other callers, and limits extraneous
blocking at the call site.
This commit is contained in:
Conner Fromknecht committed 2019-02-19 17:05:04 -08:00
1 parent 81783a60dc
commit 30f61b7630
11 files changed
+95 -42

No files matched your search

+2 -2
View File
@@ -584,7 +584,7 @@ func TestForceClose(t *testing.T) {
// Before we force close Alice's channel, we'll add the pre-image of
// Bob's HTLC to her preimage cache.
aliceChannel.pCache.AddPreimage(preimageBob[:])
aliceChannel.pCache.AddPreimages(preimageBob[:])
// With the cache populated, we'll now attempt the force close
// initiated by Alice.
@@ -4953,7 +4953,7 @@ func TestChannelUnilateralCloseHtlcResolution(t *testing.T) {
// Now that Bob has force closed, we'll modify Alice's pre image cache
// such that she now gains the ability to also settle the incoming HTLC
// from Bob.
aliceChannel.pCache.AddPreimage(preimageBob[:])
aliceChannel.pCache.AddPreimages(preimageBob[:])
// We'll then use Bob's transaction to trigger a spend notification for
// Alice.
+7 -3
View File
@@ -274,9 +274,13 @@ type PreimageCache interface {
// argument. Otherwise, it'll return false.
LookupPreimage(hash []byte) ([]byte, bool)
// AddPreimage attempts to add a new preimage to the global cache. If
// successful a nil error will be returned.
AddPreimage(preimage []byte) error
// AddPreimages adds a batch of newly discovered preimages to the global
// cache, and also signals any subscribers of the newly discovered
// witness.
//
// NOTE: The backing slice of MUST NOT be modified, otherwise the
// subscribers may be notified of the incorrect preimages.
AddPreimages(preimages ...[]byte) error
}
// WalletDriver represents a "driver" for a particular concrete
+4 -2
View File
@@ -403,11 +403,13 @@ func (m *mockPreimageCache) LookupPreimage(hash []byte) ([]byte, bool) {
return p, ok
}
func (m *mockPreimageCache) AddPreimage(preimage []byte) error {
func (m *mockPreimageCache) AddPreimages(preimages ...[]byte) error {
m.Lock()
defer m.Unlock()
m.preimageMap[sha256.Sum256(preimage[:])] = preimage
for _, preimage := range preimages {
m.preimageMap[sha256.Sum256(preimage)] = preimage
}
return nil
}