2014-01-03 19:34:37 +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.
|
|
|
|
*/
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// This file implements the notification handlers for btcd-side notifications.
|
2014-01-03 19:34:37 +01:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2014-02-28 19:03:23 +01:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-01-03 19:34:37 +01:00
|
|
|
"github.com/conformal/btcjson"
|
2014-02-24 20:35:30 +01:00
|
|
|
"github.com/conformal/btcscript"
|
2014-01-03 19:34:37 +01:00
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
"github.com/conformal/btcws"
|
|
|
|
)
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
func parseBlock(block *btcws.BlockDetails) (*tx.BlockDetails, error) {
|
|
|
|
if block == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
blksha, err := btcwire.NewShaHashFromStr(block.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tx.BlockDetails{
|
|
|
|
Height: block.Height,
|
|
|
|
Hash: *blksha,
|
|
|
|
Index: int32(block.Index),
|
|
|
|
Time: time.Unix(block.Time, 0),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-02-28 19:03:23 +01:00
|
|
|
type notificationHandler func(btcjson.Cmd) error
|
2014-01-03 19:34:37 +01:00
|
|
|
|
|
|
|
var notificationHandlers = map[string]notificationHandler{
|
|
|
|
btcws.BlockConnectedNtfnMethod: NtfnBlockConnected,
|
|
|
|
btcws.BlockDisconnectedNtfnMethod: NtfnBlockDisconnected,
|
2014-02-24 20:35:30 +01:00
|
|
|
btcws.RecvTxNtfnMethod: NtfnRecvTx,
|
|
|
|
btcws.RedeemingTxNtfnMethod: NtfnRedeemingTx,
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
// NtfnRecvTx handles the btcws.RecvTxNtfn notification.
|
2014-02-28 19:03:23 +01:00
|
|
|
func NtfnRecvTx(n btcjson.Cmd) error {
|
2014-02-24 20:35:30 +01:00
|
|
|
rtx, ok := n.(*btcws.RecvTxNtfn)
|
2014-01-03 19:34:37 +01:00
|
|
|
if !ok {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: unexpected type", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
bs, err := GetCurBlock()
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: cannot get current block: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
|
|
|
|
rawTx, err := hex.DecodeString(rtx.HexTx)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-03-17 15:24:14 +01:00
|
|
|
return fmt.Errorf("%v handler: bad hexstring: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
tx_, err := btcutil.NewTxFromBytes(rawTx)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: bad transaction bytes: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
var block *tx.BlockDetails
|
|
|
|
if rtx.Block != nil {
|
|
|
|
block, err = parseBlock(rtx.Block)
|
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: bad block: %v", n.Method(), err)
|
2014-02-24 20:35:30 +01:00
|
|
|
}
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// For transactions originating from this wallet, the sent tx history should
|
|
|
|
// be recorded before the received history. If wallet created this tx, wait
|
|
|
|
// for the sent history to finish being recorded before continuing.
|
2014-02-24 20:35:30 +01:00
|
|
|
//
|
|
|
|
// TODO(jrick) this is wrong due to tx malleability. Cannot safely use the
|
|
|
|
// txsha as an identifier.
|
2014-01-03 19:34:37 +01:00
|
|
|
req := SendTxHistSyncRequest{
|
2014-02-24 20:35:30 +01:00
|
|
|
txsha: *tx_.Sha(),
|
2014-01-03 19:34:37 +01:00
|
|
|
response: make(chan SendTxHistSyncResponse),
|
|
|
|
}
|
|
|
|
SendTxHistSyncChans.access <- req
|
|
|
|
resp := <-req.response
|
|
|
|
if resp.ok {
|
|
|
|
// Wait until send history has been recorded.
|
|
|
|
<-resp.c
|
2014-02-24 20:35:30 +01:00
|
|
|
SendTxHistSyncChans.remove <- *tx_.Sha()
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-18 21:31:06 +01:00
|
|
|
now := time.Now()
|
|
|
|
var received time.Time
|
|
|
|
if block != nil && now.After(block.Time) {
|
|
|
|
received = block.Time
|
|
|
|
} else {
|
|
|
|
received = now
|
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
// For every output, find all accounts handling that output address (if any)
|
|
|
|
// and record the received txout.
|
|
|
|
for outIdx, txout := range tx_.MsgTx().TxOut {
|
|
|
|
var accounts []*Account
|
|
|
|
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs(txout.PkScript, cfg.Net())
|
|
|
|
for _, addr := range addrs {
|
|
|
|
aname, err := LookupAccountByAddress(addr.EncodeAddress())
|
|
|
|
if err == ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// This cannot reasonably fail if the above succeeded.
|
|
|
|
a, _ := AcctMgr.Account(aname)
|
|
|
|
accounts = append(accounts, a)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
for _, a := range accounts {
|
2014-02-28 19:03:23 +01:00
|
|
|
record, err := a.TxStore.InsertRecvTxOut(tx_, uint32(outIdx), false, received, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
AcctMgr.ds.ScheduleTxStoreWrite(a)
|
|
|
|
|
|
|
|
// Notify frontends of tx. If the tx is unconfirmed, it is always
|
|
|
|
// notified and the outpoint is marked as notified. If the outpoint
|
|
|
|
// has already been notified and is now in a block, a txmined notifiction
|
|
|
|
// should be sent once to let frontends that all previous send/recvs
|
|
|
|
// for this unconfirmed tx are now confirmed.
|
|
|
|
recvTxOP := btcwire.NewOutPoint(tx_.Sha(), uint32(outIdx))
|
|
|
|
previouslyNotifiedReq := NotifiedRecvTxRequest{
|
|
|
|
op: *recvTxOP,
|
|
|
|
response: make(chan NotifiedRecvTxResponse),
|
|
|
|
}
|
|
|
|
NotifiedRecvTxChans.access <- previouslyNotifiedReq
|
|
|
|
if <-previouslyNotifiedReq.response {
|
|
|
|
NotifiedRecvTxChans.remove <- *recvTxOP
|
|
|
|
} else {
|
|
|
|
// Notify frontends of new recv tx and mark as notified.
|
|
|
|
NotifiedRecvTxChans.add <- *recvTxOP
|
|
|
|
|
|
|
|
// need access to the RecvTxOut to get the json info object
|
|
|
|
NotifyNewTxDetails(allClients, a.Name(),
|
|
|
|
record.TxInfo(a.Name(), bs.Height, a.Wallet.Net())[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify frontends of new account balance.
|
|
|
|
confirmed := a.CalculateBalance(1)
|
|
|
|
unconfirmed := a.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(allClients, a.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(allClients, a.name, unconfirmed)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 19:03:23 +01:00
|
|
|
|
|
|
|
return nil
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NtfnBlockConnected handles btcd notifications resulting from newly
|
|
|
|
// connected blocks to the main blockchain.
|
|
|
|
//
|
|
|
|
// TODO(jrick): Send block time with notification. This will be used
|
|
|
|
// to mark wallet files with a possibly-better earliest block height,
|
|
|
|
// and will greatly reduce rescan times for wallets created with an
|
|
|
|
// out of sync btcd.
|
2014-02-28 19:03:23 +01:00
|
|
|
func NtfnBlockConnected(n btcjson.Cmd) error {
|
2014-01-03 19:34:37 +01:00
|
|
|
bcn, ok := n.(*btcws.BlockConnectedNtfn)
|
|
|
|
if !ok {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: unexpected type", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
hash, err := btcwire.NewShaHashFromStr(bcn.Hash)
|
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: invalid hash string", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the blockstamp for the newly-connected block.
|
|
|
|
bs := &wallet.BlockStamp{
|
|
|
|
Height: bcn.Height,
|
|
|
|
Hash: *hash,
|
|
|
|
}
|
|
|
|
curBlock.Lock()
|
|
|
|
curBlock.BlockStamp = *bs
|
|
|
|
curBlock.Unlock()
|
|
|
|
|
|
|
|
// btcd notifies btcwallet about transactions first, and then sends
|
|
|
|
// the new block notification. New balance notifications for txs
|
|
|
|
// in blocks are therefore sent here after all tx notifications
|
|
|
|
// have arrived and finished being processed by the handlers.
|
|
|
|
workers := NotifyBalanceRequest{
|
|
|
|
block: *hash,
|
|
|
|
wg: make(chan *sync.WaitGroup),
|
|
|
|
}
|
|
|
|
NotifyBalanceSyncerChans.access <- workers
|
|
|
|
if wg := <-workers.wg; wg != nil {
|
|
|
|
wg.Wait()
|
|
|
|
NotifyBalanceSyncerChans.remove <- *hash
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.BlockNotify(bs)
|
2014-01-03 19:34:37 +01:00
|
|
|
|
|
|
|
// Pass notification to frontends too.
|
2014-01-30 16:14:02 +01:00
|
|
|
marshaled, _ := n.MarshalJSON()
|
2014-02-18 04:18:30 +01:00
|
|
|
allClients <- marshaled
|
2014-02-28 19:03:23 +01:00
|
|
|
|
|
|
|
return nil
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NtfnBlockDisconnected handles btcd notifications resulting from
|
|
|
|
// blocks disconnected from the main chain in the event of a chain
|
|
|
|
// switch and notifies frontends of the new blockchain height.
|
2014-02-28 19:03:23 +01:00
|
|
|
func NtfnBlockDisconnected(n btcjson.Cmd) error {
|
2014-01-03 19:34:37 +01:00
|
|
|
bdn, ok := n.(*btcws.BlockDisconnectedNtfn)
|
|
|
|
if !ok {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: unexpected type", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
hash, err := btcwire.NewShaHashFromStr(bdn.Hash)
|
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: invalid hash string", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rollback Utxo and Tx data stores.
|
2014-01-30 16:14:02 +01:00
|
|
|
AcctMgr.Rollback(bdn.Height, hash)
|
2014-01-03 19:34:37 +01:00
|
|
|
|
|
|
|
// Pass notification to frontends too.
|
2014-01-30 16:14:02 +01:00
|
|
|
marshaled, _ := n.MarshalJSON()
|
2014-02-18 04:18:30 +01:00
|
|
|
allClients <- marshaled
|
2014-02-28 19:03:23 +01:00
|
|
|
|
|
|
|
return nil
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
// NtfnRedeemingTx handles btcd redeemingtx notifications resulting from a
|
|
|
|
// transaction spending a watched outpoint.
|
2014-02-28 19:03:23 +01:00
|
|
|
func NtfnRedeemingTx(n btcjson.Cmd) error {
|
2014-02-24 20:35:30 +01:00
|
|
|
cn, ok := n.(*btcws.RedeemingTxNtfn)
|
2014-01-03 19:34:37 +01:00
|
|
|
if !ok {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: unexpected type", n.Method())
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
rawTx, err := hex.DecodeString(cn.HexTx)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-03-17 15:24:14 +01:00
|
|
|
return fmt.Errorf("%v handler: bad hexstring: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
tx_, err := btcutil.NewTxFromBytes(rawTx)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: bad transaction bytes: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
|
|
|
|
2014-02-24 20:35:30 +01:00
|
|
|
block, err := parseBlock(cn.Block)
|
2014-01-03 19:34:37 +01:00
|
|
|
if err != nil {
|
2014-02-28 19:03:23 +01:00
|
|
|
return fmt.Errorf("%v handler: bad block: %v", n.Method(), err)
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|
2014-02-24 20:35:30 +01:00
|
|
|
AcctMgr.RecordSpendingTx(tx_, block)
|
2014-02-28 19:03:23 +01:00
|
|
|
|
|
|
|
return nil
|
2014-01-03 19:34:37 +01:00
|
|
|
}
|