Skip to content

Commit

Permalink
Enable support for disabling RPC verification and writing to a log fi…
Browse files Browse the repository at this point in the history
…le (#46)

* Remove gocore logger

* Updates to fix CI

* Fix linter
  • Loading branch information
galt-tr authored Feb 9, 2024
1 parent 56b27eb commit ba4ca2f
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type (
Config struct {
AlertWebhookURL string `json:"alert_webhook_url" mapstructure:"alert_webhook_url"` // AlertWebhookURL is the URL for the alert webhook
Datastore DatastoreConfig `json:"datastore" mapstructure:"datastore"` // Datastore's configuration
DisableRPCVerification bool `json:"disable_rpc_verification" mapstructure:"disable_rpc_verification"` // DisableRPCVerification will disable the rpc verification check on startup. Useful if bitcoind isn't running yet
LogOutputFile string `json:"log_output_file" mapstructure:"log_output_file"` // LogOutputFile will set an output file for the logger to write to as opposed to stdout
BitcoinConfigPath string `json:"bitcoin_config_path" mapstructure:"bitcoin_config_path"` // BitcoinConfigPath is the path to the bitcoin.conf file
P2P P2PConfig `json:"p2p" mapstructure:"p2p"` // P2P is the configuration for the P2P server
RPCConnections []RPCConfig `json:"rpc_connections" mapstructure:"rpc_connections"` // RPCConnections is a list of RPC connections
Expand Down
5 changes: 4 additions & 1 deletion app/config/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"context"

"github.com/mrz1836/go-logger"

"github.com/mrz1836/go-datastore"
)

Expand All @@ -11,7 +13,8 @@ func (c *Config) loadDatastore(ctx context.Context, models []interface{}) error

// Sync collecting the options
var options []datastore.ClientOps

//TODO: pass in our own logger, but for now this doesn't work so i'm just going to silently log for now
options = append(options, datastore.WithLogger(logger.NewGormLogger(false, 0)))
// Select the datastore
if c.Datastore.Engine == datastore.SQLite {
options = append(options, datastore.WithSQLite(&datastore.SQLiteConfig{
Expand Down
3 changes: 2 additions & 1 deletion app/config/envs/local.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"alert_webhook_url": "",
"bitcoin_config_path": "",
"disable_rpc_verification": false,
"log_output_file": "",
"request_logging": true,
"bitcoin_config_path": "/home/galt/.bitcoin/bitcoin.conf",
"alert_processing_interval": "5m",
"web_server": {
"idle_timeout": "60s",
Expand Down
5 changes: 4 additions & 1 deletion app/config/envs/mainnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"alert_webhook_url": "",
"bitcoin_config_path": "",
"log_output_file": "",
"disable_rpc_verification": false,
"request_logging": true,
"alert_processing_interval": "5m",
"web_server": {
"idle_timeout": "60s",
"port": "3000",
Expand Down Expand Up @@ -55,7 +58,7 @@
"p2p": {
"ip": "0.0.0.0",
"port": "9906",
"alert_system_protocol_id": "/bitcoin/alert-system/1.0.1",
"alert_system_protocol_id": "/bitcoin/alert-system/1.0.0",
"bootstrap_peer": "",
"private_key_path": "",
"topic_name": "bitcoin_alert_system"
Expand Down
4 changes: 3 additions & 1 deletion app/config/envs/production.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"alert_webhook_url": "",
"bitcoin_config_path": "",
"log_output_file": "",
"disable_rpc_verification": false,
"request_logging": true,
"web_server": {
"idle_timeout": "60s",
Expand Down Expand Up @@ -55,7 +57,7 @@
"p2p": {
"ip": "0.0.0.0",
"port": "9906",
"alert_system_protocol_id": "/bitcoin/alert-system/1.0.1",
"alert_system_protocol_id": "/bitcoin/alert-system/1.0.0",
"bootstrap_peer": "",
"private_key_path": "",
"topic_name": "bitcoin_alert_system"
Expand Down
2 changes: 2 additions & 0 deletions app/config/envs/stn.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"alert_webhook_url": "",
"bitcoin_config_path": "",
"log_output_file": "",
"disable_rpc_verification": false,
"request_logging": true,
"alert_processing_interval": "5m",
"web_server": {
Expand Down
2 changes: 2 additions & 0 deletions app/config/envs/test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"alert_webhook_url": "https://webhook.url",
"bitcoin_config_path": "",
"log_output_file": "",
"disable_rpc_verification": false,
"request_logging": true,
"web_server": {
"idle_timeout": "60s",
Expand Down
2 changes: 2 additions & 0 deletions app/config/envs/testnet.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"alert_webhook_url": "",
"bitcoin_config_path": "",
"log_output_file": "",
"disable_rpc_verification": false,
"request_logging": true,
"alert_processing_interval": "5m",
"web_server": {
Expand Down
16 changes: 13 additions & 3 deletions app/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"errors"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"strings"
"sync"

"github.com/mrz1836/go-datastore"
"github.com/ordishs/gocore"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -214,9 +214,19 @@ func LoadConfigFile() (_appConfig *Config, err error) {
return nil, err
}

// Load the logger service (gocore.Logger meets the LoggerInterface)
// Load the logger service (ExtendedLogger meets the LoggerInterface)
writer := os.Stdout
if _appConfig.LogOutputFile != "" {
writer, err = os.OpenFile(_appConfig.LogOutputFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return nil, err
}
}

logger := log.New(writer, "bitcoin-alert-system: ", log.LstdFlags)
_appConfig.Services.Log = &ExtendedLogger{
Logger: gocore.Log(ApplicationName),
Logger: logger,
writer: writer,
}

// Set default alert processing interval if it doesn't exist
Expand Down
68 changes: 65 additions & 3 deletions app/config/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "github.com/ordishs/gocore"
import (
"fmt"
"log"
"os"
)

// LoggerInterface is the interface for the logger
// This is used to allow the logger to be mocked and tested
Expand All @@ -21,15 +25,73 @@ type LoggerInterface interface {
Warn(args ...interface{})
Warnf(msg string, args ...interface{})
Printf(format string, v ...interface{}) // Custom method for go-api-router
CloseWriter() error
// GetLogLevel() gocore.logLevel
}

// ExtendedLogger is the extended logger to satisfy the LoggerInterface
type ExtendedLogger struct {
*gocore.Logger
*log.Logger
logLevel int
writer *os.File
}

// CloseWriter close the log writer
func (es *ExtendedLogger) CloseWriter() error {
return es.writer.Close()
}

// Printf will print the log message to the console
func (es *ExtendedLogger) Printf(format string, v ...interface{}) {
es.Infof(format, v...)
es.Logger.Printf(format, v...)
}

// Debugf will print debug messages to the console
func (es *ExtendedLogger) Debugf(format string, v ...interface{}) {
es.Logger.Printf(fmt.Sprintf("\033[1;34m| DEBUG | %s\033[0m", format), v...)
}

// Debug will print debug messages to the console
func (es *ExtendedLogger) Debug(v ...interface{}) {
es.Logger.Printf("%v", v...)
}

// Error will print debug messages to the console
func (es *ExtendedLogger) Error(v ...interface{}) {
es.Logger.Printf("%v", v...)
}

// Errorf will print debug messages to the console
func (es *ExtendedLogger) Errorf(format string, v ...interface{}) {
es.Logger.Printf(fmt.Sprintf("\033[1;31m| ERROR |: %s\033[0m", format), v...)
}

// ErrorWithStack will print debug messages to the console
func (es *ExtendedLogger) ErrorWithStack(format string, v ...interface{}) {
es.Logger.Printf(format, v...)
}

// Info will print info messages to the console
func (es *ExtendedLogger) Info(v ...interface{}) {
es.Logger.Printf("%v", v...)
}

// Infof will print info messages to the console
func (es *ExtendedLogger) Infof(format string, v ...interface{}) {
es.Logger.Printf(fmt.Sprintf("\033[1;32m| INFO | %s\033[0m", format), v...)
}

// LogLevel returns the logging level
func (es *ExtendedLogger) LogLevel() int {
return es.logLevel
}

// Warn will print warning messages to the console
func (es *ExtendedLogger) Warn(v ...interface{}) {
es.Logger.Printf("%v", v...)
}

// Warnf will print warning messages to the console
func (es *ExtendedLogger) Warnf(format string, v ...interface{}) {
es.Logger.Printf(format, v...)
}
4 changes: 2 additions & 2 deletions app/models/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package model

import (
"context"
"log"
"time"

"github.com/bitcoin-sv/alert-system/app/config"
"github.com/mrz1836/go-datastore"
customTypes "github.com/mrz1836/go-datastore/custom_types"
"github.com/ordishs/gocore"
)

// Model is the generic model field(s) and interface(s)
Expand Down Expand Up @@ -71,7 +71,7 @@ func NewBaseModel(name Name, opts ...Options) (m *Model) {
// Set default logger IF NOT SET via options
if m.logger == nil {
m.logger = &config.ExtendedLogger{
Logger: gocore.Log(config.ApplicationName),
Logger: &log.Logger{},
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/models/model/model_options_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package model

import (
"log"
"os"
"testing"

"github.com/bitcoin-sv/alert-system/app/config"
"github.com/ordishs/gocore"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestWithLogger(t *testing.T) {

t.Run("valid logger", func(t *testing.T) {
l := &config.ExtendedLogger{
Logger: gocore.Log("test"),
Logger: log.New(os.Stdout, "alert-system: ", log.LstdFlags),
}
require.NotNil(t, l)
opt := WithLogger(l)
Expand Down
11 changes: 8 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ func main() {
}

// Ensure that RPC connection is valid
if _, err = _appConfig.Services.Node.BestBlockHash(context.Background()); err != nil {
_appConfig.Services.Log.Errorf("error talking to Bitcoin node with supplied RPC credentials: %s", err.Error())
return
if !_appConfig.DisableRPCVerification {
if _, err = _appConfig.Services.Node.BestBlockHash(context.Background()); err != nil {
_appConfig.Services.Log.Errorf("error talking to Bitcoin node with supplied RPC credentials: %s", err.Error())
return
}
}

// Create the p2p server
Expand Down Expand Up @@ -77,6 +79,9 @@ func main() {
}

close(idleConnectionsClosed)
if err = appConfig.Services.Log.CloseWriter(); err != nil {
log.Printf("error closing logger: %s", err)
}
}(_appConfig)

// Start the p2p server
Expand Down
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ba4ca2f

Please sign in to comment.