Skip to content

Commit

Permalink
Merge pull request #232 from bitcoin-sv/rm-blocktx-from-api
Browse files Browse the repository at this point in the history
Rm blocktx from api
  • Loading branch information
boecklim authored Jan 4, 2024
2 parents 6fbd2d0 + 9dfd690 commit d5f2980
Show file tree
Hide file tree
Showing 27 changed files with 202 additions and 837 deletions.
81 changes: 5 additions & 76 deletions api/transactionHandler/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,31 @@ package transactionHandler

import (
"context"
"errors"
"fmt"
"sync"
"time"

arc "github.com/bitcoin-sv/arc/api"
"github.com/bitcoin-sv/arc/blocktx"
"github.com/bitcoin-sv/arc/blocktx/blocktx_api"
"github.com/bitcoin-sv/arc/metamorph/metamorph_api"
"github.com/bitcoin-sv/arc/tracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/ordishs/go-utils"
"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
isCentralisedMetamorph bool
Client metamorph_api.MetaMorphAPIClient
}

// NewMetamorph creates a connection to a list of metamorph servers via gRPC.
func NewMetamorph(address string, blockTxClient blocktx.ClientI, grpcMessageSize int, isCentralisedMetamorph bool) (*Metamorph, error) {
func NewMetamorph(address string, grpcMessageSize int) (*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,
isCentralisedMetamorph: isCentralisedMetamorph,
Client: metamorph_api.NewMetaMorphAPIClient(conn),
}, nil
}

