[lbry] examples: update lbcdblocknotify to support Stratum
This commit is contained in:
parent
b6d3f5d21e
commit
be1b16b9fd
2 changed files with 76 additions and 29 deletions
|
@ -12,22 +12,33 @@ The first step is to clone the lbcd package:
|
||||||
$ git clone github.com/lbryio/lbcd
|
$ git clone github.com/lbryio/lbcd
|
||||||
```
|
```
|
||||||
|
|
||||||
Next, navigate to the example's directory and modify the `main.go` source to
|
Display available options:
|
||||||
specify the correct RPC username and password for the RPC server:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ cd rpcclient/examples/lbcdblocknotify
|
$ go run . -h
|
||||||
|
|
||||||
|
-coinid string
|
||||||
|
Coin ID (default "1425")
|
||||||
|
-rpcpass string
|
||||||
|
LBCD RPC password (default "rpcpass")
|
||||||
|
-rpcserver string
|
||||||
|
LBCD RPC server (default "localhost:9245")
|
||||||
|
-rpcuser string
|
||||||
|
LBCD RPC username (default "rpcuser")
|
||||||
|
-stratum string
|
||||||
|
Stratum server (default "lbrypool.net:3334")
|
||||||
|
-stratumpass string
|
||||||
|
Stratum server password (default "password")
|
||||||
```
|
```
|
||||||
|
|
||||||
```Go
|
Start the program:
|
||||||
User: "yourrpcuser",
|
|
||||||
Pass: "yourrpcpass",
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, run it with:
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ go run .
|
$ go run . -stratumpass <STRATUM PASSWD> -rpcuser <RPC USERNAME> -rpcpass <RPC PASSWD>
|
||||||
|
|
||||||
|
2022/01/10 23:16:21 NotifyBlocks: Registration Complete
|
||||||
|
2022/01/10 23:16:21 Block count: 1093112
|
||||||
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
|
@ -5,8 +5,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/lbryio/lbcd/rpcclient"
|
"github.com/lbryio/lbcd/rpcclient"
|
||||||
|
@ -14,19 +17,52 @@ import (
|
||||||
"github.com/lbryio/lbcutil"
|
"github.com/lbryio/lbcutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func send(stratum, stratumPass, coinid, blockHash string) error {
|
||||||
|
addr, err := net.ResolveTCPAddr("tcp", stratum)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't resolve addr: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err := net.DialTCP("tcp", nil, addr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't dial tcp: %w", err)
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
msg := fmt.Sprintf(`{"id":1,"method":"mining.update_block","params":[%q,%q,%q]}`,
|
||||||
|
stratumPass, coinid, blockHash)
|
||||||
|
|
||||||
|
_, err = conn.Write([]byte(msg))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't write message: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Only override the handlers for notifications you care about.
|
|
||||||
// Also note most of these handlers will only be called if you register
|
var (
|
||||||
// for notifications. See the documentation of the rpcclient
|
coinid = flag.String("coinid", "1425", "Coin ID")
|
||||||
// NotificationHandlers type for more details about each handler.
|
stratum = flag.String("stratum", "lbrypool.net:3334", "Stratum server")
|
||||||
|
stratumPass = flag.String("stratumpass", "password", "Stratum server password")
|
||||||
|
rpcserver = flag.String("rpcserver", "localhost:9245", "LBCD RPC server")
|
||||||
|
rpcuser = flag.String("rpcuser", "rpcuser", "LBCD RPC username")
|
||||||
|
rpcpass = flag.String("rpcpass", "rpcpass", "LBCD RPC password")
|
||||||
|
)
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
ntfnHandlers := rpcclient.NotificationHandlers{
|
ntfnHandlers := rpcclient.NotificationHandlers{
|
||||||
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
||||||
log.Printf("Block connected: %v (%d) %v",
|
|
||||||
header.BlockHash(), height, header.Timestamp)
|
blockHash := header.BlockHash().String()
|
||||||
},
|
|
||||||
OnFilteredBlockDisconnected: func(height int32, header *wire.BlockHeader) {
|
log.Printf("Block connected: %v (%d) %v", blockHash, height, header.Timestamp)
|
||||||
log.Printf("Block disconnected: %v (%d) %v",
|
|
||||||
header.BlockHash(), height, header.Timestamp)
|
if err := send(*stratum, *stratumPass, *coinid, blockHash); err != nil {
|
||||||
|
log.Printf("ERROR: failed to notify stratum: %s", err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,30 +70,30 @@ func main() {
|
||||||
lbcdHomeDir := lbcutil.AppDataDir("lbcd", false)
|
lbcdHomeDir := lbcutil.AppDataDir("lbcd", false)
|
||||||
certs, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
|
certs, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("can't read lbcd certificate: %s", err)
|
||||||
}
|
}
|
||||||
connCfg := &rpcclient.ConnConfig{
|
connCfg := &rpcclient.ConnConfig{
|
||||||
Host: "localhost:9245",
|
Host: *rpcserver,
|
||||||
Endpoint: "ws",
|
Endpoint: "ws",
|
||||||
User: "rpcuser",
|
User: *rpcuser,
|
||||||
Pass: "rpcpass",
|
Pass: *rpcpass,
|
||||||
Certificates: certs,
|
Certificates: certs,
|
||||||
}
|
}
|
||||||
client, err := rpcclient.New(connCfg, &ntfnHandlers)
|
client, err := rpcclient.New(connCfg, &ntfnHandlers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("can't create rpc client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register for block connect and disconnect notifications.
|
// Register for block connect and disconnect notifications.
|
||||||
if err := client.NotifyBlocks(); err != nil {
|
if err = client.NotifyBlocks(); err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalf("can't register block notification: %s", err)
|
||||||
}
|
}
|
||||||
log.Println("NotifyBlocks: Registration Complete")
|
log.Printf("NotifyBlocks: Registration Complete")
|
||||||
|
|
||||||
// Get the current block count.
|
// Get the current block count.
|
||||||
blockCount, err := client.GetBlockCount()
|
blockCount, err := client.GetBlockCount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatalf("can't get block count: %s", err)
|
||||||
}
|
}
|
||||||
log.Printf("Block count: %d", blockCount)
|
log.Printf("Block count: %d", blockCount)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue