Stringify the address hash for txRequests map.

This change allows map lookups using address hashes (which are
returned as []byte) instead of either copying the hash into an array,
or doing a bytes.Equal().

A stupid range over a map until the right key is found was also just
changed to a single map lookup.
This commit is contained in:
Josh Rickmar 2013-11-04 11:59:48 -05:00
parent 2511fee152
commit 53e1c2d6bd

View file

@ -6,7 +6,6 @@ package main
import ( import (
"bytes" "bytes"
"code.google.com/p/go.crypto/ripemd160"
"code.google.com/p/go.net/websocket" "code.google.com/p/go.net/websocket"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
@ -83,7 +82,9 @@ func (r *wsRequests) getOrCreateContexts(walletNotification chan []byte) *reques
rc, ok := r.m[walletNotification] rc, ok := r.m[walletNotification]
if !ok { if !ok {
rc = &requestContexts{ rc = &requestContexts{
txRequests: make(map[addressHash]interface{}), // The key is a stringified addressHash.
txRequests: make(map[string]interface{}),
spentRequests: make(map[btcwire.OutPoint]interface{}), spentRequests: make(map[btcwire.OutPoint]interface{}),
minedTxRequests: make(map[btcwire.ShaHash]bool), minedTxRequests: make(map[btcwire.ShaHash]bool),
} }
@ -93,12 +94,12 @@ func (r *wsRequests) getOrCreateContexts(walletNotification chan []byte) *reques
} }
// AddTxRequest adds the request context for new transaction notifications. // AddTxRequest adds the request context for new transaction notifications.
func (r *wsRequests) AddTxRequest(walletNotification chan []byte, addr addressHash, id interface{}) { func (r *wsRequests) AddTxRequest(walletNotification chan []byte, addrhash string, id interface{}) {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
rc := r.getOrCreateContexts(walletNotification) rc := r.getOrCreateContexts(walletNotification)
rc.txRequests[addr] = id rc.txRequests[addrhash] = id
} }
// AddSpentRequest adds a request context for notifications of a spent // AddSpentRequest adds a request context for notifications of a spent
@ -152,14 +153,13 @@ func (r *wsRequests) CloseListeners(walletNotification chan []byte) {
close(walletNotification) close(walletNotification)
} }
type addressHash [ripemd160.Size]byte
// requestContexts holds all requests for a single wallet connection. // requestContexts holds all requests for a single wallet connection.
type requestContexts struct { type requestContexts struct {
// txRequests maps between a 160-byte pubkey hash and the JSON // txRequests maps between a 160-byte pubkey hash and the JSON
// id of the requester so replies can be correctly routed back // id of the requester so replies can be correctly routed back
// to the correct btcwallet callback. // to the correct btcwallet callback. The key must be a stringified
txRequests map[addressHash]interface{} // address hash.
txRequests map[string]interface{}
// spentRequests maps between an Outpoint of an unspent // spentRequests maps between an Outpoint of an unspent
// transaction output and the JSON id of the requester so // transaction output and the JSON id of the requester so
@ -968,9 +968,7 @@ func jsonWSRead(walletNotification chan []byte, replychan chan *btcjson.Reply, b
} }
return ErrBadParamsField return ErrBadParamsField
} }
var hash addressHash s.ws.requests.AddTxRequest(walletNotification, string(addrhash), message.Id)
copy(hash[:], addrhash)
s.ws.requests.AddTxRequest(walletNotification, hash, message.Id)
rawReply = btcjson.Reply{ rawReply = btcjson.Reply{
Result: nil, Result: nil,
@ -1349,10 +1347,7 @@ func (s *rpcServer) newBlockNotifyCheckTxOut(block *btcutil.Block, tx *btcutil.T
log.Debug("Error getting payment address from tx; dropping any Tx notifications.") log.Debug("Error getting payment address from tx; dropping any Tx notifications.")
break break
} }
for addr, id := range cxt.txRequests { if id, ok := cxt.txRequests[string(txaddrhash)]; ok {
if !bytes.Equal(addr[:], txaddrhash) {
continue
}
blkhash, err := block.Sha() blkhash, err := block.Sha()
if err != nil { if err != nil {
log.Error("Error getting block sha; dropping Tx notification.") log.Error("Error getting block sha; dropping Tx notification.")