Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate wtxmgr #234

Merged
merged 5 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 34 additions & 49 deletions chain/chain.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
* Copyright (c) 2013-2015 Conformal Systems LLC <info@conformal.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -26,8 +26,8 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcrpcclient"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/legacy/txstore"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/wtxmgr"
)

// Client represents a persistent client connection to a bitcoin RPC server
Expand Down Expand Up @@ -159,18 +159,11 @@ type (
// BlockStamp was reorganized out of the best chain.
BlockDisconnected waddrmgr.BlockStamp

// RecvTx is a notification for a transaction which pays to a wallet
// address.
RecvTx struct {
Tx *btcutil.Tx // Index is guaranteed to be set.
Block *txstore.Block // nil if unmined
}

// RedeemingTx is a notification for a transaction which spends an
// output controlled by the wallet.
RedeemingTx struct {
Tx *btcutil.Tx // Index is guaranteed to be set.
Block *txstore.Block // nil if unmined
// RelevantTx is a notification for a transaction which spends wallet
// inputs or pays to a watched address.
RelevantTx struct {
TxRecord *wtxmgr.TxRecord
Block *wtxmgr.BlockMeta // nil if unmined
}

// RescanProgress is a notification describing the current status
Expand Down Expand Up @@ -209,23 +202,25 @@ func (c *Client) BlockStamp() (*waddrmgr.BlockStamp, error) {
}
}

// parseBlock parses a btcjson definition of the block a tx is mined it to the
// Block structure of the txstore package, and the block index. This is done
// parseBlock parses a btcws definition of the block a tx is mined it to the
// Block structure of the wtxmgr package, and the block index. This is done
// here since btcrpcclient doesn't parse this nicely for us.
func parseBlock(block *btcjson.BlockDetails) (blk *txstore.Block, idx int, err error) {
func parseBlock(block *btcjson.BlockDetails) (*wtxmgr.BlockMeta, error) {
if block == nil {
return nil, btcutil.TxIndexUnknown, nil
return nil, nil
}
blksha, err := wire.NewShaHashFromStr(block.Hash)
if err != nil {
return nil, btcutil.TxIndexUnknown, err
return nil, err
}
blk = &txstore.Block{
Height: block.Height,
Hash: *blksha,
Time: time.Unix(block.Time, 0),
blk := &wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Height: block.Height,
Hash: *blksha,
},
Time: time.Unix(block.Time, 0),
}
return blk, block.Index, nil
return blk, nil
}

func (c *Client) onClientConnect() {
Expand All @@ -242,35 +237,25 @@ func (c *Client) onBlockDisconnected(hash *wire.ShaHash, height int32) {
}

func (c *Client) onRecvTx(tx *btcutil.Tx, block *btcjson.BlockDetails) {
var blk *txstore.Block
index := btcutil.TxIndexUnknown
if block != nil {
var err error
blk, index, err = parseBlock(block)
if err != nil {
// Log and drop improper notification.
log.Errorf("recvtx notification bad block: %v", err)
return
}
blk, err := parseBlock(block)
if err != nil {
// Log and drop improper notification.
log.Errorf("recvtx notification bad block: %v", err)
return
}
tx.SetIndex(index)
c.enqueueNotification <- RecvTx{tx, blk}

rec, err := wtxmgr.NewTxRecordFromMsgTx(tx.MsgTx(), time.Now())
if err != nil {
log.Errorf("Cannot create transaction record for relevant "+
"tx: %v", err)
return
}
c.enqueueNotification <- RelevantTx{rec, blk}
}

func (c *Client) onRedeemingTx(tx *btcutil.Tx, block *btcjson.BlockDetails) {
var blk *txstore.Block
index := btcutil.TxIndexUnknown
if block != nil {
var err error
blk, index, err = parseBlock(block)
if err != nil {
// Log and drop improper notification.
log.Errorf("redeemingtx notification bad block: %v", err)
return
}
}
tx.SetIndex(index)
c.enqueueNotification <- RedeemingTx{tx, blk}
// Handled exactly like recvtx notifications.
c.onRecvTx(tx, block)
}

func (c *Client) onRescanProgress(hash *wire.ShaHash, height int32, blkTime time.Time) {
Expand Down
123 changes: 123 additions & 0 deletions cmd/dropwtxmgr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright (c) 2015 Conformal Systems LLC <info@conformal.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

package main

import (
"bufio"
"fmt"
"os"
"path/filepath"

"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/btcsuite/go-flags"
)

const defaultNet = "mainnet"

var datadir = btcutil.AppDataDir("btcwallet", false)

// Flags.
var opts = struct {
Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"`
}{
Force: false,
DbPath: filepath.Join(datadir, defaultNet, "wallet.db"),
}

func init() {
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
}

// Namespace keys.
var (
wtxmgrNamespace = []byte("wtxmgr")
)

func yes(s string) bool {
switch s {
case "y", "Y", "yes", "Yes":
return true
default:
return false
}
}

func no(s string) bool {
switch s {
case "n", "N", "no", "No":
return true
default:
return false
}
}

func main() {
os.Exit(mainInt())
}

func mainInt() int {
fmt.Println("Database path:", opts.DbPath)
_, err := os.Stat(opts.DbPath)
if os.IsNotExist(err) {
fmt.Println("Database file does not exist")
return 1
}

for !opts.Force {
fmt.Print("Drop all btcwallet transaction history? [y/N] ")

scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
if !scanner.Scan() {
// Exit on EOF.
return 0
}
err := scanner.Err()
if err != nil {
fmt.Println()
fmt.Println(err)
return 1
}
resp := scanner.Text()
if yes(resp) {
break
}
if no(resp) || resp == "" {
return 0
}

fmt.Println("Enter yes or no.")
}

db, err := walletdb.Open("bdb", opts.DbPath)
if err != nil {
fmt.Println("Failed to open database:", err)
return 1
}
defer db.Close()
fmt.Println("Dropping wtxmgr namespace")
err = db.DeleteNamespace(wtxmgrNamespace)
if err != nil && err != walletdb.ErrBucketNotFound {
fmt.Println("Failed to drop namespace:", err)
return 1
}

return 0
}
43 changes: 0 additions & 43 deletions disksync.go

This file was deleted.

3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Guides

[Rebuilding all transaction history with forced rescans](https://github.com/btcsuite/btcwallet/tree/master/docs/force_rescans.md)
80 changes: 80 additions & 0 deletions docs/force_rescans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Rebuilding transaction history

It is unlikely, but possible and unfortunate, that transaction history in the
wallet database may not represent reality. This may be due to a programming
mistake or the transaction database becoming corrupted. Thankfully, all
transactions are publicly recorded on the blockchain, and transactions
necessary for a fully functional wallet can be recovered. This process is
called rescanning, and the following guide will demonstrate how to force such a
rescan.

Rescans are automatically performed each time the wallet syncs to the network.
These are used to "catch up" the wallet to the newest best block in the block
chain. For example, the following log messages at startup indicate that an
out-of-sync wallet started a rescan for all addresses and unspent outputs since
some block.

```
13:45:03 2015-04-13 [INF] WLLT: Started rescan from block 00000000001703b1a9dfd4865d587cd3f3cbb2f8e6ce9b44668e78ad8d4a7377 (height 205921) for 1 address
...
13:45:49 2015-04-13 [INF] WLLT: Finished rescan for 1 address (synced to block 0000000005cecab1013ecb1275a3e0c9623c4a497a57b6b6bf0fc1525aca1fbf, height 335146)
```

During the rescan, relevant transactions from previously unseen blocks are added
to the wallet database and spend tracking is updated accordingly. After the
rescan at startup finishes, a wallet is marked in sync with the chain.

When wallet is started without any transaction history, a rescan is performed
for all blocks since the creation date of the wallet's first address. There are
two situations when this holds true:

1. The wallet is newly created or was recreated from the seed
2. The transaction history is explicitly deleted

The second case is how a forced rescan is performed.

btcwallet will not drop transaction history by itself, as this is something that
should not be necessary under normal wallet operation. However, a tool,
`dropwtxmgr`, is provided in the `cmd/dropwtxmgr` directory which may be used to
drop the wallet transaction manager (wtxmgr) history from a wallet database.
The tool may already be installed in your PATH, but if not, installing it is easy:

```
$ cd $GOPATH/src/github.com/btcsuite/btcwallet/cmd/dropwtxmgr
$ go get
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go install? I know go get will install it too, but it also tries to do more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not without -u.

```

Dropping transaction history given the default database location can be
performed by stopping wallet (to release the database) and running the tool,
answering yes to the prompt:

```
$ dropwtxmgr
Database path: /home/username/.btcwallet/mainnet/wallet.db
Drop all btcwallet transaction history? [y/N] y
Dropping wtxmgr namespace
```

If the wallet database is in another location or transaction history for a
different network (e.g. testnet or simnet) must be dropped, the full database
path may be specified:

```
$ dropwtxmgr --db ~/.btcwallet/testnet/wallet.db
Database path: /home/username/.btcwallet/testnet/wallet.db
Drop all btcwallet transaction history? [y/N] y
Dropping wtxmgr namespace
```

After dropping transaction history, btcwallet may be restarted and a full rescan
will be triggered to sync the wallet:

```
$ btcwallet
14:05:31 2015-04-13 [INF] BTCW: No recorded transaction history -- needs full rescan
...
14:05:31 2015-04-13 [INF] WLLT: Started rescan from block 000000000000e37b0f99af2e434834123b5459e31e17937169ce81ed0cc4d61c (height 193191) for 1 address
...
14:07:06 2015-04-13 [INF] WLLT: Finished rescan for 1 address (synced to block 00000000049041b5bd7f8ac86c8f1d32065053aefbe8c31e25ed03ef015a725a, height 335482)

```
Loading