Skip to content

Commit

Permalink
vanilla vigilante codebase (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Aug 11, 2022
1 parent d4a48f9 commit 4489e70
Show file tree
Hide file tree
Showing 33 changed files with 2,449 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.vscode/
main
17 changes: 17 additions & 0 deletions LICENSE
@@ -0,0 +1,17 @@
ISC License

Copyright (c) 2022-2022 The Babylon developers
Copyright (c) 2013-2017 The btcsuite developers
Copyright (c) 2015-2016 The Decred developers

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3 changes: 3 additions & 0 deletions btcclient/README.md
@@ -0,0 +1,3 @@
# btcclient

This package implements a Bitcoin client. The code is adapted from https://github.com/btcsuite/btcwallet/tree/master/chain.
58 changes: 58 additions & 0 deletions btcclient/client.go
@@ -0,0 +1,58 @@
// Copyright (c) 2022-2022 The Babylon developers
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package btcclient

import (
"errors"

"github.com/babylonchain/vigilante/config"
"github.com/babylonchain/vigilante/netparams"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/chain"
)

var _ chain.Interface = &Client{}

// Client represents a persistent client connection to a bitcoin RPC server
// for information regarding the current best block chain.
type Client struct {
*chain.RPCClient
Params *chaincfg.Params
Cfg *config.BTCConfig
}

// New creates a client connection to the server described by the
// connect string. If disableTLS is false, the remote RPC certificate must be
// provided in the certs slice. The connection is not established immediately,
// but must be done using the Start method. If the remote server does not
// operate on the same bitcoin network as described by the passed chain
// parameters, the connection will be disconnected.
func New(cfg *config.BTCConfig) (*Client, error) {
if cfg.ReconnectAttempts < 0 {
return nil, errors.New("reconnectAttempts must be positive")
}

certs := readCAFile(cfg)
params := netparams.GetBTCParams(cfg.NetParams)

rpcClient, err := chain.NewRPCClient(params, cfg.Endpoint, cfg.Username, cfg.Password, certs, cfg.DisableClientTLS, cfg.ReconnectAttempts)
if err != nil {
return nil, err
}
client := &Client{rpcClient, params, cfg}
return client, err
}

func (c *Client) ConnectLoop() {
go func() {
log.Infof("Start connecting to the BTC node %v", c.Cfg.Endpoint)
if err := c.Start(); err != nil {
log.Errorf("Unable to connect to the BTC node: %v", err)
}
log.Info("Successfully connected to the BTC node")
c.WaitForShutdown()
}()
}
7 changes: 7 additions & 0 deletions btcclient/log.go
@@ -0,0 +1,7 @@
package btcclient

import (
vlog "github.com/babylonchain/vigilante/log"
)

var log = vlog.Logger.WithField("module", "btcclient")
26 changes: 26 additions & 0 deletions btcclient/tls.go
@@ -0,0 +1,26 @@
package btcclient

import (
"io/ioutil"

"github.com/babylonchain/vigilante/config"
)

func readCAFile(cfg *config.BTCConfig) []byte {
// Read certificate file if TLS is not disabled.
var certs []byte
if !cfg.DisableClientTLS {
var err error
certs, err = ioutil.ReadFile(cfg.CAFile)
if err != nil {
log.Errorf("Cannot open CA file: %v", err)
// If there's an error reading the CA file, continue
// with nil certs and without the client connection.
certs = nil
}
} else {
log.Infof("Chain server RPC TLS is disabled")
}

return certs
}
30 changes: 30 additions & 0 deletions cmd/main.go
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/babylonchain/vigilante/cmd/reporter"
"github.com/babylonchain/vigilante/cmd/submitter"
)

// TODO: init log

func main() {
rootCmd := &cobra.Command{
Use: "vigilante",
Short: "Babylon vigilante",
}
rootCmd.AddCommand(reporter.GetCmd(), submitter.GetCmd())

if err := rootCmd.Execute(); err != nil {
switch e := err.(type) {
// TODO: dedicated error codes for vigilantes
default:
fmt.Print(e.Error())
os.Exit(1)
}
}
}
94 changes: 94 additions & 0 deletions cmd/reporter/reporter.go
@@ -0,0 +1,94 @@
package reporter

import (
"github.com/babylonchain/vigilante/btcclient"
"github.com/babylonchain/vigilante/cmd/utils"
"github.com/babylonchain/vigilante/config"
vlog "github.com/babylonchain/vigilante/log"
"github.com/babylonchain/vigilante/metrics"
"github.com/babylonchain/vigilante/rpcserver"
"github.com/babylonchain/vigilante/vigilante"
"github.com/spf13/cobra"
)

