lbcdblocknotify: support --run to execute custom command
This commit is contained in:
parent
e323751218
commit
e7319b2616
1 changed files with 52 additions and 22 deletions
|
@ -5,19 +5,69 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/lbryio/lbcd/rpcclient"
|
"github.com/lbryio/lbcd/rpcclient"
|
||||||
"github.com/lbryio/lbcd/wire"
|
"github.com/lbryio/lbcd/wire"
|
||||||
"github.com/lbryio/lbcutil"
|
"github.com/lbryio/lbcutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func send(stratum, stratumPass, coinid, blockHash string) error {
|
var (
|
||||||
|
coinid = flag.String("coinid", "1425", "Coin ID")
|
||||||
|
stratum = flag.String("stratum", "", "Stratum server")
|
||||||
|
stratumPass = flag.String("stratumpass", "", "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")
|
||||||
|
notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled")
|
||||||
|
run = flag.String("run", "", "Run custom shell command")
|
||||||
|
)
|
||||||
|
|
||||||
|
func onFilteredBlockConnected(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
||||||
|
|
||||||
|
blockHash := header.BlockHash().String()
|
||||||
|
|
||||||
|
log.Printf("Block connected: %v (%d) %v", blockHash, height, header.Timestamp)
|
||||||
|
|
||||||
|
if cmd := *run; len(cmd) != 0 {
|
||||||
|
cmd = strings.ReplaceAll(cmd, "%s", blockHash)
|
||||||
|
err := execCustomCommand(cmd)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("ERROR: execCustomCommand: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(*stratum) > 0 && len(*stratumPass) > 0 {
|
||||||
|
err := stratumUpdateBlock(*stratum, *stratumPass, *coinid, blockHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("ERROR: stratumUpdateBlock: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func execCustomCommand(cmd string) error {
|
||||||
|
strs := strings.Split(cmd, " ")
|
||||||
|
path, err := exec.LookPath(strs[0])
|
||||||
|
if errors.Is(err, exec.ErrDot) {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c := exec.Command(path, strs[1:]...)
|
||||||
|
c.Stdout = os.Stdout
|
||||||
|
return c.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func stratumUpdateBlock(stratum, stratumPass, coinid, blockHash string) error {
|
||||||
addr, err := net.ResolveTCPAddr("tcp", stratum)
|
addr, err := net.ResolveTCPAddr("tcp", stratum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't resolve addr: %w", err)
|
return fmt.Errorf("can't resolve addr: %w", err)
|
||||||
|
@ -39,32 +89,12 @@ func send(stratum, stratumPass, coinid, blockHash string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var (
|
|
||||||
coinid = flag.String("coinid", "1425", "Coin ID")
|
|
||||||
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")
|
|
||||||
notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled")
|
|
||||||
)
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
ntfnHandlers := rpcclient.NotificationHandlers{
|
ntfnHandlers := rpcclient.NotificationHandlers{
|
||||||
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
OnFilteredBlockConnected: onFilteredBlockConnected,
|
||||||
|
|
||||||
blockHash := header.BlockHash().String()
|
|
||||||
|
|
||||||
log.Printf("Block connected: %v (%d) %v", blockHash, height, header.Timestamp)
|
|
||||||
|
|
||||||
if err := send(*stratum, *stratumPass, *coinid, blockHash); err != nil {
|
|
||||||
log.Printf("ERROR: failed to notify stratum: %s", err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to local lbcd RPC server using websockets.
|
// Connect to local lbcd RPC server using websockets.
|
||||||
|
|
Loading…
Reference in a new issue