Skip to content

Commit

Permalink
core: add testnet create and precompile hook (#607)
Browse files Browse the repository at this point in the history
Early day in development, when developing for hardfork, some testnet blocks were
created with development code and not compatible with the current master code.
This commit adds testnet hooks to handle testnet bad blocks, makes the code
compatible with testnet data.
  • Loading branch information
minh-bq authored Oct 22, 2024
1 parent e6d313b commit 2d172f2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
blobSidecarsCache: blobSidecarsCache,
blobPrunePeriod: params.BlobPrunePeriod,
}

if chainConfig.ChainID != nil && chainConfig.ChainID.Cmp(big.NewInt(testnetChainId)) == 0 {
bc.evmHook = TestnetHook{}
}

bc.validator = NewBlockValidator(chainConfig, bc, engine)
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
bc.processor = NewStateProcessor(chainConfig, bc, engine)
Expand Down
107 changes: 107 additions & 0 deletions core/testnet_quirks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package core

import (
"errors"
"sort"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
)

const (
testnetChainId = 2021
)

type blacklistedAddress struct{}

func (c *blacklistedAddress) RequiredGas(input []byte) uint64 {
return params.CallValueTransferGas + params.SloadGas*2
}

func (c *blacklistedAddress) Run(input []byte) ([]byte, error) {
return nil, errors.New("address is blacklisted")
}

type TestnetHook struct{}

func (h TestnetHook) PrecompileHook(evm *vm.EVM, addr common.Address) (bool, vm.PrecompiledContract, bool) {
var (
badBlocks = []int64{
3312683,
3315192,
3315194,
3315196,
3315198,
}
blacklistAddress = common.HexToAddress("0x3a3012ec6812a00811A435c47eD79346318c8C92")
)

blockNumber := evm.Context.BlockNumber.Int64()
index := sort.Search(len(badBlocks), func(i int) bool { return badBlocks[i] >= blockNumber })

if index < len(badBlocks) && badBlocks[index] == blockNumber {
if blacklistAddress == addr {
return true, &blacklistedAddress{}, true
}
}

return false, nil, false
}

func (h TestnetHook) CreateHook(evm *vm.EVM, gas uint64, addr common.Address) (bool, []byte, common.Address, uint64, error) {
var (
badBlocks = []int64{
3281085,
3283414,
3283446,
3283452,
3283454,
3283456,
3283458,
3284174,
3284176,
3284178,
3284180,
3284188,
3284190,
3284275,
3284277,
3284279,
3284281,
3284307,
3284309,
3284311,
3284313,
3284617,
3284619,
3284621,
3284623,
3284752,
3284754,
3284756,
3284758,
3285191,
3285193,
3285195,
3285197,
3285607,
3285609,
3285611,
3285613,
}

blacklistAddress = common.HexToAddress("0x4e59b44847b379578588920ca78fbf26c0b4956c")
)

blockNumber := evm.Context.BlockNumber.Int64()
index := sort.Search(len(badBlocks), func(i int) bool { return badBlocks[i] >= blockNumber })

if index < len(badBlocks) && badBlocks[index] == blockNumber {
if addr == blacklistAddress {
return true, nil, common.Address{}, gas, vm.ErrExecutionReverted
}
}

return false, nil, common.Address{}, 0, nil
}

0 comments on commit 2d172f2

Please sign in to comment.