dropwtxmgr: re-create namespace and set sync info to rescan for lnd

This commit is contained in:
Alex 2017-06-29 18:52:02 -06:00 committed by Olaoluwa Osuntokun
parent 9988a4d500
commit 60b23c144e

View file

@ -6,14 +6,16 @@ package main
import ( import (
"bufio" "bufio"
"encoding/binary"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"github.com/jessevdk/go-flags"
"github.com/roasbeef/btcutil" "github.com/roasbeef/btcutil"
"github.com/roasbeef/btcwallet/walletdb" "github.com/roasbeef/btcwallet/walletdb"
_ "github.com/roasbeef/btcwallet/walletdb/bdb" _ "github.com/roasbeef/btcwallet/walletdb/bdb"
"github.com/jessevdk/go-flags" "github.com/roasbeef/btcwallet/wtxmgr"
) )
const defaultNet = "mainnet" const defaultNet = "mainnet"
@ -36,9 +38,16 @@ func init() {
} }
} }
// Namespace keys.
var ( var (
// Namespace keys.
syncBucketName = []byte("sync")
waddrmgrNamespace = []byte("waddrmgr")
wtxmgrNamespace = []byte("wtxmgr") wtxmgrNamespace = []byte("wtxmgr")
// Sync related key names (sync bucket).
syncedToName = []byte("syncedto")
startBlockName = []byte("startblock")
recentBlocksName = []byte("recentblocks")
) )
func yes(s string) bool { func yes(s string) bool {
@ -104,10 +113,32 @@ func mainInt() int {
defer db.Close() defer db.Close()
fmt.Println("Dropping wtxmgr namespace") fmt.Println("Dropping wtxmgr namespace")
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
return tx.DeleteTopLevelBucket(wtxmgrNamespace) err := tx.DeleteTopLevelBucket(wtxmgrNamespace)
})
if err != nil && err != walletdb.ErrBucketNotFound { if err != nil && err != walletdb.ErrBucketNotFound {
fmt.Println("Failed to drop namespace:", err) 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 {
fmt.Println("Failed to drop and re-create namespace:", err)
return 1 return 1
} }