Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
Merge lukechampine/us#142 into fix_1
Browse files Browse the repository at this point in the history
  • Loading branch information
jkawamoto committed Oct 15, 2020
2 parents 4a065f2 + df1720d commit b74eb81
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 48 deletions.
2 changes: 2 additions & 0 deletions ghost/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/ed25519"
"log"
"net"
"sync"

"gitlab.com/NebulousLabs/Sia/crypto"
"gitlab.com/NebulousLabs/Sia/modules"
Expand All @@ -23,6 +24,7 @@ type hostContract struct {
renterKey types.SiaPublicKey
sectorRoots []crypto.Hash
sectorData map[crypto.Hash][renterhost.SectorSize]byte
mu sync.Mutex
}

type Host struct {
Expand Down
45 changes: 17 additions & 28 deletions ghost/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func (h *Host) handleConn(conn net.Conn) error {
sess: hs,
conn: conn,
}
defer func() {
if s.contract != nil {
s.contract.mu.Unlock()
s.contract = nil
}
}()

rpcs := map[renterhost.Specifier]func(*session) error{
renterhost.RPCFormContractID: h.rpcFormContract,
Expand Down Expand Up @@ -278,6 +284,7 @@ func (h *Host) rpcLock(s *session) error {
s.sess.WriteResponse(nil, err)
return err
}
contract.mu.Lock()
s.contract = contract

var newChallenge [16]byte
Expand All @@ -293,6 +300,9 @@ func (h *Host) rpcLock(s *session) error {
}

func (h *Host) rpcUnlock(s *session) error {
if s.contract != nil {
s.contract.mu.Unlock()
}
s.contract = nil
return nil
}
Expand Down Expand Up @@ -565,31 +575,20 @@ func (h *Host) rpcRead(s *session) error {
return err
}

// As soon as we finish reading the request, we must begin listening for
// RPCLoopReadStop, which may arrive at any time, and must arrive before the
// RPC is considered complete.
stopSignal := make(chan error, 1)
go func() {
var id renterhost.Specifier
err := s.sess.ReadResponse(&id, 4096)
if err != nil {
stopSignal <- err
} else if id != renterhost.RPCReadStop {
stopSignal <- errors.New("expected 'stop' from renter, got " + id.String())
} else {
stopSignal <- nil
}
}()
// Make sure we read RPCLoopReadStop before returning.
//
// NOTE: technically, we should be listening for RPCLoopReadStop
// asynchronously, but currently this is not possible because
// renterhost.Session is not thread-safe.
defer s.sess.ReadResponse(new(renterhost.Specifier), 4096)

if s.contract == nil {
err := errors.New("no contract locked")
s.sess.WriteResponse(nil, err)
<-stopSignal
return err
} else if s.contract.rev.NewRevisionNumber == math.MaxUint64 {
err := errors.New("contract cannot be revised")
s.sess.WriteResponse(nil, err)
<-stopSignal
return err
}

Expand Down Expand Up @@ -682,22 +681,12 @@ func (h *Host) rpcRead(s *session) error {
Data: data,
MerkleProof: proof,
}
select {
case err := <-stopSignal:
if err != nil {
return err
}
resp.Signature = hostSig
return s.sess.WriteResponse(resp, nil)
default:
}
if i == len(req.Sections)-1 {
resp.Signature = hostSig
}
if err := s.sess.WriteResponse(resp, nil); err != nil {
return err
}
}
// The stop signal must arrive before RPC is complete.
return <-stopSignal
return nil
}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ go 1.15

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.0
github.com/pkg/errors v0.9.1
gitlab.com/NebulousLabs/Sia v1.4.8
gitlab.com/NebulousLabs/bolt v1.4.4
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe
go.etcd.io/bbolt v1.3.5
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211
lukechampine.com/frand v1.3.0
)
17 changes: 4 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ github.com/hanwen/go-fuse/v2 v2.0.2 h1:BtsqKI5RXOqDMnTgpCb0IWgvRgGLJdqYVZ/Hm6KgK
github.com/hanwen/go-fuse/v2 v2.0.2/go.mod h1:HH3ygZOoyRbP9y2q7y3+JM6hPL+Epe29IbWaS0UA81o=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
Expand Down Expand Up @@ -78,8 +76,6 @@ gitlab.com/NebulousLabs/Sia v1.4.8 h1:NBBawRM0JvyD1Z1jB5er+4byrQ9B8jpqE7kfNbOHw2
gitlab.com/NebulousLabs/Sia v1.4.8/go.mod h1:AlAQ634YMvZ040N8ffji8u0oZdFLzlzs0ogUzIZ7Thg=
gitlab.com/NebulousLabs/bolt v1.4.0 h1:6sfFp1YQtGWbSLLYoH8+0h3EtFRGbsp07L3uZNChdE0=
gitlab.com/NebulousLabs/bolt v1.4.0/go.mod h1:72gB2R0hTcUU2Ih7mBpHF0jJlIldSyPzG1cuwz1uYJY=
gitlab.com/NebulousLabs/bolt v1.4.4 h1:3UhpR2qtHs87dJBE3CIzhw48GYSoUUNByJmic0cbu1w=
gitlab.com/NebulousLabs/bolt v1.4.4/go.mod h1:ZL02cwhpLNif6aruxvUMqu/Bdy0/lFY21jMFfNAA+O8=
gitlab.com/NebulousLabs/demotemutex v0.0.0-20151003192217-235395f71c40 h1:IbucNi8u1a1ErgVFVgg8pERhSyzYe5l+o8krDMnNjWA=
gitlab.com/NebulousLabs/demotemutex v0.0.0-20151003192217-235395f71c40/go.mod h1:HfnnxM8isYA7FUlqS5h34XTeiBhPtcuCquVujKsn9aw=
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe h1:vylvMCgxVPYojpQ2p536xDooW/B3znEnw58mCxrlZow=
Expand Down Expand Up @@ -121,13 +117,11 @@ golang.org/x/crypto v0.0.0-20191107222254-f4817d981bb6/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200311171314-f7b00557c8c4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzhtallWRSm4Q0d09pL6XbQtU=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI=
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -136,15 +130,12 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634 h1:bNEHhJCnrwMKNMmOx3yAynp5vs5/gRy+XWFtZFu7NBM=
golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
Expand Down
2 changes: 1 addition & 1 deletion renter/renterutil/metadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"gitlab.com/NebulousLabs/Sia/crypto"
"gitlab.com/NebulousLabs/Sia/encoding"
"gitlab.com/NebulousLabs/bolt"
bolt "go.etcd.io/bbolt"
"lukechampine.com/us/hostdb"
"lukechampine.com/us/renter"
)
Expand Down
7 changes: 6 additions & 1 deletion renter/renterutil/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ func TestMigrate(t *testing.T) {
t.Fatal(err)
}

// close all hs1 sessions
if err := hs1.Close(); err != nil {
t.Fatal(err)
}

// create fs2 with hs2
fs2 := NewFileSystem(os.TempDir(), hs2)
defer fs2.Close()

// close one of the non-hs2 hosts; this ensures that we'll download from the new host
// remove one of the non-hs2 hosts; this ensures that we'll download from the new host
for hostKey, lh := range fs1.hosts.sessions {
if fs2.hosts.HasHost(hostKey) {
lh.s.Close()
Expand Down
2 changes: 1 addition & 1 deletion renterhost/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ type (
type Specifier [16]byte

func (s Specifier) String() string {
return string(bytes.Trim(s[:], string(0)))
return string(bytes.Trim(s[:], "\x00"))
}

func newSpecifier(str string) Specifier {
Expand Down

0 comments on commit b74eb81

Please sign in to comment.