Updates for btcutil and btcscript's btcnet conversion.

ok @davecgh
This commit is contained in:
Josh Rickmar 2014-05-27 17:44:55 -05:00
parent 4328461f36
commit 74303966c0
5 changed files with 23 additions and 20 deletions

View file

@ -524,7 +524,7 @@ func loadConfig() (*config, []string, error) {
// Check keys are valid and saved parsed versions. // Check keys are valid and saved parsed versions.
cfg.miningKeys = make([]btcutil.Address, 0, len(cfg.GetWorkKeys)) cfg.miningKeys = make([]btcutil.Address, 0, len(cfg.GetWorkKeys))
for _, strAddr := range cfg.GetWorkKeys { for _, strAddr := range cfg.GetWorkKeys {
addr, err := btcutil.DecodeAddress(strAddr, activeNetParams.Net) addr, err := btcutil.DecodeAddress(strAddr, activeNetParams.Params)
if err != nil { if err != nil {
str := "%s: the specified getworkkey '%s' failed to decode: %v" str := "%s: the specified getworkkey '%s' failed to decode: %v"
err := fmt.Errorf(str, "loadConfig", strAddr, err) err := fmt.Errorf(str, "loadConfig", strAddr, err)
@ -532,7 +532,7 @@ func loadConfig() (*config, []string, error) {
parser.WriteHelp(os.Stderr) parser.WriteHelp(os.Stderr)
return nil, nil, err return nil, nil, err
} }
if !addr.IsForNet(activeNetParams.Net) { if !addr.IsForNet(activeNetParams.Params) {
str := "%s: the specified getworkkey '%s' is on the wrong network" str := "%s: the specified getworkkey '%s' is on the wrong network"
err := fmt.Errorf(str, "loadConfig", strAddr) err := fmt.Errorf(str, "loadConfig", strAddr)
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)

View file

@ -1579,7 +1579,7 @@ func newPeerBase(s *server, inbound bool) *peer {
p := peer{ p := peer{
server: s, server: s,
protocolVersion: maxProtocolVersion, protocolVersion: maxProtocolVersion,
btcnet: s.btcnet, btcnet: s.netParams.Net,
services: btcwire.SFNodeNetwork, services: btcwire.SFNodeNetwork,
inbound: inbound, inbound: inbound,
knownAddresses: make(map[string]bool), knownAddresses: make(map[string]bool),

View file

@ -17,6 +17,7 @@ import (
"github.com/conformal/btcchain" "github.com/conformal/btcchain"
"github.com/conformal/btcdb" "github.com/conformal/btcdb"
"github.com/conformal/btcjson" "github.com/conformal/btcjson"
"github.com/conformal/btcnet"
"github.com/conformal/btcscript" "github.com/conformal/btcscript"
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
@ -588,7 +589,7 @@ func handleCreateRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, err
// Decode the provided address. // Decode the provided address.
addr, err := btcutil.DecodeAddress(encodedAddr, addr, err := btcutil.DecodeAddress(encodedAddr,
activeNetParams.Net) activeNetParams.Params)
if err != nil { if err != nil {
return nil, btcjson.Error{ return nil, btcjson.Error{
Code: btcjson.ErrInvalidAddressOrKey.Code, Code: btcjson.ErrInvalidAddressOrKey.Code,
@ -606,7 +607,7 @@ func handleCreateRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, err
default: default:
return nil, btcjson.ErrInvalidAddressOrKey return nil, btcjson.ErrInvalidAddressOrKey
} }
if !addr.IsForNet(s.server.btcnet) { if !addr.IsForNet(s.server.netParams) {
return nil, btcjson.Error{ return nil, btcjson.Error{
Code: btcjson.ErrInvalidAddressOrKey.Code, Code: btcjson.ErrInvalidAddressOrKey.Code,
Message: fmt.Sprintf("%s: %q", Message: fmt.Sprintf("%s: %q",
@ -688,7 +689,7 @@ func createVinList(mtx *btcwire.MsgTx) ([]btcjson.Vin, error) {
// createVoutList returns a slice of JSON objects for the outputs of the passed // createVoutList returns a slice of JSON objects for the outputs of the passed
// transaction. // transaction.
func createVoutList(mtx *btcwire.MsgTx, net btcwire.BitcoinNet) ([]btcjson.Vout, error) { func createVoutList(mtx *btcwire.MsgTx, net *btcnet.Params) ([]btcjson.Vout, error) {
voutList := make([]btcjson.Vout, len(mtx.TxOut)) voutList := make([]btcjson.Vout, len(mtx.TxOut))
for i, v := range mtx.TxOut { for i, v := range mtx.TxOut {
voutList[i].N = i voutList[i].N = i
@ -726,7 +727,10 @@ func createVoutList(mtx *btcwire.MsgTx, net btcwire.BitcoinNet) ([]btcjson.Vout,
// createTxRawResult converts the passed transaction and associated parameters // createTxRawResult converts the passed transaction and associated parameters
// to a raw transaction JSON object. // to a raw transaction JSON object.
func createTxRawResult(net btcwire.BitcoinNet, txSha string, mtx *btcwire.MsgTx, blk *btcutil.Block, maxidx int64, blksha *btcwire.ShaHash) (*btcjson.TxRawResult, error) { func createTxRawResult(net *btcnet.Params, txSha string, mtx *btcwire.MsgTx,
blk *btcutil.Block, maxidx int64,
blksha *btcwire.ShaHash) (*btcjson.TxRawResult, error) {
mtxHex, err := messageToHex(mtx) mtxHex, err := messageToHex(mtx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -795,7 +799,7 @@ func handleDecodeRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
vout, err := createVoutList(&mtx, s.server.btcnet) vout, err := createVoutList(&mtx, s.server.netParams)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -832,7 +836,7 @@ func handleDecodeScript(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
// Get information about the script. // Get information about the script.
// Ignore the error here since an error means the script couldn't parse // Ignore the error here since an error means the script couldn't parse
// and there is no additinal information about it anyways. // and there is no additinal information about it anyways.
net := s.server.btcnet net := s.server.netParams
scriptClass, addrs, reqSigs, _ := btcscript.ExtractPkScriptAddrs(script, net) scriptClass, addrs, reqSigs, _ := btcscript.ExtractPkScriptAddrs(script, net)
addresses := make([]string, len(addrs)) addresses := make([]string, len(addrs))
for i, addr := range addrs { for i, addr := range addrs {
@ -1040,8 +1044,8 @@ func handleGetBlock(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
txSha := tx.Sha().String() txSha := tx.Sha().String()
mtx := tx.MsgTx() mtx := tx.MsgTx()
rawTxn, err := createTxRawResult(s.server.btcnet, txSha, rawTxn, err := createTxRawResult(s.server.netParams,
mtx, blk, maxidx, sha) txSha, mtx, blk, maxidx, sha)
if err != nil { if err != nil {
rpcsLog.Errorf("Cannot create TxRawResult for "+ rpcsLog.Errorf("Cannot create TxRawResult for "+
"transaction %s: %v", txSha, err) "transaction %s: %v", txSha, err)
@ -1096,7 +1100,7 @@ func handleGetConnectionCount(s *rpcServer, cmd btcjson.Cmd) (interface{}, error
// handleGetCurrentNet implements the getcurrentnet command. // handleGetCurrentNet implements the getcurrentnet command.
func handleGetCurrentNet(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) { func handleGetCurrentNet(s *rpcServer, cmd btcjson.Cmd) (interface{}, error) {
return s.server.btcnet, nil return s.server.netParams.Net, nil
} }
// handleGetDifficulty implements the getdifficulty command. // handleGetDifficulty implements the getdifficulty command.
@ -1422,7 +1426,8 @@ func handleGetRawTransaction(s *rpcServer, cmd btcjson.Cmd) (interface{}, error)
} }
} }
rawTxn, jsonErr := createTxRawResult(s.server.btcnet, c.Txid, mtx, blk, maxidx, blksha) rawTxn, jsonErr := createTxRawResult(s.server.netParams, c.Txid, mtx,
blk, maxidx, blksha)
if err != nil { if err != nil {
rpcsLog.Errorf("Cannot create TxRawResult for txSha=%s: %v", txSha, err) rpcsLog.Errorf("Cannot create TxRawResult for txSha=%s: %v", txSha, err)
return nil, jsonErr return nil, jsonErr

View file

@ -485,7 +485,7 @@ func (m *wsNotificationManager) notifyForNewTx(clients map[chan bool]*wsClient,
for _, wsc := range clients { for _, wsc := range clients {
if wsc.verboseTxUpdates { if wsc.verboseTxUpdates {
if verboseNtfn == nil { if verboseNtfn == nil {
net := m.server.server.btcnet net := m.server.server.netParams
rawTx, err := createTxRawResult(net, txShaStr, rawTx, err := createTxRawResult(net, txShaStr,
mtx, nil, 0, nil) mtx, nil, 0, nil)
if err != nil { if err != nil {
@ -620,7 +620,7 @@ func (m *wsNotificationManager) notifyForTxOuts(ops map[btcwire.OutPoint]map[cha
wscNotified := make(map[chan bool]bool) wscNotified := make(map[chan bool]bool)
for i, txOut := range tx.MsgTx().TxOut { for i, txOut := range tx.MsgTx().TxOut {
_, txAddrs, _, err := btcscript.ExtractPkScriptAddrs( _, txAddrs, _, err := btcscript.ExtractPkScriptAddrs(
txOut.PkScript, m.server.server.btcnet) txOut.PkScript, m.server.server.netParams)
if err != nil { if err != nil {
continue continue
} }
@ -1433,7 +1433,7 @@ func handleNotifyReceived(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjso
} }
for _, addrStr := range cmd.Addresses { for _, addrStr := range cmd.Addresses {
addr, err := btcutil.DecodeAddress(addrStr, activeNetParams.Net) addr, err := btcutil.DecodeAddress(addrStr, activeNetParams.Params)
if err != nil { if err != nil {
e := btcjson.Error{ e := btcjson.Error{
Code: btcjson.ErrInvalidAddressOrKey.Code, Code: btcjson.ErrInvalidAddressOrKey.Code,
@ -1501,7 +1501,7 @@ func rescanBlock(wsc *wsClient, lookups *rescanKeys, blk *btcutil.Block) {
for txOutIdx, txout := range tx.MsgTx().TxOut { for txOutIdx, txout := range tx.MsgTx().TxOut {
_, addrs, _, _ := btcscript.ExtractPkScriptAddrs( _, addrs, _, _ := btcscript.ExtractPkScriptAddrs(
txout.PkScript, wsc.server.server.btcnet) txout.PkScript, wsc.server.server.netParams)
for _, addr := range addrs { for _, addr := range addrs {
switch a := addr.(type) { switch a := addr.(type) {
@ -1630,7 +1630,7 @@ func handleRescan(wsc *wsClient, icmd btcjson.Cmd) (interface{}, *btcjson.Error)
var compressedPubkey [33]byte var compressedPubkey [33]byte
var uncompressedPubkey [65]byte var uncompressedPubkey [65]byte
for _, addrStr := range cmd.Addresses { for _, addrStr := range cmd.Addresses {
addr, err := btcutil.DecodeAddress(addrStr, activeNetParams.Net) addr, err := btcutil.DecodeAddress(addrStr, activeNetParams.Params)
if err != nil { if err != nil {
jsonErr := btcjson.Error{ jsonErr := btcjson.Error{
Code: btcjson.ErrInvalidAddressOrKey.Code, Code: btcjson.ErrInvalidAddressOrKey.Code,

View file

@ -64,7 +64,6 @@ type server struct {
nonce uint64 nonce uint64
listeners []net.Listener listeners []net.Listener
netParams *btcnet.Params netParams *btcnet.Params
btcnet btcwire.BitcoinNet
started int32 // atomic started int32 // atomic
shutdown int32 // atomic shutdown int32 // atomic
shutdownSched int32 // atomic shutdownSched int32 // atomic
@ -1131,7 +1130,6 @@ func newServer(listenAddrs []string, db btcdb.Db, netParams *btcnet.Params) (*se
nonce: nonce, nonce: nonce,
listeners: listeners, listeners: listeners,
netParams: netParams, netParams: netParams,
btcnet: netParams.Net,
addrManager: amgr, addrManager: amgr,
newPeers: make(chan *peer, cfg.MaxPeers), newPeers: make(chan *peer, cfg.MaxPeers),
donePeers: make(chan *peer, cfg.MaxPeers), donePeers: make(chan *peer, cfg.MaxPeers),