Skip to content

Commit

Permalink
rig up gozer in bfg
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeereboom committed Feb 26, 2025
1 parent ca92e4e commit 832e911
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
2 changes: 2 additions & 0 deletions bitcoin/wallet/gozer/tbcgozer/tbcgozer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
const (
logLevel = "INFO"
defaultRequestTimeout = 3 * time.Second

DefaultURL = "ws://localhost:8082/v1/ws"
)

var log = loggo.GetLogger("tbcgozer")
Expand Down
26 changes: 22 additions & 4 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ var (

cfg = bfg.NewDefaultConfig()
cm = config.CfgMap{
"BFG_BITCOIN_SOURCE": config.Config{
Value: &cfg.BitcoinSource,
DefaultValue: "blockstream",
Help: "which bitcoin source of truth is used (blockstream and tbc)",
Print: config.PrintAll,
},
"BFG_BITCOIN_URL": config.Config{
Value: &cfg.BitcoinURL,
DefaultValue: "",
Help: "conection url for bitcoin source if needed e.g. ws://localhost:8082/v1/ws",
Print: config.PrintAll,
},
"BFG_LOG_LEVEL": config.Config{
Value: &cfg.LogLevel,
DefaultValue: defaultLogLevel,
Expand All @@ -43,10 +55,10 @@ var (
Help: "address and port bfgd listens on for http connections",
Print: config.PrintAll,
},
"BFG_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port bfgd prometheus listens on",
"BFG_NETWORK": config.Config{
Value: &cfg.Network,
DefaultValue: "mainnet",
Help: "network bfg is working on (mainnet/testnet3)",
Print: config.PrintAll,
},
"BFG_PPROF_ADDRESS": config.Config{
Expand All @@ -55,6 +67,12 @@ var (
Help: "address and port bfgd pprof listens on (open <address>/debug/pprof to see available profiles)",
Print: config.PrintAll,
},
"BFG_PROMETHEUS_ADDRESS": config.Config{
Value: &cfg.PrometheusListenAddress,
DefaultValue: "",
Help: "address and port bfgd prometheus listens on",
Print: config.PrintAll,
},
}
)

Expand Down
47 changes: 46 additions & 1 deletion service/bfg/bfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@ import (
"sync"
"time"

"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/juju/loggo"
"github.com/prometheus/client_golang/prometheus"

"github.com/hemilabs/heminetwork/api/bfgapi"
"github.com/hemilabs/heminetwork/bitcoin/wallet/gozer"
"github.com/hemilabs/heminetwork/bitcoin/wallet/gozer/blockstream"
"github.com/hemilabs/heminetwork/bitcoin/wallet/gozer/tbcgozer"
"github.com/hemilabs/heminetwork/service/deucalion"
"github.com/hemilabs/heminetwork/service/pprof"
)

const (
logLevel = "INFO"
appName = "bfg" // Prometheus

bitcoinSourceBlockstream = "blockstream"
bitcoinSourceTBC = "tbc"
)

var log = loggo.GetLogger(appName)
Expand All @@ -46,17 +53,22 @@ func init() {
}

type Config struct {
BitcoinSource string // gozer types
BitcoinURL string // only used for certain types
ListenAddress string
LogLevel string
Network string // bitcoin network
PprofListenAddress string
PrometheusListenAddress string
PrometheusNamespace string
PprofListenAddress string
}

func NewDefaultConfig() *Config {
return &Config{
BitcoinSource: bitcoinSourceBlockstream,
ListenAddress: bfgapi.DefaultListenAddress,
LogLevel: logLevel,
Network: "mainnet",
PrometheusNamespace: appName,
}
}
Expand All @@ -67,6 +79,10 @@ type Server struct {

cfg *Config

params *chaincfg.Params

g gozer.Gozer

server *http.ServeMux

// Prometheus
Expand Down Expand Up @@ -232,6 +248,35 @@ func (s *Server) Run(pctx context.Context) error {
ctx, cancel := context.WithCancel(pctx)
defer cancel()

// Setup gozer
switch s.cfg.Network {
case "mainnet":
s.params = &chaincfg.MainNetParams
case "testnet3":
s.params = &chaincfg.TestNet3Params
default:
return fmt.Errorf("invalid network: %v", s.cfg.Network)
}

switch s.cfg.BitcoinSource {
case bitcoinSourceBlockstream:
var err error
s.g, err = blockstream.BlockstreamNew(s.params)
if err != nil {
return fmt.Errorf("could not setup %v blockstream: %v",
s.cfg.Network, err)
}
case bitcoinSourceTBC:
var err error
s.g, err = tbcgozer.TBCGozerNew(ctx, s.cfg.BitcoinURL)
if err != nil {
return fmt.Errorf("could not setup %v tbc: %v",
s.cfg.Network, err)
}
default:
return fmt.Errorf("invalid bitcoin source: %v", s.cfg.BitcoinSource)
}

// HTTP server
httpErrCh := make(chan error)
if s.cfg.ListenAddress != "" {
Expand Down

0 comments on commit 832e911

Please sign in to comment.