Skip to content

Commit

Permalink
Merge pull request #2306 from mysteriumnetwork/force-settle-on-balanc…
Browse files Browse the repository at this point in the history
…e-exhaust

Force settle if balance exhausted
  • Loading branch information
vkuznecovas committed Jun 2, 2020
2 parents 9116684 + c6ded12 commit aa40348
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions session/pingpong/accountant_promise_settler.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,22 @@ func (aps *accountantPromiseSettler) Subscribe() error {
return errors.Wrap(err, "could not subscribe to service status event")
}

err = aps.eventBus.SubscribeAsync(event.AppTopicSettlementRequest, aps.handleSettlementEvent)
if err != nil {
return errors.Wrap(err, "could not subscribe to settlement event")
}

err = aps.eventBus.SubscribeAsync(event.AppTopicAccountantPromise, aps.handleAccountantPromiseReceived)
return errors.Wrap(err, "could not subscribe to accountant promise event")
}

func (aps *accountantPromiseSettler) handleSettlementEvent(event event.AppEventSettlementRequest) {
err := aps.ForceSettle(event.ProviderID, event.AccountantID)
if err != nil {
log.Error().Err(err).Msg("could not settle promise")
}
}

func (aps *accountantPromiseSettler) handleServiceEvent(event servicestate.AppEventServiceStatus) {
switch event.Status {
case string(servicestate.Running):
Expand Down
8 changes: 8 additions & 0 deletions session/pingpong/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ const (
AppTopicEarningsChanged = "earnings_change"
// AppTopicInvoicePaid is a topic for publish events exchange message send to provider as a consumer.
AppTopicInvoicePaid = "invoice_paid"
// AppTopicSettlementRequest forces the settlement of promises for given provider/accountant.
AppTopicSettlementRequest = "settlement_request"
)

// AppEventSettlementRequest represents the payload that is sent on the AppTopicSettlementRequest topic.
type AppEventSettlementRequest struct {
AccountantID common.Address
ProviderID identity.Identity
}

// AppEventAccountantPromise represents the payload that is sent on the AppTopicAccountantPromise.
type AppEventAccountantPromise struct {
Promise crypto.Promise
Expand Down
11 changes: 11 additions & 0 deletions session/pingpong/invoice_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/mysteriumnetwork/node/market"
"github.com/mysteriumnetwork/node/p2p"
sessionEvent "github.com/mysteriumnetwork/node/session/event"
"github.com/mysteriumnetwork/node/session/pingpong/event"
"github.com/mysteriumnetwork/payments/crypto"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -546,6 +547,16 @@ func (it *InvoiceTracker) handleAccountantError(err error) error {
stdErr.Is(err, ErrConsumerUnregistered):
// these are critical, return and cancel session
return err
// under normal use, this should not occur. If it does, we should drop sessions until we settle because we're not getting paid.
case stdErr.Is(err, ErrAccountantProviderBalanceExhausted):
it.deps.EventBus.Publish(
event.AppTopicSettlementRequest,
event.AppEventSettlementRequest{
AccountantID: it.deps.ProvidersAccountantID,
ProviderID: it.deps.ProviderID,
},
)
return err
default:
log.Err(err).Msgf("unknown accountant error encountered")
return err
Expand Down

0 comments on commit aa40348

Please sign in to comment.