var (
cfgFile = ""
log = vlog.Logger.WithField("module", "cmd")
)

// GetCmd returns the cli query commands for this module
func GetCmd() *cobra.Command {
// Group epoching queries under a subcommand
cmd := &cobra.Command{
Use: "reporter",
Short: "Vigilant reporter",
Run: cmdFunc,
}
addFlags(cmd)
return cmd
}

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&cfgFile, "config", "", "config file")
}

func cmdFunc(cmd *cobra.Command, args []string) {
// get the config from the given file, the default file, or generate a default config
var err error
var cfg config.Config
if len(cfgFile) != 0 {
cfg, err = config.NewFromFile(cfgFile)
} else {
cfg, err = config.New()
}
if err != nil {
panic(err)
}

// create BTC client
btcClient, err := btcclient.New(&cfg.BTC)
if err != nil {
panic(err)
}
// create reporter
reporter, err := vigilante.NewReporter(&cfg.Reporter, btcClient)
if err != nil {
panic(err)
}
// crete RPC server
server, err := rpcserver.New(&cfg.GRPC, nil, reporter)
if err != nil {
panic(err)
}

// keep trying BTC client
btcClient.ConnectLoop()
// start reporter and sync
reporter.Start()
reporter.SynchronizeRPC(btcClient)
// start RPC server
server.Start()
// start Prometheus metrics server
metrics.Start()

// SIGINT handling stuff
utils.AddInterruptHandler(func() {
// TODO: Does this need to wait for the grpc server to finish up any requests?
log.Info("Stopping RPC server...")
server.Stop()
log.Info("RPC server shutdown")
})
utils.AddInterruptHandler(func() {
log.Info("Stopping reporter...")
reporter.Stop()
log.Info("Reporter shutdown")
})
utils.AddInterruptHandler(func() {
log.Info("Stopping BTC client...")
btcClient.Stop()
log.Info("BTC client shutdown")
})

<-utils.InterruptHandlersDone
log.Info("Shutdown complete")
}
94 changes: 94 additions & 0 deletions cmd/submitter/submitter.go
@@ -0,0 +1,94 @@
package submitter

import (
"github.com/babylonchain/vigilante/btcclient"
"github.com/babylonchain/vigilante/cmd/utils"
"github.com/babylonchain/vigilante/config"
vlog "github.com/babylonchain/vigilante/log"
"github.com/babylonchain/vigilante/metrics"
"github.com/babylonchain/vigilante/rpcserver"
"github.com/babylonchain/vigilante/vigilante"
"github.com/spf13/cobra"
)

var (
cfgFile = ""
log = vlog.Logger.WithField("module", "cmd")
)

// GetCmd returns the cli query commands for this module
func GetCmd() *cobra.Command {
// Group epoching queries under a subcommand
cmd := &cobra.Command{
Use: "submitter",
Short: "Vigilant submitter",
Run: cmdFunc,
}
addFlags(cmd)
return cmd
}

func addFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&cfgFile, "config", "", "config file")
}

func cmdFunc(cmd *cobra.Command, args []string) {
// get the config from the given file, the default file, or generate a default config
var err error
var cfg config.Config
if len(cfgFile) != 0 {
cfg, err = config.NewFromFile(cfgFile)
} else {
cfg, err = config.New()
}
if err != nil {
panic(err)
}

// create BTC client
btcClient, err := btcclient.New(&cfg.BTC)
if err != nil {
panic(err)
}
// create submitter
submitter, err := vigilante.NewSubmitter(&cfg.Submitter, btcClient)
if err != nil {
panic(err)
}
// crete RPC server
server, err := rpcserver.New(&cfg.GRPC, submitter, nil)
if err != nil {
panic(err)
}

// keep trying BTC client
btcClient.ConnectLoop()
// start submitter and sync
submitter.Start()
submitter.SynchronizeRPC(btcClient)
// start RPC server
server.Start()
// start Prometheus metrics server
metrics.Start()

// SIGINT handling stuff
utils.AddInterruptHandler(func() {
// TODO: Does this need to wait for the grpc server to finish up any requests?
log.Info("Stopping RPC server...")
server.Stop()
log.Info("RPC server shutdown")
})
utils.AddInterruptHandler(func() {
log.Info("Stopping submitter...")
submitter.Stop()
log.Info("Submitter shutdown")
})
utils.AddInterruptHandler(func() {
log.Info("Stopping BTC client...")
btcClient.Stop()
log.Info("BTC client shutdown")
})

<-utils.InterruptHandlersDone
log.Info("Shutdown complete")
}

0 comments on commit 4489e70

Please sign in to comment.