Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit 2d172f2

Browse files
authored
core: add testnet create and precompile hook (#607)
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.
1 parent e6d313b commit 2d172f2

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

core/blockchain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
288288
blobSidecarsCache: blobSidecarsCache,
289289
blobPrunePeriod: params.BlobPrunePeriod,
290290
}
291+
292+
if chainConfig.ChainID != nil && chainConfig.ChainID.Cmp(big.NewInt(testnetChainId)) == 0 {
293+
bc.evmHook = TestnetHook{}
294+
}
295+
291296
bc.validator = NewBlockValidator(chainConfig, bc, engine)
292297
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine)
293298
bc.processor = NewStateProcessor(chainConfig, bc, engine)

core/testnet_quirks.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package core
2+
3+
import (
4+
"errors"
5+
"sort"
6+
7+
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/core/vm"
9+
"github.com/ethereum/go-ethereum/params"
10+
)
11+
12+
const (
13+
testnetChainId = 2021
14+
)
15+
16+
type blacklistedAddress struct{}
17+
18+
func (c *blacklistedAddress) RequiredGas(input []byte) uint64 {
19+
return params.CallValueTransferGas + params.SloadGas*2
20+
}
21+
22+
func (c *blacklistedAddress) Run(input []byte) ([]byte, error) {
23+
return nil, errors.New("address is blacklisted")
24+
}
25+
26+
type TestnetHook struct{}
27+
28+
func (h TestnetHook) PrecompileHook(evm *vm.EVM, addr common.Address) (bool, vm.PrecompiledContract, bool) {
29+
var (
30+
badBlocks = []int64{
31+
3312683,
32+
3315192,
33+
3315194,
34+
3315196,
35+
3315198,
36+
}
37+
blacklistAddress = common.HexToAddress("0x3a3012ec6812a00811A435c47eD79346318c8C92")
38+
)
39+
40+
blockNumber := evm.Context.BlockNumber.Int64()
41+
index := sort.Search(len(badBlocks), func(i int) bool { return badBlocks[i] >= blockNumber })
42+
43+
if index < len(badBlocks) && badBlocks[index] == blockNumber {
44+
if blacklistAddress == addr {
45+
return true, &blacklistedAddress{}, true
46+
}
47+
}
48+
49+
return false, nil, false
50+
}
51+
52+
func (h TestnetHook) CreateHook(evm *vm.EVM, gas uint64, addr common.Address) (bool, []byte, common.Address, uint64, error) {
53+
var (
54+
badBlocks = []int64{
55+
3281085,
56+
3283414,
57+
3283446,
58+
3283452,
59+
3283454,
60+
3283456,
61+
3283458,
62+
3284174,
63+
3284176,
64+
3284178,
65+
3284180,
66+
3284188,
67+
3284190,
68+
3284275,
69+
3284277,
70+
3284279,
71+
3284281,
72+
3284307,
73+
3284309,
74+
3284311,
75+
3284313,
76+
3284617,
77+
3284619,
78+
3284621,
79+
3284623,
80+
3284752,
81+
3284754,
82+
3284756,
83+
3284758,
84+
3285191,
85+
3285193,
86+
3285195,
87+
3285197,
88+
3285607,
89+
3285609,
90+
3285611,
91+
3285613,
92+
}
93+
94+
blacklistAddress = common.HexToAddress("0x4e59b44847b379578588920ca78fbf26c0b4956c")
95+
)
96+
97+
blockNumber := evm.Context.BlockNumber.Int64()
98+
index := sort.Search(len(badBlocks), func(i int) bool { return badBlocks[i] >= blockNumber })
99+
100+
if index < len(badBlocks) && badBlocks[index] == blockNumber {
101+
if addr == blacklistAddress {
102+
return true, nil, common.Address{}, gas, vm.ErrExecutionReverted
103+
}
104+
}
105+
106+
return false, nil, common.Address{}, 0, nil
107+
}

0 commit comments

Comments
 (0)