github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/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/TTCECO/gttc/consensus/ethash"
    24  	"github.com/TTCECO/gttc/core/vm"
    25  	"github.com/TTCECO/gttc/ethdb"
    26  	"github.com/TTCECO/gttc/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  
    34  	// Generate a common prefix for both pro-forkers and non-forkers
    35  	db := ethdb.NewMemDatabase()
    36  	gspec := new(Genesis)
    37  	genesis := gspec.MustCommit(db)
    38  	prefix, _ := GenerateChain(params.TestChainConfig, genesis, ethash.NewFaker(), db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
    39  
    40  	// Create the concurrent, conflicting two nodes
    41  	proDb := ethdb.NewMemDatabase()
    42  	gspec.MustCommit(proDb)
    43  
    44  	proConf := *params.TestChainConfig
    45  
    46  	proBc, _ := NewBlockChain(proDb, nil, &proConf, ethash.NewFaker(), vm.Config{})
    47  	defer proBc.Stop()
    48  
    49  	conDb := ethdb.NewMemDatabase()
    50  	gspec.MustCommit(conDb)
    51  
    52  	conConf := *params.TestChainConfig
    53  
    54  	conBc, _ := NewBlockChain(conDb, nil, &conConf, ethash.NewFaker(), vm.Config{})
    55  	defer conBc.Stop()
    56  
    57  	if _, err := proBc.InsertChain(prefix); err != nil {
    58  		t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
    59  	}
    60  	if _, err := conBc.InsertChain(prefix); err != nil {
    61  		t.Fatalf("con-fork: failed to import chain prefix: %v", err)
    62  	}
    63  
    64  	// Verify that contra-forkers accept pro-fork extra-datas after forking finishes
    65  	db = ethdb.NewMemDatabase()
    66  	gspec.MustCommit(db)
    67  	bc, _ := NewBlockChain(db, nil, &conConf, ethash.NewFaker(), vm.Config{})
    68  	defer bc.Stop()
    69  
    70  	blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
    71  	for j := 0; j < len(blocks)/2; j++ {
    72  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    73  	}
    74  	if _, err := bc.InsertChain(blocks); err != nil {
    75  		t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
    76  	}
    77  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
    78  		t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
    79  	}
    80  	blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
    81  	if _, err := conBc.InsertChain(blocks); err != nil {
    82  		t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
    83  	}
    84  	// Verify that pro-forkers accept contra-fork extra-datas after forking finishes
    85  	db = ethdb.NewMemDatabase()
    86  	gspec.MustCommit(db)
    87  	bc, _ = NewBlockChain(db, nil, &proConf, ethash.NewFaker(), vm.Config{})
    88  	defer bc.Stop()
    89  
    90  	blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
    91  	for j := 0; j < len(blocks)/2; j++ {
    92  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    93  	}
    94  	if _, err := bc.InsertChain(blocks); err != nil {
    95  		t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
    96  	}
    97  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
    98  		t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
    99  	}
   100  	blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
   101  	if _, err := proBc.InsertChain(blocks); err != nil {
   102  		t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
   103  	}
   104  }