This repository was archived by the owner on Mar 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
feat: Config updater for the base provider #117
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89757c4
init
davidterpay 2c3c0f2
Merge branch 'main' into terpay/config-updater
davidterpay fb53989
lint
davidterpay dc467e4
test nit
davidterpay 6f8dd50
tidy
davidterpay 2fdfb5a
label
davidterpay 5a29e00
add continue
davidterpay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package base | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
// listenOnConfigUpdater listens for updates from the config updater and updates the | ||
// provider's internal configurations. This will trigger the provider to restart | ||
// and is blocking until the context is cancelled. | ||
func (p *Provider[K, V]) listenOnConfigUpdater(ctx context.Context) { | ||
if p.updater == nil { | ||
return | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
p.logger.Info("stopping config client listener") | ||
return | ||
case ids := <-p.updater.GetIDs(): | ||
p.logger.Debug("received new ids", zap.Any("ids", ids)) | ||
p.SetIDs(ids) | ||
|
||
// Signal the provider to restart. | ||
p.restartCh <- struct{}{} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package base_test | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/skip-mev/slinky/providers/base" | ||
"github.com/skip-mev/slinky/providers/base/testutils" | ||
oracletypes "github.com/skip-mev/slinky/x/oracle/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
btcusd = oracletypes.NewCurrencyPair("BITCOIN", "USD") | ||
ethusd = oracletypes.NewCurrencyPair("ETHEREUM", "USD") | ||
solusd = oracletypes.NewCurrencyPair("SOLANA", "USD") | ||
) | ||
|
||
func TestConfigUpdater(t *testing.T) { | ||
t.Run("restart on IDs update with an API provider", func(t *testing.T) { | ||
pairs := []oracletypes.CurrencyPair{btcusd} | ||
updater := base.NewConfigUpdater[oracletypes.CurrencyPair]() | ||
apiHandler := testutils.CreateAPIQueryHandlerWithGetResponses[oracletypes.CurrencyPair, *big.Int]( | ||
t, | ||
logger, | ||
nil, | ||
) | ||
|
||
provider, err := base.NewProvider[oracletypes.CurrencyPair, *big.Int]( | ||
base.WithName[oracletypes.CurrencyPair, *big.Int](apiCfg.Name), | ||
base.WithAPIQueryHandler[oracletypes.CurrencyPair, *big.Int](apiHandler), | ||
base.WithAPIConfig[oracletypes.CurrencyPair, *big.Int](apiCfg), | ||
base.WithLogger[oracletypes.CurrencyPair, *big.Int](logger), | ||
base.WithIDs[oracletypes.CurrencyPair, *big.Int](pairs), | ||
base.WithConfigUpdater[oracletypes.CurrencyPair, *big.Int](updater), | ||
) | ||
require.NoError(t, err) | ||
|
||
// Start the provider and run it for a few seconds. | ||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) | ||
defer cancel() | ||
|
||
errCh := make(chan error) | ||
go func() { | ||
errCh <- provider.Start(ctx) | ||
}() | ||
|
||
// The initial IDs should be the same as the provider's IDs. | ||
ids := provider.GetIDs() | ||
require.Equal(t, pairs, ids) | ||
|
||
// Wait for a few seconds and update the IDs. | ||
time.Sleep(2 * time.Second) | ||
updated := []oracletypes.CurrencyPair{ethusd, solusd, btcusd} | ||
updater.UpdateIDs(updated) | ||
|
||
// Wait for the provider to restart. | ||
time.Sleep(2 * time.Second) | ||
|
||
// The IDs should be updated. | ||
ids = provider.GetIDs() | ||
require.Equal(t, updated, ids) | ||
|
||
// Check that the provider exited without error. | ||
require.Equal(t, context.DeadlineExceeded, <-errCh) | ||
}) | ||
|
||
t.Run("restart on IDs update with a websocket provider", func(t *testing.T) { | ||
pairs := []oracletypes.CurrencyPair{btcusd} | ||
updater := base.NewConfigUpdater[oracletypes.CurrencyPair]() | ||
wsHandler := testutils.CreateWebSocketQueryHandlerWithGetResponses[oracletypes.CurrencyPair, *big.Int]( | ||
t, | ||
time.Second, | ||
logger, | ||
nil, | ||
) | ||
|
||
provider, err := base.NewProvider[oracletypes.CurrencyPair, *big.Int]( | ||
base.WithName[oracletypes.CurrencyPair, *big.Int](wsCfg.Name), | ||
base.WithWebSocketQueryHandler[oracletypes.CurrencyPair, *big.Int](wsHandler), | ||
base.WithWebSocketConfig[oracletypes.CurrencyPair, *big.Int](wsCfg), | ||
base.WithLogger[oracletypes.CurrencyPair, *big.Int](logger), | ||
base.WithIDs[oracletypes.CurrencyPair, *big.Int](pairs), | ||
base.WithConfigUpdater[oracletypes.CurrencyPair, *big.Int](updater), | ||
) | ||
require.NoError(t, err) | ||
|
||
// Start the provider and run it for a few seconds. | ||
ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) | ||
defer cancel() | ||
|
||
errCh := make(chan error) | ||
go func() { | ||
errCh <- provider.Start(ctx) | ||
}() | ||
|
||
// The initial IDs should be the same as the provider's IDs. | ||
ids := provider.GetIDs() | ||
require.Equal(t, pairs, ids) | ||
|
||
// Wait for a few seconds and update the IDs. | ||
time.Sleep(2 * time.Second) | ||
updated := []oracletypes.CurrencyPair{ethusd, solusd, btcusd} | ||
updater.UpdateIDs(updated) | ||
|
||
// Wait for the provider to restart. | ||
time.Sleep(2 * time.Second) | ||
|
||
// The IDs should be updated. | ||
ids = provider.GetIDs() | ||
require.Equal(t, updated, ids) | ||
|
||
// Check that the provider exited without error. | ||
require.Equal(t, context.DeadlineExceeded, <-errCh) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unclear what p.GetIDs() is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDs are effectively currency pairs, the base provider just calls them IDs to have some generality.