Commit 4a518bee authored by Luke Champine's avatar Luke Champine

tweak siad initialization

parent 60b4cfd2
......@@ -9,8 +9,7 @@ import (
const apiTimeout = 5e9 // 5 seconds
// TODO: timeouts?
func (d *daemon) handle(addr string) {
func (d *daemon) listen(addr string) {
mux := http.NewServeMux()
// Host API Calls
......@@ -57,6 +56,10 @@ func (d *daemon) handle(addr string) {
// graceful will run until it catches a signal.
// it can also be stopped manually by stopHandler.
//
// TODO: this fails silently. The error should be checked, but then it
// will print an error even if interrupted normally. Need a better
// solution.
d.apiServer.ListenAndServe()
}
......
package main
import (
"fmt"
"html/template"
"github.com/stretchr/graceful"
......@@ -18,9 +17,8 @@ import (
type DaemonConfig struct {
// Network Variables
APIAddr string
RPCAddr string
NoBootstrap bool
APIAddr string
RPCAddr string
// Host Variables
HostDir string
......@@ -70,25 +68,38 @@ func newDaemon(config DaemonConfig) (d *daemon, err error) {
if err != nil {
return
}
// d.host, err = host.New(d.state, d.wallet)
// if err != nil {
// return
// }
/*
hostDB, err := hostdb.New()
if err != nil {
return
}
Host, err := host.New(d.state, d.wallet)
if err != nil {
return
}
Renter, err := renter.New(d.state, hostDB, d.wallet)
if err != nil {
return
}
*/
d.initializeNetwork(config.RPCAddr, config.NoBootstrap)
if err == network.ErrNoPeers {
fmt.Println("Warning: no peers responded to bootstrap request. Add peers manually to enable bootstrapping.")
} else if err != nil {
// register RPC handlers
// TODO: register all RPCs in a separate function
err = d.network.RegisterRPC("AcceptBlock", d.state.AcceptBlock)
if err != nil {
return
}
err = d.network.RegisterRPC("AcceptTransaction", d.state.AcceptTransaction)
if err != nil {
return
}
err = d.network.RegisterRPC("SendBlocks", d.SendBlocks)
if err != nil {
return
}
err = d.network.RegisterRPC("NegotiateContract", d.host.NegotiateContract)
if err != nil {
return
}
......
......@@ -83,9 +83,8 @@ func init() {
func startEnvironment(*cobra.Command, []string) {
daemonConfig := DaemonConfig{
APIAddr: config.Siad.APIaddr,
RPCAddr: config.Siacore.RPCaddr,
NoBootstrap: config.Siacore.NoBootstrap,
APIAddr: config.Siad.APIaddr,
RPCAddr: config.Siacore.RPCaddr,
HostDir: config.Siacore.HostDirectory,
......@@ -98,14 +97,19 @@ func startEnvironment(*cobra.Command, []string) {
err := config.expand()
if err != nil {
fmt.Println("Bad config value:", err)
return
}
d, err := newDaemon(daemonConfig)
if err != nil {
fmt.Println("Failed to start daemon:", err)
return
}
d.handle(daemonConfig.APIAddr)
// join the network
if !config.Siacore.NoBootstrap {
go d.bootstrap()
}
// listen for API requests
d.listen(daemonConfig.APIAddr)
}
func version(*cobra.Command, []string) {
......
......@@ -16,64 +16,29 @@ var (
moreBlocksErr = errors.New("more blocks are available")
)
// initializeNetwork registers the rpcs and bootstraps to the network,
// downlading all of the blocks and establishing a peer list.
func (d *daemon) initializeNetwork(addr string, nobootstrap bool) (err error) {
d.network, err = network.NewTCPServer(addr)
if err != nil {
// bootstrap bootstraps to the network, downlading all of the blocks and
// establishing a peer list.
func (d *daemon) bootstrap() {
// Establish an initial peer list.
if err := d.network.Bootstrap(); err != nil {
if err == network.ErrNoPeers {
println("Warning: no peers responded to bootstrap request. Add peers manually to enable bootstrapping.")
// TODO: wait for new peers?
}
// log error
return
}
err = d.network.RegisterRPC("AcceptBlock", d.state.AcceptBlock)
if err != nil {
return
}
err = d.network.RegisterRPC("AcceptTransaction", d.tpool.AcceptTransaction)
if err != nil {
return
}
err = d.network.RegisterRPC("SendBlocks", d.SendBlocks)
if err != nil {
return
}
/*
d.network.RegisterRPC("NegotiateContract", d.host.NegotiateContract)
// Every 2 minutes, call CatchUp() on a random peer. This helps with
// synchronization.
for ; ; time.Sleep(time.Minute * 2) {
peer, err := d.network.RandomPeer()
if err != nil {
return
// TODO: wait for new peers?
continue
}
d.network.RegisterRPC("RetrieveFile", d.host.RetrieveFile)
if err != nil {
return
}
*/
// If we aren't bootstrapping, then we're done.
// TODO: this means the CatchUp thread isn't spawned.
// It should probably be spawned after the first peer connects.
if nobootstrap {
return
go d.CatchUp(peer)
}
// Bootstrapping may take a while
go func() {
// Establish an initial peer list.
if err := d.network.Bootstrap(); err != nil {
// log error
return
}
// Every 2 minutes, call CatchUp() on a random peer. This helps with
// synchronization.
for ; ; time.Sleep(time.Minute * 2) {
peer, err := d.network.RandomPeer()
if err != nil {
continue
}
go d.CatchUp(peer)
}
}()
return
}
// blockHistory returns up to 32 BlockIDs, starting with the 12 most recent
......@@ -104,7 +69,11 @@ func (d *daemon) blockHistory() (blockIDs [32]consensus.BlockID) {
}
// always include the genesis block
genesis, _ := d.state.BlockAtHeight(0)
genesis, err := d.state.BlockAtHeight(0)
if err != nil {
// this should never happen
return
}
knownBlocks = append(knownBlocks, genesis.ID())
copy(blockIDs[:], knownBlocks)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment