Skip to content

Commit

Permalink
Merge pull request #92 from openrelayxyz/feature/etc-plugin
Browse files Browse the repository at this point in the history
Feature/etc plugin
  • Loading branch information
AusIV authored Oct 30, 2023
2 parents 9aec954 + 4e93654 commit 210c889
Show file tree
Hide file tree
Showing 23 changed files with 1,568 additions and 301 deletions.
50 changes: 48 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,13 @@ var (
// if none (or the empty string) is specified. If the node is starting a testnet,
// then a subdirectory of the specified datadir will be used.
func MakeDataDir(ctx *cli.Context) string {
if path := ctx.String(DataDirFlag.Name); path != "" {
if path := ctx.String(DataDirFlag.Name); path == "" {
// begin PluGeth injection
if pluginPath := pluginDefaultDataDir(path); pluginPath != "" {
log.Error("Inside datdir injection number one")
return pluginPath
}
// end PluGeth injection
if ctx.Bool(GoerliFlag.Name) {
return filepath.Join(path, "goerli")
}
Expand Down Expand Up @@ -1038,7 +1044,12 @@ func setNodeUserIdent(ctx *cli.Context, cfg *node.Config) {
// 4. default to mainnet nodes
func setBootstrapNodes(ctx *cli.Context, cfg *p2p.Config) {
urls := params.MainnetBootnodes
if ctx.IsSet(BootnodesFlag.Name) {
// begin PluGeth injection
if pluginUrls := pluginSetBootstrapNodes(); pluginUrls != nil {
urls = pluginUrls
}
// end PluGeth injection
if ctx.IsSet(BootnodesFlag.Name) {
urls = SplitAndTrim(ctx.String(BootnodesFlag.Name))
} else {
if cfg.BootstrapNodes != nil {
Expand Down Expand Up @@ -1490,7 +1501,13 @@ func setSmartCard(ctx *cli.Context, cfg *node.Config) {
}

func SetDataDir(ctx *cli.Context, cfg *node.Config) {
// begin PluGeth injection
pluginPath := pluginDefaultDataDir(node.DefaultDataDir())
switch {
case pluginPath != "" && ctx.String(DataDirFlag.Name) == node.DefaultDataDir():
log.Error("Inside datdir injection number two")
cfg.DataDir = pluginPath
// end PluGeth injection
case ctx.IsSet(DataDirFlag.Name):
cfg.DataDir = ctx.String(DataDirFlag.Name)
case ctx.Bool(DeveloperFlag.Name):
Expand Down Expand Up @@ -1669,6 +1686,20 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
setRequiredBlocks(ctx, cfg)
setLes(ctx, cfg)

// beginPluGethInjection
if pluginNetworkId := pluginNetworkId(); pluginNetworkId != nil {
cfg.NetworkId = *pluginNetworkId
}
if cfg.EthDiscoveryURLs == nil {
var lightMode bool
if cfg.SyncMode == downloader.LightSync {
lightMode = true
}
cfg.EthDiscoveryURLs = pluginETHDiscoveryURLs(lightMode)
cfg.SnapDiscoveryURLs = pluginSnapDiscoveryURLs()
}
//end PluGeth injection

// Cap the cache allowance and tune the garbage collector
mem, err := gopsutil.VirtualMemory()
if err == nil {
Expand Down Expand Up @@ -1889,6 +1920,16 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
SetDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
}
}

//begin plugeth injection
if genesis := pluginGenesisBlock(); genesis != nil {
chaindb := MakeChainDatabase(ctx, stack, false)
cfg.Genesis = genesis
rawdb.WriteChainConfig(chaindb, genesis.ToBlock().Hash(), genesis.Config)
chaindb.Close()
}
//end plugeth injection

// Set any dangling config values
if ctx.String(CryptoKZGFlag.Name) != "gokzg" && ctx.String(CryptoKZGFlag.Name) != "ckzg" {
Fatalf("--%s flag must be 'gokzg' or 'ckzg'", CryptoKZGFlag.Name)
Expand Down Expand Up @@ -2141,6 +2182,11 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
case ctx.Bool(DeveloperFlag.Name):
Fatalf("Developer chains are ephemeral")
}
//begin plugeth injection
if genesis == nil {
genesis = pluginGenesisBlock()
}
//end plugeth injection
return genesis
}

Expand Down
139 changes: 139 additions & 0 deletions cmd/utils/plugin_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package utils

import (
"encoding/json"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
)

func DefaultDataDir(pl *plugins.PluginLoader, path string) string {
dataDirPath := ""
fnList := pl.Lookup("SetDefaultDataDir", func(item interface{}) bool {
_, ok := item.(func(string) string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(string) string); ok {
dataDirPath = fn(path)
}
}
return dataDirPath
}

func pluginDefaultDataDir(path string) string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting DefaultDataDir, but default PluginLoader has not been initialized")
return ""
}
return DefaultDataDir(plugins.DefaultPluginLoader, path)
}

func PluginSetBootStrapNodes(pl *plugins.PluginLoader) []string {
var urls []string
fnList := pl.Lookup("SetBootstrapNodes", func(item interface{}) bool {
_, ok := item.(func() []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() []string); ok {
urls = fn()
}
}
return urls
}

func pluginSetBootstrapNodes() []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginSetBootStrapNodes, but default PluginLoader has not been initialized")
return nil
}
return PluginSetBootStrapNodes(plugins.DefaultPluginLoader)
}

func PluginNetworkId(pl *plugins.PluginLoader) *uint64 {
var networkId *uint64
fnList := pl.Lookup("SetNetworkId", func(item interface{}) bool {
_, ok := item.(func() *uint64)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() *uint64); ok {
networkId = fn()
}
}
return networkId
}

func pluginNetworkId() *uint64 {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginNetworkID, but default PluginLoader has not been initialized")
return nil
}
return PluginNetworkId(plugins.DefaultPluginLoader)
}

func PluginETHDiscoveryURLs(pl *plugins.PluginLoader, mode bool) []string {
var ethDiscoveryURLs []string
fnList := pl.Lookup("SetETHDiscoveryURLs", func(item interface{}) bool {
_, ok := item.(func(bool) []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func(bool) []string); ok {
ethDiscoveryURLs = fn(mode)
}
}
return ethDiscoveryURLs
}

func pluginETHDiscoveryURLs(mode bool) []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting pluginETHDiscoveryURLs, but default PluginLoader has not been initialized")
return nil
}
return PluginETHDiscoveryURLs(plugins.DefaultPluginLoader, mode)
}

func PluginSnapDiscoveryURLs(pl *plugins.PluginLoader) []string {
var snapDiscoveryURLs []string
fnList := pl.Lookup("SetSnapDiscoveryURLs", func(item interface{}) bool {
_, ok := item.(func() []string)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() []string); ok {
snapDiscoveryURLs = fn()
}
}
return snapDiscoveryURLs
}

func pluginSnapDiscoveryURLs() []string {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginSnapDiscoveryURLs, but default PluginLoader has not been initialized")
return nil
}
return PluginSnapDiscoveryURLs(plugins.DefaultPluginLoader)
}

func PluginGenesisBlock(pl *plugins.PluginLoader) *core.Genesis {
genesisJSON, ok := plugins.LookupOne[func() []byte](pl, "GenesisBlock")
if !ok {
return nil
}
var genesis core.Genesis
if err := json.Unmarshal(genesisJSON(), &genesis); err != nil {
log.Warn("Error unmarshalling genesis", "err", err)
return nil
}
return &genesis
}

func pluginGenesisBlock() *core.Genesis {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginGenesisBlock, but default PluginLoader has not been initialized")
return nil
}
return PluginGenesisBlock(plugins.DefaultPluginLoader)
}
8 changes: 6 additions & 2 deletions consensus/misc/eip1559/eip1559.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import (
func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Header) error {
// Verify that the gas limit remains within allowed bounds
parentGasLimit := parent.GasLimit
if !config.IsLondon(parent.Number) {
// begin PluGeth injection
if !config.Is1559(parent.Number) {
// end PluGeth injection
parentGasLimit = parent.GasLimit * config.ElasticityMultiplier()
}
if err := misc.VerifyGaslimit(parentGasLimit, header.GasLimit); err != nil {
Expand All @@ -56,7 +58,9 @@ func VerifyEIP1559Header(config *params.ChainConfig, parent, header *types.Heade
// CalcBaseFee calculates the basefee of the header.
func CalcBaseFee(config *params.ChainConfig, parent *types.Header) *big.Int {
// If the current block is the first EIP-1559 block, return the InitialBaseFee.
if !config.IsLondon(parent.Number) {
// begin PluGeth injection
if !config.Is1559(parent.Number) {
// end PluGeth injection
return new(big.Int).SetUint64(params.InitialBaseFee)
}

Expand Down
6 changes: 6 additions & 0 deletions core/forkid/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func checksumToBytes(hash uint32) [4]byte {
// gatherForks gathers all the known forks and creates two sorted lists out of
// them, one for the block number based forks and the second for the timestamps.
func gatherForks(config *params.ChainConfig, genesis uint64) ([]uint64, []uint64) {

// Gather all the fork block numbers via reflection
kind := reflect.TypeOf(params.ChainConfig{})
conf := reflect.ValueOf(config).Elem()
Expand Down Expand Up @@ -293,5 +294,10 @@ func gatherForks(config *params.ChainConfig, genesis uint64) ([]uint64, []uint64
for len(forksByTime) > 0 && forksByTime[0] <= genesis {
forksByTime = forksByTime[1:]
}
// begin PluGeth injection
if byBlock, byTime, ok := pluginForkIDs(forksByBlock, forksByTime); ok {
return byBlock, byTime
}
// end PluGeth injection
return forksByBlock, forksByTime
}
25 changes: 25 additions & 0 deletions core/forkid/plugin_hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package forkid

import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
)

func PluginForkIDs(pl *plugins.PluginLoader, byBlock, byTime []uint64) ([]uint64, []uint64, bool) {
f, ok := plugins.LookupOne[func([]uint64, []uint64) ([]uint64, []uint64)](pl, "ForkIDs")
if !ok {
return nil, nil, false
}
pluginByBlock, pluginByTime := f(byBlock, byTime)

return pluginByBlock, pluginByTime, ok

}

func pluginForkIDs(byBlock, byTime []uint64) ([]uint64, []uint64, bool) {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginForkIDs, but default PluginLoader has not been initialized")
return nil, nil, false
}
return PluginForkIDs(plugins.DefaultPluginLoader, byBlock, byTime)
}
10 changes: 8 additions & 2 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ func (st *StateTransition) preCheck() error {
}

// Make sure that transaction gasFeeCap is greater than the baseFee (post london)
if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) {
// begin PluGeth injection
if st.evm.ChainConfig().Is1559(st.evm.Context.BlockNumber) {
// end PluGeth injection
// Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call)
if !st.evm.Config.NoBaseFee || msg.GasFeeCap.BitLen() > 0 || msg.GasTipCap.BitLen() > 0 {
if l := msg.GasFeeCap.BitLen(); l > 256 {
Expand Down Expand Up @@ -432,7 +434,11 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
}
effectiveTip := msg.GasPrice
if rules.IsLondon {
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
// begin PluGeth injection
if st.evm.ChainConfig().Is1559(st.evm.Context.BlockNumber) {
// end PluGeth injection
effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee))
}
}

if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 {
Expand Down
9 changes: 8 additions & 1 deletion core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter {
table = &constantinopleInstructionSet
case evm.chainRules.IsByzantium:
table = &byzantiumInstructionSet
case evm.chainRules.IsEIP158:
// begin PluGeth injection
case evm.chainRules.IsEIP160:
// end PluGeth injection
table = &spuriousDragonInstructionSet
case evm.chainRules.IsEIP150:
table = &tangerineWhistleInstructionSet
Expand All @@ -95,6 +97,11 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter {
}
}
evm.Config.ExtraEips = extraEips
// begin PluGeth injection
if pluginTable := pluginOpCodeSelect(table); pluginTable != nil {
table = pluginTable
}
// end PluGeth injection
return &EVMInterpreter{evm: evm, table: table}
}

Expand Down
4 changes: 3 additions & 1 deletion core/vm/jump_table_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func LookupInstructionSet(rules params.Rules) (JumpTable, error) {
return newConstantinopleInstructionSet(), nil
case rules.IsByzantium:
return newByzantiumInstructionSet(), nil
case rules.IsEIP158:
// Begin plugeth injection
case rules.IsEIP160:
// End plugeth injection
return newSpuriousDragonInstructionSet(), nil
case rules.IsEIP150:
return newTangerineWhistleInstructionSet(), nil
Expand Down
34 changes: 34 additions & 0 deletions core/vm/plugin_hooks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
package vm

import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/plugins"
)

func (st *Stack) Len() int {
return len(st.data)
}

func PluginOpCodeSelect(pl *plugins.PluginLoader, jt *JumpTable) *JumpTable {
var opCodes []int
fnList := pl.Lookup("OpCodeSelect", func(item interface{}) bool {
_, ok := item.(func() []int)
return ok
})
for _, fni := range fnList {
if fn, ok := fni.(func() []int); ok {
opCodes = append(opCodes, fn()...)
}
}
if len(opCodes) > 0 {
jt = copyJumpTable(jt)
}
for _, idx := range opCodes {
(*jt)[idx] = &operation{execute: opUndefined, maxStack: maxStack(0, 0)}
}
return jt
}

func pluginOpCodeSelect(jt *JumpTable) *JumpTable {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting PluginOpCodeSelect, but default PluginLoader has not been initialized")
return nil
}
return PluginOpCodeSelect(plugins.DefaultPluginLoader, jt)
}

Loading

0 comments on commit 210c889

Please sign in to comment.