Skip to content

Commit

Permalink
feat(ARCO-212): Move tx finder out of internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
boecklim committed Nov 14, 2024
1 parent c9a34bf commit c3bdb0d
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 97 deletions.
31 changes: 29 additions & 2 deletions cmd/arc/services/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import (
"github.com/bitcoin-sv/arc/internal/message_queue/nats/client/nats_jetstream"
"github.com/bitcoin-sv/arc/internal/message_queue/nats/nats_connection"
"github.com/bitcoin-sv/arc/internal/metamorph/metamorph_api"
"github.com/bitcoin-sv/arc/internal/node_client"
"github.com/bitcoin-sv/arc/internal/tracing"
"github.com/bitcoin-sv/arc/internal/woc_client"
"github.com/bitcoin-sv/arc/pkg/api"
"github.com/bitcoin-sv/arc/pkg/api/handler"
txfinder "github.com/bitcoin-sv/arc/pkg/api/tx_finder"
"github.com/bitcoin-sv/arc/pkg/blocktx"
"github.com/bitcoin-sv/arc/pkg/metamorph"
)
Expand All @@ -50,11 +53,12 @@ func StartAPIServer(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), e
metamorph.WithMqClient(mqClient),
metamorph.WithLogger(logger),
}

// TODO: WithSecurityConfig(appConfig.Security)
apiOpts := []handler.Option{
handler.WithCallbackURLRestrictions(arcConfig.Metamorph.RejectCallbackContaining),
}
var finderOpts []func(f *txfinder.CachedFinder)
var nodeClientOpts []func(client *node_client.NodeClient)

shutdownFns := make([]func(), 0)

Expand All @@ -68,6 +72,8 @@ func StartAPIServer(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), e

mtmOpts = append(mtmOpts, metamorph.WithTracer(arcConfig.Tracing.KeyValueAttributes...))
apiOpts = append(apiOpts, handler.WithTracer(arcConfig.Tracing.KeyValueAttributes...))
finderOpts = append(finderOpts, txfinder.WithTracerCachedFinder(arcConfig.Tracing.KeyValueAttributes...))
nodeClientOpts = append(nodeClientOpts, node_client.WithTracer(arcConfig.Tracing.KeyValueAttributes...))
}

conn, err := metamorph.DialGRPC(arcConfig.Metamorph.DialAddr, arcConfig.PrometheusEndpoint, arcConfig.GrpcMessageSize, arcConfig.Tracing)
Expand All @@ -92,7 +98,28 @@ func StartAPIServer(logger *slog.Logger, arcConfig *config.ArcConfig) (func(), e
policy = arcConfig.API.DefaultPolicy
}

apiHandler, err := handler.NewDefault(logger, metamorphClient, blockTxClient, policy, arcConfig.PeerRPC, arcConfig.API, apiOpts...)
wocClient := woc_client.New(arcConfig.API.WocMainnet, woc_client.WithAuth(arcConfig.API.WocAPIKey))

pc := arcConfig.PeerRPC
rpcURL, err := url.Parse(fmt.Sprintf("rpc://%s:%s@%s:%d", pc.User, pc.Password, pc.Host, pc.Port))
if err != nil {
return nil, fmt.Errorf("failed to parse node rpc url: %w", err)
}

// get the transaction from the bitcoin node rpc
bitcoinClient, err := bitcoin.NewFromURL(rpcURL, false)
if err != nil {
return nil, fmt.Errorf("failed to create node client: %w", err)
}

nodeClient, err := node_client.New(bitcoinClient, nodeClientOpts...)
if err != nil {
return nil, fmt.Errorf("failed to create node client: %v", err)
}

finder := txfinder.NewCached(metamorphClient, nodeClient, wocClient, logger, finderOpts...)

