Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SHIP-3903]Remove WS requirement for data feeds soak tests #15003

Merged
merged 10 commits into from
Nov 5, 2024
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 92 additions & 57 deletions integration-tests/testsetups/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

geth "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pelletier/go-toml/v2"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -619,8 +618,8 @@ func (o *OCRSoakTest) testLoop(testDuration time.Duration, newValue int) {
newRoundTrigger := time.NewTimer(0) // Want to trigger a new round ASAP
defer newRoundTrigger.Stop()
o.setFilterQuery()
err := o.observeOCREvents()
require.NoError(o.t, err, "Error subscribing to OCR events")
err := o.pollingOCREvents(endTest)
require.NoError(o.t, err, "Error setting up polling for OCR events")

n := o.Config.GetNetworkConfig()

Expand Down Expand Up @@ -824,70 +823,106 @@ func (o *OCRSoakTest) setFilterQuery() {
Msg("Filter Query Set")
}

// observeOCREvents subscribes to OCR events and logs them to the test logger
// WARNING: Should only be used for observation and logging. This is not a reliable way to collect events.
func (o *OCRSoakTest) observeOCREvents() error {
eventLogs := make(chan types.Log)
ctx, cancel := context.WithTimeout(testcontext.Get(o.t), 5*time.Second)
eventSub, err := o.seth.Client.SubscribeFilterLogs(ctx, o.filterQuery, eventLogs)
cancel()
if err != nil {
return err
}
// pollingOCREvents Polls the blocks for OCR events and logs them to the test logger
func (o *OCRSoakTest) pollingOCREvents(endTest <-chan time.Time) error {
// Keep track of the last processed block number
startingBlockNum := o.startingBlockNum
// Initialized to an invalid block number (max value of uint64)
lastCheckedBlockNum := ^uint64(0)

go func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope, but this goroutine appears to be untracked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated how this is handled and it is now tracked. Thanks for pointing it out.

// TODO: Make this configurable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this PR or follow-up? Is there a ticket to link?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intention was for a follow up. Just created SHIP-3973 to cover it.

pollInterval := time.Second * 30
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
o.log.Info().Msg("Start Polling for Answer Updated Events")
for {
select {
case event := <-eventLogs:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think its worth keeping the WSS option also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move onto polling. Seems more reliable and not prone to disconnections.

if o.OCRVersion == "1" {
answerUpdated, err := o.ocrV1Instances[0].ParseEventAnswerUpdated(event)
if err != nil {
o.log.Warn().
Err(err).
case <-endTest:
o.log.Info().Msg("Test duration ended, stopping polling")
return
case <-ticker.C:
// Get the latest block number to search up to the current block
latestBlock, err := o.seth.Client.BlockNumber(context.Background())
if err != nil {
o.log.Error().Err(err).Msg("Error getting latest block number")
continue
}

// Skip if this block has already been checked
if latestBlock == lastCheckedBlockNum {
o.log.Debug().
Uint64("Latest Block", latestBlock).
Uint64("Last Checked Block Number", lastCheckedBlockNum).
Msg("No new blocks since last poll")
continue
}
// Check if the latest block is behind the starting block just in case
if startingBlockNum > latestBlock {
o.log.Error().
Uint64("From Block", startingBlockNum).
Uint64("To Block", latestBlock).
Msg("The latest block is behind the starting block. This could happen due to RPC issues or possibly a reorg")
// May need to `startingBlockNum = latestBlock` if this happens frequently due to reorgs
continue
}

// Prepare the filter query with updated block range
o.filterQuery.FromBlock = big.NewInt(0).SetUint64(startingBlockNum)
o.filterQuery.ToBlock = big.NewInt(0).SetUint64(latestBlock)

o.log.Debug().
Uint64("From Block", startingBlockNum).
Uint64("To Block", latestBlock).
Msg("Fetching logs for the specified range")

// Fetch logs for the specified range
logs, err := o.seth.Client.FilterLogs(context.Background(), o.filterQuery)
if err != nil {
o.log.Error().Err(err).Msg("Error fetching logs")
continue
}

// Process the fetched logs
for _, event := range logs {
if o.OCRVersion == "1" {
answerUpdated, err := o.ocrV1Instances[0].ParseEventAnswerUpdated(event)
if err != nil {
o.log.Warn().
Err(err).
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Msg("Error parsing event as AnswerUpdated")
continue
}
o.log.Info().
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Msg("Error parsing event as AnswerUpdated")
continue
}
o.log.Info().
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Uint64("Round ID", answerUpdated.RoundId.Uint64()).
Int64("Answer", answerUpdated.Current.Int64()).
Msg("Answer Updated Event")
} else if o.OCRVersion == "2" {
answerUpdated, err := o.ocrV2Instances[0].ParseEventAnswerUpdated(event)
if err != nil {
o.log.Warn().
Err(err).
Uint64("Round ID", answerUpdated.RoundId.Uint64()).
Int64("Answer", answerUpdated.Current.Int64()).
Msg("Answer Updated Event")
} else if o.OCRVersion == "2" {
answerUpdated, err := o.ocrV2Instances[0].ParseEventAnswerUpdated(event)
if err != nil {
o.log.Warn().
Err(err).
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Msg("Error parsing event as AnswerUpdated")
continue
}
o.log.Info().
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Msg("Error parsing event as AnswerUpdated")
continue
}
o.log.Info().
Str("Address", event.Address.Hex()).
Uint64("Block Number", event.BlockNumber).
Uint64("Round ID", answerUpdated.RoundId.Uint64()).
Int64("Answer", answerUpdated.Current.Int64()).
Msg("Answer Updated Event")
}
case err = <-eventSub.Err():
backoff := time.Second
for err != nil {
o.log.Info().
Err(err).
Str("Backoff", backoff.String()).
Interface("Query", o.filterQuery).
Msg("Error while subscribed to OCR Logs. Resubscribing")
ctx, cancel = context.WithTimeout(testcontext.Get(o.t), backoff)
eventSub, err = o.seth.Client.SubscribeFilterLogs(ctx, o.filterQuery, eventLogs)
cancel()
if err != nil {
time.Sleep(backoff)
backoff = time.Duration(math.Min(float64(backoff)*2, float64(30*time.Second)))
Uint64("Round ID", answerUpdated.RoundId.Uint64()).
Int64("Answer", answerUpdated.Current.Int64()).
Msg("Answer Updated Event")
}
}

// Track last checked block number and update starting block number
lastCheckedBlockNum = latestBlock
startingBlockNum = latestBlock + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: can we just repurpose startingBlockNum to check for the lastCheckedBlockNum. Not a big deal but good for readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. Wanted to have a clear separation between the initial run and subsequent runs but it does not add that much value. Simpler code is always better.

}
}
}()
Expand Down
Loading