Skip to content

Commit

Permalink
refactor(ARCO-282): Separate callbacker #642 (#642)
Browse files Browse the repository at this point in the history
  • Loading branch information
shotasilagadzetaal authored Nov 12, 2024
1 parent 1e5980e commit 120be56
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cmd/arc/services/metamorph.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bitcoin-sv/arc/internal/cache"
"github.com/bitcoin-sv/arc/internal/tracing"
"github.com/bitcoin-sv/arc/pkg/callbacker"

"github.com/libsv/go-p2p"
"github.com/ordishs/go-bitcoin"
Expand Down Expand Up @@ -57,7 +58,7 @@ func StartMetamorph(logger *slog.Logger, arcConfig *config.ArcConfig, cacheStore

optsServer := make([]metamorph.ServerOption, 0)
processorOpts := make([]metamorph.Option, 0)
callbackerOpts := make([]metamorph.CallbackerOption, 0)
callbackerOpts := make([]callbacker.Option, 0)

if arcConfig.IsTracingEnabled() {
cleanup, err := tracing.Enable(logger, "metamorph", arcConfig.Tracing)
Expand All @@ -68,7 +69,7 @@ func StartMetamorph(logger *slog.Logger, arcConfig *config.ArcConfig, cacheStore
}

optsServer = append(optsServer, metamorph.WithTracer(arcConfig.Tracing.KeyValueAttributes...))
callbackerOpts = append(callbackerOpts, metamorph.WithTracerCallbacker(arcConfig.Tracing.KeyValueAttributes...))
callbackerOpts = append(callbackerOpts, callbacker.WithTracerCallbacker(arcConfig.Tracing.KeyValueAttributes...))
processorOpts = append(processorOpts, metamorph.WithTracerProcessor(arcConfig.Tracing.KeyValueAttributes...))
}

Expand Down Expand Up @@ -125,7 +126,7 @@ func StartMetamorph(logger *slog.Logger, arcConfig *config.ArcConfig, cacheStore
return nil, fmt.Errorf("failed to create callbacker client: %v", err)
}

callbacker := metamorph.NewGrpcCallbacker(callbackerConn, procLogger, callbackerOpts...)
callbacker := callbacker.NewGrpcCallbacker(callbackerConn, procLogger, callbackerOpts...)

processorOpts = append(processorOpts, metamorph.WithCacheExpiryTime(mtmConfig.ProcessorCacheExpiryTime),
metamorph.WithProcessExpiredTxsInterval(mtmConfig.UnseenTransactionRebroadcastingInterval),
Expand Down
145 changes: 145 additions & 0 deletions internal/callbacker/callbacker_api/callbacker_api_client_mock.go

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

2 changes: 2 additions & 0 deletions internal/callbacker/callbacker_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ package callbacker

// from callbacker.go
//go:generate moq -out ./callbacker_mock.go ./ SenderI

//go:generate moq -out ./callbacker_api/callbacker_api_client_mock.go ./callbacker_api/ CallbackerAPIClient
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package metamorph
package callbacker

import (
"context"
Expand All @@ -13,6 +13,8 @@ import (
"github.com/bitcoin-sv/arc/internal/tracing"
)

var minedDoubleSpendMsg = "previously double spend attempted"

type GrpcCallbacker struct {
cc callbacker_api.CallbackerAPIClient
l *slog.Logger
Expand All @@ -33,9 +35,9 @@ func WithTracerCallbacker(attr ...attribute.KeyValue) func(*GrpcCallbacker) {
}
}

type CallbackerOption func(s *GrpcCallbacker)
type Option func(s *GrpcCallbacker)

func NewGrpcCallbacker(api callbacker_api.CallbackerAPIClient, logger *slog.Logger, opts ...CallbackerOption) GrpcCallbacker {
func NewGrpcCallbacker(api callbacker_api.CallbackerAPIClient, logger *slog.Logger, opts ...Option) GrpcCallbacker {
c := GrpcCallbacker{
cc: api,
l: logger,
Expand Down
75 changes: 75 additions & 0 deletions pkg/callbacker/callbacker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package callbacker

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

"github.com/bitcoin-sv/arc/internal/callbacker/callbacker_api"
"github.com/bitcoin-sv/arc/internal/metamorph/metamorph_api"
"github.com/bitcoin-sv/arc/internal/metamorph/store"
"github.com/libsv/go-p2p/chaincfg/chainhash"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)

func TestSendCallback(t *testing.T) {
tt := []struct {
name string
expectedCalls int
err error
data *store.Data
}{
{
name: "empty callbacks",
expectedCalls: 0,
data: &store.Data{
Status: metamorph_api.Status_UNKNOWN,
Hash: &chainhash.Hash{},
Callbacks: []store.Callback{},
},
},
{
name: "empty url",
expectedCalls: 0,

data: &store.Data{
Status: metamorph_api.Status_UNKNOWN,
Hash: &chainhash.Hash{},
Callbacks: []store.Callback{
{
CallbackURL: "",
},
},
},
},
{
name: "expected call",
expectedCalls: 1,
data: &store.Data{
Status: metamorph_api.Status_UNKNOWN,
Hash: &chainhash.Hash{},
Callbacks: []store.Callback{
{
CallbackURL: "http://someurl.comg",
},
},
},
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
apiClient := &callbacker_api.CallbackerAPIClientMock{
SendCallbackFunc: func(_ context.Context, _ *callbacker_api.SendCallbackRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) {
return nil, nil
},
}
grpcCallbacker := NewGrpcCallbacker(apiClient, slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})))
grpcCallbacker.SendCallback(context.Background(), tc.data)
require.Equal(t, tc.expectedCalls, len(apiClient.SendCallbackCalls()))
})
}
}

0 comments on commit 120be56

Please sign in to comment.