Skip to content

Commit

Permalink
fix: base fee tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Jan 15, 2025
1 parent 5788997 commit a8fe27f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 24 deletions.
8 changes: 4 additions & 4 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ var baseChainConfig = vm.ChainConfig{
Ethash: nil,
Clique: nil,
},
IstanbulBlock: nil,
ShanghaiBlock: nil,
IstanbulBlock: nil,
GalacticaBlock: nil,
}

// Output output of clause execution.
Expand Down Expand Up @@ -99,15 +99,15 @@ func New(
currentChainConfig := baseChainConfig
currentChainConfig.ConstantinopleBlock = big.NewInt(int64(forkConfig.ETH_CONST))
currentChainConfig.IstanbulBlock = big.NewInt(int64(forkConfig.ETH_IST))
currentChainConfig.ShanghaiBlock = big.NewInt(int64(forkConfig.GALACTICA))
currentChainConfig.GalacticaBlock = big.NewInt(int64(forkConfig.GALACTICA))
if chain != nil {
// use genesis id as chain id
currentChainConfig.ChainID = new(big.Int).SetBytes(chain.GenesisID().Bytes())
}

// alloc precompiled contracts
if forkConfig.GALACTICA == ctx.Number {
for addr := range vm.PrecompiledContractsShanghai {
for addr := range vm.PrecompiledContractsGalactica {
if err := state.SetCode(thor.Address(addr), EmptyRuntimeBytecode); err != nil {
panic(err)
}
Expand Down
13 changes: 6 additions & 7 deletions runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,12 @@ func TestEVMFunction(t *testing.T) {
abi: "",
methodName: "",
testFunc: func(ctx *context, t *testing.T) {
exec, _ := runtime.New(ctx.chain, ctx.state, &xenv.BlockContext{}, thor.ForkConfig{}).
exec, _ := runtime.New(ctx.chain, ctx.state, &xenv.BlockContext{BaseFee: common.Big0}, thor.ForkConfig{GALACTICA: 0}).
PrepareClause(tx.NewClause(&target), 0, math.MaxUint64, &xenv.TransactionContext{})
out, _, err := exec()
assert.Nil(t, err)
assert.Nil(t, out.VMErr)

assert.True(t, new(big.Int).SetBytes(out.Data).Cmp(big.NewInt(0)) == 0)
assert.Equal(t, uint64(2), math.MaxUint64-out.LeftOverGas)
},
},
Expand All @@ -328,13 +327,13 @@ func TestEVMFunction(t *testing.T) {
abi: "",
methodName: "",
testFunc: func(ctx *context, t *testing.T) {
exec, _ := runtime.New(ctx.chain, ctx.state, &xenv.BlockContext{}, thor.ForkConfig{}).
expectedBaseFee := big.NewInt(100_000)
exec, _ := runtime.New(ctx.chain, ctx.state, &xenv.BlockContext{BaseFee: expectedBaseFee}, thor.ForkConfig{GALACTICA: 0}).
PrepareClause(tx.NewClause(&target), 0, math.MaxUint64, &xenv.TransactionContext{})
out, _, err := exec()
assert.Nil(t, err)
assert.Nil(t, out.VMErr)

assert.True(t, new(big.Int).SetBytes(out.Data).Cmp(big.NewInt(0)) == 0)
assert.True(t, new(big.Int).SetBytes(out.Data).Cmp(expectedBaseFee) == 0)
},
},
{
Expand Down Expand Up @@ -589,15 +588,15 @@ func TestPreForkOpCode(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exec, _ := runtime.New(chain, state, &xenv.BlockContext{}, thor.NoFork).
exec, _ := runtime.New(chain, state, &xenv.BlockContext{BaseFee: common.Big0}, thor.NoFork).
PrepareClause(tx.NewClause(nil).WithData(tt.code), 0, math.MaxUint64, &xenv.TransactionContext{})
out, _, err := exec()
assert.Nil(t, err)
assert.NotNil(t, out.VMErr)
assert.Equal(t, fmt.Sprintf("invalid opcode 0x%x", int(tt.op)), out.VMErr.Error())

// this one applies a fork config that forks from the start
exec, _ = runtime.New(chain, state, &xenv.BlockContext{}, thor.ForkConfig{}).
exec, _ = runtime.New(chain, state, &xenv.BlockContext{BaseFee: common.Big0}, thor.ForkConfig{}).
PrepareClause(tx.NewClause(nil).WithData(tt.code), 0, math.MaxUint64, &xenv.TransactionContext{})
out, _, err = exec()
assert.Nil(t, err)
Expand Down
4 changes: 2 additions & 2 deletions vm/chain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Rules struct {
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
IsByzantium bool
IsIstanbul bool
IsShanghai bool
IsGalactica bool
}

// Rules ensures c's ChainID is not nil.
Expand All @@ -63,6 +63,6 @@ func (c *ChainConfig) Rules(num *big.Int) Rules {
IsEIP158: c.IsEIP158(num),
IsByzantium: c.IsByzantium(num),
IsIstanbul: c.IsIstanbul((num)),
IsShanghai: c.IsShanghai((num)),
IsGalactica: c.IsGalactica((num)),
}
}
14 changes: 7 additions & 7 deletions vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{9}): &blake2F{},
}

// PrecompiledContractsShanghai contains the default set of pre-compiled Ethereum
// PrecompiledContractsGalactica contains the default set of pre-compiled Ethereum
// contracts used in the Shanghai release.
// NOTE: Shanghai release does not introduce any changes in precompiled contracts.
// We are catching up from Istanbul, so Shanghai in thor includes eip1108 and eip2565.
var PrecompiledContractsShanghai = map[common.Address]PrecompiledContract{
var PrecompiledContractsGalactica = map[common.Address]PrecompiledContract{
common.BytesToAddress([]byte{1}): &safeEcrecover{},
common.BytesToAddress([]byte{2}): &sha256hash{},
common.BytesToAddress([]byte{3}): &ripemd160hash{},
Expand All @@ -94,7 +94,7 @@ var PrecompiledContractsShanghai = map[common.Address]PrecompiledContract{
}

var (
PrecompiledAddressesShanghai []common.Address
PrecompiledAddressesGalactica []common.Address
PrecompiledAddressesIstanbul []common.Address
PrecompiledAddressesByzantium []common.Address
PrecompiledAddressesHomestead []common.Address
Expand All @@ -110,16 +110,16 @@ func init() {
for k := range PrecompiledContractsIstanbul {
PrecompiledAddressesIstanbul = append(PrecompiledAddressesIstanbul, k)
}
for k := range PrecompiledContractsShanghai {
PrecompiledAddressesShanghai = append(PrecompiledAddressesShanghai, k)
for k := range PrecompiledContractsGalactica {
PrecompiledAddressesGalactica = append(PrecompiledAddressesGalactica, k)
}
}

// ActivePrecompiles returns the precompiles enabled with the current configuration.
func ActivePrecompiles(rules Rules) []common.Address {
switch {
case rules.IsShanghai:
return PrecompiledAddressesShanghai
case rules.IsGalactica:
return PrecompiledAddressesGalactica
case rules.IsIstanbul:
return PrecompiledAddressesIstanbul
case rules.IsByzantium:
Expand Down
6 changes: 3 additions & 3 deletions vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type (
func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) {
var precompiles map[common.Address]PrecompiledContract
switch {
case evm.chainRules.IsShanghai:
precompiles = PrecompiledContractsShanghai
case evm.chainRules.IsGalactica:
precompiles = PrecompiledContractsGalactica
case evm.chainRules.IsIstanbul:
precompiles = PrecompiledContractsIstanbul
case evm.chainRules.IsByzantium:
Expand Down Expand Up @@ -492,7 +492,7 @@ func (evm *EVM) create(caller ContractRef, code []byte, gas uint64, value *big.I
}

// Reject code starting with 0xEF if EIP-3541 is enabled.
if err == nil && len(ret) >= 1 && ret[0] == 0xEF && evm.chainRules.IsShanghai {
if err == nil && len(ret) >= 1 && ret[0] == 0xEF && evm.chainRules.IsGalactica {
err = ErrInvalidCode
}

Expand Down
2 changes: 1 addition & 1 deletion vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewInterpreter(evm *EVM, cfg Config) *Interpreter {
// we'll set the default jump table.
if cfg.JumpTable == nil {
switch {
case evm.ChainConfig().IsShanghai(evm.BlockNumber):
case evm.ChainConfig().IsGalactica(evm.BlockNumber):
cfg.JumpTable = shanghaiInstructionSet
case evm.ChainConfig().IsIstanbul(evm.BlockNumber):
cfg.JumpTable = istanbulInstructionSet
Expand Down

0 comments on commit a8fe27f

Please sign in to comment.