github.com/theQRL/go-zond@v0.1.1/core/dao_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/theQRL/go-zond/consensus/ethash"
    24  	"github.com/theQRL/go-zond/core/rawdb"
    25  	"github.com/theQRL/go-zond/core/vm"
    26  	"github.com/theQRL/go-zond/params"
    27  )
    28  
    29  // Tests that DAO-fork enabled clients can properly filter out fork-commencing
    30  // blocks based on their extradata fields.
    31  func TestDAOForkRangeExtradata(t *testing.T) {
    32  	forkBlock := big.NewInt(32)
    33  	chainConfig := *params.NonActivatedConfig
    34  	chainConfig.HomesteadBlock = big.NewInt(0)
    35  
    36  	// Generate a common prefix for both pro-forkers and non-forkers
    37  	gspec := &Genesis{
    38  		BaseFee: big.NewInt(params.InitialBaseFee),
    39  		Config:  &chainConfig,
    40  	}
    41  	genDb, prefix, _ := GenerateChainWithGenesis(gspec, ethash.NewFaker(), int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
    42  
    43  	// Create the concurrent, conflicting two nodes
    44  	proDb := rawdb.NewMemoryDatabase()
    45  	proConf := *params.NonActivatedConfig
    46  	proConf.HomesteadBlock = big.NewInt(0)
    47  	proConf.DAOForkBlock = forkBlock
    48  	proConf.DAOForkSupport = true
    49  	progspec := &Genesis{
    50  		BaseFee: big.NewInt(params.InitialBaseFee),
    51  		Config:  &proConf,
    52  	}
    53  	proBc, _ := NewBlockChain(proDb, nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
    54  	defer proBc.Stop()
    55  
    56  	conDb := rawdb.NewMemoryDatabase()
    57  	conConf := *params.NonActivatedConfig
    58  	conConf.HomesteadBlock = big.NewInt(0)
    59  	conConf.DAOForkBlock = forkBlock
    60  	conConf.DAOForkSupport = false
    61  	congspec := &Genesis{
    62  		BaseFee: big.NewInt(params.InitialBaseFee),
    63  		Config:  &conConf,
    64  	}
    65  	conBc, _ := NewBlockChain(conDb, nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
    66  	defer conBc.Stop()
    67  
    68  	if _, err := proBc.InsertChain(prefix); err != nil {
    69  		t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
    70  	}
    71  	if _, err := conBc.InsertChain(prefix); err != nil {
    72  		t.Fatalf("con-fork: failed to import chain prefix: %v", err)
    73  	}
    74  	// Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
    75  	for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
    76  		// Create a pro-fork block, and try to feed into the no-fork chain
    77  		bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
    78  
    79  		blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64()))
    80  		for j := 0; j < len(blocks)/2; j++ {
    81  			blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    82  		}
    83  		if _, err := bc.InsertChain(blocks); err != nil {
    84  			t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
    85  		}
    86  		if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil {
    87  			t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
    88  		}
    89  		bc.Stop()
    90  		blocks, _ = GenerateChain(&proConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
    91  		if _, err := conBc.InsertChain(blocks); err == nil {
    92  			t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
    93  		}
    94  		// Create a proper no-fork block for the contra-forker
    95  		blocks, _ = GenerateChain(&conConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
    96  		if _, err := conBc.InsertChain(blocks); err != nil {
    97  			t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
    98  		}
    99  		// Create a no-fork block, and try to feed into the pro-fork chain
   100  		bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
   101  
   102  		blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64()))
   103  		for j := 0; j < len(blocks)/2; j++ {
   104  			blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   105  		}
   106  		if _, err := bc.InsertChain(blocks); err != nil {
   107  			t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
   108  		}
   109  		if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil {
   110  			t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
   111  		}
   112  		bc.Stop()
   113  		blocks, _ = GenerateChain(&conConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
   114  		if _, err := proBc.InsertChain(blocks); err == nil {
   115  			t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
   116  		}
   117  		// Create a proper pro-fork block for the pro-forker
   118  		blocks, _ = GenerateChain(&proConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
   119  		if _, err := proBc.InsertChain(blocks); err != nil {
   120  			t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
   121  		}
   122  	}
   123  	// Verify that contra-forkers accept pro-fork extra-datas after forking finishes
   124  	bc, _ := NewBlockChain(rawdb.NewMemoryDatabase(), nil, congspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
   125  	defer bc.Stop()
   126  
   127  	blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().Number.Uint64()))
   128  	for j := 0; j < len(blocks)/2; j++ {
   129  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   130  	}
   131  	if _, err := bc.InsertChain(blocks); err != nil {
   132  		t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
   133  	}
   134  	if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil {
   135  		t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
   136  	}
   137  	blocks, _ = GenerateChain(&proConf, conBc.GetBlockByHash(conBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
   138  	if _, err := conBc.InsertChain(blocks); err != nil {
   139  		t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
   140  	}
   141  	// Verify that pro-forkers accept contra-fork extra-datas after forking finishes
   142  	bc, _ = NewBlockChain(rawdb.NewMemoryDatabase(), nil, progspec, nil, ethash.NewFaker(), vm.Config{}, nil, nil)
   143  	defer bc.Stop()
   144  
   145  	blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().Number.Uint64()))
   146  	for j := 0; j < len(blocks)/2; j++ {
   147  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   148  	}
   149  	if _, err := bc.InsertChain(blocks); err != nil {
   150  		t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
   151  	}
   152  	if err := bc.triedb.Commit(bc.CurrentHeader().Root, false); err != nil {
   153  		t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
   154  	}
   155  	blocks, _ = GenerateChain(&conConf, proBc.GetBlockByHash(proBc.CurrentBlock().Hash()), ethash.NewFaker(), genDb, 1, func(i int, gen *BlockGen) {})
   156  	if _, err := proBc.InsertChain(blocks); err != nil {
   157  		t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
   158  	}
   159  }