mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
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/btcjson"
|
||||||
"github.com/conformal/btcwallet/tx"
|
"github.com/conformal/btcwallet/tx"
|
||||||
"github.com/conformal/btcwallet/wallet"
|
"github.com/conformal/btcwallet/wallet"
|
||||||
|
"github.com/conformal/btcwire"
|
||||||
"github.com/conformal/seelog"
|
"github.com/conformal/seelog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -244,7 +245,39 @@ func (w *BtcWallet) ReqUtxoForAddress(addr string) {
|
||||||
|
|
||||||
replyHandlers.Lock()
|
replyHandlers.Lock()
|
||||||
replyHandlers.m[n] = func(result interface{}) bool {
|
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.
|
// Never remove this handler.
|
||||||
return false
|
return false
|
||||||
|
@ -279,3 +312,14 @@ func (w *BtcWallet) ReqTxsForAddress(addr string) {
|
||||||
|
|
||||||
btcdMsgs <- msg
|
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"
|
"fmt"
|
||||||
"github.com/conformal/btcjson"
|
"github.com/conformal/btcjson"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -269,11 +268,8 @@ func ProcessBtcdNotificationReply(b []byte) {
|
||||||
switch idStr {
|
switch idStr {
|
||||||
case "btcd:blockconnected":
|
case "btcd:blockconnected":
|
||||||
result := m["result"].(map[string]interface{})
|
result := m["result"].(map[string]interface{})
|
||||||
hashResult := result["hash"].([]interface{})
|
|
||||||
hash := new(btcwire.ShaHash)
|
hash := new(btcwire.ShaHash)
|
||||||
for i, _ := range hash[:] {
|
copy(hash[:], UnmangleJsonByteSlice(result["hash"].([]interface{})))
|
||||||
hash[i] = byte(hashResult[i].(float64))
|
|
||||||
}
|
|
||||||
height := int64(result["height"].(float64))
|
height := int64(result["height"].(float64))
|
||||||
|
|
||||||
// TODO(jrick): update TxStore and UtxoStore with new hash
|
// TODO(jrick): update TxStore and UtxoStore with new hash
|
||||||
|
|
Loading…
Add table
Reference in a new issue