-
Notifications
You must be signed in to change notification settings - Fork 53
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
offchain - commit store updates fetching optimization #195
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f21a6d3
optimize price updates fetching query
dimkouv f8ef35c
increase test durations
dimkouv ecd83bd
remove cache expiration
dimkouv 2e81f14
Merge branch 'ccip-develop' into perf/ccip736-optimize-price-updates-…
dimkouv ce09bc0
CCIP-1147 - Cache last update
jarnaud c7c6b8a
CCIP-1147 - Update cache only with most recent data
jarnaud ea8c893
CCIP-1147 - Remove unused methods
jarnaud 0d701bf
CCIP-1147 - Fix test setup
jarnaud 983009d
Merge branch 'ccip-develop' into CCIP-1147
jarnaud 7ac836e
Merge branch 'ccip-develop' into perf/ccip736-optimize-price-updates-…
dimkouv f5cb16d
CCIP-1147 - Test scenario for cache usage
jarnaud 48eea0c
CCIP-1147 - Remove useless comment
jarnaud 20e4217
Merge branch 'perf/ccip736-optimize-price-updates-fetch' into CCIP-1147
jarnaud 040a801
run gofmt
dimkouv 134da68
Merge pull request #198 from smartcontractkit/CCIP-1147
jarnaud 617640a
Merge branch 'ccip-develop' into perf/ccip736-optimize-price-updates-…
dimkouv 7ff9ba8
use single cache instance
dimkouv ce34089
nit
dimkouv b374a15
start with existing gas price update to prevent empty result
dimkouv 3221121
return empty update if it's too old
dimkouv a21673c
fix test
dimkouv 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 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
81 changes: 81 additions & 0 deletions
81
core/services/ocr2/plugins/ccip/commit_price_updates_cache.go
This file contains 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,81 @@ | ||
package ccip | ||
|
||
import ( | ||
"math/big" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
) | ||
|
||
type priceUpdatesCache struct { | ||
tokenPriceUpdates map[common.Address]update | ||
gasPriceUpdate update | ||
mu *sync.RWMutex | ||
} | ||
|
||
func newPriceUpdatesCache() *priceUpdatesCache { | ||
return &priceUpdatesCache{ | ||
tokenPriceUpdates: make(map[common.Address]update), | ||
mu: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (c *priceUpdatesCache) mostRecentTokenPriceUpdate() time.Time { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
ts := time.Time{} | ||
for _, upd := range c.tokenPriceUpdates { | ||
if upd.timestamp.After(ts) { | ||
ts = upd.timestamp | ||
} | ||
} | ||
return ts | ||
} | ||
|
||
func (c *priceUpdatesCache) updateTokenPriceIfMoreRecent(ts time.Time, tk common.Address, val *big.Int) bool { | ||
c.mu.RLock() | ||
v, exists := c.tokenPriceUpdates[tk] | ||
c.mu.RUnlock() | ||
|
||
if !exists || v.timestamp.Before(ts) { | ||
c.mu.Lock() | ||
c.tokenPriceUpdates[tk] = update{timestamp: ts, value: val} | ||
c.mu.Unlock() | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
// getTokenPriceUpdates returns all the price updates with timestamp greater than or equal to the provided | ||
func (c *priceUpdatesCache) getTokenPriceUpdates(minTs time.Time) map[common.Address]update { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
cp := make(map[common.Address]update, len(c.tokenPriceUpdates)) | ||
for k, v := range c.tokenPriceUpdates { | ||
if v.timestamp.Before(minTs) { | ||
continue | ||
} | ||
cp[k] = v | ||
} | ||
return cp | ||
} | ||
|
||
func (c *priceUpdatesCache) getGasPriceUpdate() update { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
return c.gasPriceUpdate | ||
} | ||
|
||
func (c *priceUpdatesCache) updateGasPriceIfMoreRecent(update update) bool { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
if update.timestamp.After(c.gasPriceUpdate.timestamp) { | ||
c.gasPriceUpdate = update | ||
return true | ||
} | ||
|
||
return false | ||
} |
32 changes: 32 additions & 0 deletions
32
core/services/ocr2/plugins/ccip/commit_price_updates_cache_test.go
This file contains 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,32 @@ | ||
package ccip | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_tokenPriceUpdatesCache(t *testing.T) { | ||
tk := common.HexToAddress("1") | ||
ts := time.Now().Truncate(time.Second) | ||
|
||
c := newPriceUpdatesCache() | ||
assert.Equal(t, time.Time{}, c.mostRecentTokenPriceUpdate()) | ||
|
||
c.updateTokenPriceIfMoreRecent(ts, tk, big.NewInt(100)) | ||
assert.Equal(t, ts, c.mostRecentTokenPriceUpdate()) | ||
v := c.getTokenPriceUpdates(time.Time{}) | ||
assert.Equal(t, big.NewInt(100), v[tk].value) | ||
|
||
// should not get updated if ts is older | ||
c.updateTokenPriceIfMoreRecent(ts.Add(-1*time.Minute), tk, big.NewInt(101)) | ||
v = c.getTokenPriceUpdates(time.Time{}) | ||
assert.Equal(t, big.NewInt(100), v[tk].value) | ||
|
||
// should not get anything when the provided timestamp is recent | ||
v = c.getTokenPriceUpdates(time.Now()) | ||
assert.Len(t, v, 0) | ||
} |
This file contains 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.
Shouldn't we keep the lock over the entire method?
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.
If we keep the lock over the entire method we will have a deadlock.
If we use only a
Lock()
over the entire method, instead of bothRLock()
for read andLock()
for updates then it's slightly less efficient.