Skip to content

Commit b9a80e5

Browse files
authored
Restore baseFee opcode (#2128)
Implementation of the CIP-61: Restore BaseFee opcode/header field.
1 parent 424ce92 commit b9a80e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1087
-460
lines changed

.github/workflows/config.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ concurrency:
1616
env:
1717
# Increment these to force cache rebuilding
1818
SYSTEM_CONTRACTS_CACHE_VERSION: 3
19-
CHECKOUT_MONOREPO_CACHE_VERSION: 6
19+
CHECKOUT_MONOREPO_CACHE_VERSION: 7
2020
GO_VERSION: '1.18'
2121
# Location where compiled system contracts are stored under the root of this
2222
# repo.
@@ -27,9 +27,9 @@ jobs:
2727
name: Build celo-monorepo
2828
runs-on: [self-hosted, blockchain, 8-cpu]
2929
env:
30-
NODE_VERSION: 12
30+
NODE_VERSION: 18
3131
PYTHON_VERSION: '3.9'
32-
CONTRACTS_BUILD_PATH: packages/protocol/build/contracts
32+
CONTRACTS_BUILD_PATH: packages/protocol/build
3333

3434
steps:
3535
- name: Checkout celo-blockchain repo
@@ -77,7 +77,6 @@ jobs:
7777
yarn build
7878
cd packages/protocol
7979
yarn run build:sol
80-
cp build/contracts-mento/* build/contracts/ # Include mento contracts
8180
- name: Upload built system contracts
8281
if: always()
8382
uses: actions/upload-artifact@v3
@@ -368,7 +367,7 @@ jobs:
368367
runs-on: [self-hosted, blockchain, 8-cpu]
369368
timeout-minutes: 30
370369
env:
371-
NODE_VERSION: 12
370+
NODE_VERSION: 18
372371

373372
needs:
374373
- go-modules
@@ -415,7 +414,7 @@ jobs:
415414
runs-on: [self-hosted, blockchain, 8-cpu]
416415
timeout-minutes: 30
417416
env:
418-
NODE_VERSION: 12
417+
NODE_VERSION: 18
419418

420419
needs:
421420
- go-modules
@@ -463,7 +462,7 @@ jobs:
463462
runs-on: [self-hosted, blockchain, 8-cpu]
464463
timeout-minutes: 30
465464
env:
466-
NODE_VERSION: 12
465+
NODE_VERSION: 18
467466

468467
needs:
469468
- go-modules
@@ -510,7 +509,7 @@ jobs:
510509
runs-on: [self-hosted, blockchain, 8-cpu]
511510
timeout-minutes: 30
512511
env:
513-
NODE_VERSION: 12
512+
NODE_VERSION: 18
514513

515514
needs:
516515
- go-modules
@@ -557,7 +556,7 @@ jobs:
557556
runs-on: [self-hosted, blockchain, 8-cpu]
558557
timeout-minutes: 30
559558
env:
560-
NODE_VERSION: 12
559+
NODE_VERSION: 18
561560

562561
needs:
563562
- go-modules
@@ -604,7 +603,7 @@ jobs:
604603
runs-on: [self-hosted, blockchain, 8-cpu]
605604
timeout-minutes: 30
606605
env:
607-
NODE_VERSION: 12
606+
NODE_VERSION: 18
608607

609608
needs:
610609
- go-modules
@@ -651,7 +650,7 @@ jobs:
651650
runs-on: [self-hosted, blockchain, 8-cpu]
652651
timeout-minutes: 30
653652
env:
654-
NODE_VERSION: 12
653+
NODE_VERSION: 18
655654

656655
needs:
657656
- go-modules
@@ -698,7 +697,7 @@ jobs:
698697
runs-on: [self-hosted, blockchain, 8-cpu]
699698
timeout-minutes: 30
700699
env:
701-
NODE_VERSION: 12
700+
NODE_VERSION: 18
702701

703702
needs:
704703
- go-modules

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ prepare-ethersjs-project: ./e2e_test/ethersjs-api-check/node_modules
5858
# that this repo can always access the contracts at a consistent path.
5959
prepare-system-contracts: $(MONOREPO_PATH)/packages/protocol/build
6060
@rm -rf compiled-system-contracts
61-
@mkdir -p compiled-system-contracts
62-
@cp -a $(MONOREPO_PATH)/packages/protocol/build/{contracts,contracts-mento}/ compiled-system-contracts
61+
@cp -R $(MONOREPO_PATH)/packages/protocol/build compiled-system-contracts
6362

6463
# If any of the source files in CONTRACT_SOURCE_FILES are more recent than the
6564
# build dir or the build dir does not exist then we remove the build dir, yarn
6665
# install and rebuild the contracts.
6766
$(MONOREPO_PATH)/packages/protocol/build: $(CONTRACT_SOURCE_FILES)
68-
@node --version | grep "^v12" || (echo "node v12 is required to build the monorepo (nvm use 12)" && exit 1)
67+
@node --version | grep "^v18" || (echo "node v18 is required to build the monorepo (nvm use 18)" && exit 1)
6968
@echo Running yarn install and compiling contracts
7069
@cd $(MONOREPO_PATH) && rm -rf packages/protocol/build && yarn && cd packages/protocol && yarn run build:sol
7170

accounts/abi/bind/backends/simulated.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/celo-org/celo-blockchain/eth/filters"
4242
"github.com/celo-org/celo-blockchain/ethdb"
4343
"github.com/celo-org/celo-blockchain/event"
44+
"github.com/celo-org/celo-blockchain/log"
4445
"github.com/celo-org/celo-blockchain/params"
4546
"github.com/celo-org/celo-blockchain/rpc"
4647
)
@@ -501,6 +502,38 @@ func (b *SimulatedBackend) EstimateGas(ctx context.Context, call ethereum.CallMs
501502
vmRunner := b.blockchain.NewEVMRunner(b.pendingBlock.Header(), b.pendingState)
502503
hi = blockchain_parameters.GetBlockGasLimitOrDefault(vmRunner)
503504
}
505+
// Normalize the max fee per gas the call is willing to spend.
506+
var feeCap *big.Int
507+
if call.GasPrice != nil && (call.GasFeeCap != nil || call.GasTipCap != nil) {
508+
return 0, errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
509+
} else if call.GasPrice != nil {
510+
feeCap = call.GasPrice
511+
} else if call.GasFeeCap != nil {
512+
feeCap = call.GasFeeCap
513+
} else {
514+
feeCap = common.Big0
515+
}
516+
// Recap the highest gas allowance with account's balance.
517+
if feeCap.BitLen() != 0 {
518+
balance := b.pendingState.GetBalance(call.From) // from can't be nil
519+
available := new(big.Int).Set(balance)
520+
if call.Value != nil {
521+
if call.Value.Cmp(available) >= 0 {
522+
return 0, errors.New("insufficient funds for transfer")
523+
}
524+
available.Sub(available, call.Value)
525+
}
526+
allowance := new(big.Int).Div(available, feeCap)
527+
if allowance.IsUint64() && hi > allowance.Uint64() {
528+
transfer := call.Value
529+
if transfer == nil {
530+
transfer = new(big.Int)
531+
}
532+
log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
533+
"sent", transfer, "feecap", feeCap, "fundable", allowance)
534+
hi = allowance.Uint64()
535+
}
536+
}
504537
cap = hi
505538

506539
// Create a helper to check if a gas allowance results in an executable transaction
@@ -607,10 +640,10 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
607640
// Execute the call.
608641
msg := callMsg{call}
609642

610-
// Create a new environment which holds all relevant information
611-
// about the transaction and calling mechanisms.
612643
txContext := core.NewEVMTxContext(msg)
613644
evmContext := core.NewEVMBlockContext(block.Header(), b.blockchain, nil)
645+
// Create a new environment which holds all relevant information
646+
// about the transaction and calling mechanisms.
614647
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
615648
gasPool := new(core.GasPool).AddGas(math.MaxUint64)
616649
vmRunner := b.blockchain.NewEVMRunner(block.Header(), stateDB)
@@ -766,7 +799,7 @@ func (b *SimulatedBackend) AdjustTime(adjustment time.Duration) error {
766799
defer b.mu.Unlock()
767800

768801
if len(b.pendingBlock.Transactions()) != 0 {
769-
return errors.New("could not adjust time on non-empty block")
802+
return errors.New("Could not adjust time on non-empty block")
770803
}
771804

772805
blocks, _ := core.GenerateChain(b.config, b.blockchain.CurrentBlock(), mockEngine.NewFaker(), b.database, 1, func(number int, block *core.BlockGen) {

cmd/mycelo/genesis_commands.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var createGenesisFromConfigCommand = cli.Command{
9999
func readBuildPath(ctx *cli.Context) (string, error) {
100100
buildpath := ctx.String(buildpathFlag.Name)
101101
if buildpath == "" {
102-
buildpath = path.Join(os.Getenv("CELO_MONOREPO"), "packages/protocol/build/contracts")
102+
buildpath = path.Join(os.Getenv("CELO_MONOREPO"), "packages/protocol/build")
103103
if fileutils.FileExists(buildpath) {
104104
log.Info("Missing --buildpath flag, using CELO_MONOREPO derived path", "buildpath", buildpath)
105105
} else {
@@ -127,8 +127,18 @@ func envFromTemplate(ctx *cli.Context, workdir string) (*env.Environment, *genes
127127
env.Accounts().Mnemonic = ctx.String("mnemonic")
128128
}
129129

130+
var gingerbreadBlock *big.Int
131+
if ctx.IsSet("forks.gingerbread") {
132+
gingerbreadBlockNumber := ctx.Int64("forks.gingerbread")
133+
if gingerbreadBlockNumber < 0 {
134+
gingerbreadBlock = nil
135+
} else {
136+
gingerbreadBlock = big.NewInt(gingerbreadBlockNumber)
137+
}
138+
}
139+
130140
// Genesis config
131-
genesisConfig, err := template.createGenesisConfig(env)
141+
genesisConfig, err := template.createGenesisConfig(env, gingerbreadBlock)
132142
if err != nil {
133143
return nil, nil, err
134144
}
@@ -171,14 +181,7 @@ func envFromTemplate(ctx *cli.Context, workdir string) (*env.Environment, *genes
171181
}
172182
}
173183

174-
if ctx.IsSet("forks.gingerbread") {
175-
gingerbreadBlockNumber := ctx.Int64("forks.gingerbread")
176-
if gingerbreadBlockNumber < 0 {
177-
genesisConfig.Hardforks.GingerbreadBlock = nil
178-
} else {
179-
genesisConfig.Hardforks.GingerbreadBlock = big.NewInt(gingerbreadBlockNumber)
180-
}
181-
}
184+
genesisConfig.Hardforks.GingerbreadBlock = gingerbreadBlock
182185

183186
return env, genesisConfig, nil
184187
}

cmd/mycelo/templates.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
type template interface {
1515
createEnv(workdir string) (*env.Environment, error)
16-
createGenesisConfig(*env.Environment) (*genesis.Config, error)
16+
createGenesisConfig(*env.Environment, *big.Int) (*genesis.Config, error)
1717
}
1818

1919
func templateFromString(templateStr string) template {
@@ -48,15 +48,18 @@ func (e localEnv) createEnv(workdir string) (*env.Environment, error) {
4848
return env, nil
4949
}
5050

51-
func (e localEnv) createGenesisConfig(env *env.Environment) (*genesis.Config, error) {
51+
func (e localEnv) createGenesisConfig(env *env.Environment, gingerbreadBlock *big.Int) (*genesis.Config, error) {
5252

53-
genesisConfig := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
53+
genesisConfig, err := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
5454
Epoch: 10,
5555
ProposerPolicy: 2,
5656
LookbackWindow: 3,
5757
BlockPeriod: 1,
5858
RequestTimeout: 3000,
59-
})
59+
}, gingerbreadBlock)
60+
if err != nil {
61+
return nil, err
62+
}
6063

6164
// Add balances to validator and developer accounts
6265
genesis.FundAccounts(genesisConfig, append(env.Accounts().ValidatorAccounts(), env.Accounts().DeveloperAccounts()...))
@@ -85,14 +88,17 @@ func (e loadtestEnv) createEnv(workdir string) (*env.Environment, error) {
8588
return env, nil
8689
}
8790

88-
func (e loadtestEnv) createGenesisConfig(env *env.Environment) (*genesis.Config, error) {
89-
genesisConfig := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
91+
func (e loadtestEnv) createGenesisConfig(env *env.Environment, gingerbreadBlock *big.Int) (*genesis.Config, error) {
92+
genesisConfig, err := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
9093
Epoch: 1000,
9194
ProposerPolicy: 2,
9295
LookbackWindow: 3,
9396
BlockPeriod: 5,
9497
RequestTimeout: 3000,
95-
})
98+
}, gingerbreadBlock)
99+
if err != nil {
100+
return nil, err
101+
}
96102

97103
// 10 billion gas limit, set super high on purpose
98104
genesisConfig.Blockchain.BlockGasLimit = 1000000000
@@ -130,14 +136,18 @@ func (e monorepoEnv) createEnv(workdir string) (*env.Environment, error) {
130136
return env, nil
131137
}
132138

133-
func (e monorepoEnv) createGenesisConfig(env *env.Environment) (*genesis.Config, error) {
134-
genesisConfig := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
139+
func (e monorepoEnv) createGenesisConfig(env *env.Environment, gingerbreadBlock *big.Int) (*genesis.Config, error) {
140+
genesisConfig, err := genesis.CreateCommonGenesisConfig(env.Config.ChainID, env.Accounts().AdminAccount().Address, params.IstanbulConfig{
135141
Epoch: 10,
136142
ProposerPolicy: 2,
137143
LookbackWindow: 3,
138144
BlockPeriod: 1,
139145
RequestTimeout: 3000,
140-
})
146+
}, gingerbreadBlock)
147+
if err != nil {
148+
return nil, err
149+
}
150+
141151
// To match the 'testing' config in monorepo
142152
genesisConfig.EpochRewards.TargetVotingYieldInitial = fixed.MustNew("0.00016")
143153
genesisConfig.EpochRewards.TargetVotingYieldMax = fixed.MustNew("0.0005")

cmd/utils/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ var (
449449
RPCGlobalGasPriceMultiplierFlag = cli.Float64Flag{
450450
Name: "rpc.gaspricemultiplier",
451451
Usage: "Multiplier applied to the gasPrice rpc call (1 = gasPrice, 1.3 = gasPrice + 30%, etc. Defaults to 2.0)",
452-
Value: 2.0,
452+
Value: float64(ethconfig.Defaults.RPCGasPriceMultiplier.Int64() / 100),
453453
}
454454
RPCGlobalGasCapFlag = cli.Uint64Flag{
455455
Name: "rpc.gascap",

consensus/istanbul/backend/engine.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,13 @@ func (sb *Backend) Finalize(chain consensus.ChainHeaderReader, header *types.Hea
507507
state.RevertToSnapshot(snapshot)
508508
}
509509

510-
// Trigger an update to the gas price minimum in the GasPriceMinimum contract based on block congestion
511-
snapshot = state.Snapshot()
512-
_, err = gpm.UpdateGasPriceMinimum(vmRunner, header.GasUsed)
513-
if err != nil {
514-
state.RevertToSnapshot(snapshot)
510+
if !sb.ChainConfig().IsGingerbread(header.Number) {
511+
// Trigger an update to the gas price minimum in the GasPriceMinimum contract based on block congestion
512+
snapshot = state.Snapshot()
513+
_, err = gpm.UpdateGasPriceMinimum(vmRunner, header.GasUsed)
514+
if err != nil {
515+
state.RevertToSnapshot(snapshot)
516+
}
515517
}
516518

517519
lastBlockOfEpoch := istanbul.IsLastBlockOfEpoch(header.Number.Uint64(), sb.config.Epoch)

0 commit comments

Comments
 (0)