Skip to content

Commit 18c819b

Browse files
committed
accounts: add ReqIDToPaymentHashStore and implement
In preparation for implementing trace logging that makes it easy to link state across requests and responses for more informative logs, we add a new ReqIDToPaymentHashStore interface along with an in-memory implementation. This can be expanded in future to contain other state but for now let's just go with the payment hash since that is useful to have and will provide the user useful information about the payment corresponding to a log.
1 parent e807639 commit 18c819b

File tree

3 files changed

+102
-11
lines changed

3 files changed

+102
-11
lines changed

accounts/checkers_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,16 @@ type mockService struct {
4242

4343
trackedInvoices map[lntypes.Hash]AccountID
4444
trackedPayments AccountPayments
45+
46+
*requestValuesStore
4547
}
4648

4749
func newMockService() *mockService {
4850
return &mockService{
49-
acctBalanceMsat: 0,
50-
trackedInvoices: make(map[lntypes.Hash]AccountID),
51-
trackedPayments: make(AccountPayments),
51+
acctBalanceMsat: 0,
52+
trackedInvoices: make(map[lntypes.Hash]AccountID),
53+
trackedPayments: make(AccountPayments),
54+
requestValuesStore: newRequestValuesStore(),
5255
}
5356
}
5457

accounts/interface.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,33 @@ type Service interface {
252252
// restarted.
253253
AssociatePayment(id AccountID, paymentHash lntypes.Hash,
254254
fullAmt lnwire.MilliSatoshi) error
255+
256+
RequestValuesStore
257+
}
258+
259+
// RequestValues holds various values associated with a specific request that
260+
// we may want access to when handling the response. At the moment this only
261+
// stores payment related data.
262+
type RequestValues struct {
263+
// PaymentHash is the hash of the payment that this request is
264+
// associated with.
265+
PaymentHash lntypes.Hash
266+
267+
// PaymentAmount is the value of the payment being made.
268+
PaymentAmount lnwire.MilliSatoshi
269+
}
270+
271+
// RequestValuesStore is a store that can be used to keep track of the mapping
272+
// between a request ID and various values associated with that request which
273+
// we may want access to when handling the request response.
274+
type RequestValuesStore interface {
275+
// RegisterValues stores values for the given request ID.
276+
RegisterValues(reqID uint64, values *RequestValues) error
277+
278+
// GetValues returns the corresponding request values for the given
279+
// request ID if they exist.
280+
GetValues(reqID uint64) (*RequestValues, bool)
281+
282+
// DeleteValues deletes any values stored for the given request ID.
283+
DeleteValues(reqID uint64)
255284
}

accounts/service.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type InterceptorService struct {
6666
invoiceToAccount map[lntypes.Hash]AccountID
6767
pendingPayments map[lntypes.Hash]*trackedPayment
6868

69+
*requestValuesStore
70+
6971
mainErrCallback func(error)
7072
wg sync.WaitGroup
7173
quit chan struct{}
@@ -86,14 +88,15 @@ func NewService(dir string,
8688
mainCtx, contextCancel := context.WithCancel(context.Background())
8789

8890
return &InterceptorService{
89-
store: accountStore,
90-
mainCtx: mainCtx,
91-
contextCancel: contextCancel,
92-
invoiceToAccount: make(map[lntypes.Hash]AccountID),
93-
pendingPayments: make(map[lntypes.Hash]*trackedPayment),
94-
mainErrCallback: errCallback,
95-
quit: make(chan struct{}),
96-
isEnabled: false,
91+
store: accountStore,
92+
mainCtx: mainCtx,
93+
contextCancel: contextCancel,
94+
invoiceToAccount: make(map[lntypes.Hash]AccountID),
95+
pendingPayments: make(map[lntypes.Hash]*trackedPayment),
96+
requestValuesStore: newRequestValuesStore(),
97+
mainErrCallback: errCallback,
98+
quit: make(chan struct{}),
99+
isEnabled: false,
97100
}, nil
98101
}
99102

@@ -830,3 +833,59 @@ func (s *InterceptorService) removePayment(hash lntypes.Hash,
830833
func successState(status lnrpc.Payment_PaymentStatus) bool {
831834
return status == lnrpc.Payment_SUCCEEDED
832835
}
836+
837+
// requestValuesStore is an in-memory implementation of the
838+
// RequestValuesStore interface.
839+
type requestValuesStore struct {
840+
m map[uint64]*RequestValues
841+
842+
mu sync.Mutex
843+
}
844+
845+
// A compile-time check to ensure that requestValuesStore implements the
846+
// RequestValuesStore interface.
847+
var _ RequestValuesStore = (*requestValuesStore)(nil)
848+
849+
// newRequestValuesStore constructs a new requestValuesStore which is an
850+
// implementation of the RequestValuesStore interface.
851+
func newRequestValuesStore() *requestValuesStore {
852+
return &requestValuesStore{
853+
m: make(map[uint64]*RequestValues),
854+
}
855+
}
856+
857+
// RegisterValues stores values for the given request ID.
858+
func (s *requestValuesStore) RegisterValues(reqID uint64,
859+
values *RequestValues) error {
860+
861+
s.mu.Lock()
862+
defer s.mu.Unlock()
863+
864+
if _, ok := s.m[reqID]; ok {
865+
return fmt.Errorf("values for request ID %d have already "+
866+
"been registered", reqID)
867+
}
868+
869+
s.m[reqID] = values
870+
871+
return nil
872+
}
873+
874+
// GetValues returns the corresponding request values for the given request ID
875+
// if they exist.
876+
func (s *requestValuesStore) GetValues(reqID uint64) (*RequestValues, bool) {
877+
s.mu.Lock()
878+
defer s.mu.Unlock()
879+
880+
values, ok := s.m[reqID]
881+
882+
return values, ok
883+
}
884+
885+
// DeleteValues deletes any values stored for the given request ID.
886+
func (s *requestValuesStore) DeleteValues(reqID uint64) {
887+
s.mu.Lock()
888+
defer s.mu.Unlock()
889+
890+
delete(s.m, reqID)
891+
}

0 commit comments

Comments
 (0)