apiHandler, err := handler.NewDefault(logger, metamorphClient, blockTxClient, policy, finder, apiOpts...)
if err != nil {
return nil, 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 @@ -95,7 +95,7 @@ func main() {

// initialise the arc default api handler, with our txHandler and any handler options
var handler api.ServerInterface
if handler, err = apiHandler.NewDefault(logger, metamorphClient, blockTxClient, arcConfig.API.DefaultPolicy, arcConfig.PeerRPC, arcConfig.API); err != nil {
if handler, err = apiHandler.NewDefault(logger, metamorphClient, blockTxClient, arcConfig.API.DefaultPolicy, nil); err != nil {
panic(err)
}

Expand Down
5 changes: 3 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"log/slog"
"os"

"github.com/labstack/echo/v4"

"github.com/bitcoin-sv/arc/config"
"github.com/bitcoin-sv/arc/pkg/api"
apiHandler "github.com/bitcoin-sv/arc/pkg/api/handler"
merklerootsverifier "github.com/bitcoin-sv/arc/pkg/api/merkle_roots_verifier"
"github.com/bitcoin-sv/arc/pkg/api/transaction_handler"
"github.com/labstack/echo/v4"
)

func main() {
Expand All @@ -36,7 +37,7 @@ func main() {

// initialise the arc default api handler, with our txHandler and any handler options
var handler api.ServerInterface
if handler, err = apiHandler.NewDefault(logger, txHandler, merkleRootsVerifier, arcConfig.API.DefaultPolicy, arcConfig.PeerRPC, arcConfig.API); err != nil {
if handler, err = apiHandler.NewDefault(logger, txHandler, merkleRootsVerifier, arcConfig.API.DefaultPolicy, nil); err != nil {
panic(err)
}

Expand Down
20 changes: 2 additions & 18 deletions pkg/api/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ import (
"github.com/ordishs/go-bitcoin"
"go.opentelemetry.io/otel/attribute"

"github.com/bitcoin-sv/arc/config"
"github.com/bitcoin-sv/arc/internal/beef"
"github.com/bitcoin-sv/arc/internal/metamorph/metamorph_api"
"github.com/bitcoin-sv/arc/internal/tracing"
"github.com/bitcoin-sv/arc/internal/validator"
beefValidator "github.com/bitcoin-sv/arc/internal/validator/beef"
defaultValidator "github.com/bitcoin-sv/arc/internal/validator/default"
"github.com/bitcoin-sv/arc/internal/version"
"github.com/bitcoin-sv/arc/internal/woc_client"
"github.com/bitcoin-sv/arc/pkg/api"
"github.com/bitcoin-sv/arc/pkg/api/handler/internal/merkle_verifier"
txfinder "github.com/bitcoin-sv/arc/pkg/api/handler/internal/tx_finder"
"github.com/bitcoin-sv/arc/pkg/blocktx"
"github.com/bitcoin-sv/arc/pkg/metamorph"
)
Expand Down Expand Up @@ -90,17 +87,9 @@ func NewDefault(
transactionHandler metamorph.TransactionHandler,
merkleRootsVerifier blocktx.MerkleRootsVerifier,
policy *bitcoin.Settings,
peerRPCConfig *config.PeerRPCConfig,
apiConfig *config.APIConfig,
cachedFinder validator.TxFinderI,
opts ...Option,
) (*ArcDefaultHandler, error) {
var wocClient *woc_client.WocClient
if apiConfig != nil {
wocClient = woc_client.New(apiConfig.WocMainnet, woc_client.WithAuth(apiConfig.WocAPIKey))
} else {
wocClient = woc_client.New(false)
}

mr := merkle_verifier.New(merkleRootsVerifier)

handler := &ArcDefaultHandler{
Expand All @@ -109,18 +98,13 @@ func NewDefault(
logger: logger,
now: time.Now,
mrVerifier: mr,
txFinder: cachedFinder,
}

// apply options
for _, opt := range opts {
opt(handler)
}
var finderOpts []func(f *txfinder.CachedFinder)
if handler.tracingEnabled {
finderOpts = append(finderOpts, txfinder.WithTracerCachedFinder(handler.tracingAttributes...))
}

handler.txFinder = txfinder.NewCached(transactionHandler, peerRPCConfig, wocClient, logger, finderOpts...)

return handler, nil
}
Expand Down
Loading

0 comments on commit c3bdb0d

Please sign in to comment.