Skip to content

Commit

Permalink
feat: make descartes a timestamp based fork (scroll-tech#852)
Browse files Browse the repository at this point in the history
* feat: make descartes a timestamp based fork

* chore: auto version bump [bot]

---------

Co-authored-by: omerfirmak <omerfirmak@users.noreply.github.com>
  • Loading branch information
2 people authored and lwedge99 committed Aug 27, 2024
1 parent 36b1208 commit 4b6df93
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 31 deletions.
2 changes: 1 addition & 1 deletion core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3722,7 +3722,7 @@ func TestCurieTransition(t *testing.T) {
b, _ := json.Marshal(params.AllEthashProtocolChanges)
json.Unmarshal(b, &config)
config.CurieBlock = big.NewInt(2)
config.DescartesBlock = nil
config.DescartesTime = nil

var (
db = rawdb.NewMemoryDatabase()
Expand Down
2 changes: 1 addition & 1 deletion core/state_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestStateProcessorErrors(t *testing.T) {
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DescartesBlock: big.NewInt(0),
DescartesTime: new(uint64),
Ethash: new(params.EthashConfig),
}
signer = types.LatestSigner(config)
Expand Down
2 changes: 1 addition & 1 deletion core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
var (
msg = st.msg
sender = vm.AccountRef(msg.From())
rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber)
rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Time.Uint64())
contractCreation = msg.To() == nil
)

Expand Down
2 changes: 1 addition & 1 deletion core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
StateDB: statedb,
Config: config,
chainConfig: chainConfig,
chainRules: chainConfig.Rules(blockCtx.BlockNumber),
chainRules: chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Time.Uint64()),
}
evm.interpreter = NewEVMInterpreter(evm, config)
return evm
Expand Down
8 changes: 4 additions & 4 deletions core/vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func setDefaults(cfg *Config) {
ShanghaiBlock: new(big.Int),
BernoulliBlock: new(big.Int),
CurieBlock: new(big.Int),
DescartesBlock: new(big.Int),
DescartesTime: new(uint64),
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func Execute(code, input []byte, cfg *Config) ([]byte, *state.StateDB, error) {
address = common.BytesToAddress([]byte("contract"))
vmenv = NewEnv(cfg)
sender = vm.AccountRef(cfg.Origin)
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber)
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time.Uint64())
)
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
Expand Down Expand Up @@ -156,7 +156,7 @@ func Create(input []byte, cfg *Config) ([]byte, common.Address, uint64, error) {
var (
vmenv = NewEnv(cfg)
sender = vm.AccountRef(cfg.Origin)
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber)
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time.Uint64())
)
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
Expand Down Expand Up @@ -185,7 +185,7 @@ func Call(address common.Address, input []byte, cfg *Config) ([]byte, uint64, er
vmenv = NewEnv(cfg)
sender = cfg.State.GetOrNewStateObject(cfg.Origin)
statedb = cfg.State
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber)
rules = cfg.ChainConfig.Rules(vmenv.Context.BlockNumber, vmenv.Context.Time.Uint64())
)
// Execute the preparatory steps for state transition which includes:
// - prepare accessList(post-berlin)
Expand Down
2 changes: 1 addition & 1 deletion eth/gasprice/gasprice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newTestBackend(t *testing.T, londonBlock *big.Int, pending bool, pendingTxC
config.ShanghaiBlock = londonBlock
config.BernoulliBlock = londonBlock
config.CurieBlock = londonBlock
config.DescartesBlock = londonBlock
config.DescartesTime = nil
engine := ethash.NewFaker()
db := rawdb.NewMemoryDatabase()
genesis, err := gspec.Commit(db)
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/js/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func (jst *jsTracer) CaptureStart(env *vm.EVM, from common.Address, to common.Ad
jst.ctx["block"] = env.Context.BlockNumber.Uint64()
jst.dbWrapper.db = env.StateDB
// Update list of precompiles based on current block
rules := env.ChainConfig().Rules(env.Context.BlockNumber)
rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Time.Uint64())
jst.activePrecompiles = vm.ActivePrecompiles(rules)

// Compute intrinsic gas
Expand Down
2 changes: 1 addition & 1 deletion eth/tracers/native/4byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (t *fourByteTracer) CaptureStart(env *vm.EVM, from common.Address, to commo
t.env = env

// Update list of precompiles based on current block
rules := env.ChainConfig().Rules(env.Context.BlockNumber)
rules := env.ChainConfig().Rules(env.Context.BlockNumber, env.Context.Time.Uint64())
t.activePrecompiles = vm.ActivePrecompiles(rules)

// Save the outer calldata also
Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
to = crypto.CreateAddress(args.from(), uint64(*args.Nonce))
}
// Retrieve the precompiles since they don't need to be added to the access list
precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number))
precompiles := vm.ActivePrecompiles(b.ChainConfig().Rules(header.Number, header.Time))

