Skip to content

Commit

Permalink
Merge pull request #243 from bitcoin-sv/feature/mtm-readiness
Browse files Browse the repository at this point in the history
Feature/mtm readiness
  • Loading branch information
boecklim authored Jan 11, 2024
2 parents b8fc45a + 58bb02e commit 343eb77
Show file tree
Hide file tree
Showing 23 changed files with 836 additions and 544 deletions.
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ gen:
--go-grpc_opt=paths=source_relative \
metamorph/metamorph_api/metamorph_api.proto

protoc \
--proto_path=. \
--go_out=. \
--go_opt=paths=source_relative \
--go-grpc_out=. \
--go-grpc_opt=paths=source_relative \
metamorph/health/grpc_health_check.proto

protoc \
--proto_path=. \
--go_out=. \
Expand Down
10 changes: 10 additions & 0 deletions blocktx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ClientI interface {
GetBlock(ctx context.Context, blockHash *chainhash.Hash) (*blocktx_api.Block, error)
GetLastProcessedBlock(ctx context.Context) (*blocktx_api.Block, error)
GetMinedTransactionsForBlock(ctx context.Context, blockAndSource *blocktx_api.BlockAndSource) (*blocktx_api.MinedTransactions, error)
Health(ctx context.Context) error
}

const (
Expand Down Expand Up @@ -116,6 +117,15 @@ func (btc *Client) GetMinedTransactionsForBlock(ctx context.Context, blockAndSou
return mt, nil
}

func (btc *Client) Health(ctx context.Context) error {
_, err := btc.client.Health(ctx, &emptypb.Empty{})
if err != nil {
return err
}

return nil
}

func DialGRPC(address string) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
65 changes: 37 additions & 28 deletions cmd/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
"github.com/bitcoin-sv/arc/metamorph/store/sqlite"
"github.com/libsv/go-p2p"
"github.com/ordishs/go-bitcoin"
"github.com/ordishs/go-utils"
"github.com/ordishs/go-utils/safemap"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)

const (
Expand Down Expand Up @@ -81,32 +83,6 @@ func StartMetamorph(logger *slog.Logger) (func(), error) {
return nil, fmt.Errorf("no metamorph.listenAddr setting found")
}

source := metamorphGRPCListenAddress
if viper.GetBool("metamorph.network.fixedIp") {
ip, port, err := net.SplitHostPort(metamorphGRPCListenAddress)
if err != nil {
return nil, fmt.Errorf("cannot parse ip address: %v", err)
}

if ip != "" {
source = metamorphGRPCListenAddress
} else {
hint := viper.GetString("metamorph.network.ipAddressHint")
ips, err := utils.GetIPAddressesWithHint(hint)
if err != nil {
return nil, fmt.Errorf("cannot get local ip address")
}

if len(ips) != 1 {
return nil, fmt.Errorf("cannot determine local ip address [%v]", ips)
}

source = fmt.Sprintf("%s:%s", ips[0], port)
}

logger.Info("Instance will register transactions with location", "source", source)
}

pm, statusMessageCh, err := initPeerManager(logger, s)
if err != nil {
return nil, err
Expand Down Expand Up @@ -222,7 +198,7 @@ func StartMetamorph(logger *slog.Logger) (func(), error) {
opts = append(opts, metamorph.WithBlocktxTimeout(btxTimeout))
}

serv := metamorph.NewServer(s, metamorphProcessor, btx, source, opts...)
serv := metamorph.NewServer(s, metamorphProcessor, btx, opts...)

go func() {
grpcMessageSize := viper.GetInt("grpcMessageSize")
Expand Down Expand Up @@ -264,6 +240,13 @@ func StartMetamorph(logger *slog.Logger) (func(), error) {
// pass all the started peers to the collector
_ = metamorph.NewZMQCollector(zmqCollector)

go func() {
err = StartHealthServer(serv)
if err != nil {
logger.Error("failed to start health server", slog.String("err", err.Error()))
}
}()

return func() {
logger.Info("Shutting down metamorph")

Expand All @@ -276,6 +259,32 @@ func StartMetamorph(logger *slog.Logger) (func(), error) {
}, nil
}

func StartHealthServer(serv *metamorph.Server) error {
gs := grpc.NewServer()
defer gs.Stop()

grpc_health_v1.RegisterHealthServer(gs, serv) // registration
// register your own services
reflection.Register(gs)

address, err := config.GetString("metamorph.healthServerDialAddr") //"localhost:8005"
if err != nil {
return err
}

listener, err := net.Listen("tcp", address)
if err != nil {
return err
}

err = gs.Serve(listener)
if err != nil {
return err
}

return nil
}

func NewStore(dbMode string, folder string) (s store.MetamorphStore, err error) {
switch dbMode {
case DbModePostgres:
Expand Down
4 changes: 1 addition & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ metamorph:
checkUtxos: false # force check each utxo for validity. If enabled ARC connects to bitcoin node using rpc for each utxo
statsKeypress: false # enable stats keypress. If enabled pressing any key will print stats to stdout
profilerAddr: localhost:9992 # address to start profiler server on
network:
fixedIp: true # Denotes if ARC is running with fixed IP addresses
ipAddressHint: ^172.28.*
blocktxTimeout: 1s # timeout for blocktx service
checkIfMinedInterval: 1m
healthServerDialAddr: localhost:8005

blocktx:
listenAddr: localhost:8011 # address space for blocktx to listen on. Can be for example localhost:8011 or :8011 for listening on all addresses
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,6 @@ github.com/libsv/go-bt v1.0.8 h1:nWLLcnUm0dxNO3exqrL5jvAcTGkl0dsnBuQqB6+M6vQ=
github.com/libsv/go-bt v1.0.8/go.mod h1:yO023bNYLh5DwcOYl+ZqLAeTemoy6K+2UbQlIBMv+EQ=
github.com/libsv/go-bt/v2 v2.2.5 h1:VoggBLMRW9NYoFujqe5bSYKqnw5y+fYfufgERSoubog=
github.com/libsv/go-bt/v2 v2.2.5/go.mod h1:cV45+jDlPOLfhJLfpLmpQoWzrIvVth9Ao2ZO1f6CcqU=
github.com/libsv/go-p2p v0.1.5 h1:zbUE1UQ73J7LN/s3gruQBTh3Sz0DSSmj3cWhC1ZQVM0=
github.com/libsv/go-p2p v0.1.5/go.mod h1:9KhX8e+3oEmGiYQSeF/CrHj22YNHqiof3TH77VqcMCs=
github.com/libsv/go-p2p v0.1.6 h1:YHlxAiuCSq7k+eyk7haD6M6v2+dTpo/F0pxCzOcFLI4=
github.com/libsv/go-p2p v0.1.6/go.mod h1:9KhX8e+3oEmGiYQSeF/CrHj22YNHqiof3TH77VqcMCs=
github.com/lmittmann/tint v1.0.3 h1:W5PHeA2D8bBJVvabNfQD/XW9HPLZK1XoPZH0cq8NouQ=
Expand Down
Loading

0 comments on commit 343eb77

Please sign in to comment.