[lbry] eliminated btcd dependency

This commit is contained in:
Brannon King 2021-12-14 19:54:55 -05:00 committed by Roy Lee
parent d5328e1834
commit 23f883be7e
3 changed files with 24 additions and 14 deletions

View file

@ -6,7 +6,6 @@ package chain
import ( import (
"github.com/btcsuite/btclog" "github.com/btcsuite/btclog"
"github.com/lightninglabs/neutrino/query"
) )
// log is a logger that is initialized with no output filters. This // log is a logger that is initialized with no output filters. This
@ -30,5 +29,4 @@ func DisableLog() {
// using btclog. // using btclog.
func UseLogger(logger btclog.Logger) { func UseLogger(logger btclog.Logger) {
log = logger log = logger
query.UseLogger(logger)
} }

View file

@ -5,6 +5,7 @@
package waddrmgr package waddrmgr
import ( import (
"container/list"
"crypto/rand" "crypto/rand"
"crypto/sha512" "crypto/sha512"
"fmt" "fmt"
@ -17,7 +18,6 @@ import (
"github.com/lbryio/lbcwallet/internal/zero" "github.com/lbryio/lbcwallet/internal/zero"
"github.com/lbryio/lbcwallet/snacl" "github.com/lbryio/lbcwallet/snacl"
"github.com/lbryio/lbcwallet/walletdb" "github.com/lbryio/lbcwallet/walletdb"
"github.com/lightninglabs/neutrino/cache/lru"
) )
const ( const (
@ -608,7 +608,8 @@ func (m *Manager) NewScopedKeyManager(ns walletdb.ReadWriteBucket,
rootManager: m, rootManager: m,
addrs: make(map[addrKey]ManagedAddress), addrs: make(map[addrKey]ManagedAddress),
acctInfo: make(map[uint32]*accountInfo), acctInfo: make(map[uint32]*accountInfo),
privKeyCache: lru.NewCache(defaultPrivKeyCacheSize), privKeyCache: map[DerivationPath]*list.Element{},
privKeyLru: list.New(),
} }
m.externalAddrSchemas[addrSchema.ExternalAddrType] = append( m.externalAddrSchemas[addrSchema.ExternalAddrType] = append(
m.externalAddrSchemas[addrSchema.ExternalAddrType], scope, m.externalAddrSchemas[addrSchema.ExternalAddrType], scope,
@ -1626,7 +1627,8 @@ func loadManager(ns walletdb.ReadBucket, pubPassphrase []byte,
addrSchema: *scopeSchema, addrSchema: *scopeSchema,
addrs: make(map[addrKey]ManagedAddress), addrs: make(map[addrKey]ManagedAddress),
acctInfo: make(map[uint32]*accountInfo), acctInfo: make(map[uint32]*accountInfo),
privKeyCache: lru.NewCache(defaultPrivKeyCacheSize), privKeyCache: map[DerivationPath]*list.Element{},
privKeyLru: list.New(),
} }
return nil return nil

View file

@ -1,6 +1,7 @@
package waddrmgr package waddrmgr
import ( import (
"container/list"
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
@ -15,7 +16,6 @@ import (
"github.com/lbryio/lbcwallet/internal/zero" "github.com/lbryio/lbcwallet/internal/zero"
"github.com/lbryio/lbcwallet/netparams" "github.com/lbryio/lbcwallet/netparams"
"github.com/lbryio/lbcwallet/walletdb" "github.com/lbryio/lbcwallet/walletdb"
"github.com/lightninglabs/neutrino/cache/lru"
) )
// HDVersion represents the different supported schemes of hierarchical // HDVersion represents the different supported schemes of hierarchical
@ -247,7 +247,8 @@ type ScopedKeyManager struct {
// privKeyCache stores the set of private keys that have been marked as // privKeyCache stores the set of private keys that have been marked as
// items to be cached to allow us to avoid the database and EC // items to be cached to allow us to avoid the database and EC
// operations each time a key need to be obtained. // operations each time a key need to be obtained.
privKeyCache *lru.Cache privKeyCache map[DerivationPath]*list.Element
privKeyLru *list.List
mtx sync.RWMutex mtx sync.RWMutex
} }
@ -598,6 +599,7 @@ func (s *ScopedKeyManager) AccountProperties(ns walletdb.ReadBucket,
// to be used frequently. We use this wrapper struct to be able too report the // to be used frequently. We use this wrapper struct to be able too report the
// size of a given element to the cache. // size of a given element to the cache.
type cachedKey struct { type cachedKey struct {
path DerivationPath
key *btcec.PrivateKey key *btcec.PrivateKey
} }
@ -624,9 +626,11 @@ func (s *ScopedKeyManager) DeriveFromKeyPathCache(
// First, try to look up the key itself in the proper cache, if the key // First, try to look up the key itself in the proper cache, if the key
// is here, then we don't need to do anything further. // is here, then we don't need to do anything further.
privKeyVal, err := s.privKeyCache.Get(kp) element := s.privKeyCache[kp]
if err == nil { if element != nil {
return privKeyVal.(*cachedKey).key, nil privKeyVal := element.Value.(*cachedKey)
s.privKeyLru.MoveToFront(element)
return privKeyVal.key, nil
} }
// If the key isn't already in the cache, then we'll try to look up the // If the key isn't already in the cache, then we'll try to look up the
@ -657,10 +661,16 @@ func (s *ScopedKeyManager) DeriveFromKeyPathCache(
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, err = s.privKeyCache.Put(kp, &cachedKey{key: privKey})
if err != nil { if s.privKeyLru.Len() >= defaultPrivKeyCacheSize {
return nil, err element = s.privKeyLru.Back()
delete(s.privKeyCache, element.Value.(*cachedKey).path)
element.Value = &cachedKey{key: privKey, path: kp}
s.privKeyLru.MoveToFront(element)
} else {
s.privKeyLru.PushFront(&cachedKey{key: privKey, path: kp})
} }
s.privKeyCache[kp] = element
return privKey, nil return privKey, nil
} }