Skip to content

Commit

Permalink
Merge pull request #210 from bitcoin-sv/db/metamorph-postgres
Browse files Browse the repository at this point in the history
Get metamorph client if metamorph storage is either postgres or dynamodb
  • Loading branch information
boecklim authored Dec 11, 2023
2 parents b600c80 + ff6aed4 commit e25644c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
25 changes: 13 additions & 12 deletions api/transactionHandler/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ import (
"github.com/bitcoin-sv/arc/tracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/ordishs/go-utils"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

// Metamorph is the connector to a metamorph server
type Metamorph struct {
mu sync.RWMutex
Client metamorph_api.MetaMorphAPIClient
ClientCache map[string]metamorph_api.MetaMorphAPIClient
blockTxClient blocktx.ClientI
logger utils.Logger
mu sync.RWMutex
Client metamorph_api.MetaMorphAPIClient
ClientCache map[string]metamorph_api.MetaMorphAPIClient
blockTxClient blocktx.ClientI
logger utils.Logger
isCentralisedMetamorph bool
}

// NewMetamorph creates a connection to a list of metamorph servers via gRPC
func NewMetamorph(address string, blockTxClient blocktx.ClientI, grpcMessageSize int, logger utils.Logger) (*Metamorph, error) {
func NewMetamorph(address string, blockTxClient blocktx.ClientI, grpcMessageSize int, logger utils.Logger, isCentralisedMetamorph bool) (*Metamorph, error) {
conn, err := DialGRPC(address, grpcMessageSize)
if err != nil {
return nil, fmt.Errorf("failed to get connection to address %s: %v", address, err)
}

return &Metamorph{
Client: metamorph_api.NewMetaMorphAPIClient(conn),
ClientCache: make(map[string]metamorph_api.MetaMorphAPIClient),
blockTxClient: blockTxClient,
logger: logger,
Client: metamorph_api.NewMetaMorphAPIClient(conn),
ClientCache: make(map[string]metamorph_api.MetaMorphAPIClient),
blockTxClient: blockTxClient,
logger: logger,
isCentralisedMetamorph: isCentralisedMetamorph,
}, nil
}

Expand Down Expand Up @@ -174,7 +175,7 @@ func (m *Metamorph) SubmitTransactions(ctx context.Context, txs [][]byte, txOpti
}

func (m *Metamorph) getMetamorphClientForTx(ctx context.Context, txID string) (metamorph_api.MetaMorphAPIClient, error) {
if viper.GetString("metamorph.db.mode") == "dynamodb" {
if m.isCentralisedMetamorph {
return m.Client, nil
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func LoadArcHandler(e *echo.Echo, logger utils.Logger) error {
return fmt.Errorf("grpcMessageSize not found in config")
}

txHandler, err := transactionHandler.NewMetamorph(addresses, bTx, grpcMessageSize, logger)
isCentralisedMetamorph := false
if viper.GetString("metamorph.db.mode") == "dynamodb" || viper.GetString("metamorph.db.mode") == "postgres" {
isCentralisedMetamorph = true
}

txHandler, err := transactionHandler.NewMetamorph(addresses, bTx, grpcMessageSize, logger, isCentralisedMetamorph)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/txstatus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
if grpcMessageSize == 0 {
panic("Missing grpcMessageSize")
}
txHandler, err := transactionHandler.NewMetamorph(addresses, bTx, grpcMessageSize, logger)
txHandler, err := transactionHandler.NewMetamorph(addresses, bTx, grpcMessageSize, logger, false)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func main() {
blockTxClient := blocktx.NewClient(blocktx_api.NewBlockTxAPIClient(conn))
grpcMessageSize := viper.GetInt("grpcMessageSize")
// add a single metamorph, with the BlockTx client we want to use
txHandler, err := transactionHandler.NewMetamorph("localhost:8011", blockTxClient, grpcMessageSize, logger)
txHandler, err := transactionHandler.NewMetamorph("localhost:8011", blockTxClient, grpcMessageSize, logger, false)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit e25644c

Please sign in to comment.