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:
11 files changed
+95
-42
No files matched your search
+1
-1
@@ -1417,7 +1417,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
|
||||
// any contested contracts watched by any on-chain arbitrators
|
||||
// can now sweep this HTLC on-chain.
|
||||
go func() {
|
||||
err := l.cfg.PreimageCache.AddPreimage(pre[:])
|
||||
err := l.cfg.PreimageCache.AddPreimages(pre[:])
|
||||
if err != nil {
|
||||
l.errorf("unable to add preimage=%x to "+
|
||||
"cache", pre[:])
|
||||
|
||||
+4
-2
@@ -46,11 +46,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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user