From 60b23c144e4a700aa980c7064763025fe26457a4 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 29 Jun 2017 18:52:02 -0600 Subject: [PATCH] dropwtxmgr: re-create namespace and set sync info to rescan for lnd --- cmd/dropwtxmgr/main.go | 43 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/cmd/dropwtxmgr/main.go b/cmd/dropwtxmgr/main.go index 52b4f5e..ac99377 100644 --- a/cmd/dropwtxmgr/main.go +++ b/cmd/dropwtxmgr/main.go @@ -6,14 +6,16 @@ package main import ( "bufio" + "encoding/binary" "fmt" "os" "path/filepath" + "github.com/jessevdk/go-flags" "github.com/roasbeef/btcutil" "github.com/roasbeef/btcwallet/walletdb" _ "github.com/roasbeef/btcwallet/walletdb/bdb" - "github.com/jessevdk/go-flags" + "github.com/roasbeef/btcwallet/wtxmgr" ) const defaultNet = "mainnet" @@ -36,9 +38,16 @@ func init() { } } -// Namespace keys. var ( - wtxmgrNamespace = []byte("wtxmgr") + // Namespace keys. + syncBucketName = []byte("sync") + waddrmgrNamespace = []byte("waddrmgr") + wtxmgrNamespace = []byte("wtxmgr") + + // Sync related key names (sync bucket). + syncedToName = []byte("syncedto") + startBlockName = []byte("startblock") + recentBlocksName = []byte("recentblocks") ) func yes(s string) bool { @@ -104,10 +113,32 @@ func mainInt() int { defer db.Close() fmt.Println("Dropping wtxmgr namespace") err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { - return tx.DeleteTopLevelBucket(wtxmgrNamespace) + err := tx.DeleteTopLevelBucket(wtxmgrNamespace) + if err != nil && err != walletdb.ErrBucketNotFound { + return err + } + ns, err := tx.CreateTopLevelBucket(wtxmgrNamespace) + if err != nil { + return err + } + err = wtxmgr.Create(ns) + if err != nil { + return err + } + ns = tx.ReadWriteBucket(waddrmgrNamespace).NestedReadWriteBucket(syncBucketName) + startBlock := ns.Get(startBlockName) + err = ns.Put(syncedToName, startBlock) + if err != nil { + return err + } + recentBlocks := make([]byte, 40) + copy(recentBlocks[0:4], startBlock[0:4]) + copy(recentBlocks[8:], startBlock[4:]) + binary.LittleEndian.PutUint32(recentBlocks[4:8], uint32(1)) + return ns.Put(recentBlocksName, recentBlocks) }) - if err != nil && err != walletdb.ErrBucketNotFound { - fmt.Println("Failed to drop namespace:", err) + if err != nil { + fmt.Println("Failed to drop and re-create namespace:", err) return 1 }