decred: refresh blocknotify.go for 0.6.1+

The notify interface was changed to raw bytes :/
This commit is contained in:
Tanguy Pruvot 2016-12-03 09:07:08 +01:00
parent ff136d53af
commit 4386b09c3e

View file

@ -1,10 +1,10 @@
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Copyright (c) 2015-2016 The Decred developers, YiiMP
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// Sample blocknofify tool compatible with decred
// will call the standard blocknotify yiimp tool on new block event.
// Note: this tool is connected directly to dcrd, not to the wallet!
package main
@ -13,9 +13,13 @@ import (
"log"
"os/exec"
"path/filepath"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
// "time" // dcrd < 0.6
// "github.com/decred/dcrd/chaincfg/chainhash"
"bytes" // dcrd > 0.6+
"github.com/decred/dcrd/wire"
"github.com/decred/dcrrpcclient"
// "github.com/decred/dcrutil"
)
@ -25,8 +29,8 @@ const (
stratumDest = "yaamp.com:5744" // stratum host:port
coinId = "1574" // decred database coin id
walletUser = "yiimprpc"
walletPass = "myDecredPassword"
dcrdUser = "yiimprpc"
dcrdPass = "myDcrdPassword"
debug = false
)
@ -37,27 +41,48 @@ func main() {
// for notifications. See the documentation of the dcrrpcclient
// NotificationHandlers type for more details about each handler.
ntfnHandlers := dcrrpcclient.NotificationHandlers{
OnBlockConnected: func(hash *chainhash.Hash, height int32, time time.Time, vb uint16) {
// Find the process path.
str := hash.String()
args := []string{ stratumDest, coinId, str }
out, err := exec.Command(processName, args...).Output()
if err != nil {
log.Printf("err %s", err)
} else if debug {
log.Printf("out %s", out)
}
if (debug) {
log.Printf("Block connected: %s %d", hash, height)
OnBlockConnected: func(blockHeader []byte, transactions [][]byte) {
// log.Printf("Block bytes: %v %v", blockHeader, transactions)
var bhead wire.BlockHeader
err := bhead.Deserialize(bytes.NewReader(blockHeader))
if err == nil {
str := bhead.BlockSha().String();
args := []string{ stratumDest, coinId, str }
out, err := exec.Command(processName, args...).Output()
if err != nil {
log.Printf("err %s", err)
} else if debug {
log.Printf("out %s", out)
}
if (debug) {
log.Printf("Block connected: %s", str)
}
}
},
// broken since 0.6.1 (Nov 2016):
// OnBlockConnected: func(hash *chainhash.Hash, height int32, time time.Time, vb uint16) {
//
// Find the process path.
// str := hash.String()
// args := []string{ stratumDest, coinId, str }
// out, err := exec.Command(processName, args...).Output()
// if err != nil {
// log.Printf("err %s", err)
// } else if debug {
// log.Printf("out %s", out)
// }
// if (debug) {
// log.Printf("Block connected: %s %d", hash, height)
// }
//},
}
// Connect to local dcrd RPC server using websockets.
// dcrwHomeDir := dcrutil.AppDataDir("dcrwallet", false)
// folder := dcrwHomeDir
// dcrdHomeDir := dcrutil.AppDataDir("dcrd", false)
// folder := dcrdHomeDir
folder := ""
certs, err := ioutil.ReadFile(filepath.Join(folder, "rpc.cert"))
if err != nil {
@ -66,11 +91,11 @@ func main() {
}
connCfg := &dcrrpcclient.ConnConfig{
Host: "127.0.0.1:15740",
Host: "127.0.0.1:9109",
Endpoint: "ws", // websocket
User: walletUser,
Pass: walletPass,
User: dcrdUser,
Pass: dcrdPass,
DisableTLS: (certs == nil),
Certificates: certs,