Use AddressDescriptor as key in socket.io subscribtions to addresstxid

This commit is contained in:
Martin Boehm 2018-10-03 11:20:23 +02:00
parent 8937155677
commit eb5781f218
7 changed files with 36 additions and 21 deletions

View File

@ -126,7 +126,7 @@ func TryParseOPReturn(script []byte) string {
return "" return ""
} }
// outputScriptToAddresses converts ScriptPubKey to bitcoin addresses // outputScriptToAddresses converts ScriptPubKey to addresses with a flag that the addresses are searchable
func (p *BitcoinParser) outputScriptToAddresses(script []byte) ([]string, bool, error) { func (p *BitcoinParser) outputScriptToAddresses(script []byte) ([]string, bool, error) {
sc, addresses, _, err := txscript.ExtractPkScriptAddrs(script, p.Params) sc, addresses, _, err := txscript.ExtractPkScriptAddrs(script, p.Params)
if err != nil { if err != nil {

View File

@ -83,8 +83,8 @@ func (m *NonUTXOMempool) Resync(onNewTxAddr OnNewTxAddrFunc) (int, error) {
if len(addrDesc) > 0 { if len(addrDesc) > 0 {
io = append(io, addrIndex{string(addrDesc), int32(output.N)}) io = append(io, addrIndex{string(addrDesc), int32(output.N)})
} }
if onNewTxAddr != nil && len(output.ScriptPubKey.Addresses) == 1 { if onNewTxAddr != nil {
onNewTxAddr(tx.Txid, output.ScriptPubKey.Addresses[0], true) onNewTxAddr(tx.Txid, addrDesc, true)
} }
} }
for _, input := range tx.Vin { for _, input := range tx.Vin {
@ -97,7 +97,7 @@ func (m *NonUTXOMempool) Resync(onNewTxAddr OnNewTxAddrFunc) (int, error) {
} }
io = append(io, addrIndex{string(addrDesc), int32(^i)}) io = append(io, addrIndex{string(addrDesc), int32(^i)})
if onNewTxAddr != nil { if onNewTxAddr != nil {
onNewTxAddr(tx.Txid, a, false) onNewTxAddr(tx.Txid, addrDesc, false)
} }
} }
} }

View File

@ -132,8 +132,8 @@ func (m *UTXOMempool) getTxAddrs(txid string, chanInput chan outpoint, chanResul
if len(addrDesc) > 0 { if len(addrDesc) > 0 {
io = append(io, addrIndex{string(addrDesc), int32(output.N)}) io = append(io, addrIndex{string(addrDesc), int32(output.N)})
} }
if m.onNewTxAddr != nil && len(output.ScriptPubKey.Addresses) == 1 { if m.onNewTxAddr != nil {
m.onNewTxAddr(tx.Txid, output.ScriptPubKey.Addresses[0], true) m.onNewTxAddr(tx.Txid, addrDesc, true)
} }
} }
dispatched := 0 dispatched := 0

View File

@ -144,7 +144,7 @@ func (ad AddressDescriptor) String() string {
type OnNewBlockFunc func(hash string, height uint32) type OnNewBlockFunc func(hash string, height uint32)
// OnNewTxAddrFunc is used to send notification about a new transaction/address // OnNewTxAddrFunc is used to send notification about a new transaction/address
type OnNewTxAddrFunc func(txid string, addr string, isOutput bool) type OnNewTxAddrFunc func(txid string, desc AddressDescriptor, isOutput bool)
// BlockChain defines common interface to block chain daemon // BlockChain defines common interface to block chain daemon
type BlockChain interface { type BlockChain interface {

View File

@ -495,9 +495,9 @@ func storeInternalStateLoop() {
glog.Info("storeInternalStateLoop stopped") glog.Info("storeInternalStateLoop stopped")
} }
func onNewTxAddr(txid string, addr string, isOutput bool) { func onNewTxAddr(txid string, desc bchain.AddressDescriptor, isOutput bool) {
for _, c := range callbacksOnNewTxAddr { for _, c := range callbacksOnNewTxAddr {
c(txid, addr, isOutput) c(txid, desc, isOutput)
} }
} }

View File

@ -148,8 +148,8 @@ func (s *PublicServer) OnNewBlock(hash string, height uint32) {
} }
// OnNewTxAddr notifies users subscribed to bitcoind/addresstxid about new block // OnNewTxAddr notifies users subscribed to bitcoind/addresstxid about new block
func (s *PublicServer) OnNewTxAddr(txid string, addr string, isOutput bool) { func (s *PublicServer) OnNewTxAddr(txid string, desc bchain.AddressDescriptor, isOutput bool) {
s.socketio.OnNewTxAddr(txid, addr, isOutput) s.socketio.OnNewTxAddr(txid, desc, isOutput)
} }
func (s *PublicServer) txRedirect(w http.ResponseWriter, r *http.Request) { func (s *PublicServer) txRedirect(w http.ResponseWriter, r *http.Request) {

View File

@ -726,8 +726,18 @@ func (s *SocketIoServer) onSubscribe(c *gosocketio.Channel, req []byte) interfac
onError(c.Id(), sc, "invalid data", err.Error()+", req: "+r) onError(c.Id(), sc, "invalid data", err.Error()+", req: "+r)
return nil return nil
} }
for _, a := range addrs { // normalize the addresses to AddressDescriptor
c.Join("bitcoind/addresstxid-" + a) descs := make([]bchain.AddressDescriptor, len(addrs))
for i, a := range addrs {
d, err := s.chainParser.GetAddrDescFromAddress(a)
if err != nil {
onError(c.Id(), sc, "invalid address "+a, err.Error()+", req: "+r)
return nil
}
descs[i] = d
}
for _, d := range descs {
c.Join("bitcoind/addresstxid-" + string(d))
} }
} else { } else {
sc = r[1 : len(r)-1] sc = r[1 : len(r)-1]
@ -748,13 +758,18 @@ func (s *SocketIoServer) OnNewBlockHash(hash string) {
} }
// OnNewTxAddr notifies users subscribed to bitcoind/addresstxid about new block // OnNewTxAddr notifies users subscribed to bitcoind/addresstxid about new block
func (s *SocketIoServer) OnNewTxAddr(txid string, addr string, isOutput bool) { func (s *SocketIoServer) OnNewTxAddr(txid string, desc bchain.AddressDescriptor, isOutput bool) {
data := map[string]interface{}{"address": addr, "txid": txid} addr, searchable, err := s.chainParser.GetAddressesFromAddrDesc(desc)
if !isOutput { if err != nil {
data["input"] = true glog.Error("GetAddressesFromAddrDesc error ", err, " for descriptor ", desc)
} } else if searchable && len(addr) == 1 {
c := s.server.BroadcastTo("bitcoind/addresstxid-"+addr, "bitcoind/addresstxid", data) data := map[string]interface{}{"address": addr[0], "txid": txid}
if c > 0 { if !isOutput {
glog.Info("broadcasting new txid ", txid, " for addr ", addr, " to ", c, " channels") data["input"] = true
}
c := s.server.BroadcastTo("bitcoind/addresstxid-"+string(desc), "bitcoind/addresstxid", data)
if c > 0 {
glog.Info("broadcasting new txid ", txid, " for addr ", addr[0], " to ", c, " channels")
}
} }
} }