github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/quickchain/dao_test.go (about) 1 2 package main 3 4 import ( 5 "io/ioutil" 6 "math/big" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/quickchainproject/quickchain/common" 12 "github.com/quickchainproject/quickchain/core" 13 "github.com/quickchainproject/quickchain/qctdb" 14 "github.com/quickchainproject/quickchain/params" 15 ) 16 17 // Genesis block for nodes which don't care about the DAO fork (i.e. not configured) 18 var daoOldGenesis = `{ 19 "alloc" : {}, 20 "coinbase" : "0x0000000000000000000000000000000000000000", 21 "difficulty" : "0x20000", 22 "extraData" : "", 23 "gasLimit" : "0x2fefd8", 24 "nonce" : "0x0000000000000042", 25 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 26 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 27 "timestamp" : "0x00", 28 "config" : {} 29 }` 30 31 // Genesis block for nodes which actively oppose the DAO fork 32 var daoNoForkGenesis = `{ 33 "alloc" : {}, 34 "coinbase" : "0x0000000000000000000000000000000000000000", 35 "difficulty" : "0x20000", 36 "extraData" : "", 37 "gasLimit" : "0x2fefd8", 38 "nonce" : "0x0000000000000042", 39 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 40 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 41 "timestamp" : "0x00", 42 "config" : { 43 "daoForkBlock" : 314, 44 "daoForkSupport" : false 45 } 46 }` 47 48 // Genesis block for nodes which actively support the DAO fork 49 var daoProForkGenesis = `{ 50 "alloc" : {}, 51 "coinbase" : "0x0000000000000000000000000000000000000000", 52 "difficulty" : "0x20000", 53 "extraData" : "", 54 "gasLimit" : "0x2fefd8", 55 "nonce" : "0x0000000000000042", 56 "mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 57 "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000", 58 "timestamp" : "0x00", 59 "config" : { 60 "daoForkBlock" : 314, 61 "daoForkSupport" : true 62 } 63 }` 64 65 var daoGenesisHash = common.HexToHash("5e1fc79cb4ffa4739177b5408045cd5d51c6cf766133f23f7cd72ee1f8d790e0") 66 var daoGenesisForkBlock = big.NewInt(314) 67 68 // TestDAOForkBlockNewChain tests that the DAO hard-fork number and the nodes support/opposition is correctly 69 // set in the database after various initialization procedures and invocations. 70 func TestDAOForkBlockNewChain(t *testing.T) { 71 for i, arg := range []struct { 72 genesis string 73 expectBlock *big.Int 74 expectVote bool 75 }{ 76 // Test DAO Default Mainnet 77 {"", params.MainnetChainConfig.DAOForkBlock, true}, 78 // test DAO Init Old Privnet 79 {daoOldGenesis, nil, false}, 80 // test DAO Default No Fork Privnet 81 {daoNoForkGenesis, daoGenesisForkBlock, false}, 82 // test DAO Default Pro Fork Privnet 83 {daoProForkGenesis, daoGenesisForkBlock, true}, 84 } { 85 testDAOForkBlockNewChain(t, i, arg.genesis, arg.expectBlock, arg.expectVote) 86 } 87 } 88 89 func testDAOForkBlockNewChain(t *testing.T, test int, genesis string, expectBlock *big.Int, expectVote bool) { 90 // Create a temporary data directory to use and inspect later 91 datadir := tmpdir(t) 92 defer os.RemoveAll(datadir) 93 94 // Start a Geth instance with the requested flags set and immediately terminate 95 if genesis != "" { 96 json := filepath.Join(datadir, "genesis.json") 97 if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil { 98 t.Fatalf("test %d: failed to write genesis file: %v", test, err) 99 } 100 runGeth(t, "--datadir", datadir, "init", json).WaitExit() 101 } else { 102 // Force chain initialization 103 args := []string{"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--ipcdisable", "--datadir", datadir} 104 geth := runGeth(t, append(args, []string{"--exec", "2+2", "console"}...)...) 105 geth.WaitExit() 106 } 107 // Retrieve the DAO config flag from the database 108 path := filepath.Join(datadir, "geth", "chaindata") 109 db, err := qctdb.NewLDBDatabase(path, 0, 0) 110 if err != nil { 111 t.Fatalf("test %d: failed to open test database: %v", test, err) 112 } 113 defer db.Close() 114 115 genesisHash := common.HexToHash("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3") 116 if genesis != "" { 117 genesisHash = daoGenesisHash 118 } 119 config, err := core.GetChainConfig(db, genesisHash) 120 if err != nil { 121 t.Errorf("test %d: failed to retrieve chain config: %v", test, err) 122 return // we want to return here, the other checks can't make it past this point (nil panic). 123 } 124 // Validate the DAO hard-fork block number against the expected value 125 if config.DAOForkBlock == nil { 126 if expectBlock != nil { 127 t.Errorf("test %d: dao hard-fork block mismatch: have nil, want %v", test, expectBlock) 128 } 129 } else if expectBlock == nil { 130 t.Errorf("test %d: dao hard-fork block mismatch: have %v, want nil", test, config.DAOForkBlock) 131 } else if config.DAOForkBlock.Cmp(expectBlock) != 0 { 132 t.Errorf("test %d: dao hard-fork block mismatch: have %v, want %v", test, config.DAOForkBlock, expectBlock) 133 } 134 if config.DAOForkSupport != expectVote { 135 t.Errorf("test %d: dao hard-fork support mismatch: have %v, want %v", test, config.DAOForkSupport, expectVote) 136 } 137 }