-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4a48f9
commit 4489e70
Showing
33 changed files
with
2,449 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
.vscode/ | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# btcclient | ||
|
||
This package implements a Bitcoin client. The code is adapted from https://github.com/btcsuite/btcwallet/tree/master/chain. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package btcclient | ||
|
||
import ( | ||
vlog "github.com/babylonchain/vigilante/log" | ||
) | ||
|
||
var log = vlog.Logger.WithField("module", "btcclient") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
Oops, something went wrong.