Implement Utxo notification handler to add to utxo store.
This commit is contained in:
parent
0928361f22
commit
419ceb55c6
2 changed files with 46 additions and 6 deletions
46
cmd.go
46
cmd.go
|
@ -23,6 +23,7 @@ import (
|
|||
"github.com/conformal/btcjson"
|
||||
"github.com/conformal/btcwallet/tx"
|
||||
"github.com/conformal/btcwallet/wallet"
|
||||
"github.com/conformal/btcwire"
|
||||
"github.com/conformal/seelog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
@ -244,7 +245,39 @@ func (w *BtcWallet) ReqUtxoForAddress(addr string) {
|
|||
|
||||
replyHandlers.Lock()
|
||||
replyHandlers.m[n] = func(result interface{}) bool {
|
||||
// TODO(jrick)
|
||||
v, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
log.Error("UTXO Handler: Unexpected result type.")
|
||||
return false
|
||||
}
|
||||
addr, ok1 := v["address"].(string)
|
||||
height, ok2 := v["height"].(float64)
|
||||
txhashResult, ok3 := v["txhash"].([]interface{})
|
||||
amt, ok4 := v["amount"].(float64)
|
||||
if !ok1 || !ok2 || !ok3 || !ok4 {
|
||||
log.Error("UTXO Handler: Unexpected parameters.")
|
||||
return false
|
||||
}
|
||||
txhash := UnmangleJsonByteSlice(txhashResult)
|
||||
|
||||
h, err := btcwire.NewShaHashFromStr(addr)
|
||||
if err != nil {
|
||||
log.Error("UTXO Handler: Unable to parse address hash from string.")
|
||||
return false
|
||||
}
|
||||
u := &tx.Utxo{
|
||||
Amt: int64(amt),
|
||||
Height: int64(height),
|
||||
}
|
||||
copy(u.TxHash[:], txhash)
|
||||
copy(u.Addr[:], h[:])
|
||||
|
||||
w.UtxoStore.Lock()
|
||||
// All newly saved utxos are first classified as unconfirmed.
|
||||
utxos := w.UtxoStore.s.Unconfirmed
|
||||
w.UtxoStore.s.Unconfirmed = append(utxos, u)
|
||||
w.UtxoStore.dirty = true
|
||||
w.UtxoStore.Unlock()
|
||||
|
||||
// Never remove this handler.
|
||||
return false
|
||||
|
@ -279,3 +312,14 @@ func (w *BtcWallet) ReqTxsForAddress(addr string) {
|
|||
|
||||
btcdMsgs <- msg
|
||||
}
|
||||
|
||||
// Marshalling and unmarshalling byte arrays or slices results in ugly
|
||||
// []interface{} slices where each interface{} is a float64. This
|
||||
// function unmangles this to return a byte slice.
|
||||
func UnmangleJsonByteSlice(mangled []interface{}) (unmangled []byte) {
|
||||
unmangled = make([]byte, len(mangled))
|
||||
for i, _ := range mangled[:] {
|
||||
unmangled[i] = byte(mangled[i].(float64))
|
||||
}
|
||||
return unmangled
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ import (
|
|||
"fmt"
|
||||
"github.com/conformal/btcjson"
|
||||
"github.com/conformal/btcwire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
|
@ -269,11 +268,8 @@ func ProcessBtcdNotificationReply(b []byte) {
|
|||
switch idStr {
|
||||
case "btcd:blockconnected":
|
||||
result := m["result"].(map[string]interface{})
|
||||
hashResult := result["hash"].([]interface{})
|
||||
hash := new(btcwire.ShaHash)
|
||||
for i, _ := range hash[:] {
|
||||
hash[i] = byte(hashResult[i].(float64))
|
||||
}
|
||||
copy(hash[:], UnmangleJsonByteSlice(result["hash"].([]interface{})))
|
||||
height := int64(result["height"].(float64))
|
||||
|
||||
// TODO(jrick): update TxStore and UtxoStore with new hash
|
||||
|
|
Loading…
Add table
Reference in a new issue