Guest User

Untitled

a guest
Jun 12th, 2019
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4. "log"
  5. "io/ioutil"
  6. "path/filepath"
  7. // "time"
  8.  
  9. "github.com/decred/dcrd/dcrutil"
  10. "github.com/decred/dcrd/rpcclient"
  11. )
  12.  
  13. type cliente struct {
  14. c *rpcclient.Client
  15. h *rpcclient.NotificationHandlers
  16. }
  17.  
  18. func (c *cliente) OnBlock() {
  19. func(blockHeader []byte, transactions [][]byte) {
  20. log.Printf("Block connected: %v %v", blockHeader, transactions)
  21. count, err := c.GetBlockCount()
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. }
  26. }
  27.  
  28. func main() {
  29. cli := &cliente{}
  30. cli.h = rpcclient.NotificationHandlers{OnBlock: OnBlockConnected()}
  31. dcrdHomeDir := dcrutil.AppDataDir("dcrd", false)
  32. certs, err := ioutil.ReadFile(filepath.Join(dcrdHomeDir, "rpc.cert"))
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. connCfg := &rpcclient.ConnConfig{
  37. Host: "localhost:19109",
  38. Endpoint: "ws",
  39. User: "",
  40. Pass: "",
  41. Certificates: certs,
  42. }
  43.  
  44. client, err := rpcclient.New(connCfg, cli)
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48.  
  49. cli.c = client
  50.  
  51. if err := client.NotifyBlocks(); err != nil {
  52. log.Fatal(err)
  53. }
  54. log.Println("NotifyBlocks: Registration Complete")
  55.  
  56.  
  57. // block_hash, err := client.Generate(1)
  58. // if err != nil {
  59. // log.Fatal(err)
  60. //}
  61. client.WaitForShutdown()
  62. }
RAW Paste Data