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
2019-02-19 17:05:04 -08:00
parent 81783a60dc
commit 30f61b7630
11 changed files with 95 additions and 42 deletions

View File

@@ -64,8 +64,10 @@ type WitnessBeacon interface {
// True is returned for the second argument if the preimage is found.
LookupPreimage(payhash []byte) ([]byte, bool)
// AddPreImage adds a newly discovered preimage to the global cache.
AddPreimage(pre []byte) error
// AddPreimages adds a batch of newly discovered preimages to the global
// cache, and also signals any subscribers of the newly discovered
// witness.
AddPreimages(preimages ...[]byte) error
}
// ChannelArbitratorConfig contains all the functionality that the

View File

@@ -79,7 +79,7 @@ func (h *htlcOutgoingContestResolver) Resolve() (ContractResolver, error) {
// With the preimage obtained, we can now add it to the global
// cache.
if err := h.PreimageDB.AddPreimage(preimage[:]); err != nil {
if err := h.PreimageDB.AddPreimages(preimage[:]); err != nil {
log.Errorf("%T(%v): unable to add witness to cache",
h, h.htlcResolution.ClaimOutpoint)
}