blocknotify: sample decred relay daemon

go websocket connector which call our blocknotify tool
its specific to decred, which doesn't handle the "blocknotify=" config key

Thanks to Davec for the sample tool :)
This commit is contained in:
Tanguy Pruvot 2016-02-20 15:33:51 +01:00
parent f1779325fc
commit bd0cf57a9c
2 changed files with 86 additions and 0 deletions

3
blocknotifygo/Makefile Normal file
View file

@ -0,0 +1,3 @@
all:
rm -f blocknotifygo
go build

View file

@ -0,0 +1,83 @@
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// 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.
package main
import (
"io/ioutil"
"log"
"os/exec"
"path/filepath"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrrpcclient"
"github.com/decred/dcrutil"
)
const (
processName = "blocknotify" // set the full path if required
stratumDest = "yaamp.com:5744" // stratum host:port
coinId = "1574" // decred database coin id
walletUser = "yiimprpc"
walletPass = "myDecredPassword"
)
func main() {
// Only override the handlers for notifications you care about.
// Also note most of these handlers will only be called if you register
// 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.
cmd := exec.Command(processName, stratumDest, coinId, hash.String())
if err := cmd.Run(); err != nil {
log.Fatalln(err)
}
log.Printf("Block connected: %v (%d)", hash, height)
},
}
// Connect to local dcrd RPC server using websockets.
dcrdHomeDir := dcrutil.AppDataDir("dcrd", false)
certs, err := ioutil.ReadFile(filepath.Join(dcrdHomeDir, "rpc.cert"))
if err != nil {
certs = nil
log.Printf("%s, trying without TLS...", err)
}
connCfg := &dcrrpcclient.ConnConfig{
Host: "127.0.0.1:15740",
Endpoint: "ws", // websocket
User: walletUser,
Pass: walletPass,
DisableTLS: (certs == nil),
Certificates: certs,
}
client, err := dcrrpcclient.New(connCfg, &ntfnHandlers)
if err != nil {
log.Fatalln(err)
}
// Register for block connect and disconnect notifications.
if err := client.NotifyBlocks(); err != nil {
log.Fatalln(err)
}
log.Println("NotifyBlocks: Registration Complete")
// Wait until the client either shuts down gracefully (or the user
// terminates the process with Ctrl+C).
client.WaitForShutdown()
}