Skip to content

Commit b8c0883

Browse files
committed
[release/1.4.10] accounts, core, eth: pass chain config for chain maker to test DAO
(cherry picked from commit 3291235)
1 parent 14bad7e commit b8c0883

17 files changed

+182
-133
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (b *SimulatedBackend) Commit() {
7272

7373
// Rollback aborts all pending transactions, reverting to the last committed state.
7474
func (b *SimulatedBackend) Rollback() {
75-
blocks, _ := core.GenerateChain(b.blockchain.CurrentBlock(), b.database, 1, func(int, *core.BlockGen) {})
75+
blocks, _ := core.GenerateChain(nil, b.blockchain.CurrentBlock(), b.database, 1, func(int, *core.BlockGen) {})
7676

7777
b.pendingBlock = blocks[0]
7878
b.pendingState, _ = state.New(b.pendingBlock.Root(), b.database)
@@ -178,7 +178,7 @@ func (b *SimulatedBackend) EstimateGasLimit(sender common.Address, contract *com
178178
// SendTransaction implements ContractTransactor.SendTransaction, delegating the raw
179179
// transaction injection to the remote node.
180180
func (b *SimulatedBackend) SendTransaction(tx *types.Transaction) error {
181-
blocks, _ := core.GenerateChain(b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
181+
blocks, _ := core.GenerateChain(nil, b.blockchain.CurrentBlock(), b.database, 1, func(number int, block *core.BlockGen) {
182182
for _, tx := range b.pendingBlock.Transactions() {
183183
block.AddTx(tx)
184184
}

core/bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
163163
// Generate a chain of b.N blocks using the supplied block
164164
// generator function.
165165
genesis := WriteGenesisBlockForTesting(db, GenesisAccount{benchRootAddr, benchRootFunds})
166-
chain, _ := GenerateChain(genesis, db, b.N, gen)
166+
chain, _ := GenerateChain(nil, genesis, db, b.N, gen)
167167

168168
// Time the insertion of the new chain.
169169
// State and blocks are stored in the same DB.

core/block_validator_test.go

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ethereum/go-ethereum/core/vm"
2828
"github.com/ethereum/go-ethereum/ethdb"
2929
"github.com/ethereum/go-ethereum/event"
30-
"github.com/ethereum/go-ethereum/params"
3130
"github.com/ethereum/go-ethereum/pow/ezp"
3231
)
3332

@@ -93,107 +92,3 @@ func TestPutReceipt(t *testing.T) {
9392
t.Error("expected to get 1 receipt, got none.")
9493
}
9594
}
96-
97-
// Tests that DAO-fork enabled clients can properly filter out fork-commencing
98-
// blocks based on their extradata fields.
99-
func TestDAOForkRangeExtradata(t *testing.T) {
100-
forkBlock := big.NewInt(32)
101-
102-
// Generate a common prefix for both pro-forkers and non-forkers
103-
db, _ := ethdb.NewMemDatabase()
104-
genesis := WriteGenesisBlockForTesting(db)
105-
prefix, _ := GenerateChain(genesis, db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
106-
107-
// Create the concurrent, conflicting two nodes
108-
proDb, _ := ethdb.NewMemDatabase()
109-
WriteGenesisBlockForTesting(proDb)
110-
proBc, _ := NewBlockChain(proDb, &ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: true}, new(FakePow), new(event.TypeMux))
111-
112-
conDb, _ := ethdb.NewMemDatabase()
113-
WriteGenesisBlockForTesting(conDb)
114-
conBc, _ := NewBlockChain(conDb, &ChainConfig{HomesteadBlock: big.NewInt(0), DAOForkBlock: forkBlock, DAOForkSupport: false}, new(FakePow), new(event.TypeMux))
115-
116-
if _, err := proBc.InsertChain(prefix); err != nil {
117-
t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
118-
}
119-
if _, err := conBc.InsertChain(prefix); err != nil {
120-
t.Fatalf("con-fork: failed to import chain prefix: %v", err)
121-
}
122-
// Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
123-
for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
124-
// Create a pro-fork block, and try to feed into the no-fork chain
125-
db, _ = ethdb.NewMemDatabase()
126-
WriteGenesisBlockForTesting(db)
127-
bc, _ := NewBlockChain(db, &ChainConfig{HomesteadBlock: big.NewInt(0)}, new(FakePow), new(event.TypeMux))
128-
129-
blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
130-
for j := 0; j < len(blocks)/2; j++ {
131-
blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
132-
}
133-
if _, err := bc.InsertChain(blocks); err != nil {
134-
t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
135-
}
136-
blocks, _ = GenerateChain(conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) { gen.SetExtra(params.DAOForkBlockExtra) })
137-
if _, err := conBc.InsertChain(blocks); err == nil {
138-
t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
139-
}
140-
// Create a proper no-fork block for the contra-forker
141-
blocks, _ = GenerateChain(conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
142-
if _, err := conBc.InsertChain(blocks); err != nil {
143-
t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
144-
}
145-
// Create a no-fork block, and try to feed into the pro-fork chain
146-
db, _ = ethdb.NewMemDatabase()
147-
WriteGenesisBlockForTesting(db)
148-
bc, _ = NewBlockChain(db, &ChainConfig{HomesteadBlock: big.NewInt(0)}, new(FakePow), new(event.TypeMux))
149-
150-
blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
151-
for j := 0; j < len(blocks)/2; j++ {
152-
blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
153-
}
154-
if _, err := bc.InsertChain(blocks); err != nil {
155-
t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
156-
}
157-
blocks, _ = GenerateChain(proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
158-
if _, err := proBc.InsertChain(blocks); err == nil {
159-
t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
160-
}
161-
// Create a proper pro-fork block for the pro-forker
162-
blocks, _ = GenerateChain(proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) { gen.SetExtra(params.DAOForkBlockExtra) })
163-
if _, err := proBc.InsertChain(blocks); err != nil {
164-
t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
165-
}
166-
}
167-
// Verify that contra-forkers accept pro-fork extra-datas after forking finishes
168-
db, _ = ethdb.NewMemDatabase()
169-
WriteGenesisBlockForTesting(db)
170-
bc, _ := NewBlockChain(db, &ChainConfig{HomesteadBlock: big.NewInt(0)}, new(FakePow), new(event.TypeMux))
171-
172-
blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()+1))
173-
for j := 0; j < len(blocks)/2; j++ {
174-
blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
175-
}
176-
if _, err := bc.InsertChain(blocks); err != nil {
177-
t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
178-
}
179-
blocks, _ = GenerateChain(conBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) { gen.SetExtra(params.DAOForkBlockExtra) })
180-
if _, err := conBc.InsertChain(blocks); err != nil {
181-
t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
182-
}
183-
// Verify that pro-forkers accept contra-fork extra-datas after forking finishes
184-
db, _ = ethdb.NewMemDatabase()
185-
WriteGenesisBlockForTesting(db)
186-
bc, _ = NewBlockChain(db, &ChainConfig{HomesteadBlock: big.NewInt(0)}, new(FakePow), new(event.TypeMux))
187-
188-
blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()+1))
189-
for j := 0; j < len(blocks)/2; j++ {
190-
blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
191-
}
192-
if _, err := bc.InsertChain(blocks); err != nil {
193-
t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
194-
}
195-
blocks, _ = GenerateChain(proBc.CurrentBlock(), db, 1, func(i int, gen *BlockGen) {})
196-
if _, err := proBc.InsertChain(blocks); err != nil {
197-
t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
198-
}
199-
}

