Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. // Copyright (c) 2016 The btcsuite developers
  2. // Copyright (c) 2015-2016 The Decred developers
  3. // Use of this source code is governed by an ISC
  4. // license that can be found in the LICENSE file.
  5.  
  6. package main
  7.  
  8. import (
  9. "io/ioutil"
  10. "log"
  11. "os/exec"
  12. "path/filepath"
  13. "time"
  14.  
  15. "github.com/decred/dcrd/chaincfg/chainhash"
  16. "github.com/decred/dcrrpcclient"
  17. "github.com/decred/dcrutil"
  18. )
  19.  
  20. const (
  21. processName = "blocknotify"
  22. )
  23.  
  24. func main() {
  25. // Only override the handlers for notifications you care about.
  26. // Also note most of these handlers will only be called if you register
  27. // for notifications. See the documentation of the dcrrpcclient
  28. // NotificationHandlers type for more details about each handler.
  29. ntfnHandlers := dcrrpcclient.NotificationHandlers{
  30. OnBlockConnected: func(hash *chainhash.Hash, height int32, time time.Time, vb uint16) {
  31. // Find the process path.
  32. cmd := exec.Command(processName, "1574", hash.String())
  33. if err := cmd.Run(); err != nil {
  34. log.Fatalln(err)
  35. }
  36. log.Printf("Block connected: %v (%d)", hash, height)
  37. },
  38. }
  39.  
  40. // Connect to local dcrd RPC server using websockets.
  41. dcrdHomeDir := dcrutil.AppDataDir("dcrd", false)
  42. certs, err := ioutil.ReadFile(filepath.Join(dcrdHomeDir, "rpc.cert"))
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. connCfg := &dcrrpcclient.ConnConfig{
  47. Host: "localhost:9109",
  48. Endpoint: "ws",
  49. User: "test",
  50. Pass: "tabc",
  51. Certificates: certs,
  52. }
  53. client, err := dcrrpcclient.New(connCfg, &ntfnHandlers)
  54. if err != nil {
  55. log.Fatalln(err)
  56. }
  57.  
  58. // Register for block connect and disconnect notifications.
  59. if err := client.NotifyBlocks(); err != nil {
  60. log.Fatalln(err)
  61. }
  62. log.Println("NotifyBlocks: Registration Complete")
  63.  
  64. // Wait until the client either shuts down gracefully (or the user
  65. // terminates the process with Ctrl+C).
  66. client.WaitForShutdown()
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement