Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Get unmined ancestors #648

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions cmd/arc/services/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ 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"
tx_finder "github.com/bitcoin-sv/arc/internal/tx_finder"
"github.com/bitcoin-sv/arc/internal/woc_client"
"github.com/bitcoin-sv/arc/pkg/api"
"github.com/bitcoin-sv/arc/pkg/api/handler"
"github.com/bitcoin-sv/arc/pkg/blocktx"
Expand All @@ -50,11 +53,13 @@ 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 cachedFinderOpts []func(f *tx_finder.CachedFinder)
var finderOpts []func(f *tx_finder.Finder)
var nodeClientOpts []func(client *node_client.NodeClient)

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

Expand All @@ -68,6 +73,9 @@ 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...))
cachedFinderOpts = append(cachedFinderOpts, tx_finder.WithTracerCachedFinder(arcConfig.Tracing.KeyValueAttributes...))
finderOpts = append(finderOpts, tx_finder.WithTracerFinder(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 +100,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)
}

bitcoinClient, err := bitcoin.NewFromURL(rpcURL, false)
if err != nil {
return nil, fmt.Errorf("failed to create bitcoin 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 := tx_finder.New(metamorphClient, nodeClient, wocClient, logger, finderOpts...)
cachedFinder := tx_finder.NewCached(finder, cachedFinderOpts...)

apiHandler, err := handler.NewDefault(logger, metamorphClient, blockTxClient, policy, cachedFinder, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ package txfinder

import (
"context"
"log/slog"
"runtime"
"time"

sdkTx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/patrickmn/go-cache"
"go.opentelemetry.io/otel/attribute"

"github.com/bitcoin-sv/arc/config"
"github.com/bitcoin-sv/arc/internal/tracing"
"github.com/bitcoin-sv/arc/internal/validator"
"github.com/bitcoin-sv/arc/internal/woc_client"
"github.com/bitcoin-sv/arc/pkg/metamorph"
)

const (
Expand Down Expand Up @@ -42,36 +39,44 @@ func WithTracerCachedFinder(attr ...attribute.KeyValue) func(s *CachedFinder) {
}
}

func NewCached(th metamorph.TransactionHandler, pc *config.PeerRPCConfig, w *woc_client.WocClient, l *slog.Logger, opts ...func(f *CachedFinder)) CachedFinder {
func WithCacheStore(store *cache.Cache) func(s *CachedFinder) {
return func(p *CachedFinder) {
p.cacheStore = store
}
}

func NewCached(finder *Finder, opts ...func(f *CachedFinder)) CachedFinder {
c := CachedFinder{
cacheStore: cache.New(cacheExpiration, cacheCleanup),
finder: finder,
}

for _, opt := range opts {
opt(&c)
}
var finderOpts []func(f *Finder)
if c.tracingEnabled {
finderOpts = append(finderOpts, WithTracerFinder(c.tracingAttributes...))
}

c.finder = New(th, pc, w, l, finderOpts...)

return c
}

func (f CachedFinder) GetRawTxs(ctx context.Context, source validator.FindSourceFlag, ids []string) ([]validator.RawTx, error) {
func (f CachedFinder) GetMempoolAncestors(ctx context.Context, ids []string) ([]string, error) {
return f.finder.GetMempoolAncestors(ctx, ids)
}

func (f CachedFinder) GetRawTxs(ctx context.Context, source validator.FindSourceFlag, ids []string) ([]*sdkTx.Transaction, error) {
ctx, span := tracing.StartTracing(ctx, "CachedFinder_GetRawTxs", f.tracingEnabled, f.tracingAttributes...)
defer tracing.EndTracing(span)

cachedTxs := make([]validator.RawTx, 0, len(ids))
cachedTxs := make([]*sdkTx.Transaction, 0, len(ids))
var toFindIDs []string

// check cache
for _, id := range ids {
value, found := f.cacheStore.Get(id)
if found {
cachedTxs = append(cachedTxs, value.(validator.RawTx))
cahchedTx, ok := value.(sdkTx.Transaction)
if ok {
cachedTxs = append(cachedTxs, &cahchedTx)
}
} else {
toFindIDs = append(toFindIDs, id)
}
Expand All @@ -89,7 +94,7 @@ func (f CachedFinder) GetRawTxs(ctx context.Context, source validator.FindSource

// update cache
for _, tx := range foundTxs {
f.cacheStore.Set(tx.TxID, tx, cacheExpiration)
f.cacheStore.Set(tx.TxID(), *tx, cacheExpiration)
}

return append(cachedTxs, foundTxs...), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,44 @@ package txfinder

import (
"context"
"log/slog"
"os"
"testing"
"time"

sdkTx "github.com/bitcoin-sv/go-sdk/transaction"
"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/require"

"github.com/bitcoin-sv/arc/internal/testdata"
"github.com/bitcoin-sv/arc/internal/validator"
"github.com/bitcoin-sv/arc/pkg/metamorph"
"github.com/bitcoin-sv/arc/pkg/metamorph/mocks"
)

// Mocked data for RawTx
var rawTx1 = validator.RawTx{TxID: "tx1"}
var rawTx2 = validator.RawTx{TxID: "tx2"}

func TestCachedFinder_GetRawTxs_AllFromCache(t *testing.T) {
tt := []struct {
name string
cachedTx []validator.RawTx
cachedTx []sdkTx.Transaction
fetchedTx []*metamorph.Transaction
}{
{
name: "all from cache",
cachedTx: []validator.RawTx{rawTx1, rawTx2},
cachedTx: []sdkTx.Transaction{*testdata.TX1Raw, *testdata.TX6Raw},
},
{
name: "all from finder",
fetchedTx: []*metamorph.Transaction{{TxID: rawTx1.TxID}, {TxID: rawTx2.TxID}},
name: "all from finder",
fetchedTx: []*metamorph.Transaction{
{TxID: testdata.TX1Raw.TxID(), Bytes: testdata.TX1Raw.Bytes()},
{TxID: testdata.TX6Raw.TxID(), Bytes: testdata.TX6Raw.Bytes()},
},
},
{
name: "cached and fetched mixed",
cachedTx: []validator.RawTx{rawTx1},
fetchedTx: []*metamorph.Transaction{{TxID: rawTx2.TxID}},
name: "cached and fetched mixed",
cachedTx: []sdkTx.Transaction{*testdata.TX1Raw},
fetchedTx: []*metamorph.Transaction{
{TxID: testdata.TX6Raw.TxID(), Bytes: testdata.TX6Raw.Bytes()},
},
},
}

Expand All @@ -49,19 +54,15 @@ func TestCachedFinder_GetRawTxs_AllFromCache(t *testing.T) {

c := cache.New(10*time.Second, 10*time.Second)
for _, r := range tc.cachedTx {
c.Set(r.TxID, r, cache.DefaultExpiration)
}

sut := CachedFinder{
finder: &Finder{
transactionHandler: thMq,
},
cacheStore: c,
c.Set(r.TxID(), r, cache.DefaultExpiration)
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
finder := New(thMq, nil, nil, logger)
sut := NewCached(finder, WithCacheStore(c))

// when
// try to find in cache or with TransactionHandler only
res, err := sut.GetRawTxs(context.Background(), validator.SourceTransactionHandler, []string{rawTx1.TxID, rawTx2.TxID})
res, err := sut.GetRawTxs(context.Background(), validator.SourceTransactionHandler, []string{testdata.TX1Raw.TxID(), testdata.TX6Raw.TxID()})

// then
require.NoError(t, err)
Expand Down
Loading
Loading