2013-08-06 23:55:22 +02:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2013-08-14 22:55:31 +02:00
|
|
|
"bytes"
|
|
|
|
"code.google.com/p/go.net/websocket"
|
2013-10-01 22:43:45 +02:00
|
|
|
"encoding/base64"
|
2013-08-14 22:55:31 +02:00
|
|
|
"encoding/hex"
|
2013-08-06 23:55:22 +02:00
|
|
|
"encoding/json"
|
2013-08-14 22:55:31 +02:00
|
|
|
"errors"
|
2013-10-01 22:43:45 +02:00
|
|
|
"fmt"
|
2013-08-06 23:55:22 +02:00
|
|
|
"github.com/conformal/btcchain"
|
2013-08-14 22:55:31 +02:00
|
|
|
"github.com/conformal/btcdb"
|
2013-08-06 23:55:22 +02:00
|
|
|
"github.com/conformal/btcjson"
|
|
|
|
"github.com/conformal/btcscript"
|
2013-10-08 18:37:06 +02:00
|
|
|
"github.com/conformal/btcutil"
|
2013-08-06 23:55:22 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-11-06 17:20:36 +01:00
|
|
|
"github.com/conformal/btcws"
|
2013-08-06 23:55:22 +02:00
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2013-10-03 01:33:42 +02:00
|
|
|
"sync/atomic"
|
2013-08-06 23:55:22 +02:00
|
|
|
)
|
|
|
|
|
2013-08-14 22:55:31 +02:00
|
|
|
// Errors
|
|
|
|
var (
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
// ErrBadParamsField describes an error where the parameters JSON
|
|
|
|
// field cannot be properly parsed.
|
2013-08-14 22:55:31 +02:00
|
|
|
ErrBadParamsField = errors.New("bad params field")
|
|
|
|
)
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// rpcServer holds the items the rpc server may need to access (config,
|
|
|
|
// shutdown, main server, etc.)
|
|
|
|
type rpcServer struct {
|
2013-10-03 01:33:42 +02:00
|
|
|
started int32
|
|
|
|
shutdown int32
|
2013-08-07 17:38:39 +02:00
|
|
|
server *server
|
2013-08-14 22:55:31 +02:00
|
|
|
ws wsContext
|
2013-08-07 17:38:39 +02:00
|
|
|
wg sync.WaitGroup
|
|
|
|
rpcport string
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
listeners []net.Listener
|
2013-08-14 22:55:31 +02:00
|
|
|
quit chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
// wsContext holds the items the RPC server needs to handle websocket
|
|
|
|
// connections for wallets.
|
|
|
|
type wsContext struct {
|
2013-10-23 17:07:00 +02:00
|
|
|
// walletListeners holds a map of each currently connected wallet
|
|
|
|
// listener as the key. The value is ignored, as this is only used as
|
|
|
|
// a set. A mutex is used to prevent incorrect multiple access.
|
|
|
|
walletListeners struct {
|
|
|
|
sync.RWMutex
|
|
|
|
m map[chan []byte]bool
|
|
|
|
}
|
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
// requests holds all wallet notification requests.
|
|
|
|
requests wsRequests
|
2013-08-14 22:55:31 +02:00
|
|
|
|
|
|
|
// Any chain notifications meant to be received by every connected
|
|
|
|
// wallet are sent across this channel.
|
|
|
|
walletNotificationMaster chan []byte
|
|
|
|
}
|
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
// wsRequests maps request contexts for wallet notifications to a
|
|
|
|
// wallet notification channel. A Mutex is used to protect incorrect
|
|
|
|
// concurrent access to the map.
|
|
|
|
type wsRequests struct {
|
|
|
|
sync.Mutex
|
|
|
|
m map[chan []byte]*requestContexts
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrCreateContexts gets the request contexts, or creates and adds a
|
|
|
|
// new context if one for this wallet is not already present.
|
|
|
|
func (r *wsRequests) getOrCreateContexts(walletNotification chan []byte) *requestContexts {
|
|
|
|
rc, ok := r.m[walletNotification]
|
|
|
|
if !ok {
|
|
|
|
rc = &requestContexts{
|
2013-11-04 17:59:48 +01:00
|
|
|
// The key is a stringified addressHash.
|
2013-11-04 19:50:24 +01:00
|
|
|
txRequests: make(map[string]interface{}),
|
2013-11-04 17:59:48 +01:00
|
|
|
|
2013-10-23 17:07:00 +02:00
|
|
|
spentRequests: make(map[btcwire.OutPoint]interface{}),
|
|
|
|
minedTxRequests: make(map[btcwire.ShaHash]bool),
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
}
|
|
|
|
r.m[walletNotification] = rc
|
|
|
|
}
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTxRequest adds the request context for new transaction notifications.
|
2013-11-04 17:59:48 +01:00
|
|
|
func (r *wsRequests) AddTxRequest(walletNotification chan []byte, addrhash string, id interface{}) {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
rc := r.getOrCreateContexts(walletNotification)
|
2013-11-04 17:59:48 +01:00
|
|
|
rc.txRequests[addrhash] = id
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddSpentRequest adds a request context for notifications of a spent
|
|
|
|
// Outpoint.
|
|
|
|
func (r *wsRequests) AddSpentRequest(walletNotification chan []byte, op *btcwire.OutPoint, id interface{}) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
rc := r.getOrCreateContexts(walletNotification)
|
|
|
|
rc.spentRequests[*op] = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveSpentRequest removes a request context for notifications of a
|
|
|
|
// spent Outpoint.
|
|
|
|
func (r *wsRequests) RemoveSpentRequest(walletNotification chan []byte, op *btcwire.OutPoint) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
rc := r.getOrCreateContexts(walletNotification)
|
|
|
|
delete(rc.spentRequests, *op)
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:07:00 +02:00
|
|
|
// AddMinedTxRequest adds request contexts for notifications of a
|
|
|
|
// mined transaction.
|
|
|
|
func (r *wsRequests) AddMinedTxRequest(walletNotification chan []byte, txID *btcwire.ShaHash) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
rc := r.getOrCreateContexts(walletNotification)
|
|
|
|
rc.minedTxRequests[*txID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMinedTxRequest removes request contexts for notifications of a
|
|
|
|
// mined transaction.
|
|
|
|
func (r *wsRequests) RemoveMinedTxRequest(walletNotification chan []byte, txID *btcwire.ShaHash) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
rc := r.getOrCreateContexts(walletNotification)
|
|
|
|
delete(rc.minedTxRequests, *txID)
|
|
|
|
}
|
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
// CloseListeners removes all request contexts for notifications sent
|
|
|
|
// to a wallet notification channel and closes the channel to stop all
|
|
|
|
// goroutines currently serving that wallet.
|
|
|
|
func (r *wsRequests) CloseListeners(walletNotification chan []byte) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
delete(r.m, walletNotification)
|
|
|
|
close(walletNotification)
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestContexts holds all requests for a single wallet connection.
|
|
|
|
type requestContexts struct {
|
|
|
|
// txRequests maps between a 160-byte pubkey hash and the JSON
|
|
|
|
// id of the requester so replies can be correctly routed back
|
2013-11-04 17:59:48 +01:00
|
|
|
// to the correct btcwallet callback. The key must be a stringified
|
|
|
|
// address hash.
|
|
|
|
txRequests map[string]interface{}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
|
|
|
// spentRequests maps between an Outpoint of an unspent
|
|
|
|
// transaction output and the JSON id of the requester so
|
|
|
|
// replies can be correctly routed back to the correct
|
|
|
|
// btcwallet callback.
|
|
|
|
spentRequests map[btcwire.OutPoint]interface{}
|
2013-10-23 17:07:00 +02:00
|
|
|
|
|
|
|
// minedTxRequests holds a set of transaction IDs (tx hashes) of
|
|
|
|
// transactions created by a wallet. A wallet may request
|
|
|
|
// notifications of when a tx it created is mined into a block and
|
|
|
|
// removed from the mempool. Once a tx has been mined into a
|
|
|
|
// block, wallet may remove the raw transaction from its unmined tx
|
|
|
|
// pool.
|
|
|
|
minedTxRequests map[btcwire.ShaHash]bool
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start is used by server.go to start the rpc listener.
|
|
|
|
func (s *rpcServer) Start() {
|
2013-10-03 01:33:42 +02:00
|
|
|
if atomic.AddInt32(&s.started, 1) != 1 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Trace("RPCS: Starting RPC server")
|
2013-10-23 17:45:43 +02:00
|
|
|
rpcServeMux := http.NewServeMux()
|
|
|
|
httpServer := &http.Server{Handler: rpcServeMux}
|
|
|
|
rpcServeMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
2013-10-01 22:43:45 +02:00
|
|
|
login := s.username + ":" + s.password
|
|
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
|
2013-10-03 14:12:18 +02:00
|
|
|
authhdr := r.Header["Authorization"]
|
|
|
|
if len(authhdr) > 0 && authhdr[0] == auth {
|
2013-10-01 22:43:45 +02:00
|
|
|
jsonRPCRead(w, r, s)
|
|
|
|
} else {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Warnf("RPCS: Auth failure.")
|
2013-10-01 22:43:45 +02:00
|
|
|
jsonAuthFail(w, r, s)
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
})
|
2013-08-14 22:55:31 +02:00
|
|
|
go s.walletListenerDuplicator()
|
2013-10-23 17:45:43 +02:00
|
|
|
rpcServeMux.Handle("/wallet", websocket.Handler(func(ws *websocket.Conn) {
|
2013-08-14 22:55:31 +02:00
|
|
|
s.walletReqsNotifications(ws)
|
|
|
|
}))
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
2013-09-13 00:24:37 +02:00
|
|
|
s.wg.Add(1)
|
2013-08-07 17:38:39 +02:00
|
|
|
go func(listener net.Listener) {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Infof("RPCS: RPC server listening on %s", listener.Addr())
|
2013-08-07 17:38:39 +02:00
|
|
|
httpServer.Serve(listener)
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Tracef("RPCS: RPC listener done for %s", listener.Addr())
|
2013-08-07 17:38:39 +02:00
|
|
|
s.wg.Done()
|
|
|
|
}(listener)
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is used by server.go to stop the rpc listener.
|
|
|
|
func (s *rpcServer) Stop() error {
|
2013-10-03 01:33:42 +02:00
|
|
|
if atomic.AddInt32(&s.shutdown, 1) != 1 {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Infof("RPCS: RPC server is already in the process of shutting down")
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Warnf("RPCS: RPC server shutting down")
|
2013-08-07 17:38:39 +02:00
|
|
|
for _, listener := range s.listeners {
|
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Errorf("RPCS: Problem shutting down rpc: %v", err)
|
2013-08-07 17:38:39 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Infof("RPCS: RPC server shutdown complete")
|
2013-08-06 23:55:22 +02:00
|
|
|
s.wg.Wait()
|
2013-08-14 22:55:31 +02:00
|
|
|
close(s.quit)
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-18 07:36:40 +02:00
|
|
|
// newRPCServer returns a new instance of the rpcServer struct.
|
|
|
|
func newRPCServer(s *server) (*rpcServer, error) {
|
2013-08-06 23:55:22 +02:00
|
|
|
rpc := rpcServer{
|
|
|
|
server: s,
|
2013-10-20 18:05:35 +02:00
|
|
|
quit: make(chan int),
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
// Get values from config
|
2013-09-18 07:36:40 +02:00
|
|
|
rpc.rpcport = cfg.RPCPort
|
|
|
|
rpc.username = cfg.RPCUser
|
|
|
|
rpc.password = cfg.RPCPass
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-08-14 22:55:31 +02:00
|
|
|
// initialize memory for websocket connections
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
rpc.ws.requests.m = make(map[chan []byte]*requestContexts)
|
2013-10-23 17:07:00 +02:00
|
|
|
rpc.ws.walletListeners.m = make(map[chan []byte]bool)
|
2013-08-14 22:55:31 +02:00
|
|
|
rpc.ws.walletNotificationMaster = make(chan []byte)
|
|
|
|
|
2013-08-07 17:38:39 +02:00
|
|
|
// IPv4 listener.
|
|
|
|
var listeners []net.Listener
|
|
|
|
listenAddr4 := net.JoinHostPort("127.0.0.1", rpc.rpcport)
|
|
|
|
listener4, err := net.Listen("tcp4", listenAddr4)
|
|
|
|
if err != nil {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Errorf("RPCS: Couldn't create listener: %v", err)
|
2013-08-07 17:38:39 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listeners = append(listeners, listener4)
|
|
|
|
|
|
|
|
// IPv6 listener.
|
|
|
|
listenAddr6 := net.JoinHostPort("::1", rpc.rpcport)
|
|
|
|
listener6, err := net.Listen("tcp6", listenAddr6)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Errorf("RPCS: Couldn't create listener: %v", err)
|
2013-08-06 23:55:22 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-07 17:38:39 +02:00
|
|
|
listeners = append(listeners, listener6)
|
|
|
|
|
|
|
|
rpc.listeners = listeners
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
return &rpc, err
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:43:45 +02:00
|
|
|
// jsonAuthFail sends a message back to the client if the http auth is rejected.
|
|
|
|
func jsonAuthFail(w http.ResponseWriter, r *http.Request, s *rpcServer) {
|
|
|
|
fmt.Fprint(w, "401 Unauthorized.\n")
|
|
|
|
}
|
|
|
|
|
2013-08-14 22:55:31 +02:00
|
|
|
// jsonRPCRead is the RPC wrapper around the jsonRead function to handles
|
|
|
|
// reading and responding to RPC messages.
|
2013-09-18 07:36:40 +02:00
|
|
|
func jsonRPCRead(w http.ResponseWriter, r *http.Request, s *rpcServer) {
|
2013-08-06 23:55:22 +02:00
|
|
|
r.Close = true
|
2013-10-03 01:33:42 +02:00
|
|
|
if atomic.LoadInt32(&s.shutdown) != 0 {
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
body, err := btcjson.GetRaw(r.Body)
|
|
|
|
if err != nil {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Errorf("RPCS: Error getting json message: %v", err)
|
2013-08-06 23:55:22 +02:00
|
|
|
return
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
// Error is intentionally ignored here. It's used in in the
|
|
|
|
// websocket handler to tell when a method is not supported by
|
|
|
|
// the standard RPC API, and is not needed here. Error logging
|
|
|
|
// is done inside jsonRead, so no need to log the error here.
|
2013-10-23 17:07:00 +02:00
|
|
|
reply, _ := jsonRead(body, s, nil)
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
log.Tracef("[RPCS] reply: %v", reply)
|
2013-08-14 22:55:31 +02:00
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
msg, err := btcjson.MarshallAndSend(reply, w)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf(msg)
|
|
|
|
return
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
log.Debugf(msg)
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
// TODO(jrick): Remove the wallet notification chan.
|
2013-10-29 16:42:34 +01:00
|
|
|
type commandHandler func(*rpcServer, btcjson.Cmd, chan []byte) (interface{}, error)
|
|
|
|
|
|
|
|
var handlers = map[string]commandHandler{
|
2013-10-29 18:18:53 +01:00
|
|
|
"addnode": handleAddNode,
|
2013-10-29 16:42:34 +01:00
|
|
|
"decoderawtransaction": handleDecodeRawTransaction,
|
|
|
|
"getbestblockhash": handleGetBestBlockHash,
|
|
|
|
"getblock": handleGetBlock,
|
|
|
|
"getblockcount": handleGetBlockCount,
|
|
|
|
"getblockhash": handleGetBlockHash,
|
|
|
|
"getconnectioncount": handleGetConnectionCount,
|
|
|
|
"getdifficulty": handleGetDifficulty,
|
|
|
|
"getgenerate": handleGetGenerate,
|
|
|
|
"gethashespersec": handleGetHashesPerSec,
|
2013-10-21 19:45:30 +02:00
|
|
|
"getpeerinfo": handleGetPeerInfo,
|
2013-10-29 16:42:34 +01:00
|
|
|
"getrawmempool": handleGetRawMempool,
|
|
|
|
"getrawtransaction": handleGetRawTransaction,
|
|
|
|
"sendrawtransaction": handleSendRawTransaction,
|
|
|
|
"setgenerate": handleSetGenerate,
|
|
|
|
"stop": handleStop,
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
type wsCommandHandler func(*rpcServer, btcjson.Cmd, chan []byte) error
|
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
var wsHandlers = map[string]wsCommandHandler{
|
|
|
|
"getcurrentnet": handleGetCurrentNet,
|
|
|
|
"getbestblock": handleGetBestBlock,
|
|
|
|
"rescan": handleRescan,
|
2013-11-06 17:20:36 +01:00
|
|
|
"notifynewtxs": handleNotifyNewTXs,
|
2013-11-04 19:50:24 +01:00
|
|
|
"notifyspent": handleNotifySpent,
|
|
|
|
}
|
|
|
|
|
2013-10-29 18:18:53 +01:00
|
|
|
// handleDecodeRawTransaction handles decoderawtransaction commands.
|
|
|
|
func handleAddNode(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) (interface{}, error) {
|
|
|
|
c := cmd.(*btcjson.AddNodeCmd)
|
|
|
|
|
|
|
|
addr := normalizePeerAddress(c.Addr)
|
|
|
|
var err error
|
|
|
|
switch c.SubCmd {
|
|
|
|
case "add":
|
|
|
|
err = s.server.AddAddr(addr, true)
|
|
|
|
case "remove":
|
|
|
|
err = s.server.RemoveAddr(addr)
|
|
|
|
case "onetry":
|
|
|
|
err = s.server.AddAddr(addr, false)
|
|
|
|
default:
|
|
|
|
err = errors.New("Invalid subcommand for addnode")
|
|
|
|
}
|
|
|
|
|
|
|
|
// no data returned unless an error.
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleDecodeRawTransaction handles decoderawtransaction commands.
|
|
|
|
func handleDecodeRawTransaction(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) (interface{}, error) {
|
|
|
|
// TODO: use c.HexTx and fill result with info.
|
|
|
|
return btcjson.TxRawDecodeResult{}, nil
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetBestBlockHash implements the getbestblockhash command.
|
|
|
|
func handleGetBestBlockHash(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
var sha *btcwire.ShaHash
|
|
|
|
sha, _, err := s.server.db.NewestSha()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBestBlockHash
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return sha.String, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetBlock implements the getblock command.
|
|
|
|
func handleGetBlock(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
c := cmd.(*btcjson.GetBlockCmd)
|
|
|
|
sha, err := btcwire.NewShaHashFromStr(c.Hash)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error generating sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
|
|
|
}
|
|
|
|
blk, err := s.server.db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error fetching sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
|
|
|
}
|
|
|
|
idx := blk.Height()
|
|
|
|
buf, err := blk.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error fetching block: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
txList, _ := blk.TxShas()
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
txNames := make([]string, len(txList))
|
|
|
|
for i, v := range txList {
|
|
|
|
txNames[i] = v.String()
|
|
|
|
}
|
2013-10-28 23:53:16 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
_, maxidx, err := s.server.db.NewestSha()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
blockHeader := &blk.MsgBlock().Header
|
|
|
|
blockReply := btcjson.BlockResult{
|
|
|
|
Hash: c.Hash,
|
|
|
|
Version: blockHeader.Version,
|
|
|
|
MerkleRoot: blockHeader.MerkleRoot.String(),
|
|
|
|
PreviousHash: blockHeader.PrevBlock.String(),
|
|
|
|
Nonce: blockHeader.Nonce,
|
|
|
|
Time: blockHeader.Timestamp.Unix(),
|
|
|
|
Confirmations: uint64(1 + maxidx - idx),
|
|
|
|
Height: idx,
|
|
|
|
Tx: txNames,
|
|
|
|
Size: len(buf),
|
|
|
|
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
|
|
|
|
Difficulty: getDifficultyRatio(blockHeader.Bits),
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// Get next block unless we are already at the top.
|
|
|
|
if idx < maxidx {
|
|
|
|
var shaNext *btcwire.ShaHash
|
|
|
|
shaNext, err = s.server.db.FetchBlockShaByHeight(int64(idx + 1))
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-10-29 16:42:34 +01:00
|
|
|
log.Errorf("RPCS: No next block: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
blockReply.NextHash = shaNext.String()
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return blockReply, nil
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetBlockCount implements the getblockcount command.
|
|
|
|
func handleGetBlockCount(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
_, maxidx, err := s.server.db.NewestSha()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error getting newest sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockCount
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return maxidx, nil
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetBlockHash implements the getblockhash command.
|
|
|
|
func handleGetBlockHash(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
c := cmd.(*btcjson.GetBlockHashCmd)
|
|
|
|
sha, err := s.server.db.FetchBlockShaByHeight(c.Index)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("[RCPS] Error getting block: %v", err)
|
|
|
|
return nil, btcjson.ErrOutOfRange
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return sha.String(), nil
|
|
|
|
}
|
2013-08-06 23:55:22 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetConnectionCount implements the getconnectioncount command.
|
|
|
|
func handleGetConnectionCount(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
2013-10-21 19:45:30 +02:00
|
|
|
return s.server.ConnectedCount(), nil
|
2013-10-29 16:42:34 +01:00
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetDifficulty implements the getdifficulty command.
|
|
|
|
func handleGetDifficulty(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
sha, _, err := s.server.db.NewestSha()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error getting sha: %v", err)
|
|
|
|
return nil, btcjson.ErrDifficulty
|
|
|
|
}
|
|
|
|
blk, err := s.server.db.FetchBlockBySha(sha)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error getting block: %v", err)
|
|
|
|
return nil, btcjson.ErrDifficulty
|
|
|
|
}
|
|
|
|
blockHeader := &blk.MsgBlock().Header
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return getDifficultyRatio(blockHeader.Bits), nil
|
|
|
|
}
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetGenerate implements the getgenerate command.
|
|
|
|
func handleGetGenerate(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
// btcd does not do mining so we can hardcode replies here.
|
|
|
|
return false, nil
|
|
|
|
}
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetHashesPerSec implements the gethashespersec command.
|
|
|
|
func handleGetHashesPerSec(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
// btcd does not do mining so we can hardcode replies here.
|
|
|
|
return 0, nil
|
|
|
|
}
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-21 19:45:30 +02:00
|
|
|
// handleGetPeerInfo implements the getpeerinfo command.
|
|
|
|
func handleGetPeerInfo(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
2013-10-29 18:18:53 +01:00
|
|
|
return s.server.PeerInfo(), nil
|
2013-10-21 19:45:30 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// handleGetRawMempool implements the getrawmempool command.
|
|
|
|
func handleGetRawMempool(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
hashes := s.server.txMemPool.TxShas()
|
|
|
|
hashStrings := make([]string, len(hashes))
|
|
|
|
for i := 0; i < len(hashes); i++ {
|
|
|
|
hashStrings[i] = hashes[i].String()
|
|
|
|
}
|
|
|
|
return hashStrings, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetRawTransaction implements the getrawtransaction command.
|
|
|
|
func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
c := cmd.(*btcjson.GetRawTransactionCmd)
|
|
|
|
if c.Verbose {
|
|
|
|
// TODO: check error code. tx is not checked before
|
|
|
|
// this point.
|
|
|
|
txSha, _ := btcwire.NewShaHashFromStr(c.Txid)
|
|
|
|
txList, err := s.server.db.FetchTxBySha(txSha)
|
2013-10-29 01:43:09 +01:00
|
|
|
if err != nil {
|
2013-10-29 16:42:34 +01:00
|
|
|
log.Errorf("RPCS: Error fetching tx: %v", err)
|
|
|
|
return nil, btcjson.ErrNoTxInfo
|
2013-10-29 01:43:09 +01:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
|
|
|
|
lastTx := len(txList) - 1
|
|
|
|
txS := txList[lastTx].Tx
|
|
|
|
blksha := txList[lastTx].BlkSha
|
|
|
|
blk, err := s.server.db.FetchBlockBySha(blksha)
|
2013-10-29 01:43:09 +01:00
|
|
|
if err != nil {
|
2013-10-29 16:42:34 +01:00
|
|
|
log.Errorf("RPCS: Error fetching sha: %v", err)
|
|
|
|
return nil, btcjson.ErrBlockNotFound
|
2013-10-29 01:43:09 +01:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
idx := blk.Height()
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
txOutList := txS.TxOut
|
|
|
|
voutList := make([]btcjson.Vout, len(txOutList))
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
txInList := txS.TxIn
|
|
|
|
vinList := make([]btcjson.Vin, len(txInList))
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
for i, v := range txInList {
|
|
|
|
vinList[i].Sequence = float64(v.Sequence)
|
|
|
|
disbuf, _ := btcscript.DisasmString(v.SignatureScript)
|
|
|
|
vinList[i].ScriptSig.Asm = strings.Replace(disbuf, " ", "", -1)
|
|
|
|
vinList[i].Vout = i + 1
|
|
|
|
log.Debugf(disbuf)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
for i, v := range txOutList {
|
|
|
|
voutList[i].N = i
|
|
|
|
voutList[i].Value = float64(v.Value) / 100000000
|
|
|
|
isbuf, _ := btcscript.DisasmString(v.PkScript)
|
|
|
|
voutList[i].ScriptPubKey.Asm = isbuf
|
|
|
|
voutList[i].ScriptPubKey.ReqSig = strings.Count(isbuf, "OP_CHECKSIG")
|
|
|
|
_, addrhash, err := btcscript.ScriptToAddrHash(v.PkScript)
|
2013-08-06 23:55:22 +02:00
|
|
|
if err != nil {
|
2013-10-29 16:42:34 +01:00
|
|
|
// TODO: set and return error?
|
|
|
|
log.Errorf("RPCS: Error getting address hash for %v: %v", txSha, err)
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
if addr, err := btcutil.EncodeAddress(addrhash, s.server.btcnet); err != nil {
|
|
|
|
// TODO: set and return error?
|
|
|
|
addrList := make([]string, 1)
|
|
|
|
addrList[0] = addr
|
|
|
|
voutList[i].ScriptPubKey.Addresses = addrList
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
_, maxidx, err := s.server.db.NewestSha()
|
2013-08-14 22:55:31 +02:00
|
|
|
if err != nil {
|
2013-10-29 16:42:34 +01:00
|
|
|
log.Errorf("RPCS: Cannot get newest sha: %v", err)
|
|
|
|
return nil, btcjson.ErrNoNewestBlockInfo
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
confirmations := uint64(1 + maxidx - idx)
|
|
|
|
|
|
|
|
blockHeader := &blk.MsgBlock().Header
|
|
|
|
txReply := btcjson.TxRawResult{
|
|
|
|
Txid: c.Txid,
|
|
|
|
Vout: voutList,
|
|
|
|
Vin: vinList,
|
|
|
|
Version: txS.Version,
|
|
|
|
LockTime: txS.LockTime,
|
|
|
|
// This is not a typo, they are identical in
|
|
|
|
// bitcoind as well.
|
|
|
|
Time: blockHeader.Timestamp.Unix(),
|
|
|
|
Blocktime: blockHeader.Timestamp.Unix(),
|
|
|
|
BlockHash: blksha.String(),
|
|
|
|
Confirmations: confirmations,
|
|
|
|
}
|
|
|
|
return txReply, nil
|
|
|
|
} else {
|
|
|
|
// Don't return details
|
|
|
|
// not used yet
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleSendRawTransaction implements the sendrawtransaction command.
|
|
|
|
func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
c := cmd.(*btcjson.SendRawTransactionCmd)
|
|
|
|
// Deserialize and send off to tx relay
|
|
|
|
serializedTx, err := hex.DecodeString(c.HexTx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, btcjson.ErrDecodeHexString
|
|
|
|
}
|
|
|
|
msgtx := btcwire.NewMsgTx()
|
|
|
|
err = msgtx.Deserialize(bytes.NewBuffer(serializedTx))
|
|
|
|
if err != nil {
|
|
|
|
err := btcjson.Error{
|
2013-10-30 01:41:38 +01:00
|
|
|
Code: btcjson.ErrDeserialization.Code,
|
2013-10-29 16:42:34 +01:00
|
|
|
Message: "Unable to deserialize raw tx",
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-10-31 06:28:37 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
tx := btcutil.NewTx(msgtx)
|
|
|
|
err = s.server.txMemPool.ProcessTransaction(tx)
|
|
|
|
if err != nil {
|
2013-10-30 20:11:11 +01:00
|
|
|
// When the error is a rule error, it means the transaction was
|
|
|
|
// simply rejected as opposed to something actually going wrong,
|
|
|
|
// so log it as such. Otherwise, something really did go wrong,
|
|
|
|
// so log it as an actual error.
|
|
|
|
if _, ok := err.(TxRuleError); ok {
|
2013-10-31 00:40:55 +01:00
|
|
|
log.Debugf("RPCS: Rejected transaction %v: %v", tx.Sha(),
|
|
|
|
err)
|
2013-10-30 20:11:11 +01:00
|
|
|
} else {
|
2013-10-31 00:40:55 +01:00
|
|
|
log.Errorf("RPCS: Failed to process transaction %v: %v",
|
|
|
|
tx.Sha(), err)
|
2013-10-30 20:11:11 +01:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
err = btcjson.Error{
|
2013-10-30 01:41:38 +01:00
|
|
|
Code: btcjson.ErrDeserialization.Code,
|
2013-10-29 16:42:34 +01:00
|
|
|
Message: "Failed to process transaction",
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-29 16:42:34 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
// If called from websocket code, add a mined tx hashes
|
|
|
|
// request.
|
|
|
|
if walletNotification != nil {
|
2013-10-31 06:28:37 +01:00
|
|
|
s.ws.requests.AddMinedTxRequest(walletNotification, tx.Sha())
|
2013-10-29 16:42:34 +01:00
|
|
|
}
|
2013-10-23 17:07:00 +02:00
|
|
|
|
2013-10-31 06:28:37 +01:00
|
|
|
return tx.Sha().String(), nil
|
2013-10-29 16:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleSetGenerate implements the setgenerate command.
|
|
|
|
func handleSetGenerate(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
// btcd does not do mining so we can hardcode replies here.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleStop implements the stop command.
|
|
|
|
func handleStop(s *rpcServer, cmd btcjson.Cmd, walletNotification chan []byte) (interface{}, error) {
|
|
|
|
s.server.Stop()
|
|
|
|
return "btcd stopping.", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// jsonRead abstracts the JSON unmarshalling and reply handling used
|
|
|
|
// by both RPC and websockets. If called from websocket code, a non-nil
|
|
|
|
// wallet notification channel can be used to automatically register
|
|
|
|
// various notifications for the wallet.
|
|
|
|
func jsonRead(body []byte, s *rpcServer, walletNotification chan []byte) (reply btcjson.Reply, err error) {
|
|
|
|
cmd, err := btcjson.ParseMarshaledCmd(body)
|
|
|
|
if err != nil {
|
2013-10-30 04:23:22 +01:00
|
|
|
var id interface{}
|
|
|
|
if cmd != nil {
|
|
|
|
// Unmarshaling a valid JSON-RPC message succeeded. Use
|
|
|
|
// the provided id for errors.
|
|
|
|
id = cmd.Id()
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-10-30 04:23:22 +01:00
|
|
|
if jsonErr, ok := err.(btcjson.Error); ok {
|
|
|
|
reply = btcjson.Reply{
|
|
|
|
Result: nil,
|
|
|
|
Error: &jsonErr,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reply = btcjson.Reply{
|
|
|
|
Result: nil,
|
|
|
|
Error: &jsonErr,
|
|
|
|
Id: &id,
|
|
|
|
}
|
2013-10-29 01:43:09 +01:00
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
log.Tracef("RPCS: reply: %v", reply)
|
|
|
|
|
2013-10-30 04:23:22 +01:00
|
|
|
return reply, err
|
2013-10-29 16:42:34 +01:00
|
|
|
}
|
|
|
|
log.Tracef("RPCS: received: %v", cmd)
|
|
|
|
|
|
|
|
id := cmd.Id()
|
2013-10-29 01:43:09 +01:00
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
handler, ok := handlers[cmd.Method()]
|
|
|
|
if !ok {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
reply = btcjson.Reply{
|
|
|
|
Result: nil,
|
2013-10-30 01:41:38 +01:00
|
|
|
Error: &btcjson.ErrMethodNotFound,
|
2013-10-28 23:53:16 +01:00
|
|
|
Id: &id,
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
}
|
2013-10-30 04:23:22 +01:00
|
|
|
return reply, btcjson.ErrMethodNotFound
|
2013-10-29 16:42:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result, err := handler(s, cmd, walletNotification)
|
|
|
|
if err != nil {
|
|
|
|
if jsonErr, ok := err.(btcjson.Error); ok {
|
|
|
|
reply = btcjson.Reply{
|
|
|
|
Error: &jsonErr,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
err = errors.New(jsonErr.Message)
|
|
|
|
} else {
|
|
|
|
// In the case where we did not have a btcjson
|
|
|
|
// error to begin with, make a new one to send,
|
|
|
|
// but this really should not happen.
|
|
|
|
rawJSONError := btcjson.Error{
|
2013-10-30 01:41:38 +01:00
|
|
|
Code: btcjson.ErrInternal.Code,
|
2013-10-29 16:42:34 +01:00
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
reply = btcjson.Reply{
|
|
|
|
Error: &rawJSONError,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reply = btcjson.Reply{
|
|
|
|
Result: result,
|
|
|
|
Id: &id,
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 16:42:34 +01:00
|
|
|
return reply, err
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
// handleGetCurrentNet implements the getcurrentnet command extension
|
|
|
|
// for websocket connections.
|
2013-11-06 17:20:36 +01:00
|
|
|
func handleGetCurrentNet(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) error {
|
|
|
|
|
|
|
|
id := cmd.Id()
|
|
|
|
reply := &btcjson.Reply{Id: &id}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
var net btcwire.BitcoinNet
|
|
|
|
if cfg.TestNet3 {
|
|
|
|
net = btcwire.TestNet3
|
|
|
|
} else {
|
|
|
|
net = btcwire.MainNet
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
reply.Result = float64(net)
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
// handleGetBestBlock implements the getbestblock command extension
|
|
|
|
// for websocket connections.
|
2013-11-06 17:20:36 +01:00
|
|
|
func handleGetBestBlock(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) error {
|
|
|
|
|
|
|
|
id := cmd.Id()
|
|
|
|
reply := &btcjson.Reply{Id: &id}
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
// All other "get block" commands give either the height, the
|
|
|
|
// hash, or both but require the block SHA. This gets both for
|
|
|
|
// the best block.
|
|
|
|
sha, height, err := s.server.db.NewestSha()
|
|
|
|
if err != nil {
|
2013-11-06 17:20:36 +01:00
|
|
|
return btcjson.ErrBestBlockHash
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
|
|
|
|
reply.Result = map[string]interface{}{
|
|
|
|
"hash": sha.String(),
|
|
|
|
"height": height,
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
// handleRescan implements the rescan command extension for websocket
|
|
|
|
// connections.
|
2013-11-06 17:20:36 +01:00
|
|
|
func handleRescan(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) error {
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
id := cmd.Id()
|
|
|
|
reply := &btcjson.Reply{Id: &id}
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
rescanCmd, ok := cmd.(*btcws.RescanCmd)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-10-31 03:56:45 +01:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
log.Debugf("RPCS: Begining rescan")
|
2013-10-31 03:56:45 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
minblock := int64(rescanCmd.BeginBlock)
|
|
|
|
maxblock := int64(rescanCmd.EndBlock)
|
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
// FetchHeightRange may not return a complete list of block shas for
|
|
|
|
// the given range, so fetch range as many times as necessary.
|
|
|
|
for {
|
2013-11-06 17:20:36 +01:00
|
|
|
blkshalist, err := s.server.db.FetchHeightRange(minblock,
|
|
|
|
maxblock)
|
2013-11-04 19:50:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
if len(blkshalist) == 0 {
|
|
|
|
break
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
for i := range blkshalist {
|
|
|
|
blk, err := s.server.db.FetchBlockBySha(&blkshalist[i])
|
2013-08-14 22:55:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
txShaList, err := blk.TxShas()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
txList := s.server.db.FetchTxByShaList(txShaList)
|
|
|
|
for _, txReply := range txList {
|
|
|
|
if txReply.Err != nil || txReply.Tx == nil {
|
|
|
|
continue
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
for txOutIdx, txout := range txReply.Tx.TxOut {
|
|
|
|
st, txaddrhash, err := btcscript.ScriptToAddrHash(txout.PkScript)
|
|
|
|
if st != btcscript.ScriptAddr || err != nil {
|
2013-10-20 18:50:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
txaddr, err := btcutil.EncodeAddress(txaddrhash, s.server.btcnet)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error encoding address: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
if _, ok := rescanCmd.Addresses[txaddr]; ok {
|
|
|
|
reply.Result = struct {
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Receiver string `json:"receiver"`
|
|
|
|
BlockHash string `json:"blockhash"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
TxHash string `json:"txhash"`
|
|
|
|
Index uint32 `json:"index"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
PkScript string `json:"pkscript"`
|
|
|
|
Spent bool `json:"spent"`
|
|
|
|
}{
|
|
|
|
Sender: "Unknown", // TODO(jrick)
|
|
|
|
Receiver: txaddr,
|
|
|
|
BlockHash: blkshalist[i].String(),
|
|
|
|
Height: blk.Height(),
|
|
|
|
TxHash: txReply.Sha.String(),
|
|
|
|
Index: uint32(txOutIdx),
|
|
|
|
Amount: txout.Value,
|
|
|
|
PkScript: btcutil.Base58Encode(txout.PkScript),
|
|
|
|
Spent: txReply.TxSpent[txOutIdx],
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-11-04 19:50:24 +01:00
|
|
|
if maxblock-minblock > int64(len(blkshalist)) {
|
|
|
|
minblock += int64(len(blkshalist))
|
|
|
|
} else {
|
|
|
|
break
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
|
|
|
|
log.Debug("RPCS: Finished rescan")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
// handleNotifyNewTXs implements the notifynewtxs command extension for
|
2013-11-04 19:50:24 +01:00
|
|
|
// websocket connections.
|
2013-11-06 17:20:36 +01:00
|
|
|
func handleNotifyNewTXs(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) error {
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
id := cmd.Id()
|
|
|
|
reply := &btcjson.Reply{Id: &id}
|
|
|
|
|
|
|
|
notifyCmd, ok := cmd.(*btcws.NotifyNewTXsCmd)
|
2013-11-04 19:50:24 +01:00
|
|
|
if !ok {
|
2013-11-06 17:20:36 +01:00
|
|
|
return btcjson.ErrInternal
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
|
|
|
|
for _, addr := range notifyCmd.Addresses {
|
|
|
|
hash, _, err := btcutil.DecodeAddress(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot decode address: %v", err)
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
s.ws.requests.AddTxRequest(walletNotification,
|
|
|
|
string(hash[:]), id)
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleNotifySpent implements the notifyspent command extension for
|
|
|
|
// websocket connections.
|
2013-11-06 17:20:36 +01:00
|
|
|
func handleNotifySpent(s *rpcServer, cmd btcjson.Cmd,
|
|
|
|
walletNotification chan []byte) error {
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
id := cmd.Id()
|
|
|
|
reply := &btcjson.Reply{Id: &id}
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
notifyCmd, ok := cmd.(*btcws.NotifySpentCmd)
|
|
|
|
if !ok {
|
|
|
|
return btcjson.ErrInternal
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
|
|
|
|
s.ws.requests.AddSpentRequest(walletNotification, notifyCmd.OutPoint, id)
|
|
|
|
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
func jsonWSRead(body []byte, s *rpcServer, walletNotification chan []byte) error {
|
|
|
|
var reply btcjson.Reply
|
|
|
|
|
|
|
|
cmd, err := btcjson.ParseMarshaledCmd(body)
|
2013-11-04 19:50:24 +01:00
|
|
|
if err != nil {
|
2013-11-06 17:20:36 +01:00
|
|
|
if cmd != nil {
|
|
|
|
// Unmarshaling a valid JSON-RPC message succeeded. Use
|
|
|
|
// the provided id for errors.
|
|
|
|
id := cmd.Id()
|
|
|
|
reply.Id = &id
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
jsonErr, ok := err.(btcjson.Error)
|
|
|
|
if !ok {
|
|
|
|
jsonErr = btcjson.Error{
|
|
|
|
Code: btcjson.ErrMisc.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reply.Error = &jsonErr
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
|
|
|
return err
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
id := cmd.Id()
|
|
|
|
reply.Id = &id
|
2013-11-04 19:50:24 +01:00
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
wsHandler, ok := wsHandlers[cmd.Method()]
|
2013-11-04 19:50:24 +01:00
|
|
|
if !ok {
|
2013-11-06 17:20:36 +01:00
|
|
|
reply.Error = &btcjson.ErrMethodNotFound
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
2013-11-04 19:50:24 +01:00
|
|
|
return btcjson.ErrMethodNotFound
|
|
|
|
}
|
|
|
|
|
2013-11-06 17:20:36 +01:00
|
|
|
if err := wsHandler(s, cmd, walletNotification); err != nil {
|
|
|
|
jsonErr, ok := err.(btcjson.Error)
|
|
|
|
if ok {
|
|
|
|
reply.Error = &jsonErr
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
|
|
|
return errors.New(jsonErr.Message)
|
2013-11-04 19:50:24 +01:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
|
|
|
|
// In the case where we did not have a btcjson
|
|
|
|
// error to begin with, make a new one to send,
|
|
|
|
// but this really should not happen.
|
|
|
|
jsonErr = btcjson.Error{
|
|
|
|
Code: btcjson.ErrInternal.Code,
|
|
|
|
Message: err.Error(),
|
|
|
|
}
|
|
|
|
reply.Error = &jsonErr
|
|
|
|
mreply, _ := json.Marshal(reply)
|
|
|
|
walletNotification <- mreply
|
|
|
|
return err
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
2013-11-06 17:20:36 +01:00
|
|
|
return nil
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getDifficultyRatio returns the proof-of-work difficulty as a multiple of the
|
|
|
|
// minimum difficulty using the passed bits field from the header of a block.
|
|
|
|
func getDifficultyRatio(bits uint32) float64 {
|
|
|
|
// The minimum difficulty is the max possible proof-of-work limit bits
|
|
|
|
// converted back to a number. Note this is not the same as the the
|
|
|
|
// proof of work limit directly because the block difficulty is encoded
|
|
|
|
// in a block with the compact form which loses precision.
|
|
|
|
max := btcchain.CompactToBig(activeNetParams.powLimitBits)
|
|
|
|
target := btcchain.CompactToBig(bits)
|
|
|
|
|
|
|
|
difficulty := new(big.Rat).SetFrac(max, target)
|
|
|
|
outString := difficulty.FloatString(2)
|
|
|
|
diff, err := strconv.ParseFloat(outString, 64)
|
|
|
|
if err != nil {
|
2013-10-10 21:13:54 +02:00
|
|
|
log.Errorf("RPCS: Cannot get difficulty: %v", err)
|
2013-08-06 23:55:22 +02:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return diff
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
|
|
|
// AddWalletListener adds a channel to listen for new messages from a
|
|
|
|
// wallet.
|
|
|
|
func (s *rpcServer) AddWalletListener(c chan []byte) {
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.Lock()
|
|
|
|
s.ws.walletListeners.m[c] = true
|
|
|
|
s.ws.walletListeners.Unlock()
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveWalletListener removes a wallet listener channel.
|
|
|
|
func (s *rpcServer) RemoveWalletListener(c chan []byte) {
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.Lock()
|
|
|
|
delete(s.ws.walletListeners.m, c)
|
|
|
|
s.ws.walletListeners.Unlock()
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// walletListenerDuplicator listens for new wallet listener channels
|
|
|
|
// and duplicates messages sent to walletNotificationMaster to all
|
|
|
|
// connected listeners.
|
|
|
|
func (s *rpcServer) walletListenerDuplicator() {
|
|
|
|
// Duplicate all messages sent across walletNotificationMaster to each
|
|
|
|
// listening wallet.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ntfn := <-s.ws.walletNotificationMaster:
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.RLock()
|
|
|
|
for c := range s.ws.walletListeners.m {
|
2013-08-14 22:55:31 +02:00
|
|
|
c <- ntfn
|
|
|
|
}
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.RUnlock()
|
2013-08-14 22:55:31 +02:00
|
|
|
|
|
|
|
case <-s.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// walletReqsNotifications is the handler function for websocket
|
|
|
|
// connections from a btcwallet instance. It reads messages from wallet and
|
|
|
|
// sends back replies, as well as notififying wallets of chain updates.
|
|
|
|
func (s *rpcServer) walletReqsNotifications(ws *websocket.Conn) {
|
|
|
|
// Add wallet notification channel so this handler receives btcd chain
|
|
|
|
// notifications.
|
|
|
|
c := make(chan []byte)
|
|
|
|
s.AddWalletListener(c)
|
|
|
|
defer s.RemoveWalletListener(c)
|
|
|
|
|
|
|
|
// msgs is a channel for all messages received over the websocket.
|
|
|
|
msgs := make(chan []byte)
|
|
|
|
|
|
|
|
// Receive messages from websocket and send across reqs until the
|
|
|
|
// connection is lost.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.quit:
|
|
|
|
close(msgs)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
var m []byte
|
|
|
|
if err := websocket.Message.Receive(ws, &m); err != nil {
|
|
|
|
close(msgs)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msgs <- m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m, ok := <-msgs:
|
|
|
|
if !ok {
|
|
|
|
// Wallet disconnected.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Handle request here.
|
|
|
|
go s.websocketJSONHandler(c, m)
|
|
|
|
case ntfn, _ := <-c:
|
|
|
|
// Send btcd notification to btcwallet instance over
|
|
|
|
// websocket.
|
|
|
|
if err := websocket.Message.Send(ws, ntfn); err != nil {
|
|
|
|
// Wallet disconnected.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-s.quit:
|
|
|
|
// Server closed.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// websocketJSONHandler parses and handles a marshalled json message,
|
|
|
|
// sending the marshalled reply to a wallet notification channel.
|
|
|
|
func (s *rpcServer) websocketJSONHandler(walletNotification chan []byte, msg []byte) {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
s.wg.Add(1)
|
2013-10-23 17:07:00 +02:00
|
|
|
reply, err := jsonRead(msg, s, walletNotification)
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
s.wg.Done()
|
2013-08-14 22:55:31 +02:00
|
|
|
|
2013-10-30 04:23:22 +01:00
|
|
|
if err != btcjson.ErrMethodNotFound {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
replyBytes, err := json.Marshal(reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Error marshalling reply: %v", err)
|
|
|
|
}
|
|
|
|
walletNotification <- replyBytes
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try websocket extensions
|
2013-10-30 04:23:22 +01:00
|
|
|
s.wg.Add(1)
|
2013-11-06 17:20:36 +01:00
|
|
|
err = jsonWSRead(msg, s, walletNotification)
|
2013-10-30 04:23:22 +01:00
|
|
|
s.wg.Done()
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyBlockConnected creates and marshalls a JSON message to notify
|
|
|
|
// of a new block connected to the main chain. The notification is sent
|
|
|
|
// to each connected wallet.
|
|
|
|
func (s *rpcServer) NotifyBlockConnected(block *btcutil.Block) {
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.RLock()
|
|
|
|
for wltNtfn := range s.ws.walletListeners.m {
|
|
|
|
// Create notification with basic information filled in.
|
|
|
|
// This data is the same for every connected wallet.
|
|
|
|
hash, err := block.Sha()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Bad block; connected block notification dropped.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ntfnResult := struct {
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
MinedTXs []string `json:"minedtxs"`
|
2013-08-14 22:55:31 +02:00
|
|
|
}{
|
|
|
|
Hash: hash.String(),
|
|
|
|
Height: block.Height(),
|
2013-10-23 17:07:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in additional wallet-specific notifications. If there
|
|
|
|
// is no request context for this wallet, no need to give this
|
|
|
|
// wallet any extra notifications.
|
|
|
|
if cxt := s.ws.requests.m[wltNtfn]; cxt != nil {
|
|
|
|
// Create list of all txs created by this wallet that appear in this
|
|
|
|
// block.
|
|
|
|
minedTxShas := make([]string, 0, len(cxt.minedTxRequests))
|
|
|
|
|
|
|
|
// TxShas does not return a non-nil error.
|
|
|
|
txShaList, _ := block.TxShas()
|
|
|
|
txList := s.server.db.FetchTxByShaList(txShaList)
|
|
|
|
for _, txReply := range txList {
|
|
|
|
if txReply.Err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, ok := cxt.minedTxRequests[*txReply.Sha]; ok {
|
|
|
|
minedTxShas = append(minedTxShas, txReply.Sha.String())
|
|
|
|
s.ws.requests.RemoveMinedTxRequest(wltNtfn, txReply.Sha)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ntfnResult.MinedTXs = minedTxShas
|
|
|
|
}
|
|
|
|
|
|
|
|
var id interface{} = "btcd:blockconnected"
|
|
|
|
ntfn := btcjson.Reply{
|
|
|
|
Result: ntfnResult,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
m, _ := json.Marshal(ntfn)
|
|
|
|
wltNtfn <- m
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-23 17:07:00 +02:00
|
|
|
s.ws.walletListeners.RUnlock()
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
2013-10-23 17:07:00 +02:00
|
|
|
// NotifyBlockDisconnected creates and marshals a JSON message to notify
|
2013-08-14 22:55:31 +02:00
|
|
|
// of a new block disconnected from the main chain. The notification is sent
|
|
|
|
// to each connected wallet.
|
|
|
|
func (s *rpcServer) NotifyBlockDisconnected(block *btcutil.Block) {
|
|
|
|
var id interface{} = "btcd:blockdisconnected"
|
|
|
|
hash, err := block.Sha()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Bad block; connected block notification dropped.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ntfn := btcjson.Reply{
|
|
|
|
Result: struct {
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
}{
|
|
|
|
Hash: hash.String(),
|
|
|
|
Height: block.Height(),
|
|
|
|
},
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
m, _ := json.Marshal(ntfn)
|
|
|
|
s.ws.walletNotificationMaster <- m
|
|
|
|
}
|
|
|
|
|
2013-10-23 17:07:00 +02:00
|
|
|
// NotifyBlockTXs creates and marshals a JSON message to notify wallets
|
2013-08-14 22:55:31 +02:00
|
|
|
// of new transactions (with both spent and unspent outputs) for a watched
|
|
|
|
// address.
|
2013-10-23 17:07:00 +02:00
|
|
|
func (s *rpcServer) NotifyBlockTXs(db btcdb.Db, block *btcutil.Block) {
|
2013-10-29 21:48:22 +01:00
|
|
|
// Build a map of in-flight transactions to see if any of the inputs in
|
|
|
|
// this block are referencing other transactions earlier in this block.
|
|
|
|
txInFlight := map[btcwire.ShaHash]int{}
|
|
|
|
transactions := block.Transactions()
|
|
|
|
spent := make([][]bool, len(transactions))
|
|
|
|
for i, tx := range transactions {
|
|
|
|
spent[i] = make([]bool, len(tx.MsgTx().TxOut))
|
|
|
|
txInFlight[*tx.Sha()] = i
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-29 21:48:22 +01:00
|
|
|
|
|
|
|
// The newBlockNotifyCheckTxOut current needs spent data. This can
|
|
|
|
// this can ultimately be optimized out by making sure the notifications
|
|
|
|
// are in order. For now, just create the spent data.
|
|
|
|
for i, tx := range transactions[1:] {
|
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
|
|
|
originHash := &txIn.PreviousOutpoint.Hash
|
|
|
|
if inFlightIndex, ok := txInFlight[*originHash]; ok &&
|
|
|
|
i >= inFlightIndex {
|
|
|
|
|
|
|
|
prevIndex := txIn.PreviousOutpoint.Index
|
|
|
|
spent[inFlightIndex][prevIndex] = true
|
|
|
|
}
|
2013-10-20 18:50:31 +02:00
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
2013-10-29 21:48:22 +01:00
|
|
|
|
|
|
|
for i, tx := range transactions {
|
|
|
|
go s.newBlockNotifyCheckTxIn(tx)
|
|
|
|
go s.newBlockNotifyCheckTxOut(block, tx, spent[i])
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newBlockNotifyCheckTxIn is a helper function to iterate through
|
|
|
|
// each transaction input of a new block and perform any checks and
|
|
|
|
// notify listening frontends when necessary.
|
2013-10-29 21:48:22 +01:00
|
|
|
func (s *rpcServer) newBlockNotifyCheckTxIn(tx *btcutil.Tx) {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
for wltNtfn, cxt := range s.ws.requests.m {
|
2013-10-29 21:48:22 +01:00
|
|
|
for _, txin := range tx.MsgTx().TxIn {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
for op, id := range cxt.spentRequests {
|
|
|
|
if txin.PreviousOutpoint != op {
|
|
|
|
continue
|
|
|
|
}
|
2013-08-14 22:55:31 +02:00
|
|
|
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
reply := &btcjson.Reply{
|
|
|
|
Result: struct {
|
|
|
|
TxHash string `json:"txhash"`
|
|
|
|
Index uint32 `json:"index"`
|
|
|
|
}{
|
|
|
|
TxHash: op.Hash.String(),
|
|
|
|
Index: uint32(op.Index),
|
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
replyBytes, err := json.Marshal(reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Unable to marshal spent notification: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wltNtfn <- replyBytes
|
|
|
|
s.ws.requests.RemoveSpentRequest(wltNtfn, &op)
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newBlockNotifyCheckTxOut is a helper function to iterate through
|
|
|
|
// each transaction output of a new block and perform any checks and
|
|
|
|
// notify listening frontends when necessary.
|
2013-10-29 21:48:22 +01:00
|
|
|
func (s *rpcServer) newBlockNotifyCheckTxOut(block *btcutil.Block, tx *btcutil.Tx, spent []bool) {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
for wltNtfn, cxt := range s.ws.requests.m {
|
2013-10-29 21:48:22 +01:00
|
|
|
for i, txout := range tx.MsgTx().TxOut {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
_, txaddrhash, err := btcscript.ScriptToAddrHash(txout.PkScript)
|
2013-08-14 22:55:31 +02:00
|
|
|
if err != nil {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
log.Debug("Error getting payment address from tx; dropping any Tx notifications.")
|
2013-08-14 22:55:31 +02:00
|
|
|
break
|
|
|
|
}
|
2013-11-04 17:59:48 +01:00
|
|
|
if id, ok := cxt.txRequests[string(txaddrhash)]; ok {
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
blkhash, err := block.Sha()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error getting block sha; dropping Tx notification.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
txaddr, err := btcutil.EncodeAddress(txaddrhash, s.server.btcnet)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error encoding address; dropping Tx notification.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
reply := &btcjson.Reply{
|
|
|
|
Result: struct {
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
Receiver string `json:"receiver"`
|
|
|
|
BlockHash string `json:"blockhash"`
|
|
|
|
Height int64 `json:"height"`
|
|
|
|
TxHash string `json:"txhash"`
|
|
|
|
Index uint32 `json:"index"`
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
PkScript string `json:"pkscript"`
|
|
|
|
Spent bool `json:"spent"`
|
|
|
|
}{
|
|
|
|
Sender: "Unknown", // TODO(jrick)
|
|
|
|
Receiver: txaddr,
|
|
|
|
BlockHash: blkhash.String(),
|
|
|
|
Height: block.Height(),
|
2013-10-29 21:48:22 +01:00
|
|
|
TxHash: tx.Sha().String(),
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
Index: uint32(i),
|
|
|
|
Amount: txout.Value,
|
|
|
|
PkScript: btcutil.Base58Encode(txout.PkScript),
|
2013-10-29 21:48:22 +01:00
|
|
|
Spent: spent[i],
|
Clean up notification contexts and goroutines after ws disconnect.
This refactors the wallet notification code to reverse the order of
how notification contexts are stored. Before, watched addresses and
outpoints were used as keys, with a special reply channel as the
value. This channel was read from and replies were marshalled and
sent to the main wallet notification chan, but the goroutine handling
this marshalling never exited because the reply channel was never
closed (and couldn't have been, because there was no way to tell it
was handling notifications for any particular wallet).
Notification contexts are now primarily mapped by wallet notification
channels, and code to send the notifications send directly to the
wallet channel, with the previous goroutine reading the reply chan
properly closing.
The RPC code is also refactored with this change as well, to separate
it more from websocket code. Websocket JSON extensions are no longer
available to RPC clients.
While here, unbreak RPC. Previously, replies were never sent back.
This broke when I merged in my websocket code, as sends for the reply
channel in jsonRead blocked before a reader for the channel was
opened. A 3 liner could have fixed this, but doing a proper fix
(changing jsonRead so it did not use the reply channel as it is
unneeded for the standard RPC API) is preferred.
2013-10-16 20:12:00 +02:00
|
|
|
},
|
|
|
|
Error: nil,
|
|
|
|
Id: &id,
|
|
|
|
}
|
|
|
|
replyBytes, err := json.Marshal(reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RPCS: Unable to marshal tx notification: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wltNtfn <- replyBytes
|
2013-08-14 22:55:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|