Skip to content

Commit

Permalink
feature: Optimize GasEstimate method
Browse files Browse the repository at this point in the history
feature: Optimize GasEstimate method

[R4R]-[estimateGas]feat: enlarge a buffer for Estimation

feature: Optimize GasEstimate method

feature: Optimize GasEstimate method

feature: Optimize GasEstimate method02
  • Loading branch information
DevDynamo2024 committed Feb 21, 2024
1 parent d7755f6 commit 9bab5f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 49 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ profile.cov
/dashboard/assets/package-lock.json

**/yarn-error.log
node_modules
45 changes: 6 additions & 39 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/consensus"
"math/big"
"strings"
"time"

"github.com/ethereum/go-ethereum"

"github.com/davecgh/go-spew/spew"
"github.com/tyler-smith/go-bip39"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -51,13 +46,17 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"

"github.com/davecgh/go-spew/spew"
"github.com/tyler-smith/go-bip39"
)

// estimateGasErrorRatio is the amount of overestimation eth_estimateGas is
// allowed to produce in order to speed up calculations.
// gasBuffer is used to enlarge a buffer for Estimation
const (
estimateGasErrorRatio = 0.015
gasBuffer = uint64(120 / 100)
gasBuffer = uint64(120)
)

// EthereumAPI provides an API to access Ethereum related information.
Expand Down Expand Up @@ -1373,7 +1372,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
hi = mid
}
}
return hexutil.Uint64(hi * gasBuffer), nil
return hexutil.Uint64(hi * gasBuffer / 100), nil
}

func calculateGasWithAllowance(ctx context.Context, b Backend, args TransactionArgs, blockNrOrHash rpc.BlockNumberOrHash, gasPriceForEstimate *big.Int, gasCap uint64) (uint64, error) {
Expand Down Expand Up @@ -1443,38 +1442,6 @@ func (s *BlockChainAPI) EstimateGas(ctx context.Context, args TransactionArgs, b
return DoEstimateGas(ctx, s.b, args, bNrOrHash, s.b.RPCGasCap())
}

// ChainContextBackend provides methods required to implement ChainContext.
type ChainContextBackend interface {
Engine() consensus.Engine
HeaderByNumber(context.Context, rpc.BlockNumber) (*types.Header, error)
}

// ChainContext is an implementation of core.ChainContext. It's main use-case
// is instantiating a vm.BlockContext without having access to the BlockChain object.
type ChainContext struct {
b ChainContextBackend
ctx context.Context
}

func (context *ChainContext) Engine() consensus.Engine {
return context.b.Engine()
}

func (context *ChainContext) GetHeader(hash common.Hash, number uint64) *types.Header {
// This method is called to get the hash for a block number when executing the BLOCKHASH
// opcode. Hence no need to search for non-canonical blocks.
header, err := context.b.HeaderByNumber(context.ctx, rpc.BlockNumber(number))
if err != nil || header.Hash() != hash {
return nil
}
return header
}

// NewChainContext creates a new ChainContext object.
func NewChainContext(ctx context.Context, backend ChainContextBackend) *ChainContext {
return &ChainContext{ctx: ctx, b: backend}
}

// RPCMarshalHeader converts the given header to the RPC output .
func RPCMarshalHeader(head *types.Header) map[string]interface{} {
result := map[string]interface{}{
Expand Down
19 changes: 10 additions & 9 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,32 @@ import (
"crypto/ecdsa"
"encoding/json"
"errors"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/beacon"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/bloombits"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/exp/slices"
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"
)

func TestNewRPCTransactionDepositTx(t *testing.T) {
Expand Down

0 comments on commit 9bab5f8

Please sign in to comment.