Skip to content

Commit

Permalink
Activate refund reduction EIP, simplify code (erigontech#2016)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
  • Loading branch information
AlexeyAkhunov and Alex Sharp authored May 25, 2021
1 parent 95bcaf4 commit 03cb54a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
5 changes: 5 additions & 0 deletions cmd/integration/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
file string
txtrace bool // Whether to trace the execution (should only be used together eith `block`)
storageMode string
chain string // Which chain to use (mainnet, ropsten, rinkeby, goerli, etc.)
)

func must(err error) {
Expand Down Expand Up @@ -132,3 +133,7 @@ func withSilkworm(cmd *cobra.Command) {
func withTxTrace(cmd *cobra.Command) {
cmd.Flags().BoolVar(&txtrace, "txtrace", false, "enable tracing of transactions")
}

func withChain(cmd *cobra.Command) {
cmd.Flags().StringVar(&chain, "chain", "", "pick a chain to assume (mainnet, ropsten, etc.)")
}
27 changes: 24 additions & 3 deletions cmd/integration/commands/stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func init() {
withBatchSize(cmdStageExec)
withSilkworm(cmdStageExec)
withTxTrace(cmdStageExec)
withChain(cmdStageExec)

rootCmd.AddCommand(cmdStageExec)

Expand All @@ -276,6 +277,7 @@ func init() {
withBlock(cmdStageHashState)
withUnwind(cmdStageHashState)
withBatchSize(cmdStageHashState)
withChain(cmdStageHashState)

rootCmd.AddCommand(cmdStageHashState)

Expand All @@ -284,6 +286,7 @@ func init() {
withBlock(cmdStageTrie)
withUnwind(cmdStageTrie)
withIntegrityChecks(cmdStageTrie)
withChain(cmdStageTrie)

rootCmd.AddCommand(cmdStageTrie)

Expand Down Expand Up @@ -776,13 +779,31 @@ func newSync(db ethdb.RwKV) (ethdb.StorageMode, consensus.Engine, *params.ChainC
panic(err)
}
vmConfig := &vm.Config{NoReceipts: !sm.Receipts}
chainConfig := params.MainnetChainConfig
var chainConfig *params.ChainConfig
var genesis *core.Genesis
switch chain {
case "", params.MainnetChainName:
chainConfig = params.MainnetChainConfig
genesis = core.DefaultGenesisBlock()
case params.RopstenChainName:
chainConfig = params.RopstenChainConfig
genesis = core.DefaultRopstenGenesisBlock()
case params.GoerliChainName:
chainConfig = params.GoerliChainConfig
genesis = core.DefaultGoerliGenesisBlock()
case params.RinkebyChainName:
chainConfig = params.RinkebyChainConfig
genesis = core.DefaultRinkebyGenesisBlock()
case params.BaikalChainName:
chainConfig = params.BaikalChainConfig
genesis = core.DefaultBaikalGenesisBlock()
}
events := remotedbserver.NewEvents()

txCacher := core.NewTxSenderCacher(1)
txPool := core.NewTxPool(ethconfig.Defaults.TxPool, chainConfig, ethdb.NewObjectDatabase(db), txCacher)

chainConfig, genesis, genesisErr := core.SetupGenesisBlock(ethdb.NewObjectDatabase(db), core.DefaultGenesisBlock(), sm.History)
chainConfig, genesisBlock, genesisErr := core.SetupGenesisBlock(ethdb.NewObjectDatabase(db), genesis, sm.History)
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok {
panic(genesisErr)
}
Expand All @@ -794,7 +815,7 @@ func newSync(db ethdb.RwKV) (ethdb.StorageMode, consensus.Engine, *params.ChainC

engine := ethash.NewFaker()
blockDownloaderWindow := 65536
downloadServer, err := download.NewControlServer(db, "", chainConfig, genesis.Hash(), engine, 1, nil, blockDownloaderWindow)
downloadServer, err := download.NewControlServer(db, "", chainConfig, genesisBlock.Hash(), engine, 1, nil, blockDownloaderWindow)
if err != nil {
panic(err)
}
Expand Down
14 changes: 2 additions & 12 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ func (st *StateTransition) to() common.Address {
}

func (st *StateTransition) buyGas(gasBailout bool) error {
price := st.gasPrice
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// price = min(tip, feeCap - baseFee) + baseFee
price = cmath.Min256(new(uint256.Int).Add(st.tip, st.evm.Context.BaseFee), st.feeCap)
}
mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), price.ToBig())
mgval := new(big.Int).Mul(new(big.Int).SetUint64(st.msg.Gas()), st.gasPrice.ToBig())
gasCost, overflow := uint256.FromBig(mgval)
if have, want := st.state.GetBalance(st.msg.From()), mgval; overflow || st.state.GetBalance(st.msg.From()).Lt(gasCost) {
if !gasBailout {
Expand Down Expand Up @@ -344,12 +339,7 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
st.gas += refund

// Return ETH for remaining gas, exchanged at the original rate.
price := st.gasPrice
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// price = min(tip, feeCap - baseFee) + baseFee
price = cmath.Min256(new(uint256.Int).Add(st.tip, st.evm.Context.BaseFee), st.feeCap)
}
remaining := new(uint256.Int).Mul(new(uint256.Int).SetUint64(st.gas), price)
remaining := new(uint256.Int).Mul(new(uint256.Int).SetUint64(st.gas), st.gasPrice)
st.state.AddBalance(st.msg.From(), remaining)

// Also return remaining gas to the block gas counter so it is
Expand Down
1 change: 1 addition & 0 deletions core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type JumpTable [256]*operation
// contantinople, istanbul, petersburg, berlin, and london instructions.
func newLondonInstructionSet() JumpTable {
instructionSet := newBerlinInstructionSet()
enable3529(&instructionSet) // EIP-3529: Reduction in refunds https://eips.ethereum.org/EIPS/eip-3529
enable3198(&instructionSet) // Base fee opcode https://eips.ethereum.org/EIPS/eip-3198
return instructionSet
}
Expand Down

0 comments on commit 03cb54a

Please sign in to comment.