From 419ceb55c699b782f1c2069d97cb3c59f13a6025 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 4 Sep 2013 09:54:06 -0400 Subject: [PATCH] Implement Utxo notification handler to add to utxo store. --- cmd.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- sockets.go | 6 +----- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cmd.go b/cmd.go index 0d939bb..2be1677 100644 --- a/cmd.go +++ b/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 +} diff --git a/sockets.go b/sockets.go index 50b51d9..f3ef9c5 100644 --- a/sockets.go +++ b/sockets.go @@ -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