-
Notifications
You must be signed in to change notification settings - Fork 12
Track & apply surge-factor pricing changes #867
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
Open
m-Peter
wants to merge
12
commits into
main
Choose a base branch
from
mpeter/track-surge-factor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ad3d14f
Track and index the latest Flow fees surge factor
m-Peter d236255
Multiply eth_gasPrice with the latest surge factor
m-Peter c9168e2
Update tx submission logic to take into account the surge factor changes
m-Peter 2a03e82
Multiply eth_maxPriorityFeePerGas with the latest surge factor
m-Peter 2e61b5b
Move gas price calculation to FeeParameters
m-Peter 36b1a36
Update eth_feeHistory to take into account changes to Flow fees surge…
m-Peter 08fafda
Logging and gas calculation improvements
m-Peter 86d82c1
Bootstrap surge factor using the network's current value
m-Peter 3b19086
Fix format verb for feeParameters argument
m-Peter 16e67bd
Add newline at the end of Cadence script
m-Peter 4df6ee5
Improve eth_feeHistory block reward calculation
m-Peter b89311e
Fallback to default fee parameters in case of error
m-Peter 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,7 @@ type BlockChainAPI struct { | |
| blocks storage.BlockIndexer | ||
| transactions storage.TransactionIndexer | ||
| receipts storage.ReceiptIndexer | ||
| feeParameters storage.FeeParametersIndexer | ||
| indexingResumedHeight uint64 | ||
| rateLimiter RateLimiter | ||
| collector metrics.Collector | ||
|
|
@@ -98,6 +99,7 @@ func NewBlockChainAPI( | |
| blocks storage.BlockIndexer, | ||
| transactions storage.TransactionIndexer, | ||
| receipts storage.ReceiptIndexer, | ||
| feeParameters storage.FeeParametersIndexer, | ||
| rateLimiter RateLimiter, | ||
| collector metrics.Collector, | ||
| indexingResumedHeight uint64, | ||
|
|
@@ -109,6 +111,7 @@ func NewBlockChainAPI( | |
| blocks: blocks, | ||
| transactions: transactions, | ||
| receipts: receipts, | ||
| feeParameters: feeParameters, | ||
| indexingResumedHeight: indexingResumedHeight, | ||
| rateLimiter: rateLimiter, | ||
| collector: collector, | ||
|
|
@@ -180,7 +183,13 @@ func (b *BlockChainAPI) SendRawTransaction( | |
| return common.Hash{}, err | ||
| } | ||
|
|
||
| id, err := b.evm.SendRawTransaction(ctx, input) | ||
| feeParams, err := b.feeParameters.Get() | ||
| if err != nil { | ||
| b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price") | ||
| feeParams = models.DefaultFeeParameters() | ||
| } | ||
|
|
||
| id, err := b.evm.SendRawTransaction(ctx, input, feeParams) | ||
| if err != nil { | ||
| return handleError[common.Hash](err, l, b.collector) | ||
| } | ||
|
|
@@ -850,8 +859,17 @@ func (b *BlockChainAPI) FeeHistory( | |
| maxCount := min(uint64(blockCount), lastBlockNumber) | ||
|
|
||
| blockRewards := make([]*hexutil.Big, len(rewardPercentiles)) | ||
| gasPrice := b.config.GasPrice | ||
|
|
||
| feeParams, err := b.feeParameters.Get() | ||
| if err != nil { | ||
| b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price") | ||
| } else { | ||
| gasPrice = feeParams.CalculateGasPrice(b.config.GasPrice) | ||
| } | ||
|
Comment on lines
+864
to
+869
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can reuse b.GasPrice(ctx) |
||
|
|
||
| for i := range rewardPercentiles { | ||
| blockRewards[i] = (*hexutil.Big)(b.config.GasPrice) | ||
| blockRewards[i] = (*hexutil.Big)(gasPrice) | ||
| } | ||
|
|
||
| for i := maxCount; i >= uint64(1); i-- { | ||
|
|
@@ -1050,7 +1068,13 @@ func (b *BlockChainAPI) Coinbase(ctx context.Context) (common.Address, error) { | |
|
|
||
| // GasPrice returns a suggestion for a gas price for legacy transactions. | ||
| func (b *BlockChainAPI) GasPrice(ctx context.Context) (*hexutil.Big, error) { | ||
| return (*hexutil.Big)(b.config.GasPrice), nil | ||
| feeParams, err := b.feeParameters.Get() | ||
| if err != nil { | ||
| b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price") | ||
| return (*hexutil.Big)(b.config.GasPrice), nil | ||
| } | ||
| gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice) | ||
| return (*hexutil.Big)(gasPrice), nil | ||
| } | ||
|
|
||
| // GetUncleCountByBlockHash returns number of uncles in the block for the given block hash | ||
|
|
@@ -1091,7 +1115,13 @@ func (b *BlockChainAPI) GetUncleByBlockNumberAndIndex( | |
|
|
||
| // MaxPriorityFeePerGas returns a suggestion for a gas tip cap for dynamic fee transactions. | ||
| func (b *BlockChainAPI) MaxPriorityFeePerGas(ctx context.Context) (*hexutil.Big, error) { | ||
| return (*hexutil.Big)(b.config.GasPrice), nil | ||
| feeParams, err := b.feeParameters.Get() | ||
| if err != nil { | ||
| b.logger.Warn().Err(err).Msg("fee parameters unavailable; falling back to base gas price") | ||
| return (*hexutil.Big)(b.config.GasPrice), nil | ||
| } | ||
| gasPrice := feeParams.CalculateGasPrice(b.config.GasPrice) | ||
| return (*hexutil.Big)(gasPrice), nil | ||
|
Comment on lines
+1118
to
+1124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks like it's the same as GasPrice |
||
| } | ||
|
|
||
| // Mining returns true if client is actively mining new blocks. | ||
|
|
||
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,5 @@ | ||
| import FlowFees | ||
|
|
||
| access(all) fun main(): UFix64 { | ||
| return FlowFees.getFeeParameters().surgeFactor | ||
| } |
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,64 @@ | ||
| package models | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/ethereum/go-ethereum/rlp" | ||
| "github.com/onflow/cadence" | ||
| ) | ||
|
|
||
| const feeParamsPrecision = 100_000_000 | ||
|
|
||
| var surgeFactorScale = big.NewInt(feeParamsPrecision) | ||
|
|
||
| func DefaultFeeParameters() *FeeParameters { | ||
| return &FeeParameters{ | ||
| SurgeFactor: cadence.UFix64(feeParamsPrecision), | ||
| InclusionEffortCost: cadence.UFix64(feeParamsPrecision), | ||
| ExecutionEffortCost: cadence.UFix64(feeParamsPrecision), | ||
| } | ||
| } | ||
|
|
||
| type FeeParameters struct { | ||
| SurgeFactor cadence.UFix64 `cadence:"surgeFactor"` | ||
| InclusionEffortCost cadence.UFix64 `cadence:"inclusionEffortCost"` | ||
| ExecutionEffortCost cadence.UFix64 `cadence:"executionEffortCost"` | ||
| } | ||
|
|
||
| func (f *FeeParameters) ToBytes() ([]byte, error) { | ||
| return rlp.EncodeToBytes(f) | ||
| } | ||
|
|
||
| func (f *FeeParameters) CalculateGasPrice(currentGasPrice *big.Int) *big.Int { | ||
| if currentGasPrice == nil { | ||
| return new(big.Int) // zero | ||
| } | ||
|
|
||
| // gasPrice = (currentGasPrice * surgeFactor) / feeParamsPrecision | ||
| surgeFactor := new(big.Int).SetUint64(uint64(f.SurgeFactor)) | ||
| gasPrice := new(big.Int).Mul(currentGasPrice, surgeFactor) | ||
| return new(big.Int).Quo(gasPrice, surgeFactorScale) | ||
| } | ||
|
|
||
| func NewFeeParametersFromBytes(data []byte) (*FeeParameters, error) { | ||
| feeParameters := &FeeParameters{} | ||
| if err := rlp.DecodeBytes(data, feeParameters); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return feeParameters, nil | ||
| } | ||
|
|
||
| func decodeFeeParametersChangedEvent(event cadence.Event) (*FeeParameters, error) { | ||
| feeParameters := &FeeParameters{} | ||
| if err := cadence.DecodeFields(event, feeParameters); err != nil { | ||
| return nil, fmt.Errorf( | ||
| "failed to Cadence-decode FlowFees.FeeParametersChanged event [%s]: %w", | ||
| event.String(), | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| return feeParameters, nil | ||
| } |
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.
Is this value cached in storage? Does it change after startup?
We should probably log when the fee parameters is initialized as well as updated.