github.com/dim4egster/coreth@v0.10.2/core/dao_test.go (about)

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