Skip to content

Commit

Permalink
refactor(SPV-1020): require cache to be not nil, small adjustments in…
Browse files Browse the repository at this point in the history
… variables names.
  • Loading branch information
dorzepowski committed Sep 13, 2024
1 parent eb0ab89 commit 7f241c0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
28 changes: 14 additions & 14 deletions engine/action_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ import (
)

// UpsertContact adds a new contact if not exists or updates the existing one.
func (c *Client) UpsertContact(ctx context.Context, ctcFName, ctcPaymail, requesterXPubID, requesterPaymail string, opts ...ModelOps) (*Contact, error) {
reqPm, err := c.getPaymail(ctx, requesterXPubID, requesterPaymail)
func (c *Client) UpsertContact(ctx context.Context, ctcFName, ctcPaymail, requesterXPubID, requesterPaymailAddress string, opts ...ModelOps) (*Contact, error) {
requesterPaymail, err := c.getPaymail(ctx, requesterXPubID, requesterPaymailAddress)
if err != nil {
return nil, err
}

paymailService := c.PaymailService()

contactPm, err := paymailService.GetSanitizedPaymail(ctcPaymail)
contactPaymail, err := paymailService.GetSanitizedPaymail(ctcPaymail)
if err != nil {
return nil, spverrors.Wrapf(err, "requested contact paymail is invalid")
}

contact, err := c.upsertContact(ctx, paymailService, requesterXPubID, ctcFName, contactPm, opts...)
contact, err := c.upsertContact(ctx, paymailService, requesterXPubID, ctcFName, contactPaymail, opts...)
if err != nil {
return nil, err
}

// request new contact
requesterContactRequest := paymail.PikeContactRequestPayload{
FullName: reqPm.PublicName,
Paymail: reqPm.String(),
FullName: requesterPaymail.PublicName,
Paymail: requesterPaymail.String(),
}
if _, err = paymailService.AddContactRequest(ctx, contactPm, &requesterContactRequest); err != nil {
if _, err = paymailService.AddContactRequest(ctx, contactPaymail, &requesterContactRequest); err != nil {
c.Logger().Warn().
Str("requesterPaymail", reqPm.String()).
Str("requesterPaymail", requesterPaymail.String()).
Str("requestedContact", ctcPaymail).
Msgf("adding contact request failed: %s", err.Error())

Expand All @@ -50,20 +50,20 @@ func (c *Client) UpsertContact(ctx context.Context, ctcFName, ctcPaymail, reques
func (c *Client) AddContactRequest(ctx context.Context, fullName, paymailAdress, requesterXPubID string, opts ...ModelOps) (*Contact, error) {
paymailService := c.PaymailService()

contactPm, err := paymailService.GetSanitizedPaymail(paymailAdress)
contactPaymail, err := paymailService.GetSanitizedPaymail(paymailAdress)
if err != nil {
c.Logger().Error().Msgf("requested contact paymail is invalid. Reason: %s", err.Error())
return nil, spverrors.ErrRequestedContactInvalid
}

contactPki, err := paymailService.GetPkiForPaymail(ctx, contactPm)
contactPki, err := paymailService.GetPkiForPaymail(ctx, contactPaymail)
if err != nil {
c.Logger().Error().Msgf("getting PKI for %s failed. Reason: %v", paymailAdress, err)
return nil, spverrors.ErrGettingPKIFailed
}

// check if exists already
contact, err := getContact(ctx, contactPm.Address, requesterXPubID, c.DefaultModelOptions()...)
contact, err := getContact(ctx, contactPaymail.Address, requesterXPubID, c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
Expand All @@ -74,7 +74,7 @@ func (c *Client) AddContactRequest(ctx context.Context, fullName, paymailAdress,
} else {
contact = newContact(
fullName,
contactPm.Address,
contactPaymail.Address,
contactPki.PubKey,
requesterXPubID,
ContactAwaitAccept,
Expand Down Expand Up @@ -368,8 +368,8 @@ func (c *Client) getPaymail(ctx context.Context, xpubID, paymailAddr string) (*P
return paymails[0], nil
}

func (c *Client) upsertContact(ctx context.Context, pmSrvnt paymailclient.ServiceClient, reqXPubID, ctcFName string, ctcPaymail *paymail.SanitisedPaymail, opts ...ModelOps) (*Contact, error) {
contactPki, err := pmSrvnt.GetPkiForPaymail(ctx, ctcPaymail)
func (c *Client) upsertContact(ctx context.Context, paymailService paymailclient.ServiceClient, reqXPubID, ctcFName string, ctcPaymail *paymail.SanitisedPaymail, opts ...ModelOps) (*Contact, error) {
contactPki, err := paymailService.GetPkiForPaymail(ctx, ctcPaymail)
if err != nil {
return nil, spverrors.ErrGettingPKIFailed
}
Expand Down
16 changes: 4 additions & 12 deletions engine/paymail/paymail_service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewServiceClient(cache cachestore.ClientInterface, paymailClient paymail.Cl
}

if cache == nil {
log.Info().Msg("Doesn't receive cachestore, won't use cache for capabilities")
panic(spverrors.Newf("cache is required to create a new paymail service"))
}

return &service{
Expand Down Expand Up @@ -60,7 +60,7 @@ func (s *service) GetCapabilities(ctx context.Context, domain string) (*paymail.
return nil, err
}
if capabilities != nil {
return capabilities, err
return capabilities, nil
}

response, err := s.loadCapabilities(domain)
Expand All @@ -74,10 +74,6 @@ func (s *service) GetCapabilities(ctx context.Context, domain string) (*paymail.
}

func (s *service) loadCapabilitiesFromCache(ctx context.Context, key string) (*paymail.CapabilitiesPayload, error) {
if s.cache == nil {
return nil, nil
}

capabilities := new(paymail.CapabilitiesPayload)
err := s.cache.GetModel(ctx, key, capabilities)
if errors.Is(err, cachestore.ErrKeyNotFound) {
Expand All @@ -95,23 +91,19 @@ func (s *service) loadCapabilitiesFromCache(ctx context.Context, key string) (*p
}

func (s *service) putCapabilitiesInCache(ctx context.Context, key string, capabilities paymail.CapabilitiesPayload) {
if s.cache == nil || s.cache.Engine().IsEmpty() {
return
}

err := s.cache.SetModel(ctx, key, capabilities, cacheTTLCapabilities)
if err != nil {
s.log.Warn().Err(err).Msgf("failed to store capabilities for key %s in cache", key)
}
}

func (s *service) loadCapabilities(domain string) (response *paymail.CapabilitiesResponse, err error) {
func (s *service) loadCapabilities(domain string) (*paymail.CapabilitiesResponse, error) {
// Get SRV record (domain can be different!)
srv, err := s.paymailClient.GetSRVRecord(
paymail.DefaultServiceName, paymail.DefaultProtocol, domain,
)
if err != nil {
return
return nil, err

Check failure on line 106 in engine/paymail/paymail_service_client.go

View workflow job for this annotation

GitHub Actions / error-lint

error returned from interface method should be wrapped: sig: func (github.com/bitcoin-sv/go-paymail.ClientInterface).GetSRVRecord(service string, protocol string, domainName string) (srv *net.SRV, err error) (wrapcheck)
}
return s.paymailClient.GetCapabilities(srv.Target, int(srv.Port)) //nolint:wrapcheck // we have handler for paymail errors
}
Expand Down
6 changes: 3 additions & 3 deletions engine/paymail/paymail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_GetP2P(t *testing.T) {
client := paymailmock.MockClient(testDomain)
client.WillRespondWithBasicCapabilities()

paymailClient := paymailclient.NewServiceClient(nil, client, xtester.Logger())
paymailClient := paymailclient.NewServiceClient(xtester.CacheStore(), client, xtester.Logger())

hasP2P, p2pDestinationURL, p2pSubmitTxURL, _ := paymailClient.GetP2P(context.Background(), testDomain)
assert.False(t, hasP2P)
Expand All @@ -45,7 +45,7 @@ func Test_GetP2P(t *testing.T) {
client := paymailmock.MockClient(testDomain)
client.WillRespondWithP2PCapabilities()

paymailClient := paymailclient.NewServiceClient(nil, client, xtester.Logger())
paymailClient := paymailclient.NewServiceClient(xtester.CacheStore(), client, xtester.Logger())

hasP2P, p2pDestinationURL, p2pSubmitTxURL, format := paymailClient.GetP2P(context.Background(), testDomain)
assert.True(t, hasP2P)
Expand All @@ -58,7 +58,7 @@ func Test_GetP2P(t *testing.T) {
client := paymailmock.MockClient(testDomain)
client.WillRespondWithP2PWithBEEFCapabilities()

paymailClient := paymailclient.NewServiceClient(nil, client, xtester.Logger())
paymailClient := paymailclient.NewServiceClient(xtester.CacheStore(), client, xtester.Logger())

hasP2P, p2pDestinationURL, p2pSubmitTxURL, format := paymailClient.GetP2P(context.Background(), testDomain)
assert.True(t, hasP2P)
Expand Down
10 changes: 10 additions & 0 deletions engine/tester/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import (
"context"
"time"

"github.com/bitcoin-sv/spv-wallet/engine/spverrors"
"github.com/gomodule/redigo/redis"
"github.com/mrz1836/go-cache"
"github.com/mrz1836/go-cachestore"
"github.com/rafaeljusto/redigomock"
)

func CacheStore() cachestore.ClientInterface {

Check failure on line 14 in engine/tester/cache.go

View workflow job for this annotation

GitHub Actions / error-lint

exported: exported function CacheStore should have comment or be unexported (revive)
cacheStore, err := cachestore.NewClient(context.Background(), cachestore.WithFreeCache())
if err != nil {
panic(spverrors.Wrapf(err, "cannot create cache store for tests"))
}
return cacheStore
}

// LoadMockRedis will load a mocked redis connection
func LoadMockRedis(
idleTimeout time.Duration,
Expand Down
4 changes: 2 additions & 2 deletions engine/tester/paymailmock/paymail_service_fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package paymailmock

import (
"github.com/bitcoin-sv/spv-wallet/engine/paymail"
xtester "github.com/bitcoin-sv/spv-wallet/engine/tester"
"github.com/bitcoin-sv/spv-wallet/engine/tester"
)

// PaymailClientServiceMock is a paymail.ServiceClient with mocked paymail.Client
Expand All @@ -14,7 +14,7 @@ type PaymailClientServiceMock struct {
// CreatePaymailClientService creates a new paymail.ServiceClient with mocked paymail.Client
func CreatePaymailClientService(domain string, otherDomains ...string) *PaymailClientServiceMock {
pmClient := MockClient(domain, otherDomains...)
client := paymail.NewServiceClient(nil, pmClient, xtester.Logger())
client := paymail.NewServiceClient(tester.CacheStore(), pmClient, tester.Logger())

return &PaymailClientServiceMock{
ServiceClient: client,
Expand Down

0 comments on commit 7f241c0

Please sign in to comment.