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