gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/explorer/explorer_test.go (about) 1 package explorer 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "gitlab.com/SiaPrime/SiaPrime/build" 8 "gitlab.com/SiaPrime/SiaPrime/crypto" 9 "gitlab.com/SiaPrime/SiaPrime/modules" 10 "gitlab.com/SiaPrime/SiaPrime/modules/consensus" 11 "gitlab.com/SiaPrime/SiaPrime/modules/gateway" 12 "gitlab.com/SiaPrime/SiaPrime/modules/miner" 13 "gitlab.com/SiaPrime/SiaPrime/modules/transactionpool" 14 "gitlab.com/SiaPrime/SiaPrime/modules/wallet" 15 "gitlab.com/SiaPrime/SiaPrime/persist" 16 "gitlab.com/SiaPrime/SiaPrime/types" 17 ) 18 19 // Explorer tester struct is the helper object for explorer 20 // testing. It holds the helper modules for its testing 21 type explorerTester struct { 22 cs modules.ConsensusSet 23 gateway modules.Gateway 24 miner modules.TestMiner 25 tpool modules.TransactionPool 26 wallet modules.Wallet 27 walletKey crypto.CipherKey 28 29 explorer *Explorer 30 testdir string 31 } 32 33 // createExplorerTester creates a tester object for the explorer module. 34 func createExplorerTester(name string) (*explorerTester, error) { 35 if testing.Short() { 36 panic("createExplorerTester called when in a short test") 37 } 38 39 // Create and assemble the dependencies. 40 testdir := build.TempDir(modules.ExplorerDir, name) 41 g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir)) 42 if err != nil { 43 return nil, err 44 } 45 cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir)) 46 if err != nil { 47 return nil, err 48 } 49 tp, err := transactionpool.New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir)) 50 if err != nil { 51 return nil, err 52 } 53 w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) 54 if err != nil { 55 return nil, err 56 } 57 key := crypto.GenerateSiaKey(crypto.TypeDefaultWallet) 58 _, err = w.Encrypt(key) 59 if err != nil { 60 return nil, err 61 } 62 err = w.Unlock(key) 63 if err != nil { 64 return nil, err 65 } 66 m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.RenterDir)) 67 if err != nil { 68 return nil, err 69 } 70 e, err := New(cs, filepath.Join(testdir, modules.ExplorerDir)) 71 if err != nil { 72 return nil, err 73 } 74 et := &explorerTester{ 75 cs: cs, 76 gateway: g, 77 miner: m, 78 tpool: tp, 79 wallet: w, 80 walletKey: key, 81 82 explorer: e, 83 testdir: testdir, 84 } 85 86 // Mine until the wallet has money. 87 for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { 88 b, _ := et.miner.FindBlock() 89 err = et.cs.AcceptBlock(b) 90 if err != nil { 91 return nil, err 92 } 93 } 94 return et, nil 95 } 96 97 // reorgToBlank creates a bunch of empty blocks on top of the genesis block 98 // that reorgs the explorer to a state of all blank blocks. 99 func (et *explorerTester) reorgToBlank() error { 100 // Get a unique directory name to house the persistence of the miner 101 // dependencies. 102 dir := et.testdir + " - " + persist.RandomSuffix() 103 104 // Create a miner and all dependencies to create an alternate chain. 105 g, err := gateway.New("localhost:0", false, filepath.Join(dir, modules.GatewayDir)) 106 if err != nil { 107 return err 108 } 109 cs, err := consensus.New(g, false, filepath.Join(dir, modules.ConsensusDir)) 110 if err != nil { 111 return err 112 } 113 tp, err := transactionpool.New(cs, g, filepath.Join(dir, modules.TransactionPoolDir)) 114 if err != nil { 115 return err 116 } 117 w, err := wallet.New(cs, tp, filepath.Join(dir, modules.WalletDir)) 118 if err != nil { 119 return err 120 } 121 key := crypto.GenerateSiaKey(crypto.TypeDefaultWallet) 122 _, err = w.Encrypt(key) 123 if err != nil { 124 return err 125 } 126 err = w.Unlock(key) 127 if err != nil { 128 return err 129 } 130 m, err := miner.New(cs, tp, w, filepath.Join(dir, modules.RenterDir)) 131 if err != nil { 132 return err 133 } 134 135 // Mine blocks until the height is higher than the existing consensus, 136 // submitting each block to the explorerTester. 137 currentHeight := cs.Height() 138 for i := types.BlockHeight(0); i <= currentHeight+1; i++ { 139 block, err := m.AddBlock() 140 if err != nil { 141 return err 142 } 143 et.cs.AcceptBlock(block) // error is not checked, will not always be nil 144 } 145 return nil 146 } 147 148 // TestNilExplorerDependencies tries to initialize an explorer with nil 149 // dependencies, checks that the correct error is returned. 150 func TestNilExplorerDependencies(t *testing.T) { 151 _, err := New(nil, "expdir") 152 if err != errNilCS { 153 t.Fatal("Expecting errNilCS") 154 } 155 } 156 157 // TestExplorerGenesisHeight checks that when the explorer is initialized and given the 158 // genesis block, the result has the correct height. 159 func TestExplorerGenesisHeight(t *testing.T) { 160 if testing.Short() { 161 t.SkipNow() 162 } 163 // Create the dependencies. 164 testdir := build.TempDir(modules.HostDir, t.Name()) 165 g, err := gateway.New("localhost:0", false, filepath.Join(testdir, modules.GatewayDir)) 166 if err != nil { 167 t.Fatal(err) 168 } 169 cs, err := consensus.New(g, false, filepath.Join(testdir, modules.ConsensusDir)) 170 if err != nil { 171 t.Fatal(err) 172 } 173 174 // Create the explorer - from the subscription only the genesis block will 175 // be received. 176 e, err := New(cs, testdir) 177 if err != nil { 178 t.Fatal(err) 179 } 180 block, height, exists := e.Block(types.GenesisID) 181 if !exists { 182 t.Error("explorer missing genesis block after initialization") 183 } 184 if block.ID() != types.GenesisID { 185 t.Error("explorer returned wrong genesis block") 186 } 187 if height != 0 { 188 t.Errorf("genesis block hash wrong height: expected 0, got %v", height) 189 } 190 }