core/blockchain_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ func TestFastVsFullChains(t *testing.T) {
712712
funds = big.NewInt(1000000000)
713713
genesis = GenesisBlockForTesting(gendb, address, funds)
714714
)
715-
blocks, receipts := GenerateChain(genesis, gendb, 1024, func(i int, block *BlockGen) {
715+
blocks, receipts := GenerateChain(nil, genesis, gendb, 1024, func(i int, block *BlockGen) {
716716
block.SetCoinbase(common.Address{0x00})
717717

718718
// If the block number is multiple of 3, send a few bonus transactions to the miner
@@ -795,7 +795,7 @@ func TestLightVsFastVsFullChainHeads(t *testing.T) {
795795
genesis = GenesisBlockForTesting(gendb, address, funds)
796796
)
797797
height := uint64(1024)
798-
blocks, receipts := GenerateChain(genesis, gendb, int(height), nil)
798+
blocks, receipts := GenerateChain(nil, genesis, gendb, int(height), nil)
799799

800800
// Configure a subchain to roll back
801801
remove := []common.Hash{}
@@ -895,7 +895,7 @@ func TestChainTxReorgs(t *testing.T) {
895895
// - futureAdd: transaction added after the reorg has already finished
896896
var pastAdd, freshAdd, futureAdd *types.Transaction
897897

898-
chain, _ := GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {
898+
chain, _ := GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {
899899
switch i {
900900
case 0:
901901
pastDrop, _ = types.NewTransaction(gen.TxNonce(addr2), addr2, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key2)
@@ -920,7 +920,7 @@ func TestChainTxReorgs(t *testing.T) {
920920
}
921921

922922
// overwrite the old chain
923-
chain, _ = GenerateChain(genesis, db, 5, func(i int, gen *BlockGen) {
923+
chain, _ = GenerateChain(nil, genesis, db, 5, func(i int, gen *BlockGen) {
924924
switch i {
925925
case 0:
926926
pastAdd, _ = types.NewTransaction(gen.TxNonce(addr3), addr3, big.NewInt(1000), params.TxGas, nil, nil).SignECDSA(key3)
@@ -990,7 +990,7 @@ func TestLogReorgs(t *testing.T) {
990990
blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
991991

992992
subs := evmux.Subscribe(RemovedLogsEvent{})
993-
chain, _ := GenerateChain(genesis, db, 2, func(i int, gen *BlockGen) {
993+
chain, _ := GenerateChain(nil, genesis, db, 2, func(i int, gen *BlockGen) {
994994
if i == 1 {
995995
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), code).SignECDSA(key1)
996996
if err != nil {
@@ -1003,7 +1003,7 @@ func TestLogReorgs(t *testing.T) {
10031003
t.Fatalf("failed to insert chain: %v", err)
10041004
}
10051005

1006-
chain, _ = GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {})
1006+
chain, _ = GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {})
10071007
if _, err := blockchain.InsertChain(chain); err != nil {
10081008
t.Fatalf("failed to insert forked chain: %v", err)
10091009
}
@@ -1025,12 +1025,12 @@ func TestReorgSideEvent(t *testing.T) {
10251025
evmux := &event.TypeMux{}
10261026
blockchain, _ := NewBlockChain(db, testChainConfig(), FakePow{}, evmux)
10271027

1028-
chain, _ := GenerateChain(genesis, db, 3, func(i int, gen *BlockGen) {})
1028+
chain, _ := GenerateChain(nil, genesis, db, 3, func(i int, gen *BlockGen) {})
10291029
if _, err := blockchain.InsertChain(chain); err != nil {
10301030
t.Fatalf("failed to insert chain: %v", err)
10311031
}
10321032

1033-
replacementBlocks, _ := GenerateChain(genesis, db, 4, func(i int, gen *BlockGen) {
1033+
replacementBlocks, _ := GenerateChain(nil, genesis, db, 4, func(i int, gen *BlockGen) {
10341034
tx, err := types.NewContractCreation(gen.TxNonce(addr1), new(big.Int), big.NewInt(1000000), new(big.Int), nil).SignECDSA(key1)
10351035
if i == 2 {
10361036
gen.OffsetTime(-1)

core/chain_makers.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/core/vm"
2727
"github.com/ethereum/go-ethereum/ethdb"
2828
"github.com/ethereum/go-ethereum/event"
29+
"github.com/ethereum/go-ethereum/params"
2930
"github.com/ethereum/go-ethereum/pow"
3031
)
3132

@@ -35,7 +36,11 @@ import (
3536

3637
// MakeChainConfig returns a new ChainConfig with the ethereum default chain settings.
3738
func MakeChainConfig() *ChainConfig {
38-
return &ChainConfig{HomesteadBlock: big.NewInt(0)}
39+
return &ChainConfig{
40+
HomesteadBlock: big.NewInt(0),
41+
DAOForkBlock: nil,
42+
DAOForkSupport: true,
43+
}
3944
}
4045

4146
// FakePow is a non-validating proof of work implementation.
@@ -173,10 +178,27 @@ func (b *BlockGen) OffsetTime(seconds int64) {
173178
// Blocks created by GenerateChain do not contain valid proof of work
174179
// values. Inserting them into BlockChain requires use of FakePow or
175180
// a similar non-validating proof of work implementation.
176-
func GenerateChain(parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
181+
func GenerateChain(config *ChainConfig, parent *types.Block, db ethdb.Database, n int, gen func(int, *BlockGen)) ([]*types.Block, []types.Receipts) {
177182
blocks, receipts := make(types.Blocks, n), make([]types.Receipts, n)
178183
genblock := func(i int, h *types.Header, statedb *state.StateDB) (*types.Block, types.Receipts) {
179184
b := &BlockGen{parent: parent, i: i, chain: blocks, header: h, statedb: statedb}
185+
186+
// Mutate the state and block according to any hard-fork specs
187+
if config == nil {
188+
config = MakeChainConfig()
189+
}
190+
if daoBlock := config.DAOForkBlock; daoBlock != nil {
191+
limit := new(big.Int).Add(daoBlock, params.DAOForkExtraRange)
192+
if h.Number.Cmp(daoBlock) >= 0 && h.Number.Cmp(limit) < 0 {
193+
if config.DAOForkSupport {
194+
h.Extra = common.CopyBytes(params.DAOForkBlockExtra)
195+
}
196+
}
197+
}
198+
if config.DAOForkSupport && config.DAOForkBlock != nil && config.DAOForkBlock.Cmp(h.Number) == 0 {
199+
ApplyDAOHardFork(statedb)
200+
}
201+
// Execute any user modifications to the block and finalize it
180202
if gen != nil {
181203
gen(i, b)
182204
}
@@ -261,7 +283,7 @@ func makeHeaderChain(parent *types.Header, n int, db ethdb.Database, seed int) [
261283

262284
// makeBlockChain creates a deterministic chain of blocks rooted at parent.
263285
func makeBlockChain(parent *types.Block, n int, db ethdb.Database, seed int) []*types.Block {
264-
blocks, _ := GenerateChain(parent, db, n, func(i int, b *BlockGen) {
286+
blocks, _ := GenerateChain(nil, parent, db, n, func(i int, b *BlockGen) {
265287
b.SetCoinbase(common.Address{0: byte(seed), 19: byte(i)})
266288
})
267289
return blocks

core/chain_makers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func ExampleGenerateChain() {
4747
// This call generates a chain of 5 blocks. The function runs for
4848
// each block and adds different features to gen based on the
4949
// block index.
50-
chain, _ := GenerateChain(genesis, db, 5, func(i int, gen *BlockGen) {
50+
chain, _ := GenerateChain(nil, genesis, db, 5, func(i int, gen *BlockGen) {
5151
switch i {
5252
case 0:
5353
// In block 1, addr1 sends addr2 some ether.

core/chain_pow_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestPowVerification(t *testing.T) {
6060
var (
6161
testdb, _ = ethdb.NewMemDatabase()
6262
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
63-
blocks, _ = GenerateChain(genesis, testdb, 8, nil)
63+
blocks, _ = GenerateChain(nil, genesis, testdb, 8, nil)
6464
)
6565
headers := make([]*types.Header, len(blocks))
6666
for i, block := range blocks {
@@ -115,7 +115,7 @@ func testPowConcurrentVerification(t *testing.T, threads int) {
115115
var (
116116
testdb, _ = ethdb.NewMemDatabase()
117117
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
118-
blocks, _ = GenerateChain(genesis, testdb, 8, nil)
118+
blocks, _ = GenerateChain(nil, genesis, testdb, 8, nil)
119119
)
120120
headers := make([]*types.Header, len(blocks))
121121
for i, block := range blocks {
@@ -186,7 +186,7 @@ func testPowConcurrentAbortion(t *testing.T, threads int) {
186186
var (
187187
testdb, _ = ethdb.NewMemDatabase()
188188
genesis = GenesisBlockForTesting(testdb, common.Address{}, new(big.Int))
189-
blocks, _ = GenerateChain(genesis, testdb, 1024, nil)
189+
blocks, _ = GenerateChain(nil, genesis, testdb, 1024, nil)
190190
)
191191
headers := make([]*types.Header, len(blocks))
192192
for i, block := range blocks {

0 commit comments

Comments
 (0)