Commit 8f77995f authored by David Vorick's avatar David Vorick

renter downloads can now follow dynamic IPs

parent 50dd615b
package api
import (
"bytes"
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"testing"
"time"
)
// TestHostDBHostsActiveHandler checks the behavior of the call to
......@@ -218,3 +223,153 @@ func TestHostDBHostsHandler(t *testing.T) {
t.Error("One value in host score breakdown")
}
}
// TestHostDBAndRenterDynamicIPs checks that the hostdb and the renter are
// successfully able to follow a host that has changed IP addresses and then
// re-announced.
func TestHostDBAndRenterDynamicIPs(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
st, err := createServerTester("TestHostDBAndRenterDynamicIPs")
if err != nil {
t.Fatal(err)
}
stHost, err := blankServerTester("TestHostDBAndRenterDynamicIPs-Host")
if err != nil {
t.Fatal(err)
}
sts := []*serverTester{st, stHost}
err = fullyConnectNodes(sts)
if err != nil {
t.Fatal(err)
}
err = fundAllNodes(sts)
if err != nil {
t.Fatal(err)
}
// Announce the host.
err = stHost.acceptContracts()
if err != nil {
t.Fatal(err)
}
err = stHost.setHostStorage()
if err != nil {
t.Fatal(err)
}
err = stHost.announceHost()
if err != nil {
t.Fatal(err)
}
// Pull the host's net address and pubkey from the hostdb.
var ah HostdbActiveGET
if err = st.getAPI("/hostdb/active", &ah); err != nil {
t.Fatal(err)
}
if len(ah.Hosts) != 1 {
t.Fatalf("expected 1 host, got %v", len(ah.Hosts))
}
addr := ah.Hosts[0].NetAddress
pks := ah.Hosts[0].PublicKeyString
// Upload a file to the host.
allowanceValues := url.Values{}
testFunds := "10000000000000000000000000000" // 10k SC
testPeriod := "10"
allowanceValues.Set("funds", testFunds)
allowanceValues.Set("period", testPeriod)
err = st.stdPostAPI("/renter", allowanceValues)
if err != nil {
t.Fatal(err)
}
// Create a file.
path := filepath.Join(st.dir, "test.dat")
err = createRandFile(path, 1024)
if err != nil {
t.Fatal(err)
}
// Upload the file to the renter.
uploadValues := url.Values{}
uploadValues.Set("source", path)
err = st.stdPostAPI("/renter/upload/test", uploadValues)
if err != nil {
t.Fatal(err)
}
// Only one piece will be uploaded (10% at current redundancy).
var rf RenterFiles
for i := 0; i < 200 && (len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10); i++ {
st.getAPI("/renter/files", &rf)
time.Sleep(100 * time.Millisecond)
}
if len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10 {
t.Fatal("the uploading is not succeeding for some reason:", rf.Files[0])
}
// Try downloading the file.
downpath := filepath.Join(st.dir, "testdown.dat")
err = st.stdGetAPI("/renter/download/test?destination=" + downpath)
if err != nil {
t.Fatal(err)
}
// Check that the download has the right contents.
orig, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
download, err := ioutil.ReadFile(downpath)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(orig, download) != 0 {
t.Fatal("data mismatch when downloading a file")
}
// Close and re-open the host. This should reset the host's address, as the
// host should now be on a new port.
err = stHost.server.Close()
if err != nil {
t.Fatal(err)
}
stHost, err = assembleServerTester(stHost.walletKey, stHost.dir)
if err != nil {
t.Fatal(err)
}
sts[1] = stHost
err = fullyConnectNodes(sts)
if err != nil {
t.Fatal(err)
}
err = stHost.announceHost()
if err != nil {
t.Fatal(err)
}
// Pull the host's net address and pubkey from the hostdb.
if err = st.getAPI("/hostdb/active", &ah); err != nil {
t.Fatal(err)
}
if len(ah.Hosts) != 1 {
t.Fatalf("expected 1 host, got %v", len(ah.Hosts))
}
if ah.Hosts[0].PublicKeyString != pks {
t.Error("public key appears to have changed for host")
}
if ah.Hosts[0].NetAddress == addr {
t.Log("NetAddress did not change for the new host")
}
// Try downloading the file.
err = st.stdGetAPI("/renter/download/test?destination=" + downpath)
if err != nil {
t.Fatal(err)
}
// Check that the download has the right contents.
download, err = ioutil.ReadFile(downpath)
if err != nil {
t.Fatal(err)
}
if bytes.Compare(orig, download) != 0 {
t.Fatal("data mismatch when downloading a file")
}
}
......@@ -521,7 +521,7 @@ func (st *serverTester) announceHost() error {
acceptingContractsValues.Set("acceptingcontracts", "true")
err := st.stdPostAPI("/host", acceptingContractsValues)
if err != nil {
return err
return build.ExtendErr("couldn't make an api call to the host:", err)
}
announceValues := url.Values{}
......
......@@ -167,6 +167,13 @@ func (c *Contractor) Downloader(id types.FileContractID) (_ Downloader, err erro
}
}
// Update the contract to the most recent net address for the host.
host, exists := c.hdb.Host(contract.HostPublicKey)
if !exists {
return nil, errors.New("unable to find the contract's host in the host database")
}
contract.NetAddress = host.NetAddress
// create downloader
d, err := proto.NewDownloader(host, contract)
if proto.IsRevisionMismatch(err) {
......
......@@ -30,8 +30,6 @@ type hdbTester struct {
// bareHostDB returns a HostDB with its fields initialized, but without any
// dependencies or scanning threads. It is only intended for use in unit tests.
//
// TODO: purge
func bareHostDB() *HostDB {
hdb := &HostDB{
log: persist.NewLogger(ioutil.Discard),
......
......@@ -25,10 +25,6 @@ func (quitAfterLoadDeps) disrupt(s string) bool {
}
// TestSaveLoad tests that the hostdb can save and load itself.
//
// TODO: By extending the hdbTester and adding some helper functions, we can
// eliminate the necessary disruption by adding real hosts + blocks instead of
// fake ones.
func TestSaveLoad(t *testing.T) {
if testing.Short() {
t.SkipNow()
......
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