Merge pull request #702 from carlaKC/dropwtxmgr-keeplabels

dropwtxmgr: add drop labels option
This commit is contained in:
Olaoluwa Osuntokun 2020-05-28 16:07:41 -07:00 committed by GitHub
commit 43c9c2e2f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 2 deletions

View file

@ -10,6 +10,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
@ -26,6 +27,7 @@ var datadir = btcutil.AppDataDir("btcwallet", false)
var opts = struct { var opts = struct {
Force bool `short:"f" description:"Force removal without prompt"` Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"` DbPath string `long:"db" description:"Path to wallet database"`
DropLabels bool `long:"droplabels" description:"Drop transaction labels"`
}{ }{
Force: false, Force: false,
DbPath: filepath.Join(datadir, defaultNet, "wallet.db"), DbPath: filepath.Join(datadir, defaultNet, "wallet.db"),
@ -42,6 +44,9 @@ var (
// Namespace keys. // Namespace keys.
waddrmgrNamespace = []byte("waddrmgr") waddrmgrNamespace = []byte("waddrmgr")
wtxmgrNamespace = []byte("wtxmgr") wtxmgrNamespace = []byte("wtxmgr")
// Bucket names.
bucketTxLabels = []byte("l")
) )
func yes(s string) bool { func yes(s string) bool {
@ -109,6 +114,16 @@ func mainInt() int {
fmt.Println("Dropping btcwallet transaction history") fmt.Println("Dropping btcwallet transaction history")
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
// If we want to keep our tx labels, we read them out so we
// can re-add them after we have deleted our wtxmgr.
var labels map[chainhash.Hash]string
if !opts.DropLabels {
labels, err = fetchAllLabels(tx)
if err != nil {
return err
}
}
err := tx.DeleteTopLevelBucket(wtxmgrNamespace) err := tx.DeleteTopLevelBucket(wtxmgrNamespace)
if err != nil && err != walletdb.ErrBucketNotFound { if err != nil && err != walletdb.ErrBucketNotFound {
return err return err
@ -122,6 +137,13 @@ func mainInt() int {
return err return err
} }
// If we want to re-add our labels, we do so now.
if !opts.DropLabels {
if err := putTxLabels(ns, labels); err != nil {
return err
}
}
ns = tx.ReadWriteBucket(waddrmgrNamespace) ns = tx.ReadWriteBucket(waddrmgrNamespace)
birthdayBlock, err := waddrmgr.FetchBirthdayBlock(ns) birthdayBlock, err := waddrmgr.FetchBirthdayBlock(ns)
if err != nil { if err != nil {
@ -154,3 +176,69 @@ func mainInt() int {
return 0 return 0
} }
// fetchAllLabels returns a map of hex-encoded txid to label.
func fetchAllLabels(tx walletdb.ReadWriteTx) (map[chainhash.Hash]string,
error) {
// Get our top level bucket, if it does not exist we just exit.
txBucket := tx.ReadBucket(wtxmgrNamespace)
if txBucket == nil {
return nil, nil
}
// If we do not have a labels bucket, there are no labels so we exit.
labelsBucket := txBucket.NestedReadBucket(bucketTxLabels)
if labelsBucket == nil {
return nil, nil
}
labels := make(map[chainhash.Hash]string)
if err := labelsBucket.ForEach(func(k, v []byte) error {
txid, err := chainhash.NewHash(k)
if err != nil {
return err
}
label, err := wtxmgr.DeserializeLabel(v)
if err != nil {
return err
}
// Add an entry to our map of labels.
labels[*txid] = label
return nil
}); err != nil {
return nil, err
}
return labels, nil
}
// putTxLabels re-adds a nested labels bucket and entries to the bucket provided
// if there are any labels present.
func putTxLabels(ns walletdb.ReadWriteBucket,
labels map[chainhash.Hash]string) error {
// If there are no labels, exit early.
if len(labels) == 0 {
return nil
}
// First, we create a labels bucket which we will add all labels to.
labelBucket, err := ns.CreateBucketIfNotExists(bucketTxLabels)
if err != nil {
return err
}
// Next, we re-add every label to the bucket.
for txid, label := range labels {
err := wtxmgr.PutTxLabel(labelBucket, txid, label)
if err != nil {
return err
}
}
return nil
}

View file

@ -984,6 +984,15 @@ func (s *Store) PutTxLabel(ns walletdb.ReadWriteBucket, txid chainhash.Hash,
return err return err
} }
return PutTxLabel(labelBucket, txid, label)
}
// PutTxLabel writes a label for a tx to the bucket provided. Note that it does
// not perform any validation on the label provided, or check whether there is
// an existing label for the txid.
func PutTxLabel(labelBucket walletdb.ReadWriteBucket, txid chainhash.Hash,
label string) error {
// We expect the label length to be limited on creation, so we can // We expect the label length to be limited on creation, so we can
// store the label's length as a uint16. // store the label's length as a uint16.
labelLen := uint16(len(label)) labelLen := uint16(len(label))
@ -1016,6 +1025,12 @@ func FetchTxLabel(ns walletdb.ReadBucket, txid chainhash.Hash) (string, error) {
return "", ErrTxLabelNotFound return "", ErrTxLabelNotFound
} }
return DeserializeLabel(v)
}
// DeserializeLabel reads a deserializes a length-value encoded label from the
// byte array provided.
func DeserializeLabel(v []byte) (string, error) {
// If the label is empty, return an error. // If the label is empty, return an error.
length := binary.BigEndian.Uint16(v[0:2]) length := binary.BigEndian.Uint16(v[0:2])
if length == 0 { if length == 0 {