Skip to content

Commit

Permalink
Remove hardcoded estimate gas & reduce gas limit (ethereum#22)
Browse files Browse the repository at this point in the history
* Remove hardcoded estimate gas & reduce gas limit

* Remove commented code

* Set default target gas limit to 8mm

* Fix more cases of non-8mm gas

* Fix lint error

* Fix min gas limit bug
  • Loading branch information
karlfloersch authored Sep 19, 2020
1 parent 50adf39 commit 91d2823
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 76 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ make geth
```

```
rm -rf da-test-chain-dir && ./build/bin/geth --datadir da-test-chain-dir --rpc --dev --rpcaddr "0.0.0.0" --rpccorsdomain "*" --networkid 108 --rpcapi 'eth,net' --gasprice '0' --targetgaslimit '4294967295' --nousb --gcmode=archive
rm -rf da-test-chain-dir && ./build/bin/geth --datadir da-test-chain-dir --rpc --dev --rpcaddr "0.0.0.0" --rpccorsdomain "*" --networkid 108 --rpcapi 'eth,net' --gasprice '0' --targetgaslimit '8000000' --nousb --gcmode=archive
```
You can also enable logs with
```
rm -rf da-test-chain-dir && ./build/bin/geth --datadir da-test-chain-dir --rpc --dev --rpcaddr "0.0.0.0" --rpccorsdomain "*" --networkid 108 --rpcapi 'eth,net' --gasprice '0' --targetgaslimit '4294967295' --nousb --gcmode=archive --verbosity=5
rm -rf da-test-chain-dir && ./build/bin/geth --datadir da-test-chain-dir --rpc --dev --rpcaddr "0.0.0.0" --rpccorsdomain "*" --networkid 108 --rpcapi 'eth,net' --gasprice '0' --targetgaslimit '8000000' --nousb --gcmode=archive --verbosity=5
```


Expand Down
2 changes: 1 addition & 1 deletion cmd/puppeth/testdata/stureby_aleth.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"constantinopleForkBlock": "0x9c40",
"constantinopleFixForkBlock": "0x9c40",
"istanbulForkBlock": "0xc350",
"minGasLimit": "0xee6b2800",
"minGasLimit": "0x3d0900",
"maxGasLimit": "0x7fffffffffffffff",
"tieBreakingGas": false,
"gasLimitBoundDivisor": "0x400",
Expand Down
2 changes: 1 addition & 1 deletion cmd/puppeth/testdata/stureby_parity.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"params": {
"accountStartNonce": "0x0",
"maximumExtraDataSize": "0x20",
"minGasLimit": "0xee6b2800",
"minGasLimit": "0x3d0900",
"gasLimitBoundDivisor": "0x400",
"networkID": "0x4cb2e",
"chainID": "0x4cb2e",
Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
return &Genesis{
Config: &config,
ExtraData: append(append(make([]byte, 32), faucet[:]...), make([]byte, crypto.SignatureLength)...),
GasLimit: 4294967295,
GasLimit: 8000000,
Difficulty: big.NewInt(1),
Alloc: map[common.Address]GenesisAccount{
common.BytesToAddress([]byte{1}): {Balance: big.NewInt(1)}, // ECRecover
Expand Down
4 changes: 2 additions & 2 deletions docker/dev_entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fi

echo "Starting Geth..."
## Command to kick off geth
TARGET_GAS_LIMIT=${TARGET_GAS_LIMIT:-4294967295}
TARGET_GAS_LIMIT=${TARGET_GAS_LIMIT:-8000000}
./build/bin/geth --dev \
--datadir $VOLUME_PATH \
--rpc \
Expand All @@ -28,4 +28,4 @@ TARGET_GAS_LIMIT=${TARGET_GAS_LIMIT:-4294967295}
--targetgaslimit $TARGET_GAS_LIMIT \
--nousb \
--gcmode=archive \
--verbosity "6"
--verbosity "6"
2 changes: 1 addition & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# PORT=8545
# NETWORK_ID=108
CLEAR_DATA_FILE_PATH="${VOLUME_PATH}/.clear_data_key_${CLEAR_DATA_KEY}"
TARGET_GAS_LIMIT=${TARGET_GAS_LIMIT:-4294967295}
TARGET_GAS_LIMIT=${TARGET_GAS_LIMIT:-8000000}

if [[ -n "$CLEAR_DATA_KEY" && ! -f "$CLEAR_DATA_FILE_PATH" ]]; then
echo "Detected change in CLEAR_DATA_KEY. Purging data."
Expand Down
65 changes: 6 additions & 59 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,66 +886,13 @@ func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNrOr
}

func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap *big.Int) (hexutil.Uint64, error) {
// Binary search the gas requirement, as it may be higher than the amount used
var (
lo uint64 = params.TxGas - 1
hi uint64
cap uint64
)
if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
hi = uint64(*args.Gas)
} else {
// Retrieve the block to act as the gas ceiling
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
if err != nil {
return 0, err
}
hi = block.GasLimit()
}
if gasCap != nil && hi > gasCap.Uint64() {
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", gasCap)
hi = gasCap.Uint64()
}
cap = hi

// Set sender address or use a default if none specified
if args.From == nil {
if wallets := b.AccountManager().Wallets(); len(wallets) > 0 {
if accounts := wallets[0].Accounts(); len(accounts) > 0 {
args.From = &accounts[0].Address
}
}
}
// Use zero-address if none other is available
if args.From == nil {
args.From = &common.Address{}
}
// Create a helper to check if a gas allowance results in an executable transaction
executable := func(gas uint64) bool {
args.Gas = (*hexutil.Uint64)(&gas)

_, _, failed, err := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
if err != nil || failed {
return false
}
return true
}
// Execute the binary search and hone in on an executable gas limit
for lo+1 < hi {
mid := (hi + lo) / 2
if !executable(mid) {
lo = mid
} else {
hi = mid
}
}
// Reject the transaction as invalid if it still fails at the highest allowance
if hi == cap {
if !executable(hi) {
return 0, fmt.Errorf("gas required exceeds allowance (%d) or always failing transaction", cap)
}
// Retrieve the block to act as the gas ceiling
block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
if err != nil {
return 0, err
}
return hexutil.Uint64(hi), nil
// For now always return the gas limit
return hexutil.Uint64(block.GasLimit() - 1), nil
}

// EstimateGas returns an estimate of the amount of gas needed to execute the
Expand Down
6 changes: 3 additions & 3 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package params
import "math/big"

const (
GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
MinGasLimit uint64 = 4000000000 // Minimum the gas limit may ever be.
GenesisGasLimit uint64 = 4294967295 // Gas limit of the Genesis block.
GasLimitBoundDivisor uint64 = 1024 // The bound divisor of the gas limit, used in update calculations.
MinGasLimit uint64 = 4000000 // Minimum the gas limit may ever be.
GenesisGasLimit uint64 = 8000000 // Gas limit of the Genesis block.

MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
Expand Down
7 changes: 1 addition & 6 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,7 @@ func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMes
func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage {
var result interface{}
var err error
// TODO: think about long term maintainability of altered RPC methods
if msg.Method == "eth_estimateGas" {
result = 0xffffffff //Gas Limit
} else {
result, err = callb.call(ctx, msg.Method, args)
}
result, err = callb.call(ctx, msg.Method, args)
if err != nil {
return msg.errorResponse(err)
}
Expand Down

0 comments on commit 91d2823

Please sign in to comment.