Commit d3cdce5b authored by Luke Champine's avatar Luke Champine

get build working again

parent 1cb9da0c
......@@ -2,17 +2,17 @@ package host
import (
"errors"
// "fmt"
// "io"
"fmt"
"io"
"net"
// "os"
// "path/filepath"
"os"
"path/filepath"
"strconv"
"github.com/NebulousLabs/Sia/consensus"
// "github.com/NebulousLabs/Sia/encoding"
// "github.com/NebulousLabs/Sia/hash"
// "github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/encoding"
"github.com/NebulousLabs/Sia/hash"
"github.com/NebulousLabs/Sia/modules"
)
var (
......@@ -246,7 +246,7 @@ func (h *Host) NegotiateContract(conn net.Conn) (err error) {
//fmt.Println("Accepted contract")
// Submit the transaction.
h.state.AcceptTransaction(txn)
h.tpool.AcceptTransaction(txn)
return
}
......@@ -28,11 +28,12 @@ type contractObligation struct {
type Host struct {
state *consensus.State
tpool modules.TransactionPool
wallet modules.Wallet
latestBlock consensus.BlockID
// embed HostEntry fields for convenience
modules.HostAnnouncement
// our HostEntry settings, embedded for convenience
modules.HostEntry
hostDir string
spaceRemaining int64
......@@ -54,7 +55,7 @@ func New(state *consensus.State, wallet modules.Wallet) (h *Host, err error) {
return
}
// addr, _, err := wallet.CoinAddress()
addr, _, err := wallet.CoinAddress()
if err != nil {
return
}
......@@ -62,7 +63,7 @@ func New(state *consensus.State, wallet modules.Wallet) (h *Host, err error) {
state: state,
wallet: wallet,
HostAnnouncement: modules.HostAnnouncement{
HostEntry: modules.HostEntry{
MaxFilesize: 4 * 1000 * 1000,
MaxDuration: 1008, // One week.
MinWindow: 20,
......@@ -118,16 +119,16 @@ func (h *Host) RetrieveFile(conn net.Conn) (err error) {
return
}
// SetAnnouncement updates the host's internal announcement object. To modify
// SetConfig updates the host's internal HostEntry object. To modify
// a specific field, use a combination of Info and SetAnnouncement.
func (h *Host) SetAnnouncement(ha modules.HostAnnouncement) {
func (h *Host) SetConfig(entry modules.HostEntry) {
h.mu.Lock()
defer h.mu.Unlock()
h.HostAnnouncement = ha
h.HostEntry = entry
}
type HostInfo struct {
modules.HostAnnouncement
modules.HostEntry
StorageRemaining int64
ContractCount int
......@@ -138,7 +139,7 @@ func (h *Host) Info() HostInfo {
defer h.mu.RUnlock()
info := HostInfo{
HostAnnouncement: h.HostAnnouncement,
HostEntry: h.HostEntry,
StorageRemaining: h.spaceRemaining,
ContractCount: len(h.contracts),
......
......@@ -9,12 +9,22 @@ const (
HostAnnouncementPrefix = 1
)
// the Host struct is kept in the client package because it's what the client
// uses to weigh hosts and pick them out when storing files.
// A HostEntry contains information about a host on the network. The HostDB
// uses this information to select an optimal host.
type HostEntry struct {
HostAnnouncement
ID string
Freeze consensus.Currency
IPAddress network.Address
TotalStorage int64 // Can go negative.
MinFilesize uint64
MaxFilesize uint64
MinDuration consensus.BlockHeight
MaxDuration consensus.BlockHeight
MinWindow consensus.BlockHeight
Price consensus.Currency
Burn consensus.Currency
Freeze consensus.Currency
CoinAddress consensus.CoinAddress // Host may want to give different addresses to each client.
SpendConditions consensus.SpendConditions
}
type HostDB interface {
......@@ -22,15 +32,15 @@ type HostDB interface {
// HostDB may decide to remove the host, or just reduce the weight, or it
// may decide to ignore the flagging. If the flagging is ignored, an error
// will be returned explaining why.
FlagHost(id string) error
FlagHost(network.Address) error
// Insert puts a host entry into the host database.
// Insert adds a host to the database.
InsertHost(HostEntry) error
// RandomHost pulls a host entry at random from the database, weighted
// according to whatever score is assigned the hosts.
RandomHost() (HostEntry, error)
// Remove pulls a host entry from the host database.
RemoveHost(id string) error
// Remove deletes the host with the given address from the database.
RemoveHost(network.Address) error
}
......@@ -4,27 +4,8 @@ import (
"github.com/NebulousLabs/Sia/consensus"
"github.com/NebulousLabs/Sia/encoding"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/network"
)
// A HostAnnouncement is a struct that can appear in the arbitrary data field.
// It is preceded by 8 bytes that decode to the integer 1.
type HostAnnouncement struct {
IPAddress network.Address
TotalStorage int64 // Can go negative.
MinFilesize uint64
MaxFilesize uint64
MinDuration consensus.BlockHeight
MaxDuration consensus.BlockHeight
MinWindow consensus.BlockHeight
Price consensus.Currency
Burn consensus.Currency
CoinAddress consensus.CoinAddress // Host may want to give different addresses to each client.
SpendConditions consensus.SpendConditions
FreezeIndex uint64 // The index of the output that froze coins.
}
// findHostAnnouncements scans a block and pulls out every host announcement
// that appears in the block, returning a list of entries that correspond with
// the announcements.
......@@ -38,31 +19,31 @@ func findHostAnnouncements(height consensus.BlockHeight, b consensus.Block) (ent
continue
}
// TODO: switch dataIndicator
// TODO: new announcement struct
dataIndicator := encoding.DecUint64([]byte(t.ArbitraryData[0][0:8]))
if dataIndicator == 1 {
var ha HostAnnouncement
err = encoding.Unmarshal([]byte(t.ArbitraryData[0][8:]), &ha)
if dataIndicator == modules.HostAnnouncementPrefix {
var entry modules.HostEntry
err = encoding.Unmarshal([]byte(t.ArbitraryData[0][8:]), &entry)
if err != nil {
return
}
// Verify that the host has declared values that are relevant to our
// interests.
if ha.SpendConditions.CoinAddress() != t.Outputs[ha.FreezeIndex].SpendHash {
continue
}
freeze := consensus.Currency(ha.SpendConditions.TimeLock-height) * t.Outputs[ha.FreezeIndex].Value
if freeze <= 0 {
continue
}
// Add the host to the host database.
entryID := t.OutputID(int(ha.FreezeIndex))
entries = append(entries, modules.HostEntry{
HostAnnouncement: ha,
ID: string(entryID[:]),
Freeze: freeze,
})
// TODO: need a way to get the freeze index
/*
if entry.SpendConditions.CoinAddress() != t.Outputs[entry.FreezeIndex].SpendHash {
continue
}
entry.Freeze = consensus.Currency(entry.SpendConditions.TimeLock-height) * t.Outputs[entry.FreezeIndex].Value
if entry.Freeze <= 0 {
continue
}
*/
entries = append(entries, entry)
}
}
......
......@@ -8,6 +8,7 @@ import (
"github.com/NebulousLabs/Sia/consensus"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/network"
)
// TODO: Add a whole set of features to the host database that allow hosts to
......@@ -21,8 +22,8 @@ import (
// The HostDB is a set of hosts that get weighted and inserted into a tree
type HostDB struct {
hostTree *hostNode
activeHosts map[string]*hostNode
inactiveHosts map[string]*modules.HostEntry
activeHosts map[network.Address]*hostNode
inactiveHosts map[network.Address]*modules.HostEntry
mu sync.RWMutex
}
......@@ -30,25 +31,25 @@ type HostDB struct {
// New returns an empty HostDatabase.
func New() (hdb *HostDB, err error) {
hdb = &HostDB{
activeHosts: make(map[string]*hostNode),
inactiveHosts: make(map[string]*modules.HostEntry),
activeHosts: make(map[network.Address]*hostNode),
inactiveHosts: make(map[network.Address]*modules.HostEntry),
}
return
}
// insert will add a host entry to the state.
func (hdb *HostDB) insert(entry modules.HostEntry) error {
_, exists := hdb.activeHosts[entry.ID]
_, exists := hdb.activeHosts[entry.IPAddress]
if exists {
return errors.New("entry of given id already exists in host db")
}
if hdb.hostTree == nil {
hdb.hostTree = createNode(nil, entry)
hdb.activeHosts[entry.ID] = hdb.hostTree
hdb.activeHosts[entry.IPAddress] = hdb.hostTree
} else {
_, hostNode := hdb.hostTree.insert(entry)
hdb.activeHosts[entry.ID] = hostNode
hdb.activeHosts[entry.IPAddress] = hostNode
}
return nil
}
......@@ -63,36 +64,36 @@ func (hdb *HostDB) Insert(entry modules.HostEntry) error {
return hdb.insert(entry)
}
func (hdb *HostDB) FlagHost(id string) error {
func (hdb *HostDB) FlagHost(addr network.Address) error {
// Check that we're online at all.
// Remove the flagged host.
//
// TODO: Smarter flagging code, perhaps cut the weight for example.
return hdb.Remove(id)
return hdb.Remove(addr)
}
// Remove deletes an entry from the hostdb.
func (hdb *HostDB) Remove(id string) error {
func (hdb *HostDB) Remove(addr network.Address) error {
hdb.mu.Lock()
defer hdb.mu.Unlock()
// See if the node is in the set of active hosts.
node, exists := hdb.activeHosts[id]
node, exists := hdb.activeHosts[addr]
if !exists {
// If the node is in the set of inactive hosts, delete from that set,
// otherwise return a not found error.
_, exists := hdb.inactiveHosts[id]
_, exists := hdb.inactiveHosts[addr]
if exists {
delete(hdb.inactiveHosts, id)
delete(hdb.inactiveHosts, addr)
return nil
} else {
return errors.New("id not found in host database")
return errors.New("address not found in host database")
}
}
// Delete the node from the active hosts, and remove it from the tree.
delete(hdb.activeHosts, id)
delete(hdb.activeHosts, addr)
node.remove()
return nil
......@@ -114,7 +115,7 @@ func (hdb *HostDB) Update(initialStateHeight consensus.BlockHeight, rewoundBlock
}
for _, entry := range entries {
err = hdb.Remove(entry.ID)
err = hdb.Remove(entry.IPAddress)
if err != nil {
return
}
......
......@@ -8,6 +8,7 @@ import (
"github.com/NebulousLabs/Sia/consensus"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/network"
)
// uniformTreeVerification checks that everything makes sense in the tree given
......@@ -35,14 +36,14 @@ func uniformTreeVerification(hdb *HostDB, numEntries int, t *testing.T) {
if !testing.Short() {
// Pull a bunch of random hosts and count how many times we pull each
// host.
selectionMap := make(map[string]int)
selectionMap := make(map[network.Address]int)
expected := 100
for i := 0; i < expected*numEntries; i++ {
entry, err := hdb.RandomHost()
if err != nil {
t.Fatal(err)
}
selectionMap[entry.ID] = selectionMap[entry.ID] + 1
selectionMap[entry.IPAddress] = selectionMap[entry.IPAddress] + 1
}
// See if each host was selected enough times.
......@@ -71,7 +72,7 @@ func TestWeightedList(t *testing.T) {
entry.Burn = 10
entry.Price = 10
entry.Freeze = 10
entry.ID = strconv.Itoa(i)
entry.IPAddress = network.Address(strconv.Itoa(i))
hdb.Insert(entry)
}
uniformTreeVerification(hdb, firstInsertions, t)
......@@ -96,7 +97,7 @@ func TestWeightedList(t *testing.T) {
}
// Remove the entry and add it to the list of removed entries
err := hdb.Remove(strconv.Itoa(randInt))
err := hdb.Remove(network.Address(strconv.Itoa(randInt)))
if err != nil {
t.Fatal(err)
}
......@@ -111,7 +112,7 @@ func TestWeightedList(t *testing.T) {
entry.Burn = 10
entry.Price = 10
entry.Freeze = 10
entry.ID = strconv.Itoa(i)
entry.IPAddress = network.Address(strconv.Itoa(i))
hdb.Insert(entry)
}
uniformTreeVerification(hdb, firstInsertions-removals+secondInsertions, t)
......
......@@ -296,7 +296,7 @@ func (r *Renter) proposeSmallContract(fullFile []byte, duration consensus.BlockH
}
fmt.Println("Problem from NegotiateContract:", err)
err = r.hostDB.FlagHost(host.ID)
err = r.hostDB.FlagHost(host.IPAddress)
if err != nil {
return
}
......
......@@ -119,7 +119,7 @@ func (r *Renter) Download(nickname, filename string) (err error) {
return
} else {
fmt.Println("Renter got error:", err)
r.hostDB.FlagHost(piece.Host.ID)
r.hostDB.FlagHost(piece.Host.IPAddress)
}
}
......
......@@ -6,18 +6,18 @@ import (
)
func (d *daemon) hostConfigHandler(w http.ResponseWriter, req *http.Request) {
// load current announcement
ha := d.host.Info().HostAnnouncement
// load current settings
config := d.host.Info().HostEntry
// map each query string to a field in the host announcement object
qsVars := map[string]interface{}{
"totalstorage": &ha.TotalStorage,
"minfilesize": &ha.MinFilesize,
"maxfilesize": &ha.MaxFilesize,
"minduration": &ha.MinDuration,
"maxduration": &ha.MaxDuration,
"price": &ha.Price,
"burn": &ha.Burn,
"totalstorage": &config.TotalStorage,
"minfilesize": &config.MinFilesize,
"maxfilesize": &config.MaxFilesize,
"minduration": &config.MinDuration,
"maxduration": &config.MaxDuration,
"price": &config.Price,
"burn": &config.Burn,
}
for qs := range qsVars {
......@@ -31,7 +31,7 @@ func (d *daemon) hostConfigHandler(w http.ResponseWriter, req *http.Request) {
}
}
d.host.SetAnnouncement(ha)
d.host.SetConfig(config)
writeSuccess(w)
}
......
......@@ -89,7 +89,7 @@ func newDaemon(config DaemonConfig) (d *daemon, err error) {
if err != nil {
return
}
err = d.network.RegisterRPC("AcceptTransaction", d.state.AcceptTransaction)
err = d.network.RegisterRPC("AcceptTransaction", d.tpool.AcceptTransaction)
if err != nil {
return
}
......
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