Skip to content

Commit b657020

Browse files
committed
feat(SPV-1592): wrap engine.V2 into engine V1 as temporary adapting.
1 parent c6bbc35 commit b657020

File tree

18 files changed

+176
-271
lines changed

18 files changed

+176
-271
lines changed

actions/transactions/broadcast_callback.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
// broadcastCallback will handle a broadcastCallback call from the broadcast api
1313
func broadcastCallback(c *gin.Context) {
1414
logger := reqctx.Logger(c)
15-
config := reqctx.AppConfig(c)
1615
var callbackResp chainmodels.TXInfo
1716

1817
err := c.Bind(&callbackResp)
@@ -21,11 +20,7 @@ func broadcastCallback(c *gin.Context) {
2120
return
2221
}
2322

24-
if config.ExperimentalFeatures.V2 {
25-
err = reqctx.Engine(c).TxSyncService().Handle(c, callbackResp)
26-
} else {
27-
err = reqctx.Engine(c).HandleTxCallback(c, &callbackResp)
28-
}
23+
err = reqctx.Engine(c).HandleTxCallback(c, &callbackResp)
2924

3025
if err != nil {
3126
logger.Err(err).Any("TxInfo", callbackResp).Msgf("failed to update transaction in ARC broadcast callback handler")

actions/v2/callback/callbacks.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package callback
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/bitcoin-sv/spv-wallet/config"
8+
"github.com/bitcoin-sv/spv-wallet/engine"
9+
"github.com/bitcoin-sv/spv-wallet/engine/chain/models"
10+
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
11+
"github.com/bitcoin-sv/spv-wallet/engine/v2/utils/must"
12+
"github.com/bitcoin-sv/spv-wallet/server/middleware"
13+
"github.com/bitcoin-sv/spv-wallet/server/reqctx"
14+
"github.com/gin-gonic/gin"
15+
)
16+
17+
type txSyncService interface {
18+
Handle(ctx context.Context, txInfo chainmodels.TXInfo) error
19+
}
20+
21+
// RegisterRoutes registers endpoints for callbacks.
22+
func RegisterRoutes(ginEngine *gin.Engine, cfg *config.AppConfig, engine engine.V2Interface) {
23+
if cfg.ARCCallbackEnabled() {
24+
callbackURL, err := cfg.ARC.Callback.ShouldGetURL()
25+
must.HaveNoErrorf(err, "couldn't get callback URL from configuration")
26+
27+
broadcastCallback := ginEngine.Group("", middleware.CallbackTokenMiddleware())
28+
broadcastCallback.POST(callbackURL.Path, broadcastCallbackHandler(engine.TxSyncService()))
29+
}
30+
}
31+
32+
func broadcastCallbackHandler(service txSyncService) gin.HandlerFunc {
33+
return func(c *gin.Context) {
34+
logger := reqctx.Logger(c)
35+
var callbackResp chainmodels.TXInfo
36+
37+
err := c.Bind(&callbackResp)
38+
if err != nil {
39+
spverrors.ErrorResponse(c, spverrors.ErrCannotBindRequest, logger)
40+
return
41+
}
42+
43+
err = service.Handle(c, callbackResp)
44+
45+
if err != nil {
46+
logger.Err(err).Ctx(c).Any("TxInfo", callbackResp).Msgf("failed to update transaction in ARC broadcast callback handler")
47+
}
48+
49+
c.Status(http.StatusOK)
50+
}
51+
}

actions/v2/register.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package v2
22

33
import (
4+
"github.com/bitcoin-sv/spv-wallet/actions/paymailserver"
5+
"github.com/bitcoin-sv/spv-wallet/actions/v2/callback"
46
"github.com/bitcoin-sv/spv-wallet/actions/v2/swagger"
5-
"github.com/bitcoin-sv/spv-wallet/server/handlers"
7+
"github.com/bitcoin-sv/spv-wallet/config"
8+
"github.com/bitcoin-sv/spv-wallet/engine"
9+
"github.com/gin-gonic/gin"
610
)
711

812
// RegisterNonOpenAPIRoutes collects all the action's routes that aren't part of the Open API documentation and registers them using the handlersManager.
9-
func RegisterNonOpenAPIRoutes(handlersManager *handlers.Manager) {
10-
swagger.RegisterRoutes(handlersManager)
13+
func RegisterNonOpenAPIRoutes(ginEngine *gin.Engine, cfg *config.AppConfig, engine engine.V2Interface) {
14+
paymailserver.Register(engine.PaymailServerConfiguration(), ginEngine)
15+
swagger.RegisterRoutes(ginEngine, cfg)
16+
callback.RegisterRoutes(ginEngine, cfg, engine)
1117
}

actions/v2/swagger/swagger-ui.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66
"strings"
77

88
"github.com/bitcoin-sv/spv-wallet/api"
9-
routes "github.com/bitcoin-sv/spv-wallet/server/handlers"
9+
"github.com/bitcoin-sv/spv-wallet/config"
1010
"github.com/gin-gonic/gin"
1111
swaggerfiles "github.com/swaggo/files"
1212
ginSwagger "github.com/swaggo/gin-swagger"
1313
)
1414

1515
// RegisterRoutes creates the specific package routes
16-
func RegisterRoutes(handlersManager *routes.Manager) {
17-
root := handlersManager.Get(routes.GroupRoot)
16+
func RegisterRoutes(engine *gin.Engine, cfg *config.AppConfig) {
17+
root := engine.Group("")
1818

1919
root.GET("v2/swagger", func(c *gin.Context) {
2020
c.Redirect(http.StatusMovedPermanently, "v2/swagger/index.html")
2121
})
2222

23-
api.Yaml = strings.Replace(api.Yaml, "version: main", fmt.Sprintf("version: '%s'", handlersManager.APIVersion()), 1)
24-
api.Yaml = strings.Replace(api.Yaml, "https://github.com/bitcoin-sv/spv-wallet/blob/main", fmt.Sprintf("https://github.com/bitcoin-sv/spv-wallet/blob/%s", handlersManager.APIVersion()), 1)
23+
api.Yaml = strings.Replace(api.Yaml, "version: main", fmt.Sprintf("version: '%s'", cfg.Version), 1)
24+
api.Yaml = strings.Replace(api.Yaml, "https://github.com/bitcoin-sv/spv-wallet/blob/main", fmt.Sprintf("https://github.com/bitcoin-sv/spv-wallet/blob/%s", cfg.Version), 1)
2525

2626
root.GET("/api/gen.api.yaml", func(c *gin.Context) {
2727
c.Header("Content-Type", "application/yaml")

engine/client.go

Lines changed: 41 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,7 @@ import (
1717
paymailclient "github.com/bitcoin-sv/spv-wallet/engine/paymail"
1818
"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
1919
"github.com/bitcoin-sv/spv-wallet/engine/taskmanager"
20-
"github.com/bitcoin-sv/spv-wallet/engine/v2/addresses"
21-
"github.com/bitcoin-sv/spv-wallet/engine/v2/data"
22-
"github.com/bitcoin-sv/spv-wallet/engine/v2/database/repository"
23-
"github.com/bitcoin-sv/spv-wallet/engine/v2/operations"
24-
"github.com/bitcoin-sv/spv-wallet/engine/v2/paymails"
25-
"github.com/bitcoin-sv/spv-wallet/engine/v2/transaction/outlines"
26-
"github.com/bitcoin-sv/spv-wallet/engine/v2/transaction/record"
27-
"github.com/bitcoin-sv/spv-wallet/engine/v2/transaction/txsync"
28-
"github.com/bitcoin-sv/spv-wallet/engine/v2/users"
20+
"github.com/bitcoin-sv/spv-wallet/engine/v2/engine"
2921
"github.com/bitcoin-sv/spv-wallet/models/bsv"
3022
"github.com/go-resty/resty/v2"
3123
"github.com/mrz1836/go-cachestore"
@@ -37,39 +29,32 @@ type (
3729
// Client is the SPV Wallet Engine client & options
3830
Client struct {
3931
options *clientOptions
32+
// TEMPORARY: to limit the changes in the codebase
33+
V2Interface
4034
}
4135

4236
// clientOptions holds all the configuration for the client
4337
clientOptions struct {
44-
cacheStore *cacheStoreOptions // Configuration options for Cachestore (ristretto, redis, etc.)
45-
cluster *clusterOptions // Configuration options for the cluster coordinator
46-
dataStore *dataStoreOptions // Configuration options for the DataStore (PostgreSQL, etc.)
47-
debug bool // If the client is in debug mode
48-
encryptionKey string // Encryption key for encrypting sensitive information (IE: paymail xPub) (hex encoded key)
49-
httpClient *resty.Client // HTTP client to use for http calls
50-
iuc bool // (Input UTXO Check) True will check input utxos when saving transactions
51-
logger *zerolog.Logger // Internal logging
52-
metrics *metrics.Metrics // Metrics with a collector interface
53-
notifications *notificationsOptions // Configuration options for Notifications
54-
paymail *paymailOptions // Paymail options & client
55-
transactionOutlinesService outlines.Service // Service for transaction outlines
56-
transactionRecordService *record.Service // Service for recording transactions
57-
taskManager *taskManagerOptions // Configuration options for the TaskManager (TaskQ, etc.)
58-
userAgent string // User agent for all outgoing requests
59-
chainService chain.Service // Chain service
60-
arcConfig chainmodels.ARCConfig // Configuration for ARC
61-
bhsConfig chainmodels.BHSConfig // Configuration for BHS
62-
feeUnit *bsv.FeeUnit // Fee unit for transactions
63-
64-
// v2
65-
repositories *repository.All // Repositories for all db models
66-
users *users.Service // User domain service
67-
paymails *paymails.Service // Paymail domain service
68-
addresses *addresses.Service
69-
operations *operations.Service
70-
txSync *txsync.Service
71-
data *data.Service
72-
config *config.AppConfig
38+
cacheStore *cacheStoreOptions // Configuration options for Cachestore (ristretto, redis, etc.)
39+
cluster *clusterOptions // Configuration options for the cluster coordinator
40+
dataStore *dataStoreOptions // Configuration options for the DataStore (PostgreSQL, etc.)
41+
debug bool // If the client is in debug mode
42+
encryptionKey string // Encryption key for encrypting sensitive information (IE: paymail xPub) (hex encoded key)
43+
httpClient *resty.Client // HTTP client to use for http calls
44+
iuc bool // (Input UTXO Check) True will check input utxos when saving transactions
45+
logger *zerolog.Logger // Internal logging
46+
metrics *metrics.Metrics // Metrics with a collector interface
47+
notifications *notificationsOptions // Configuration options for Notifications
48+
paymail *paymailOptions // Paymail options & client
49+
taskManager *taskManagerOptions // Configuration options for the TaskManager (TaskQ, etc.)
50+
userAgent string // User agent for all outgoing requests
51+
feeUnit *bsv.FeeUnit // Fee unit for transactions
52+
53+
chainService chain.Service // Chain service
54+
arcConfig chainmodels.ARCConfig // Configuration for ARC
55+
bhsConfig chainmodels.BHSConfig // Configuration for BHS
56+
57+
config *config.AppConfig
7358
}
7459

7560
// cacheStoreOptions holds the cache configuration and client
@@ -138,6 +123,16 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
138123
client.options.logger = logging.GetDefaultLogger()
139124
}
140125

126+
if client.options.config != nil && client.options.config.ExperimentalFeatures.V2 {
127+
client.V2Interface = engine.NewEngine(
128+
client.options.config,
129+
*client.options.logger,
130+
engine.WithResty(client.options.httpClient),
131+
engine.WithPaymailClient(client.options.paymail.client),
132+
)
133+
return client, nil
134+
}
135+
141136
// Load the Cachestore client
142137
var err error
143138
if err = client.loadCache(ctx); err != nil {
@@ -158,14 +153,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
158153
return nil, err
159154
}
160155

161-
client.loadRepositories()
162-
163-
client.loadUsersService()
164-
client.loadPaymailsService()
165-
client.loadAddressesService()
166-
client.loadDataService()
167-
client.loadOperationsService()
168-
169156
// Load the Paymail client and service (if does not exist)
170157
if err = client.loadPaymailComponents(); err != nil {
171158
return nil, err
@@ -182,11 +169,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
182169
}
183170

184171
client.loadChainService()
185-
client.loadTxSyncService()
186-
187-
if err = client.loadTransactionRecordService(); err != nil {
188-
return nil, err
189-
}
190172

191173
// Register all cron jobs
192174
if err = client.registerCronJobs(); err != nil {
@@ -203,10 +185,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
203185
}
204186
}
205187

206-
if err = client.loadTransactionOutlinesService(); err != nil {
207-
return nil, err
208-
}
209-
210188
// Return the client
211189
return client, nil
212190
}
@@ -229,6 +207,14 @@ func (c *Client) Cluster() cluster.ClientInterface {
229207

230208
// Close will safely close any open connections (cache, datastore, etc.)
231209
func (c *Client) Close(ctx context.Context) error {
210+
if c.V2Interface != nil {
211+
err := c.V2Interface.Close(ctx)
212+
if err != nil {
213+
return spverrors.Wrapf(err, "failed to close envine V2")
214+
}
215+
return nil
216+
}
217+
232218
// Close WebhookManager
233219
if c.options.notifications != nil && c.options.notifications.webhookManager != nil {
234220
c.options.notifications.webhookManager.Stop()
@@ -351,38 +337,3 @@ func (c *Client) LogBHSReadiness(ctx context.Context) {
351337
func (c *Client) FeeUnit() bsv.FeeUnit {
352338
return *c.options.feeUnit
353339
}
354-
355-
// Repositories will return all the repositories
356-
func (c *Client) Repositories() *repository.All {
357-
return c.options.repositories
358-
}
359-
360-
// UsersService will return the user domain service
361-
func (c *Client) UsersService() *users.Service {
362-
return c.options.users
363-
}
364-
365-
// PaymailsService will return the paymail domain service
366-
func (c *Client) PaymailsService() *paymails.Service {
367-
return c.options.paymails
368-
}
369-
370-
// AddressesService will return the address domain service
371-
func (c *Client) AddressesService() *addresses.Service {
372-
return c.options.addresses
373-
}
374-
375-
// DataService will return the data domain service
376-
func (c *Client) DataService() *data.Service {
377-
return c.options.data
378-
}
379-
380-
// OperationsService will return the operations domain service
381-
func (c *Client) OperationsService() *operations.Service {
382-
return c.options.operations
383-
}
384-
385-
// TxSyncService will return the transaction sync service
386-
func (c *Client) TxSyncService() *txsync.Service {
387-
return c.options.txSync
388-
}

0 commit comments

Comments
 (0)