Skip to content

Commit

Permalink
Merge branch 'uddugteam:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
andskur authored May 26, 2024
2 parents 92402ba + 4bb4eab commit bc7c1d5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
36 changes: 36 additions & 0 deletions internal/service/coinAveragePriceSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,45 @@ func (s *service) SendCoinAveragePrice(tokens []string) {

go sender.runWriter()

currentEpoch, err := s.getCurrentPriceEpochDataRetries(0)
if err != nil {
logErr(fmt.Sprintf("err get currency epoch data: %v", err.Error()), "SendCoinAveragePrice")
return
}

durationSeconds := currentEpoch.EndTimestamp.Uint64() - currentEpoch.CurrentTimestamp.Uint64()
durationSeconds++

timerDuration, err := time.ParseDuration(fmt.Sprintf("%vs", durationSeconds))
if err != nil {
logErr(fmt.Sprintf("err parse timer duration: %v", err.Error()), "SendCoinAveragePrice")
return
}

logInfo(fmt.Sprintf("wait for the next epoch start: %v", timerDuration), "SendCoinAveragePrice")
timer := time.NewTimer(timerDuration)
<-timer.C

sender.ticker = time.NewTicker(time.Minute * 3)
logInfo("set ticker for sender to 3m", "SendCoinAveragePrice")
go sender.runSender()
}

func (s *service) getCurrentPriceEpochDataRetries(attempts int) (*contracts.PriceEpochData, error) {
data, err := s.flare.GetCurrentPriceEpochData()
if err != nil {
sleep, _ := time.ParseDuration(fmt.Sprintf("%vms", attempts*500))

logWarn(fmt.Sprintf("err GetCurrentPriceEpochData: %s retry in %v", err.Error(), sleep), "SendCoinAveragePrice")

time.Sleep(sleep)
attempts++
return s.getCurrentPriceEpochDataRetries(attempts)
}

return data, nil
}

// coinAVGPriceSender is a struct of the coin average prices sender
type coinAVGPriceSender struct {
// id is a WS id
Expand All @@ -56,6 +90,8 @@ type coinAVGPriceSender struct {

ticker *time.Ticker

epochs syncmap.Map

// tokens are the tokens for each submit-reveal flow
tokens []contracts.TokenID
// prices are the prices for next submit-reveal flow
Expand Down
47 changes: 39 additions & 8 deletions internal/service/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"time"
)

type epochData struct {
id *big.Int
random *big.Int
tokens []contracts.TokenID
prices []*big.Int
}

func (s *coinAVGPriceSender) runSender() {
for {
select {
Expand All @@ -22,18 +29,24 @@ func (s *coinAVGPriceSender) runSender() {
continue
}

logInfo(fmt.Sprintf("epochID: %v current: %v reveal end: %v", epoch.EpochID, epoch.CurrentTimestamp, epoch.RevealEndTimestamp), "Sender")

sleep, _ := time.ParseDuration(fmt.Sprintf("%vs", epoch.RevealEndTimestamp.Uint64()-epoch.CurrentTimestamp.Uint64()-60))
epochID := epoch.EpochID
random := s.getRandom()
tokens := s.tokens
prices := s.parsePrices()

if err := s.flare.CommitPrices(epoch.EpochID, s.tokens, s.parsePrices(), random); err != nil {
continue
}
logInfo(fmt.Sprintf("epochID: %v current: %v reveal end: %v", epochID, epoch.CurrentTimestamp, epoch.RevealEndTimestamp), "Sender")

sleepS := epoch.EndTimestamp.Uint64() - epoch.CurrentTimestamp.Uint64()
sleepS += 30
sleep, _ := time.ParseDuration(fmt.Sprintf("%vs", sleepS))

timer := time.NewTimer(sleep)
logInfo(fmt.Sprintf("time for reverl: %v", sleep), "Sender")
go s.reveal(timer, epoch.EpochID, s.tokens, s.parsePrices(), random)
go s.reveal(timer, epochID, tokens, prices, random)

if err := s.flare.CommitPrices(epochID, tokens, prices, random); err != nil {
continue
}
}
}
}
Expand All @@ -43,7 +56,25 @@ func (s *coinAVGPriceSender) reveal(timer *time.Timer, epochID *big.Int, indices
logInfo(fmt.Sprintf("received for reveal: epochID %v, indices %v, prices %v, random %v", epochID, indices, prices, random), "Sender")
<-timer.C
logInfo(fmt.Sprintf("revealing price for the epochID: %v", epochID.Int64()), "Sender")
if err := s.flare.RevealPrices(epochID, indices, prices, random); err != nil {
if err := s.revealRetries(epochID, indices, prices, random, 0); err != nil {
logErr("err reveal", "Sender")
}
}

func (s *coinAVGPriceSender) revealRetries(epochID *big.Int, indices []contracts.TokenID, prices []*big.Int, random *big.Int, attempts int) error {
if err := s.flare.RevealPrices(epochID, indices, prices, random); err != nil {
if attempts == 10 {
return err
}

sleep, _ := time.ParseDuration(fmt.Sprintf("%vms", attempts*500))

logWarn(fmt.Sprintf("err reveal: epochID %v, attempt %v, retry in %v: %s", epochID, attempts, sleep, err.Error()), "Sender")

time.Sleep(sleep)
attempts++
return s.revealRetries(epochID, indices, prices, random, attempts)
}

return nil
}

0 comments on commit bc7c1d5

Please sign in to comment.