github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/dao_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/quickchainproject/quickchain/consensus/qcthash"
     8  	"github.com/quickchainproject/quickchain/core/vm"
     9  	"github.com/quickchainproject/quickchain/qctdb"
    10  	"github.com/quickchainproject/quickchain/params"
    11  )
    12  
    13  // Tests that DAO-fork enabled clients can properly filter out fork-commencing
    14  // blocks based on their extradata fields.
    15  func TestDAOForkRangeExtradata(t *testing.T) {
    16  	forkBlock := big.NewInt(32)
    17  
    18  	// Generate a common prefix for both pro-forkers and non-forkers
    19  	db, _ := qctdb.NewMemDatabase()
    20  	gspec := new(Genesis)
    21  	genesis := gspec.MustCommit(db)
    22  	prefix, _ := GenerateChain(params.TestChainConfig, genesis, qcthash.NewFaker(), db, int(forkBlock.Int64()-1), func(i int, gen *BlockGen) {})
    23  
    24  	// Create the concurrent, conflicting two nodes
    25  	proDb, _ := qctdb.NewMemDatabase()
    26  	gspec.MustCommit(proDb)
    27  
    28  	proConf := *params.TestChainConfig
    29  	proConf.DAOForkBlock = forkBlock
    30  	proConf.DAOForkSupport = true
    31  
    32  	proBc, _ := NewBlockChain(proDb, nil, &proConf, qcthash.NewFaker(), vm.Config{})
    33  	defer proBc.Stop()
    34  
    35  	conDb, _ := qctdb.NewMemDatabase()
    36  	gspec.MustCommit(conDb)
    37  
    38  	conConf := *params.TestChainConfig
    39  	conConf.DAOForkBlock = forkBlock
    40  	conConf.DAOForkSupport = false
    41  
    42  	conBc, _ := NewBlockChain(conDb, nil, &conConf, qcthash.NewFaker(), vm.Config{})
    43  	defer conBc.Stop()
    44  
    45  	if _, err := proBc.InsertChain(prefix); err != nil {
    46  		t.Fatalf("pro-fork: failed to import chain prefix: %v", err)
    47  	}
    48  	if _, err := conBc.InsertChain(prefix); err != nil {
    49  		t.Fatalf("con-fork: failed to import chain prefix: %v", err)
    50  	}
    51  	// Try to expand both pro-fork and non-fork chains iteratively with other camp's blocks
    52  	for i := int64(0); i < params.DAOForkExtraRange.Int64(); i++ {
    53  		// Create a pro-fork block, and try to feed into the no-fork chain
    54  		db, _ = qctdb.NewMemDatabase()
    55  		gspec.MustCommit(db)
    56  		bc, _ := NewBlockChain(db, nil, &conConf, qcthash.NewFaker(), vm.Config{})
    57  		defer bc.Stop()
    58  
    59  		blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
    60  		for j := 0; j < len(blocks)/2; j++ {
    61  			blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    62  		}
    63  		if _, err := bc.InsertChain(blocks); err != nil {
    64  			t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
    65  		}
    66  		if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
    67  			t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
    68  		}
    69  		blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
    70  		if _, err := conBc.InsertChain(blocks); err == nil {
    71  			t.Fatalf("contra-fork chain accepted pro-fork block: %v", blocks[0])
    72  		}
    73  		// Create a proper no-fork block for the contra-forker
    74  		blocks, _ = GenerateChain(&conConf, conBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
    75  		if _, err := conBc.InsertChain(blocks); err != nil {
    76  			t.Fatalf("contra-fork chain didn't accepted no-fork block: %v", err)
    77  		}
    78  		// Create a no-fork block, and try to feed into the pro-fork chain
    79  		db, _ = qctdb.NewMemDatabase()
    80  		gspec.MustCommit(db)
    81  		bc, _ = NewBlockChain(db, nil, &proConf, qcthash.NewFaker(), vm.Config{})
    82  		defer bc.Stop()
    83  
    84  		blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
    85  		for j := 0; j < len(blocks)/2; j++ {
    86  			blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
    87  		}
    88  		if _, err := bc.InsertChain(blocks); err != nil {
    89  			t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
    90  		}
    91  		if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
    92  			t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
    93  		}
    94  		blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
    95  		if _, err := proBc.InsertChain(blocks); err == nil {
    96  			t.Fatalf("pro-fork chain accepted contra-fork block: %v", blocks[0])
    97  		}
    98  		// Create a proper pro-fork block for the pro-forker
    99  		blocks, _ = GenerateChain(&proConf, proBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
   100  		if _, err := proBc.InsertChain(blocks); err != nil {
   101  			t.Fatalf("pro-fork chain didn't accepted pro-fork block: %v", err)
   102  		}
   103  	}
   104  	// Verify that contra-forkers accept pro-fork extra-datas after forking finishes
   105  	db, _ = qctdb.NewMemDatabase()
   106  	gspec.MustCommit(db)
   107  	bc, _ := NewBlockChain(db, nil, &conConf, qcthash.NewFaker(), vm.Config{})
   108  	defer bc.Stop()
   109  
   110  	blocks := conBc.GetBlocksFromHash(conBc.CurrentBlock().Hash(), int(conBc.CurrentBlock().NumberU64()))
   111  	for j := 0; j < len(blocks)/2; j++ {
   112  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   113  	}
   114  	if _, err := bc.InsertChain(blocks); err != nil {
   115  		t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
   116  	}
   117  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
   118  		t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
   119  	}
   120  	blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
   121  	if _, err := conBc.InsertChain(blocks); err != nil {
   122  		t.Fatalf("contra-fork chain didn't accept pro-fork block post-fork: %v", err)
   123  	}
   124  	// Verify that pro-forkers accept contra-fork extra-datas after forking finishes
   125  	db, _ = qctdb.NewMemDatabase()
   126  	gspec.MustCommit(db)
   127  	bc, _ = NewBlockChain(db, nil, &proConf, qcthash.NewFaker(), vm.Config{})
   128  	defer bc.Stop()
   129  
   130  	blocks = proBc.GetBlocksFromHash(proBc.CurrentBlock().Hash(), int(proBc.CurrentBlock().NumberU64()))
   131  	for j := 0; j < len(blocks)/2; j++ {
   132  		blocks[j], blocks[len(blocks)-1-j] = blocks[len(blocks)-1-j], blocks[j]
   133  	}
   134  	if _, err := bc.InsertChain(blocks); err != nil {
   135  		t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
   136  	}
   137  	if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
   138  		t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
   139  	}
   140  	blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), qcthash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
   141  	if _, err := proBc.InsertChain(blocks); err != nil {
   142  		t.Fatalf("pro-fork chain didn't accept contra-fork block post-fork: %v", err)
   143  	}
   144  }