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

Update SuggestTipCap default tip cap #44

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 44 additions & 25 deletions node/cn/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewOracle(backend OracleBackend, config Config, txPool TxPool, governance G

return &Oracle{
backend: backend,
lastPrice: config.Default,
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
lastPrice: common.Big0,
maxPrice: maxPrice,
checkBlocks: blocks,
maxEmpty: blocks / 2,
Expand Down Expand Up @@ -190,17 +190,23 @@ func (gpo *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
if gpo.backend.ChainConfig().IsKaiaForkEnabled(nextNum) {
// After Kaia, return using fee history.
// If the next baseFee is lower bound, return 0.
// Otherwise, by default config, this will return 60% percentile of last 20 blocks
// Otherwise, by default config, this will return 60% percentile of last 20 blocks.
// See node/cn/config.go for the default config.
pset, err := gpo.gov.EffectiveParams(nextNum.Uint64())
if pset != nil && err == nil {
header := gpo.backend.CurrentBlock().Header()
nextBaseFee := misc.NextMagmaBlockBaseFee(header, pset.ToKIP71Config())
if nextBaseFee.Cmp(big.NewInt(int64(pset.LowerBoundBaseFee()))) <= 0 {
return common.Big0, nil
}
header := gpo.backend.CurrentBlock().Header()
headHash := header.Hash()
// If the latest gasprice is still available, return it.
if lastPrice, ok := gpo.checkCache(headHash); ok {
return new(big.Int).Set(lastPrice), nil
}
if gpo.isRelaxedNetwork(header) {
gpo.writeCache(headHash, common.Big0)
return common.Big0, nil
}
return gpo.suggestTipCapUsingFeeHistory(ctx)
price, err := gpo.suggestTipCapUsingFeeHistory(ctx)
if err == nil {
gpo.writeCache(headHash, price)
}
return price, err
} else if gpo.backend.ChainConfig().IsMagmaForkEnabled(nextNum) {
// After Magma, return zero
return common.Big0, nil
Expand All @@ -216,20 +222,10 @@ func (oracle *Oracle) suggestTipCapUsingFeeHistory(ctx context.Context) (*big.In
head, _ := oracle.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
headHash := head.Hash()

// If the latest gasprice is still available, return it.
oracle.cacheLock.RLock()
lastHead, lastPrice := oracle.lastHead, oracle.lastPrice
oracle.cacheLock.RUnlock()
if headHash == lastHead {
return new(big.Int).Set(lastPrice), nil
}
oracle.fetchLock.Lock()
defer oracle.fetchLock.Unlock()

// Try checking the cache again, maybe the last fetch fetched what we need
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
oracle.cacheLock.RLock()
lastHead, lastPrice = oracle.lastHead, oracle.lastPrice
oracle.cacheLock.RUnlock()
lastHead, lastPrice := oracle.readCache()
if headHash == lastHead {
return new(big.Int).Set(lastPrice), nil
}
Expand Down Expand Up @@ -283,10 +279,6 @@ func (oracle *Oracle) suggestTipCapUsingFeeHistory(ctx context.Context) (*big.In
if price.Cmp(oracle.maxPrice) > 0 {
price = new(big.Int).Set(oracle.maxPrice)
}
oracle.cacheLock.Lock()
oracle.lastHead = headHash
oracle.lastPrice = price
oracle.cacheLock.Unlock()

return new(big.Int).Set(price), nil
}
Expand Down Expand Up @@ -339,6 +331,33 @@ func (oracle *Oracle) getBlockValues(ctx context.Context, blockNum uint64, limit
}
}

func (oracle *Oracle) isRelaxedNetwork(header *types.Header) bool {
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
pset, err := oracle.gov.EffectiveParams(header.Number.Uint64() + 1)
if pset != nil && err == nil {
nextBaseFee := misc.NextMagmaBlockBaseFee(header, pset.ToKIP71Config())
return nextBaseFee.Cmp(big.NewInt(int64(pset.LowerBoundBaseFee()))) <= 0
}
return false
}

func (oracle *Oracle) checkCache(headHash common.Hash) (*big.Int, bool) {
blukat29 marked this conversation as resolved.
Show resolved Hide resolved
lastHead, lastPrice := oracle.readCache()
return lastPrice, headHash == lastHead
}

func (oracle *Oracle) readCache() (common.Hash, *big.Int) {
oracle.cacheLock.RLock()
defer oracle.cacheLock.RUnlock()
return oracle.lastHead, oracle.lastPrice
}

func (oracle *Oracle) writeCache(head common.Hash, price *big.Int) {
oracle.cacheLock.Lock()
oracle.lastHead = head
oracle.lastPrice = price
oracle.cacheLock.Unlock()
}

func (oracle *Oracle) PurgeCache() {
oracle.cacheLock.Lock()
oracle.historyCache.Purge()
Expand Down
10 changes: 5 additions & 5 deletions node/cn/gasprice/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestGasPrice_NewOracle(t *testing.T) {
params := Config{}
oracle := NewOracle(mockBackend, params, nil, nil)

assert.Nil(t, oracle.lastPrice)
assert.Equal(t, big.NewInt(0), oracle.lastPrice)
assert.Equal(t, 1, oracle.checkBlocks)
assert.Equal(t, 0, oracle.maxEmpty)
assert.Equal(t, 5, oracle.maxBlocks)
Expand All @@ -167,7 +167,7 @@ func TestGasPrice_NewOracle(t *testing.T) {
params = Config{Blocks: 2}
oracle = NewOracle(mockBackend, params, nil, nil)

assert.Nil(t, oracle.lastPrice)
assert.Equal(t, big.NewInt(0), oracle.lastPrice)
assert.Equal(t, 2, oracle.checkBlocks)
assert.Equal(t, 1, oracle.maxEmpty)
assert.Equal(t, 10, oracle.maxBlocks)
Expand All @@ -176,7 +176,7 @@ func TestGasPrice_NewOracle(t *testing.T) {
params = Config{Percentile: -1}
oracle = NewOracle(mockBackend, params, nil, nil)

assert.Nil(t, oracle.lastPrice)
assert.Equal(t, big.NewInt(0), oracle.lastPrice)
assert.Equal(t, 1, oracle.checkBlocks)
assert.Equal(t, 0, oracle.maxEmpty)
assert.Equal(t, 5, oracle.maxBlocks)
Expand All @@ -185,7 +185,7 @@ func TestGasPrice_NewOracle(t *testing.T) {
params = Config{Percentile: 101}
oracle = NewOracle(mockBackend, params, nil, nil)

assert.Nil(t, oracle.lastPrice)
assert.Equal(t, big.NewInt(0), oracle.lastPrice)
assert.Equal(t, 1, oracle.checkBlocks)
assert.Equal(t, 0, oracle.maxEmpty)
assert.Equal(t, 5, oracle.maxBlocks)
Expand All @@ -194,7 +194,7 @@ func TestGasPrice_NewOracle(t *testing.T) {
params = Config{Percentile: 101, Default: big.NewInt(123)}
oracle = NewOracle(mockBackend, params, nil, nil)

assert.Equal(t, big.NewInt(123), oracle.lastPrice)
assert.Equal(t, big.NewInt(0), oracle.lastPrice)
assert.Equal(t, 1, oracle.checkBlocks)
assert.Equal(t, 0, oracle.maxEmpty)
assert.Equal(t, 5, oracle.maxBlocks)
Expand Down
Loading