// Create an initial tracer
prevTracer := vm.NewAccessListTracer(nil, args.from(), to, precompiles)
Expand Down
39 changes: 21 additions & 18 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ var (
ShanghaiBlock: nil,
BernoulliBlock: nil,
CurieBlock: nil,
DescartesBlock: nil,
DescartesTime: nil,
Clique: &CliqueConfig{
Period: 3,
Epoch: 30000,
Expand Down Expand Up @@ -320,7 +320,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(3747132),
CurieBlock: big.NewInt(4740239),
DescartesBlock: nil,
DescartesTime: nil,
Clique: &CliqueConfig{
Period: 3,
Epoch: 30000,
Expand Down Expand Up @@ -359,7 +359,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(5220340),
CurieBlock: big.NewInt(7096836),
DescartesBlock: nil,
DescartesTime: nil,
Clique: &CliqueConfig{
Period: 3,
Epoch: 30000,
Expand Down Expand Up @@ -404,7 +404,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DescartesBlock: big.NewInt(0),
DescartesTime: new(uint64),
TerminalTotalDifficulty: nil,
Ethash: new(EthashConfig),
Clique: nil,
Expand Down Expand Up @@ -447,7 +447,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DescartesBlock: big.NewInt(0),
DescartesTime: new(uint64),
TerminalTotalDifficulty: nil,
Ethash: nil,
Clique: &CliqueConfig{Period: 0, Epoch: 30000},
Expand Down Expand Up @@ -485,7 +485,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DescartesBlock: big.NewInt(0),
DescartesTime: new(uint64),
TerminalTotalDifficulty: nil,
Ethash: new(EthashConfig),
Clique: nil,
Expand All @@ -501,7 +501,7 @@ var (
ScrollChainAddress: common.HexToAddress("0x0000000000000000000000000000000000000000"),
},
}}
TestRules = TestChainConfig.Rules(new(big.Int))
TestRules = TestChainConfig.Rules(new(big.Int), 0)

TestNoL1DataFeeChainConfig = &ChainConfig{
ChainID: big.NewInt(1),
Expand All @@ -524,7 +524,7 @@ var (
ShanghaiBlock: big.NewInt(0),
BernoulliBlock: big.NewInt(0),
CurieBlock: big.NewInt(0),
DescartesBlock: big.NewInt(0),
DescartesTime: new(uint64),
TerminalTotalDifficulty: nil,
Ethash: new(EthashConfig),
Clique: nil,
Expand Down Expand Up @@ -622,7 +622,7 @@ type ChainConfig struct {
ShanghaiBlock *big.Int `json:"shanghaiBlock,omitempty"` // Shanghai switch block (nil = no fork, 0 = already on shanghai)
BernoulliBlock *big.Int `json:"bernoulliBlock,omitempty"` // Bernoulli switch block (nil = no fork, 0 = already on bernoulli)
CurieBlock *big.Int `json:"curieBlock,omitempty"` // Curie switch block (nil = no fork, 0 = already on curie)
DescartesBlock *big.Int `json:"descartesBlock,omitempty"` // Descartes switch block (nil = no fork, 0 = already on descartes)
DescartesTime *uint64 `json:"descartesTime,omitempty"` // Descartes switch time (nil = no fork, 0 = already on descartes)

// TerminalTotalDifficulty is the amount of total difficulty reached by
// the network that triggers the consensus upgrade.
Expand Down Expand Up @@ -759,7 +759,7 @@ func (c *ChainConfig) String() string {
c.ShanghaiBlock,
c.BernoulliBlock,
c.CurieBlock,
c.DescartesBlock,
c.DescartesTime,
engine,
c.Scroll,
)
Expand Down Expand Up @@ -853,8 +853,8 @@ func (c *ChainConfig) IsCurie(num *big.Int) bool {
}

// IsDescartes returns whether num is either equal to the Descartes fork block or greater.
func (c *ChainConfig) IsDescartes(num *big.Int) bool {
return isForked(c.DescartesBlock, num)
func (c *ChainConfig) IsDescartes(now uint64) bool {
return isForkedTime(now, c.DescartesTime)
}

// IsTerminalPoWBlock returns whether the given block is the last block of PoW stage.
Expand Down Expand Up @@ -910,7 +910,6 @@ func (c *ChainConfig) CheckConfigForkOrder() error {
{name: "shanghaiBlock", block: c.ShanghaiBlock, optional: true},
{name: "bernoulliBlock", block: c.BernoulliBlock, optional: true},
{name: "curieBlock", block: c.CurieBlock, optional: true},
{name: "descartesBlock", block: c.DescartesBlock, optional: true},
} {
if lastFork.name != "" {
// Next one must be higher number
Expand Down Expand Up @@ -995,9 +994,6 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, head *big.Int) *Confi
if isForkIncompatible(c.CurieBlock, newcfg.CurieBlock, head) {
return newCompatError("Curie fork block", c.CurieBlock, newcfg.CurieBlock)
}
if isForkIncompatible(c.DescartesBlock, newcfg.DescartesBlock, head) {
return newCompatError("Descartes fork block", c.DescartesBlock, newcfg.DescartesBlock)
}
return nil
}

Expand All @@ -1015,6 +1011,13 @@ func isForked(s, head *big.Int) bool {
return s.Cmp(head) <= 0
}

func isForkedTime(now uint64, forkTime *uint64) bool {
if forkTime == nil {
return false
}
return now >= *forkTime
}

func configNumEqual(x, y *big.Int) bool {
if x == nil {
return y == nil
Expand Down Expand Up @@ -1070,7 +1073,7 @@ type Rules struct {
}

// Rules ensures c's ChainID is not nil.
func (c *ChainConfig) Rules(num *big.Int) Rules {
func (c *ChainConfig) Rules(num *big.Int, time uint64) Rules {
chainID := c.ChainID
if chainID == nil {
chainID = new(big.Int)
Expand All @@ -1091,6 +1094,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
IsShanghai: c.IsShanghai(num),
IsBernoulli: c.IsBernoulli(num),
IsCurie: c.IsCurie(num),
IsDescartes: c.IsDescartes(num),
IsDescartes: c.IsDescartes(time),
}
}
40 changes: 40 additions & 0 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"math/big"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestCheckCompatible(t *testing.T) {
Expand Down Expand Up @@ -96,3 +98,41 @@ func TestCheckCompatible(t *testing.T) {
}
}
}

func TestIsForkedTime(t *testing.T) {
timePtr := func(t uint64) *uint64 {
return &t
}

tests := map[string]struct {
forkTime *uint64
now uint64
isForked bool
}{
"not configured": {
forkTime: nil,
isForked: false,
},
"before fork time": {
forkTime: timePtr(10),
now: 3,
isForked: false,
},
"on fork time": {
forkTime: timePtr(10),
now: 10,
isForked: true,
},
"after fork time": {
forkTime: timePtr(10),
now: 11,
isForked: true,
},
}

for desc, test := range tests {
t.Run(desc, func(t *testing.T) {
require.Equal(t, test.isForked, isForkedTime(test.now, test.forkTime))
})
}
}
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 5 // Minor version component of the current release
VersionPatch = 0 // Patch version component of the current release
VersionPatch = 1 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down

0 comments on commit 4b6df93

Please sign in to comment.