2014-01-30 16:14:02 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014 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 (
|
|
|
|
"errors"
|
2014-06-12 18:39:26 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/conformal/btcrpcclient"
|
|
|
|
"github.com/conformal/btcscript"
|
2014-02-24 20:35:30 +01:00
|
|
|
"github.com/conformal/btcutil"
|
2014-06-12 18:39:26 +02:00
|
|
|
"github.com/conformal/btcwallet/txstore"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2014-01-30 16:14:02 +01:00
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"github.com/conformal/btcws"
|
|
|
|
)
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// InvalidNotificationError describes an error due to an invalid chain server
|
|
|
|
// notification and should be warned by wallet, but does not indicate an
|
|
|
|
// problem with the current wallet state.
|
|
|
|
type InvalidNotificationError struct {
|
|
|
|
error
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
var (
|
|
|
|
// MismatchingNetworks represents an error where a client connection
|
|
|
|
// to btcd cannot succeed due to btcwallet and btcd operating on
|
|
|
|
// different bitcoin networks.
|
|
|
|
ErrMismatchedNets = errors.New("mismatched networks")
|
|
|
|
)
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
const (
|
|
|
|
// maxConcurrentClientRequests is the maximum number of
|
|
|
|
// unhandled/running requests that the server will run for a websocket
|
|
|
|
// client at a time. Beyond this limit, additional request reads will
|
|
|
|
// block until a running request handler finishes. This limit exists to
|
|
|
|
// prevent a single connection from causing a denial of service attack
|
|
|
|
// with an unnecessarily large number of requests.
|
|
|
|
maxConcurrentClientRequests = 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type blockSummary struct {
|
|
|
|
hash *btcwire.ShaHash
|
|
|
|
height int32
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
type acceptedTx struct {
|
|
|
|
tx *btcutil.Tx
|
|
|
|
block *btcws.BlockDetails // nil if unmined
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Notification types. These are defined here and processed from from reading
|
|
|
|
// a notificationChan to avoid handling these notifications directly in
|
|
|
|
// btcrpcclient callbacks, which isn't very go-like and doesn't allow
|
|
|
|
// blocking client calls.
|
|
|
|
type (
|
|
|
|
// Container type for any notification.
|
|
|
|
notification interface {
|
2014-07-07 23:57:00 +02:00
|
|
|
handleNotification(*rpcClient) error
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
blockConnected blockSummary
|
|
|
|
blockDisconnected blockSummary
|
|
|
|
recvTx acceptedTx
|
|
|
|
redeemingTx acceptedTx
|
2014-06-16 21:19:32 +02:00
|
|
|
rescanFinished struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
rescanProgress int32
|
2014-06-12 18:39:26 +02:00
|
|
|
)
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n blockConnected) handleNotification(c *rpcClient) error {
|
2014-06-12 18:39:26 +02:00
|
|
|
// Update the blockstamp for the newly-connected block.
|
2014-07-07 23:57:00 +02:00
|
|
|
bs := wallet.BlockStamp{
|
2014-06-12 18:39:26 +02:00
|
|
|
Height: n.height,
|
|
|
|
Hash: *n.hash,
|
|
|
|
}
|
2014-07-07 23:57:00 +02:00
|
|
|
c.mtx.Lock()
|
|
|
|
c.blockStamp = bs
|
|
|
|
c.mtx.Unlock()
|
2014-06-12 18:39:26 +02:00
|
|
|
|
2014-06-16 21:19:32 +02:00
|
|
|
AcctMgr.Grab()
|
2014-07-07 23:57:00 +02:00
|
|
|
AcctMgr.BlockNotify(&bs)
|
2014-06-16 21:19:32 +02:00
|
|
|
AcctMgr.Release()
|
2014-06-12 18:39:26 +02:00
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Pass notification to wallet clients too.
|
2014-06-12 18:39:26 +02:00
|
|
|
if server != nil {
|
|
|
|
// TODO: marshaling should be perfomred by the server, and
|
|
|
|
// sent only to client that have requested the notification.
|
|
|
|
marshaled, err := n.MarshalJSON()
|
|
|
|
// The parsed notification is expected to be marshalable.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
server.broadcasts <- marshaled
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
return nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// MarshalJSON creates the JSON encoding of the chain notification to pass
|
|
|
|
// to any connected wallet clients. This should never error.
|
|
|
|
func (n blockConnected) MarshalJSON() ([]byte, error) {
|
|
|
|
nn := btcws.NewBlockConnectedNtfn(n.hash.String(), n.height)
|
|
|
|
return nn.MarshalJSON()
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n blockDisconnected) handleNotification(c *rpcClient) error {
|
2014-06-16 21:19:32 +02:00
|
|
|
AcctMgr.Grab()
|
|
|
|
defer AcctMgr.Release()
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Rollback Utxo and Tx data stores.
|
|
|
|
if err := AcctMgr.Rollback(n.height, n.hash); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Pass notification to wallet clients too.
|
2014-06-12 18:39:26 +02:00
|
|
|
if server != nil {
|
|
|
|
// TODO: marshaling should be perfomred by the server, and
|
|
|
|
// sent only to client that have requested the notification.
|
|
|
|
marshaled, err := n.MarshalJSON()
|
|
|
|
// A btcws.BlockDisconnectedNtfn is expected to marshal without error.
|
|
|
|
// If it does, it indicates that one of its struct fields is of a
|
|
|
|
// non-marshalable type.
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
server.broadcasts <- marshaled
|
2014-02-28 19:03:23 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
return nil
|
2014-02-28 19:03:23 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// MarshalJSON creates the JSON encoding of the chain notification to pass
|
|
|
|
// to any connected wallet clients. This should never error.
|
|
|
|
func (n blockDisconnected) MarshalJSON() ([]byte, error) {
|
|
|
|
nn := btcws.NewBlockDisconnectedNtfn(n.hash.String(), n.height)
|
|
|
|
return nn.MarshalJSON()
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func parseBlock(block *btcws.BlockDetails) (*txstore.Block, int, error) {
|
|
|
|
if block == nil {
|
|
|
|
return nil, btcutil.TxIndexUnknown, nil
|
|
|
|
}
|
|
|
|
blksha, err := btcwire.NewShaHashFromStr(block.Hash)
|
2014-05-28 06:54:50 +02:00
|
|
|
if err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
return nil, btcutil.TxIndexUnknown, err
|
2014-05-28 06:54:50 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
b := &txstore.Block{
|
|
|
|
Height: block.Height,
|
|
|
|
Hash: *blksha,
|
|
|
|
Time: time.Unix(block.Time, 0),
|
|
|
|
}
|
|
|
|
return b, block.Index, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n recvTx) handleNotification(c *rpcClient) error {
|
2014-06-12 18:39:26 +02:00
|
|
|
block, txIdx, err := parseBlock(n.block)
|
|
|
|
if err != nil {
|
|
|
|
return InvalidNotificationError{err}
|
|
|
|
}
|
|
|
|
n.tx.SetIndex(txIdx)
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
bs, err := c.BlockStamp()
|
2014-06-12 18:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot get current block: %v", err)
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
|
2014-06-16 21:19:32 +02:00
|
|
|
AcctMgr.Grab()
|
|
|
|
defer AcctMgr.Release()
|
|
|
|
|
2014-06-18 23:46:18 +02:00
|
|
|
// For every output, if it pays to a wallet address, insert the
|
|
|
|
// transaction into the store (possibly moving it from unconfirmed to
|
|
|
|
// confirmed), and add a credit record if one does not already exist.
|
|
|
|
var txr *txstore.TxRecord
|
|
|
|
txInserted := false
|
|
|
|
for i, txout := range n.tx.MsgTx().TxOut {
|
2014-06-12 18:39:26 +02:00
|
|
|
// Errors don't matter here. If addrs is nil, the range below
|
|
|
|
// does nothing.
|
|
|
|
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs(txout.PkScript,
|
|
|
|
activeNet.Params)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
a, err := AcctMgr.AccountByAddress(addr)
|
|
|
|
if err != nil {
|
2014-06-18 23:46:18 +02:00
|
|
|
continue // try next address, if any
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-18 23:46:18 +02:00
|
|
|
if !txInserted {
|
|
|
|
txr, err = a.TxStore.InsertTx(n.tx, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
txInserted = true
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-18 23:46:18 +02:00
|
|
|
|
|
|
|
// Insert and notify websocket clients of the credit if it is
|
|
|
|
// not a duplicate, otherwise, check the next txout if the
|
|
|
|
// credit has already been inserted.
|
|
|
|
if txr.HasCredit(i) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cred, err := txr.AddCredit(uint32(i), false)
|
2014-06-12 18:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
AcctMgr.ds.ScheduleTxStoreWrite(a)
|
2014-06-18 23:46:18 +02:00
|
|
|
ltr, err := cred.ToJSON(a.Name(), bs.Height, a.Wallet.Net())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-18 23:46:18 +02:00
|
|
|
server.NotifyNewTxDetails(a.Name(), ltr)
|
|
|
|
break // check whether next txout is a wallet txout
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-18 23:46:18 +02:00
|
|
|
server.NotifyBalances()
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
return nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n redeemingTx) handleNotification(c *rpcClient) error {
|
2014-06-12 18:39:26 +02:00
|
|
|
block, txIdx, err := parseBlock(n.block)
|
2014-01-30 16:14:02 +01:00
|
|
|
if err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
return InvalidNotificationError{err}
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
n.tx.SetIndex(txIdx)
|
2014-06-16 21:19:32 +02:00
|
|
|
|
|
|
|
AcctMgr.Grab()
|
|
|
|
err = AcctMgr.RecordSpendingTx(n.tx, block)
|
|
|
|
AcctMgr.Release()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n rescanFinished) handleNotification(c *rpcClient) error {
|
2014-06-16 21:19:32 +02:00
|
|
|
AcctMgr.rm.MarkFinished(n)
|
|
|
|
return nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
func (n rescanProgress) handleNotification(c *rpcClient) error {
|
2014-06-12 18:39:26 +02:00
|
|
|
AcctMgr.rm.MarkProgress(n)
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
type rpcClient struct {
|
|
|
|
*btcrpcclient.Client // client to btcd
|
2014-07-07 23:57:00 +02:00
|
|
|
|
|
|
|
mtx sync.Mutex
|
|
|
|
blockStamp wallet.BlockStamp
|
|
|
|
|
|
|
|
enqueueNotification chan notification
|
|
|
|
dequeueNotification chan notification
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func newRPCClient(certs []byte) (*rpcClient, error) {
|
|
|
|
client := rpcClient{
|
2014-07-07 23:57:00 +02:00
|
|
|
blockStamp: wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
},
|
2014-06-17 17:17:43 +02:00
|
|
|
enqueueNotification: make(chan notification),
|
|
|
|
dequeueNotification: make(chan notification),
|
2014-06-24 23:00:27 +02:00
|
|
|
quit: make(chan struct{}),
|
2014-05-28 06:54:50 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
initializedClient := make(chan struct{})
|
|
|
|
ntfnCallbacks := btcrpcclient.NotificationHandlers{
|
|
|
|
OnClientConnected: func() {
|
|
|
|
log.Info("Established connection to btcd")
|
|
|
|
<-initializedClient
|
|
|
|
|
|
|
|
// nil client to broadcast to all connected clients
|
|
|
|
server.NotifyConnectionStatus(nil)
|
|
|
|
|
|
|
|
err := client.Handshake()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot complete handshake: %v", err)
|
|
|
|
client.Stop()
|
|
|
|
}
|
|
|
|
},
|
2014-06-17 17:17:43 +02:00
|
|
|
OnBlockConnected: client.onBlockConnected,
|
|
|
|
OnBlockDisconnected: client.onBlockDisconnected,
|
|
|
|
OnRecvTx: client.onRecvTx,
|
|
|
|
OnRedeemingTx: client.onRedeemingTx,
|
|
|
|
OnRescanFinished: client.onRescanFinished,
|
|
|
|
OnRescanProgress: client.onRescanProgress,
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
conf := btcrpcclient.ConnConfig{
|
|
|
|
Host: cfg.RPCConnect,
|
|
|
|
Endpoint: "ws",
|
|
|
|
User: cfg.BtcdUsername,
|
|
|
|
Pass: cfg.BtcdPassword,
|
|
|
|
Certificates: certs,
|
|
|
|
}
|
|
|
|
c, err := btcrpcclient.New(&conf, &ntfnCallbacks)
|
|
|
|
if err != nil {
|
2014-06-04 02:52:04 +02:00
|
|
|
return nil, err
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
client.Client = c
|
|
|
|
close(initializedClient)
|
|
|
|
return &client, nil
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func (c *rpcClient) Start() {
|
2014-06-17 17:17:43 +02:00
|
|
|
c.wg.Add(2)
|
|
|
|
go c.notificationQueue()
|
2014-06-12 18:39:26 +02:00
|
|
|
go c.handleNotifications()
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func (c *rpcClient) Stop() {
|
2014-06-25 19:01:40 +02:00
|
|
|
log.Warn("Disconnecting chain server client connection")
|
|
|
|
c.Client.Shutdown()
|
2014-06-24 23:00:27 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.quit:
|
|
|
|
default:
|
|
|
|
close(c.quit)
|
|
|
|
close(c.enqueueNotification)
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func (c *rpcClient) WaitForShutdown() {
|
|
|
|
c.Client.WaitForShutdown()
|
|
|
|
c.wg.Wait()
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-06-17 17:17:43 +02:00
|
|
|
func (c *rpcClient) onBlockConnected(hash *btcwire.ShaHash, height int32) {
|
|
|
|
c.enqueueNotification <- (blockConnected)(blockSummary{hash, height})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) onBlockDisconnected(hash *btcwire.ShaHash, height int32) {
|
|
|
|
c.enqueueNotification <- (blockDisconnected)(blockSummary{hash, height})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) onRecvTx(tx *btcutil.Tx, block *btcws.BlockDetails) {
|
|
|
|
c.enqueueNotification <- recvTx{tx, block}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) onRedeemingTx(tx *btcutil.Tx, block *btcws.BlockDetails) {
|
|
|
|
c.enqueueNotification <- redeemingTx{tx, block}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) onRescanProgress(height int32) {
|
|
|
|
c.enqueueNotification <- rescanProgress(height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) onRescanFinished(height int32) {
|
|
|
|
c.enqueueNotification <- rescanFinished{error: nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *rpcClient) notificationQueue() {
|
|
|
|
// TODO: Rather than leaving this as an unbounded queue for all types of
|
|
|
|
// notifications, try dropping ones where a later enqueued notification
|
|
|
|
// can fully invalidate one waiting to be processed. For example,
|
|
|
|
// blockconnected notifications for greater block heights can remove the
|
|
|
|
// need to process earlier blockconnected notifications still waiting
|
|
|
|
// here.
|
|
|
|
|
|
|
|
var q []notification
|
|
|
|
enqueue := c.enqueueNotification
|
|
|
|
var dequeue chan notification
|
|
|
|
var next notification
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n, ok := <-enqueue:
|
|
|
|
if !ok {
|
|
|
|
// If no notifications are queued for handling,
|
|
|
|
// the queue is finished.
|
|
|
|
if len(q) == 0 {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
// nil channel so no more reads can occur.
|
|
|
|
enqueue = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(q) == 0 {
|
|
|
|
next = n
|
|
|
|
dequeue = c.dequeueNotification
|
|
|
|
}
|
|
|
|
q = append(q, n)
|
|
|
|
|
|
|
|
case dequeue <- next:
|
|
|
|
q[0] = nil
|
|
|
|
q = q[1:]
|
|
|
|
if len(q) != 0 {
|
|
|
|
next = q[0]
|
|
|
|
} else {
|
|
|
|
// If no more notifications can be enqueued, the
|
|
|
|
// queue is finished.
|
|
|
|
if enqueue == nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
dequeue = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c.dequeueNotification)
|
|
|
|
c.wg.Done()
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
func (c *rpcClient) handleNotifications() {
|
2014-06-17 17:17:43 +02:00
|
|
|
for n := range c.dequeueNotification {
|
2014-07-07 23:57:00 +02:00
|
|
|
err := n.handleNotification(c)
|
2014-06-12 18:39:26 +02:00
|
|
|
if err != nil {
|
|
|
|
switch e := err.(type) {
|
|
|
|
case InvalidNotificationError:
|
|
|
|
log.Warnf("Ignoring invalid notification: %v", e)
|
|
|
|
default:
|
|
|
|
log.Errorf("Cannot handle notification: %v", e)
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 15:38:23 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
c.wg.Done()
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 23:57:00 +02:00
|
|
|
// BlockStamp returns (as a blockstamp) the height and hash of the last seen
|
|
|
|
// block from the RPC client. If no blocks have been seen (the height is -1),
|
|
|
|
// the chain server is queried for the block and the result is saved for future
|
|
|
|
// calls, or an error is returned if the RPC is unsuccessful.
|
|
|
|
func (c *rpcClient) BlockStamp() (wallet.BlockStamp, error) {
|
|
|
|
c.mtx.Lock()
|
|
|
|
defer c.mtx.Unlock()
|
|
|
|
|
|
|
|
if c.blockStamp.Height != int32(btcutil.BlockHeightUnknown) {
|
|
|
|
return c.blockStamp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, height, err := c.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return wallet.BlockStamp{}, err
|
|
|
|
}
|
|
|
|
bs := wallet.BlockStamp{
|
|
|
|
Hash: *hash,
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
c.blockStamp = bs
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Handshake first checks that the websocket connection between btcwallet and
|
|
|
|
// btcd is valid, that is, that there are no mismatching settings between
|
|
|
|
// the two processes (such as running on different Bitcoin networks). If the
|
|
|
|
// sanity checks pass, all wallets are set to be tracked against chain
|
|
|
|
// notifications from this btcd connection.
|
|
|
|
//
|
|
|
|
// TODO(jrick): Track and Rescan commands should be replaced with a
|
|
|
|
// single TrackSince function (or similar) which requests address
|
|
|
|
// notifications and performs the rescan since some block height.
|
|
|
|
func (c *rpcClient) Handshake() error {
|
|
|
|
net, err := c.GetCurrentNet()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if net != activeNet.Net {
|
|
|
|
return ErrMismatchedNets
|
|
|
|
}
|
2014-03-21 21:36:42 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Request notifications for connected and disconnected blocks.
|
|
|
|
if err := c.NotifyBlocks(); err != nil {
|
|
|
|
return err
|
2014-04-11 04:01:23 +02:00
|
|
|
}
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
// Get current best block. If this is before than the oldest
|
|
|
|
// saved block hash, assume that this btcd instance is not yet
|
|
|
|
// synced up to a previous btcd that was last used with this
|
|
|
|
// wallet.
|
2014-07-07 23:57:00 +02:00
|
|
|
bs, err := c.BlockStamp()
|
2014-05-28 06:54:50 +02:00
|
|
|
if err != nil {
|
2014-07-07 23:57:00 +02:00
|
|
|
return err
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
if server != nil {
|
|
|
|
server.NotifyNewBlockChainHeight(&bs)
|
2014-06-18 23:46:18 +02:00
|
|
|
server.NotifyBalances()
|
2014-05-28 06:54:50 +02:00
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Get default account. Only the default account is used to
|
|
|
|
// track recently-seen blocks.
|
|
|
|
a, err := AcctMgr.Account("")
|
2014-05-28 06:54:50 +02:00
|
|
|
if err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
// No account yet is not a handshake error, but means our
|
|
|
|
// handshake is done.
|
|
|
|
return nil
|
2014-05-28 06:54:50 +02:00
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// TODO(jrick): if height is less than the earliest-saved block
|
|
|
|
// height, should probably wait for btcd to catch up.
|
2014-02-24 20:35:30 +01:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Check that there was not any reorgs done since last connection.
|
|
|
|
// If so, rollback and rescan to catch up.
|
|
|
|
it := a.Wallet.NewIterateRecentBlocks()
|
|
|
|
for cont := it != nil; cont; cont = it.Prev() {
|
|
|
|
bs := it.BlockStamp()
|
|
|
|
log.Debugf("Checking for previous saved block with height %v hash %v",
|
|
|
|
bs.Height, bs.Hash)
|
2014-04-18 00:13:53 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
if _, err := c.GetBlock(&bs.Hash); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2014-04-09 18:07:09 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
log.Debug("Found matching block.")
|
|
|
|
|
|
|
|
// If we had to go back to any previous blocks (it.Next
|
|
|
|
// returns true), then rollback the next and all child blocks.
|
|
|
|
// This rollback is done here instead of in the blockMissing
|
|
|
|
// check above for each removed block because Rollback will
|
|
|
|
// try to write new tx and utxo files on each rollback.
|
|
|
|
if it.Next() {
|
|
|
|
bs := it.BlockStamp()
|
|
|
|
err := AcctMgr.Rollback(bs.Height, &bs.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default account to be marked in sync with the current
|
|
|
|
// blockstamp. This invalidates the iterator.
|
|
|
|
a.Wallet.SetSyncedWith(bs)
|
|
|
|
|
|
|
|
// Begin tracking wallets against this btcd instance.
|
|
|
|
AcctMgr.Track()
|
2014-06-16 21:19:32 +02:00
|
|
|
if err := AcctMgr.RescanActiveAddresses(nil); err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Only begin tracking new unspent outputs as a result
|
|
|
|
// of the rescan. This is also pretty racy, as a new block
|
|
|
|
// could arrive between rescan and by the time the new outpoint
|
|
|
|
// is added to btcd's websocket's unspent output set.
|
|
|
|
AcctMgr.Track()
|
|
|
|
|
|
|
|
// (Re)send any unmined transactions to btcd in case of a btcd restart.
|
|
|
|
AcctMgr.ResendUnminedTxs()
|
|
|
|
|
|
|
|
// Get current blockchain height and best block hash.
|
|
|
|
return nil
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Iterator was invalid (wallet has never been synced) or there was a
|
|
|
|
// huge chain fork + reorg (more than 20 blocks).
|
|
|
|
AcctMgr.Track()
|
2014-06-16 21:19:32 +02:00
|
|
|
if err := AcctMgr.RescanActiveAddresses(&bs); err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: only begin tracking new unspent outputs as a result of the
|
|
|
|
// rescan. This is also racy (see comment for second Track above).
|
|
|
|
AcctMgr.Track()
|
|
|
|
AcctMgr.ResendUnminedTxs()
|
|
|
|
return nil
|
2014-04-18 00:13:53 +02:00
|
|
|
}
|