github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/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/neatio-net/neatio/chain/core/rawdb"
    24  	"github.com/neatio-net/neatio/chain/core/vm"
    25  	"github.com/neatio-net/neatio/params"
    26  )
    27  
    28  // Tests that DAO-fork enabled clients can properly filter out fork-commencing
    29  // blocks based on their extradata fields.
    30  func TestDAOForkRangeExtradata(t *testing.T) {
    31  	forkBlock := big.NewInt(32)
    32  
    33  	// Generate a common prefix for both pro-forkers and non-forkers
    34  	db := rawdb.NewMemoryDatabase()
    35  	gspec := new(Genesis)
    36  	genesis := gspec.MustCommit(db)
    37  	prefix, _ := GenerateChain(params.TestChainConfig, genesis, nil, db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
    38  
    39  	// Create the concurrent, conflicting two nodes
    40  	proDb := rawdb.NewMemoryDatabase()
    41  	gspec.MustCommit(proDb)
    42  
    43  	proConf := *params.TestChainConfig
    44  
    45  	proBc, _ := NewBlockChain(proDb, nil, &proConf, nil, vm.Config{}, nil)
    46  	defer proBc.Stop()
    47  
    48  	conDb := rawdb.NewMemoryDatabase()
    49  	gspec.MustCommit(conDb)
    50  
    51  	conConf := *params.TestChainConfig
    52  
    53  	conBc, _ := NewBlockChain(conDb, nil, &conConf, nil, vm.Config{}, nil)
    54  	defer conBc.Stop()
    55  
    56  	if _, err := proBc.InsertChain(prefix); err != nil {
    57  		t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
    58  	}
    59  	if _, err := conBc.InsertChain(prefix); err != nil {
    60  		t.Fatalf("con-fork: failed to import chain prefix: %v", err)
    61  	}
    62  	// Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
    63  	for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
    64  		// Create a pro-fork block, and try to feed into the no-fork chain
    65  		db = rawdb.NewMemoryDatabase()
    66  		gspec.MustCommit(db)
    67  		bc, _ := NewBlockChain(db, nil, &conConf, nil, vm.Config{}, nil)
    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(), nil, db, 1, func(i int, gen *BlockGen) {})
    81  		if _, err := conBc.InsertChain(blocks); err == nil {
    82  			t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
    83  		}
    84  		// Create a proper no-fork block for the contra-forker
    85  		blocks, _ = GenerateChain(&conConf, conBc.CurrentBlock(), nil, db, 1, func(i int, gen *BlockGen) {})
    86  		if _, err := conBc.InsertChain(blocks); err != nil {
    87  			t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
    88  		}
    89  		// Create a no-fork block, and try to feed into the pro-fork chain
    90  		db = rawdb.NewMemoryDatabase()
    91  		gspec.MustCommit(db)
    92  		bc, _ = NewBlockChain(db, nil, &proConf, nil, vm.Config{}, nil)
    93  		defer bc.Stop()
    94  
    95  		blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
    96  		for j := 0; j < len(blocks)/2; j++ {
    97  			blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    98  		}
    99  		if _, err := bc.InsertChain(blocks); err != nil {
   100  			t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
   101  		}
   102  		if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
   103  			t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
   104  		}
   105  		blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), nil, db, 1, func(i int, gen *BlockGen) {})
   106  		if _, err := proBc.InsertChain(blocks); err == nil {
   107  			t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
   108  		}
   109  		// Create a proper pro-fork block for the pro-forker
   110  		blocks, _ = GenerateChain(&proConf, proBc.CurrentBlock(), nil, db, 1, func(i int, gen *BlockGen) {})
   111  		if _, err := proBc.InsertChain(blocks); err != nil {
   112  			t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
   113  		}
   114  	}
   115  	// Verify that contra-forkers accept pro-fork extra-datas after forking finishes
   116  	db = rawdb.NewMemoryDatabase()
   117  	gspec.MustCommit(db)
   118  	bc, _ := NewBlockChain(db, nil, &conConf, nil, vm.Config{}, nil)
   119  	defer bc.Stop()
   120  
   121  	blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
   122  	for j := 0; j < len(blocks)/2; j++ {
   123  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   124  	}
   125  	if _, err := bc.InsertChain(blocks); err != nil {
   126  		t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
   127  	}
   128  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
   129  		t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
   130  	}
   131  	blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), nil, db, 1, func(i int, gen *BlockGen) {})
   132  	if _, err := conBc.InsertChain(blocks); err != nil {
   133  		t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
   134  	}
   135  	// Verify that pro-forkers accept contra-fork extra-datas after forking finishes
   136  	db = rawdb.NewMemoryDatabase()
   137  	gspec.MustCommit(db)
   138  	bc, _ = NewBlockChain(db, nil, &proConf, nil, vm.Config{}, nil)
   139  	defer bc.Stop()
   140  
   141  	blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
   142  	for j := 0; j < len(blocks)/2; j++ {
   143  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   144  	}
   145  	if _, err := bc.InsertChain(blocks); err != nil {
   146  		t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
   147  	}
   148  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
   149  		t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
   150  	}
   151  	blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), nil, db, 1, func(i int, gen *BlockGen) {})
   152  	if _, err := proBc.InsertChain(blocks); err != nil {
   153  		t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
   154  	}
   155  }