Expand All @@ -61,13 +49,8 @@ func DialGRPC(address string, grpcMessageSize int) (*grpc.ClientConn, error) {

// GetTransaction gets the transaction bytes from metamorph.
func (m *Metamorph) GetTransaction(ctx context.Context, txID string) ([]byte, error) {
client, err := m.getMetamorphClientForTx(ctx, txID)
if err != nil {
return nil, err
}

var tx *metamorph_api.Transaction
tx, err = client.GetTransaction(ctx, &metamorph_api.TransactionStatusRequest{
tx, err := m.Client.GetTransaction(ctx, &metamorph_api.TransactionStatusRequest{
Txid: txID,
})
if err != nil {
Expand All @@ -83,13 +66,8 @@ func (m *Metamorph) GetTransaction(ctx context.Context, txID string) ([]byte, er

// GetTransactionStatus gets the status of a transaction.
func (m *Metamorph) GetTransactionStatus(ctx context.Context, txID string) (status *TransactionStatus, err error) {
var client metamorph_api.MetaMorphAPIClient
if client, err = m.getMetamorphClientForTx(ctx, txID); err != nil {
return nil, err
}

var tx *metamorph_api.TransactionStatus
tx, err = client.GetTransactionStatus(ctx, &metamorph_api.TransactionStatusRequest{
tx, err = m.Client.GetTransactionStatus(ctx, &metamorph_api.TransactionStatusRequest{
Txid: txID,
})
if err != nil {
Expand Down Expand Up @@ -171,52 +149,3 @@ func (m *Metamorph) SubmitTransactions(ctx context.Context, txs [][]byte, txOpti

return ret, nil
}

func (m *Metamorph) getMetamorphClientForTx(ctx context.Context, txID string) (metamorph_api.MetaMorphAPIClient, error) {
if m.isCentralisedMetamorph {
return m.Client, nil
}

hash, err := utils.DecodeAndReverseHexString(txID)
if err != nil {
return nil, err
}

var target string
if target, err = m.blockTxClient.LocateTransaction(ctx, &blocktx_api.Transaction{
Hash: hash,
}); err != nil {
if errors.Is(err, blocktx.ErrTransactionNotFound) {
return nil, ErrTransactionNotFound
}
return nil, err
}

if target == "" {
// TODO what do we do in this case? Reach out to all metamorph servers or reach out to a node?
return nil, ErrTransactionNotFound
}

m.mu.RLock()
client, found := m.ClientCache[target]
m.mu.RUnlock()

if !found {
var conn *grpc.ClientConn
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithChainUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor),
grpc.WithChainStreamInterceptor(grpc_prometheus.StreamClientInterceptor),
}
if conn, err = grpc.Dial(target, tracing.AddGRPCDialOptions(opts)...); err != nil {
return nil, err
}
client = metamorph_api.NewMetaMorphAPIClient(conn)

m.mu.Lock()
m.ClientCache[target] = client
m.mu.Unlock()
}

return client, nil
}
315 changes: 122 additions & 193 deletions blocktx/blocktx_api/blocktx_api.pb.go

Large diffs are not rendered by default.

9 changes: 0 additions & 9 deletions blocktx/blocktx_api/blocktx_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ service BlockTxAPI {
// RegisterTransaction registers a transaction with the API.
rpc RegisterTransaction (TransactionAndSource) returns (RegisterTransactionResponse) {}

// LocateTransaction returns the source of a transaction.
rpc LocateTransaction(Transaction) returns (Source) {}

// GetTransactionMerklePath returns the merkle path of a transaction.
rpc GetTransactionMerklePath(Transaction) returns (MerklePath) {}

Expand Down Expand Up @@ -98,12 +95,6 @@ message Hash {
bytes hash = 1;
}

// swagger:model Source
message Source {
string source = 1;
}

// swagger:model Source
message MerklePath {
string merklePath = 1;
}
Expand Down
39 changes: 0 additions & 39 deletions blocktx/blocktx_api/blocktx_api_grpc.pb.go

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

56 changes: 0 additions & 56 deletions blocktx/blocktx_api/mock/blocktx_api_client_mock.go

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

10 changes: 0 additions & 10 deletions blocktx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
// ClientI is the interface for the block-tx transactionHandler.
type ClientI interface {
Start(minedBlockChan chan *blocktx_api.Block)
LocateTransaction(ctx context.Context, transaction *blocktx_api.Transaction) (string, error)
GetTransactionMerklePath(ctx context.Context, transaction *blocktx_api.Transaction) (string, error)
RegisterTransaction(ctx context.Context, transaction *blocktx_api.TransactionAndSource) (*blocktx_api.RegisterTransactionResponse, error)
GetTransactionBlocks(ctx context.Context, transaction *blocktx_api.Transactions) (*blocktx_api.TransactionBlocks, error)
Expand Down Expand Up @@ -124,15 +123,6 @@ func (btc *Client) Shutdown() {
<-btc.shutdownComplete
}

func (btc *Client) LocateTransaction(ctx context.Context, transaction *blocktx_api.Transaction) (string, error) {
location, err := btc.client.LocateTransaction(ctx, transaction)
if err != nil {
return "", ErrTransactionNotFound
}

return location.GetSource(), nil
}

func (btc *Client) GetTransactionMerklePath(ctx context.Context, hash *blocktx_api.Transaction) (string, error) {
merklePath, err := btc.client.GetTransactionMerklePath(ctx, hash)
if err != nil {
Expand Down
19 changes: 0 additions & 19 deletions blocktx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,6 @@ func (s *Server) Health(_ context.Context, _ *emptypb.Empty) (*blocktx_api.Healt
}, nil
}

func (s *Server) LocateTransaction(ctx context.Context, transaction *blocktx_api.Transaction) (*blocktx_api.Source, error) {
hash, err := chainhash.NewHash(transaction.GetHash())
if err != nil {
return nil, err
}

source, err := s.store.GetTransactionSource(ctx, hash)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
return nil, ErrTransactionNotFound
}
return nil, err
}

return &blocktx_api.Source{
Source: source,
}, nil
}

func (s *Server) GetTransactionMerklePath(ctx context.Context, transaction *blocktx_api.Transaction) (*blocktx_api.MerklePath, error) {
hash, err := chainhash.NewHash(transaction.GetHash())
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion blocktx/store/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Interface interface {
RegisterTransaction(ctx context.Context, transaction *blocktx_api.TransactionAndSource) (string, string, []byte, uint64, error)
TryToBecomePrimary(ctx context.Context, myHostName string) error
PrimaryBlocktx(ctx context.Context) (string, error)
GetTransactionSource(ctx context.Context, hash *chainhash.Hash) (string, error)
GetTransactionMerklePath(ctx context.Context, hash *chainhash.Hash) (string, error)
GetBlock(ctx context.Context, hash *chainhash.Hash) (*blocktx_api.Block, error)
GetBlockForHeight(ctx context.Context, height uint64) (*blocktx_api.Block, error)
Expand Down
Loading

0 comments on commit d5f2980

Please sign in to comment.