aboutsummaryrefslogtreecommitdiff
path: root/eth
diff options
context:
space:
mode:
authorTed Yin <[email protected]>2020-09-20 16:08:07 -0400
committerGitHub <[email protected]>2020-09-20 16:08:07 -0400
commite233062dfde3ac938b937c860bc96caf57a7fe3f (patch)
tree0c4878ab45c8420506c7c0d6ee27534c5f3789ad /eth
parent9284f6d1193247adfaa29025934a3514a46188a3 (diff)
parent99a0be028ab8df4bc6028f9a105e131a8984a599 (diff)
Merge pull request #38 from ava-labs/gas-estimate
Fix handling of requests for pending block
Diffstat (limited to 'eth')
-rw-r--r--eth/api_backend.go28
-rw-r--r--eth/gasprice/gasprice.go72
2 files changed, 17 insertions, 83 deletions
diff --git a/eth/api_backend.go b/eth/api_backend.go
index bbc8691..6c5ebd8 100644
--- a/eth/api_backend.go
+++ b/eth/api_backend.go
@@ -65,10 +65,12 @@ func (b *EthAPIBackend) SetHead(number uint64) {
}
func (b *EthAPIBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) {
- // Pending block is only known by the miner
+ // Pending block is only known by the miner in
+ // Ethereum, but Coreth does not have the same notion.
+ // So we treat requests for the pending block identically
+ // to the latest accepted block instead
if number == rpc.PendingBlockNumber {
- block := b.eth.miner.PendingBlock()
- return block.Header(), nil
+ return b.eth.AcceptedBlock().Header(), nil
}
// Otherwise resolve and return the block
if number == rpc.LatestBlockNumber {
@@ -102,10 +104,12 @@ func (b *EthAPIBackend) HeaderByHash(ctx context.Context, hash common.Hash) (*ty
}
func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) {
- // Pending block is only known by the miner
+ // Pending block is only known by the miner in
+ // Ethereum, but Coreth does not have the same notion.
+ // So we treat requests for the pending block identically
+ // to the latest accepted block instead
if number == rpc.PendingBlockNumber {
- block := b.eth.miner.PendingBlock()
- return block, nil
+ return b.eth.AcceptedBlock(), nil
}
// Otherwise resolve and return the block
if number == rpc.LatestBlockNumber {
@@ -143,12 +147,12 @@ func (b *EthAPIBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash r
}
func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*state.StateDB, *types.Header, error) {
- // Pending state is only known by the miner
- if number == rpc.PendingBlockNumber {
- block, state := b.eth.miner.Pending()
- return state, block.Header(), nil
- }
- // Otherwise resolve the block number and return its state
+ // Note: Ethereum typically first checks if the request is for
+ // the pending block (by rpc.PendingBlockNumber), but Coreth
+ // does not have the same notion of the miner having a pending
+ // block, so this is skipped here
+
+ // Request the block by its number and retrieve its state
header, err := b.HeaderByNumber(ctx, number)
if err != nil {
return nil, nil, err
diff --git a/eth/gasprice/gasprice.go b/eth/gasprice/gasprice.go
index 14f50b1..14476ab 100644
--- a/eth/gasprice/gasprice.go
+++ b/eth/gasprice/gasprice.go
@@ -95,77 +95,7 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
// SuggestPrice returns a gasprice so that newly created transaction can
// have a very high chance to be included in the following blocks.
func (gpo *Oracle) SuggestPrice(ctx context.Context) (*big.Int, error) {
- head, _ := gpo.backend.HeaderByNumber(ctx, rpc.LatestBlockNumber)
- headHash := head.Hash()
-
- // If the latest gasprice is still available, return it.
- gpo.cacheLock.RLock()
- lastHead, lastPrice := gpo.lastHead, gpo.lastPrice
- gpo.cacheLock.RUnlock()
- if headHash == lastHead {
- return lastPrice, nil
- }
- gpo.fetchLock.Lock()
- defer gpo.fetchLock.Unlock()
-
- // Try checking the cache again, maybe the last fetch fetched what we need
- gpo.cacheLock.RLock()
- lastHead, lastPrice = gpo.lastHead, gpo.lastPrice
- gpo.cacheLock.RUnlock()
- if headHash == lastHead {
- return lastPrice, nil
- }
- var (
- sent, exp int
- number = head.Number.Uint64()
- result = make(chan getBlockPricesResult, gpo.checkBlocks)
- quit = make(chan struct{})
- txPrices []*big.Int
- )
- for sent < gpo.checkBlocks && number > 0 {
- go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, result, quit)
- sent++
- exp++
- number--
- }
- for exp > 0 {
- res := <-result
- if res.err != nil {
- close(quit)
- return lastPrice, res.err
- }
- exp--
- // Nothing returned. There are two special cases here:
- // - The block is empty
- // - All the transactions included are sent by the miner itself.
- // In these cases, use the latest calculated price for samping.
- if len(res.prices) == 0 {
- res.prices = []*big.Int{lastPrice}
- }
- // Besides, in order to collect enough data for sampling, if nothing
- // meaningful returned, try to query more blocks. But the maximum
- // is 2*checkBlocks.
- if len(res.prices) == 1 && len(txPrices)+1+exp < gpo.checkBlocks*2 && number > 0 {
- go gpo.getBlockPrices(ctx, types.MakeSigner(gpo.backend.ChainConfig(), big.NewInt(int64(number))), number, sampleNumber, result, quit)
- sent++
- exp++
- number--
- }
- txPrices = append(txPrices, res.prices...)
- }
- price := lastPrice
- if len(txPrices) > 0 {
- sort.Sort(bigIntArray(txPrices))
- price = txPrices[(len(txPrices)-1)*gpo.percentile/100]
- }
- if price.Cmp(gpo.maxPrice) > 0 {
- price = new(big.Int).Set(gpo.maxPrice)
- }
- gpo.cacheLock.Lock()
- gpo.lastHead = headHash
- gpo.lastPrice = price
- gpo.cacheLock.Unlock()
- return price, nil
+ return params.MinGasPrice, nil
}
type getBlockPricesResult struct {