Skip to content

Commit

Permalink
Merge pull request #5935 from mysteriumnetwork/recover-missing-last-p…
Browse files Browse the repository at this point in the history
…romise

allow to recover from missing/invalid last promise state
  • Loading branch information
Guillembonet authored Dec 13, 2023
2 parents e791f90 + 7624d14 commit 56a93e7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions session/pingpong/consumer_balance_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func (cbt *ConsumerBalanceTracker) alignWithHermes(chainID int64, id identity.Id
promised = consumer.LatestPromise.Amount
}

// if settled is bigger than promised, use settled as promised
if consumer.Settled != nil && consumer.Settled.Cmp(promised) == 1 {
promised = consumer.Settled
}

previous, _ := cbt.getBalance(chainID, id)
cbt.setBalance(chainID, id, ConsumerBalance{
BCBalance: consumer.Balance,
Expand Down Expand Up @@ -649,19 +654,24 @@ func (cbt *ConsumerBalanceTracker) recoverGrandTotalPromised(chainID int64, iden
return err
}

if data.LatestPromise.Amount == nil {
data.LatestPromise.Amount = new(big.Int)
latestPromised := big.NewInt(0)
if data.LatestPromise.Amount != nil {
latestPromised = data.LatestPromise.Amount
}
// if settled is bigger than promised, use settled as promised
if data.Settled != nil && data.Settled.Cmp(latestPromised) == 1 {
latestPromised = data.Settled
}

log.Debug().Msgf("Loaded hermes state: already promised: %v", data.LatestPromise.Amount)
log.Debug().Msgf("Loaded hermes state: already promised: %v", latestPromised)

hermes, err := cbt.addressProvider.GetActiveHermes(chainID)
if err != nil {
log.Error().Err(err).Msg("could not get hermes address")
return err
}

return cbt.consumerGrandTotalsStorage.Store(chainID, identity, hermes, data.LatestPromise.Amount)
return cbt.consumerGrandTotalsStorage.Store(chainID, identity, hermes, latestPromised)
}

func (cbt *ConsumerBalanceTracker) handleStopEvent() {
Expand Down

0 comments on commit 56a93e7

Please sign in to comment.