// startDaemonCmd uses the config parameters to start siad. func startDaemon() error { // Establish multithreading. runtime.GOMAXPROCS(runtime.NumCPU()) // Create all of the modules. gateway, err := gateway.New(config.Siad.RPCaddr, filepath.Join(config.Siad.SiaDir, modules.GatewayDir)) if err != nil { return err } state, err := consensus.New(gateway, filepath.Join(config.Siad.SiaDir, modules.ConsensusDir)) if err != nil { return err } hostdb, err := hostdb.New(state, gateway) if err != nil { return err } explorer, err := explorer.New(state, filepath.Join(config.Siad.SiaDir, modules.ExplorerDir)) if err != nil { return err } srv, err := api.NewServer(config.Siad.APIaddr, state, gateway, nil, hostdb, nil, nil, nil, nil, explorer) if err != nil { return err } // Bootstrap to the network. if !config.Siad.NoBootstrap { for i := range modules.BootstrapPeers { go gateway.Connect(modules.BootstrapPeers[i]) } } // Send a struct down the started channel, so the testing package knows // that daemon startup has completed. A gofunc is used with the hope that // srv.Serve() will start running before the value is sent down the // channel. go func() { started <- struct{}{} }() // Start serving api requests. err = srv.Serve() if err != nil { return err } return nil }
// newRenterTester creates a ready-to-use renter tester with money in the // wallet. func newRenterTester(name string) (*renterTester, error) { testdir := build.TempDir("renter", name) // Create the gateway. g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir)) if err != nil { return nil, err } // Create the consensus set. cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir)) if err != nil { return nil, err } // Create the hostdb. hdb, err := hostdb.New(cs, g) if err != nil { return nil, err } // Create the tpool. tp, err := transactionpool.New(cs, g) if err != nil { return nil, err } // Create the wallet. w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) if err != nil { return nil, err } key, err := crypto.GenerateTwofishKey() if err != nil { return nil, err } _, err = w.Encrypt(key) if err != nil { return nil, err } err = w.Unlock(key) if err != nil { return nil, err } // Create the renter. r, err := New(cs, hdb, w, tp, filepath.Join(testdir, modules.RenterDir)) if err != nil { return nil, err } // Create the miner. m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir)) if err != nil { return nil, err } // Assemble all pieces into a renter tester. rt := &renterTester{ cs: cs, gateway: g, hostdb: hdb, miner: m, tpool: tp, wallet: w, renter: r, } // Mine blocks until there is money in the wallet. for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { _, err := rt.miner.AddBlock() if err != nil { return nil, err } } return rt, nil }
// startDaemonCmd uses the config parameters to start siad. func startDaemon() error { // Establish multithreading. runtime.GOMAXPROCS(runtime.NumCPU()) // Print a startup message. // // TODO: This message can be removed once the api starts up in under 1/2 // second. fmt.Println("siad is loading, may take a minute or two") loadStart := time.Now().UnixNano() // Create all of the modules. gateway, err := gateway.New(config.Siad.RPCaddr, filepath.Join(config.Siad.SiaDir, modules.GatewayDir)) if err != nil { return err } state, err := consensus.New(gateway, filepath.Join(config.Siad.SiaDir, modules.ConsensusDir)) if err != nil { return err } tpool, err := transactionpool.New(state, gateway) if err != nil { return err } wallet, err := wallet.New(state, tpool, filepath.Join(config.Siad.SiaDir, modules.WalletDir)) if err != nil { return err } miner, err := miner.New(state, tpool, wallet, filepath.Join(config.Siad.SiaDir, modules.MinerDir)) if err != nil { return err } hostdb, err := hostdb.New(state, gateway) if err != nil { return err } host, err := host.New(state, hostdb, tpool, wallet, config.Siad.HostAddr, filepath.Join(config.Siad.SiaDir, modules.HostDir)) if err != nil { return err } renter, err := renter.New(state, hostdb, wallet, filepath.Join(config.Siad.SiaDir, modules.RenterDir)) if err != nil { return err } srv, err := api.NewServer(config.Siad.APIaddr, state, gateway, host, hostdb, miner, renter, tpool, wallet, nil) if err != nil { return err } // Bootstrap to the network. if !config.Siad.NoBootstrap { for i := range modules.BootstrapPeers { go gateway.Connect(modules.BootstrapPeers[i]) } } // Print a 'startup complete' message. // // TODO: This message can be removed once the api starts up in under 1/2 // second. startupTime := time.Now().UnixNano() - loadStart fmt.Println("siad has finished loading after", float64(startupTime)/1e9, "seconds") // Start serving api requests. err = srv.Serve() if err != nil { return err } return nil }
// createServerTester creates a server tester object that is ready for testing, // including money in the wallet and all modules initalized. func createServerTester(name string) (*serverTester, error) { // Create the testing directory and assign the api port. testdir := build.TempDir("api", name) APIAddr := ":" + strconv.Itoa(APIPort) APIPort++ // Create the modules. g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir)) if err != nil { return nil, err } cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir)) if err != nil { return nil, err } tp, err := transactionpool.New(cs, g) if err != nil { return nil, err } w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) if err != nil { return nil, err } key, err := crypto.GenerateTwofishKey() if err != nil { return nil, err } _, err = w.Encrypt(key) if err != nil { return nil, err } err = w.Unlock(key) if err != nil { return nil, err } m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir)) if err != nil { return nil, err } hdb, err := hostdb.New(cs, g) if err != nil { return nil, err } h, err := host.New(cs, hdb, tp, w, ":0", filepath.Join(testdir, modules.HostDir)) if err != nil { return nil, err } r, err := renter.New(cs, hdb, w, filepath.Join(testdir, modules.RenterDir)) if err != nil { return nil, err } exp, err := explorer.New(cs, filepath.Join(testdir, modules.ExplorerDir)) if err != nil { return nil, err } srv, err := NewServer(APIAddr, cs, g, h, hdb, m, r, tp, w, exp) if err != nil { return nil, err } // Assemble the serverTester. st := &serverTester{ cs: cs, gateway: g, host: h, hostdb: hdb, miner: m, renter: r, tpool: tp, exp: exp, wallet: w, walletKey: key, server: srv, } // TODO: A more reasonable way of listening for server errors. go func() { listenErr := srv.Serve() if listenErr != nil { panic(listenErr) } }() // Mine blocks until the wallet has confirmed money. for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { _, err := st.miner.AddBlock() if err != nil { return nil, err } } return st, nil }
// CreateHostTester initializes a HostTester. func CreateHostTester(name string, t *testing.T) *hostTester { testdir := build.TempDir(modules.HostDir, name) // Create the modules. g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir)) if err != nil { t.Fatal(err) } cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir)) if err != nil { t.Fatal(err) } tp, err := transactionpool.New(cs, g) if err != nil { t.Fatal(err) } w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) if err != nil { t.Fatal(err) } key, err := crypto.GenerateTwofishKey() if err != nil { t.Fatal(err) } _, err = w.Encrypt(key) if err != nil { t.Fatal(err) } err = w.Unlock(key) if err != nil { t.Fatal(err) } m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir)) if err != nil { t.Fatal(err) } hdb, err := hostdb.New(cs, g, filepath.Join(testdir, modules.HostDBDir)) if err != nil { t.Fatal(err) } h, err := New(cs, hdb, tp, w, ":0", filepath.Join(testdir, modules.HostDir)) if err != nil { t.Fatal(err) } // Assemble all objects into a hostTester ht := &hostTester{ cs: cs, gateway: g, miner: m, tpool: tp, wallet: w, host: h, t: t, } // Mine blocks until there is money in the wallet. for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { b, _ := m.FindBlock() err := cs.AcceptBlock(b) if err != nil { t.Fatal(err) } } return ht }
// newRenterTester creates a ready-to-use renter tester with money in the // wallet. func newRenterTester(name string, t *testing.T) *renterTester { testdir := build.TempDir("renter", name) // Create the gateway. g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir)) if err != nil { t.Fatal(err) } // Create the consensus set. cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir)) if err != nil { t.Fatal(err) } // Create the hostdb. hdb, err := hostdb.New(cs, g) if err != nil { t.Fatal(err) } // Create the tpool. tp, err := transactionpool.New(cs, g) if err != nil { t.Fatal(err) } // Create the wallet. w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) if err != nil { t.Fatal(err) } // Create the renter. r, err := New(cs, hdb, w, filepath.Join(testdir, modules.RenterDir)) if err != nil { t.Fatal(err) } // Create the miner. m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.RenterDir)) if err != nil { t.Fatal(err) } // Assemble all pieces into a renter tester. rt := &renterTester{ cs: cs, hostdb: hdb, miner: m, tpool: tp, wallet: w, renter: r, csUpdateChan: cs.ConsensusSetNotify(), hostdbUpdateChan: hdb.HostDBNotify(), renterUpdateChan: r.RenterNotify(), tpoolUpdateChan: tp.TransactionPoolNotify(), minerUpdateChan: m.MinerNotify(), walletUpdateChan: w.WalletNotify(), t: t, } rt.csUpdateWait() // Mine blocks until there is money in the wallet. for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { b, _ := rt.miner.FindBlock() err := rt.cs.AcceptBlock(b) if err != nil { t.Fatal(err) } rt.csUpdateWait() } return rt }
// startDaemonCmd uses the config parameters to start siad. func startDaemon() error { // Establish multithreading. runtime.GOMAXPROCS(runtime.NumCPU()) // Print a startup message. // // TODO: This message can be removed once the api starts up in under 1/2 // second. fmt.Println("Loading...") loadStart := time.Now() // Create all of the modules. gateway, err := gateway.New(config.Siad.RPCaddr, filepath.Join(config.Siad.SiaDir, modules.GatewayDir)) if err != nil { return err } cs, err := consensus.New(gateway, filepath.Join(config.Siad.SiaDir, modules.ConsensusDir)) if err != nil { return err } tpool, err := transactionpool.New(cs, gateway) if err != nil { return err } wallet, err := wallet.New(cs, tpool, filepath.Join(config.Siad.SiaDir, modules.WalletDir)) if err != nil { return err } miner, err := miner.New(cs, tpool, wallet, filepath.Join(config.Siad.SiaDir, modules.MinerDir)) if err != nil { return err } hostdb, err := hostdb.New(cs, gateway) if err != nil { return err } host, err := host.New(cs, hostdb, tpool, wallet, config.Siad.HostAddr, filepath.Join(config.Siad.SiaDir, modules.HostDir)) if err != nil { return err } renter, err := renter.New(cs, hostdb, wallet, tpool, filepath.Join(config.Siad.SiaDir, modules.RenterDir)) if err != nil { return err } srv, err := api.NewServer(config.Siad.APIaddr, cs, gateway, host, hostdb, miner, renter, tpool, wallet, nil) if err != nil { return err } // Bootstrap to the network. if !config.Siad.NoBootstrap { // connect to 3 random bootstrap nodes perm := crypto.Perm(len(modules.BootstrapPeers)) for _, i := range perm[:3] { go gateway.Connect(modules.BootstrapPeers[i]) } } // Print a 'startup complete' message. // // TODO: This message can be removed once the api starts up in under 1/2 // second. startupTime := time.Since(loadStart) fmt.Println("Finished loading in", startupTime.Seconds(), "seconds") // Start serving api requests. err = srv.Serve() if err != nil { return err } return nil }
// CreateHostTester initializes a HostTester. func CreateHostTester(name string, t *testing.T) *hostTester { testdir := build.TempDir(modules.HostDir, name) // Create the modules. g, err := gateway.New(":0", filepath.Join(testdir, modules.GatewayDir)) if err != nil { t.Fatal(err) } cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir)) if err != nil { t.Fatal(err) } tp, err := transactionpool.New(cs, g) if err != nil { t.Fatal(err) } w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir)) if err != nil { t.Fatal(err) } m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir)) if err != nil { t.Fatal(err) } hdb, err := hostdb.New(cs, g) if err != nil { t.Fatal(err) } h, err := New(cs, hdb, tp, w, ":0", filepath.Join(testdir, modules.HostDir)) if err != nil { t.Fatal(err) } // Assemble all objects into a hostTester ht := &hostTester{ cs: cs, gateway: g, miner: m, tpool: tp, wallet: w, host: h, csUpdateChan: cs.ConsensusSetNotify(), hostUpdateChan: h.HostNotify(), tpoolUpdateChan: tp.TransactionPoolNotify(), minerUpdateChan: m.MinerNotify(), walletUpdateChan: w.WalletNotify(), t: t, } ht.csUpdateWait() // Mine blocks until there is money in the wallet. for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ { b, _ := m.FindBlock() err := cs.AcceptBlock(b) if err != nil { t.Fatal(err) } ht.csUpdateWait() } return ht }