-
Notifications
You must be signed in to change notification settings - Fork 45
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
feat: block history estimator #688
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ef9564
block history estimator
aalu1418 e7dab0c
tidy + lint
aalu1418 31a5a3e
Merge branch 'develop' into feat/block-history-est
aalu1418 9dd225d
fix broken txm tests
aalu1418 cbe294d
replace {} (empty = finalized) with confirmed commitment
aalu1418 743a9c8
add blockhistory estimator config
aalu1418 2f4a7b0
Merge branch 'develop' into feat/block-history-est
aalu1418 3cdce0a
Merge branch 'develop' into feat/block-history-est
aalu1418 666b610
Merge branch 'develop' into feat/block-history-est
aalu1418 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,137 @@ | ||
package fees | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/services" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils/mathutil" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/client" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/config" | ||
) | ||
|
||
var _ Estimator = &blockHistoryEstimator{} | ||
|
||
type blockHistoryEstimator struct { | ||
starter services.StateMachine | ||
chStop chan struct{} | ||
done sync.WaitGroup | ||
|
||
client *utils.LazyLoad[client.ReaderWriter] | ||
cfg config.Config | ||
lgr logger.Logger | ||
|
||
price uint64 | ||
lock sync.RWMutex | ||
} | ||
|
||
// NewBlockHistoryEstimator creates a new fee estimator that parses historical fees from a fetched block | ||
// Note: getRecentPrioritizationFees is not used because it provides the lowest prioritization fee for an included tx in the block | ||
// which is not effective enough for increasing the chances of block inclusion | ||
func NewBlockHistoryEstimator(c *utils.LazyLoad[client.ReaderWriter], cfg config.Config, lgr logger.Logger) (*blockHistoryEstimator, error) { | ||
return &blockHistoryEstimator{ | ||
chStop: make(chan struct{}), | ||
client: c, | ||
cfg: cfg, | ||
lgr: lgr, | ||
price: cfg.ComputeUnitPriceDefault(), // use default value | ||
}, nil | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) Start(ctx context.Context) error { | ||
return bhe.starter.StartOnce("solana_blockHistoryEstimator", func() error { | ||
bhe.done.Add(1) | ||
go bhe.run() | ||
bhe.lgr.Debugw("BlockHistoryEstimator: started") | ||
return nil | ||
}) | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) run() { | ||
defer bhe.done.Done() | ||
|
||
tick := time.After(0) | ||
for { | ||
select { | ||
case <-bhe.chStop: | ||
return | ||
case <-tick: | ||
if err := bhe.calculatePrice(); err != nil { | ||
bhe.lgr.Error(fmt.Errorf("BlockHistoryEstimator failed to fetch price: %w", err)) | ||
} | ||
} | ||
|
||
tick = time.After(utils.WithJitter(bhe.cfg.BlockHistoryPollPeriod())) | ||
} | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) Close() error { | ||
close(bhe.chStop) | ||
bhe.done.Wait() | ||
bhe.lgr.Debugw("BlockHistoryEstimator: stopped") | ||
return nil | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) BaseComputeUnitPrice() uint64 { | ||
price := bhe.readRawPrice() | ||
if price >= bhe.cfg.ComputeUnitPriceMin() && price <= bhe.cfg.ComputeUnitPriceMax() { | ||
return price | ||
} | ||
|
||
if price < bhe.cfg.ComputeUnitPriceMin() { | ||
bhe.lgr.Warnw("BlockHistoryEstimator: estimation below minimum consider lowering ComputeUnitPriceMin", "min", bhe.cfg.ComputeUnitPriceMin(), "calculated", price) | ||
return bhe.cfg.ComputeUnitPriceMin() | ||
} | ||
|
||
bhe.lgr.Warnw("BlockHistoryEstimator: estimation above maximum consider increasing ComputeUnitPriceMax", "min", bhe.cfg.ComputeUnitPriceMax(), "calculated", price) | ||
return bhe.cfg.ComputeUnitPriceMax() | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) readRawPrice() uint64 { | ||
bhe.lock.RLock() | ||
defer bhe.lock.RUnlock() | ||
return bhe.price | ||
} | ||
|
||
func (bhe *blockHistoryEstimator) calculatePrice() error { | ||
// fetch client | ||
c, err := bhe.client.Get() | ||
if err != nil { | ||
return fmt.Errorf("failed to get client in blockHistoryEstimator.getFee: %w", err) | ||
} | ||
|
||
// get latest block based on configured confirmation | ||
block, err := c.GetLatestBlock() | ||
if err != nil { | ||
return fmt.Errorf("failed to get block in blockHistoryEstimator.getFee: %w", err) | ||
} | ||
|
||
// parse block for fee data | ||
feeData, err := ParseBlock(block) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse block in blockHistoryEstimator.getFee: %w", err) | ||
} | ||
|
||
// take median of returned fee values | ||
v, err := mathutil.Median(feeData.Prices...) | ||
if err != nil { | ||
return fmt.Errorf("failed to find median in blockHistoryEstimator.getFee: %w", err) | ||
} | ||
|
||
// set data | ||
bhe.lock.Lock() | ||
bhe.price = uint64(v) // ComputeUnitPrice is uint64 underneath | ||
bhe.lock.Unlock() | ||
bhe.lgr.Debugw("BlockHistoryEstimator: updated", | ||
"computeUnitPrice", v, | ||
"blockhash", block.Blockhash, | ||
"slot", block.ParentSlot+1, | ||
"count", len(feeData.Prices), | ||
) | ||
return nil | ||
} |
Oops, something went wrong.
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.