Handle status updates from senders of payments (#24)

* Handle status updates from senders of payments

* Lint
This commit is contained in:
Pietralberto Mazza
2023-11-30 16:54:47 +01:00
committed by GitHub
parent be1f4654f9
commit 89aaf4d5c6
2 changed files with 31 additions and 8 deletions

View File

@@ -87,6 +87,10 @@ func (s *service) ClaimVtxos(ctx context.Context, creds string, receivers []doma
return s.paymentRequests.update(*payment) return s.paymentRequests.update(*payment)
} }
func (s *service) UpdatePaymenStatus(_ context.Context, id string) error {
return s.paymentRequests.updatePingTimestamp(id)
}
func (s *service) SignVtxos(ctx context.Context, forfeitTxs map[string]string) error { func (s *service) SignVtxos(ctx context.Context, forfeitTxs map[string]string) error {
for txid, tx := range forfeitTxs { for txid, tx := range forfeitTxs {
if err := s.forfeitTxs.sign(txid, tx); err != nil { if err := s.forfeitTxs.sign(txid, tx); err != nil {

View File

@@ -13,17 +13,18 @@ import (
type timedPayment struct { type timedPayment struct {
domain.Payment domain.Payment
timestamp time.Time timestamp time.Time
pingTimestamp time.Time
} }
type paymentsMap struct { type paymentsMap struct {
lock *sync.RWMutex lock *sync.RWMutex
payments map[string]timedPayment payments map[string]*timedPayment
} }
func newPaymentsMap(payments []domain.Payment) *paymentsMap { func newPaymentsMap(payments []domain.Payment) *paymentsMap {
paymentsById := make(map[string]timedPayment) paymentsById := make(map[string]*timedPayment)
for _, p := range payments { for _, p := range payments {
paymentsById[p.Id] = timedPayment{p, time.Now()} paymentsById[p.Id] = &timedPayment{p, time.Now(), time.Time{}}
} }
lock := &sync.RWMutex{} lock := &sync.RWMutex{}
return &paymentsMap{lock, paymentsById} return &paymentsMap{lock, paymentsById}
@@ -44,7 +45,7 @@ func (m *paymentsMap) push(payment domain.Payment) error {
return fmt.Errorf("duplicated inputs") return fmt.Errorf("duplicated inputs")
} }
m.payments[payment.Id] = timedPayment{payment, time.Now()} m.payments[payment.Id] = &timedPayment{payment, time.Now(), time.Time{}}
return nil return nil
} }
@@ -62,7 +63,11 @@ func (m *paymentsMap) pop(num int64) []domain.Payment {
if len(p.Receivers) <= 0 { if len(p.Receivers) <= 0 {
continue continue
} }
paymentsByTime = append(paymentsByTime, p) // Skip payments for which users didn't notify to be online in the last minute.
if p.pingTimestamp.IsZero() || time.Since(p.pingTimestamp).Minutes() > 1 {
continue
}
paymentsByTime = append(paymentsByTime, *p)
} }
sort.SliceStable(paymentsByTime, func(i, j int) bool { sort.SliceStable(paymentsByTime, func(i, j int) bool {
return paymentsByTime[i].timestamp.Before(paymentsByTime[j].timestamp) return paymentsByTime[i].timestamp.Before(paymentsByTime[j].timestamp)
@@ -80,11 +85,25 @@ func (m *paymentsMap) update(payment domain.Payment) error {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
if _, ok := m.payments[payment.Id]; !ok { p, ok := m.payments[payment.Id]
if !ok {
return fmt.Errorf("payment %s not found", payment.Id) return fmt.Errorf("payment %s not found", payment.Id)
} }
m.payments[payment.Id] = timedPayment{payment, m.payments[payment.Id].timestamp} p.Payment = payment
return nil
}
func (m *paymentsMap) updatePingTimestamp(id string) error {
m.lock.Lock()
defer m.lock.Unlock()
payment, ok := m.payments[id]
if !ok {
return fmt.Errorf("payment %s not found", id)
}
payment.pingTimestamp = time.Now()
return nil return nil
} }