c39e35318d
This updates all code to make use of the new chainhash package since the old wire.ShaHash type and related functions have been removed in favor of the abstracted package. Also, while here, rename all variables that included sha in their name to include hash instead.
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
// Copyright (c) 2014-2015 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
"github.com/btcsuite/btcrpcclient"
|
|
"github.com/btcsuite/btcutil"
|
|
)
|
|
|
|
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 btcrpcclient
|
|
// NotificationHandlers type for more details about each handler.
|
|
ntfnHandlers := btcrpcclient.NotificationHandlers{
|
|
OnBlockConnected: func(hash *chainhash.Hash, height int32, time time.Time) {
|
|
log.Printf("Block connected: %v (%d) %v", hash, height, time)
|
|
},
|
|
OnBlockDisconnected: func(hash *chainhash.Hash, height int32, time time.Time) {
|
|
log.Printf("Block disconnected: %v (%d) %v", hash, height, time)
|
|
},
|
|
}
|
|
|
|
// Connect to local btcd RPC server using websockets.
|
|
btcdHomeDir := btcutil.AppDataDir("btcd", false)
|
|
certs, err := ioutil.ReadFile(filepath.Join(btcdHomeDir, "rpc.cert"))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
connCfg := &btcrpcclient.ConnConfig{
|
|
Host: "localhost:8334",
|
|
Endpoint: "ws",
|
|
User: "yourrpcuser",
|
|
Pass: "yourrpcpass",
|
|
Certificates: certs,
|
|
}
|
|
client, err := btcrpcclient.New(connCfg, &ntfnHandlers)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Register for block connect and disconnect notifications.
|
|
if err := client.NotifyBlocks(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Println("NotifyBlocks: Registration Complete")
|
|
|
|
// Get the current block count.
|
|
blockCount, err := client.GetBlockCount()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
log.Printf("Block count: %d", blockCount)
|
|
|
|
// For this example gracefully shutdown the client after 10 seconds.
|
|
// Ordinarily when to shutdown the client is highly application
|
|
// specific.
|
|
log.Println("Client shutdown in 10 seconds...")
|
|
time.AfterFunc(time.Second*10, func() {
|
|
log.Println("Client shutting down...")
|
|
client.Shutdown()
|
|
log.Println("Client shutdown complete.")
|
|
})
|
|
|
|
// Wait until the client either shuts down gracefully (or the user
|
|
// terminates the process with Ctrl+C).
|
|
client.WaitForShutdown()
|
|
}
|