Skip to content

Commit

Permalink
add helpers on Host for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 authored and ChrisSchinnerl committed Nov 19, 2024
1 parent e28f25f commit a0c70f7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
24 changes: 16 additions & 8 deletions explorer/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (e *Explorer) scanV1Host(locator geoip.Locator, host Host) (HostScan, error
}

resolved, err := net.ResolveIPAddr("ip", hostIP)
// if we can resolve the address
if err != nil {
return HostScan{}, fmt.Errorf("scanHost: failed to resolve host address: %w", err)
}
Expand Down Expand Up @@ -118,7 +117,11 @@ func (e *Explorer) scanV2Host(locator geoip.Locator, host Host) (HostScan, error
ctx, cancel := context.WithTimeout(e.ctx, e.scanCfg.Timeout)
defer cancel()

addr := host.V2NetAddresses[0].Address
addr, ok := host.V2SiamuxAddr()
if !ok {
return HostScan{}, fmt.Errorf("host has no v2 siamux address")
}

transport, err := crhpv4.DialSiaMux(ctx, addr, host.PublicKey)
if err != nil {
return HostScan{}, fmt.Errorf("failed to dial host: %w", err)
Expand All @@ -136,7 +139,6 @@ func (e *Explorer) scanV2Host(locator geoip.Locator, host Host) (HostScan, error
}

resolved, err := net.ResolveIPAddr("ip", hostIP)
// if we can resolve the address
if err != nil {
return HostScan{}, fmt.Errorf("scanHost: failed to resolve host address: %w", err)
}
Expand Down Expand Up @@ -175,13 +177,19 @@ func (e *Explorer) addHostScans(hosts chan Host) {

var scan HostScan
var addr string
var ok bool
var err error
if len(host.V2NetAddresses) == 0 {
addr = host.NetAddress
scan, err = e.scanV1Host(locator, host)

if host.IsV2() {
addr, ok = host.V2SiamuxAddr()
if !ok {
e.log.Debug("Host did not have any v2 siamux net addresses in its announcement, unable to scan", zap.Stringer("pk", host.PublicKey))
continue
} else {
scan, err = e.scanV2Host(locator, host)
}
} else {
addr = host.V2NetAddresses[0].Address
scan, err = e.scanV2Host(locator, host)
scan, err = e.scanV1Host(locator, host)
}
if err != nil {
scans = append(scans, HostScan{
Expand Down
18 changes: 18 additions & 0 deletions explorer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
rhpv4 "go.sia.tech/core/rhp/v4"
"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
crhpv4 "go.sia.tech/coreutils/rhp/v4"
)

// A Source represents where a siacoin output came from.
Expand Down Expand Up @@ -336,6 +337,23 @@ type Host struct {
RHPV4Settings rhpv4.HostSettings `json:"rhpV4Settings"`
}

// V2SiaMuxAddr returns the `Address` of the first TCP siamux `NetAddress` it
// finds in the host's list of net addresses. The protocol for this address is
// ProtocolTCPSiaMux.
func (h Host) V2SiamuxAddr() (string, bool) {
for _, netAddr := range h.V2NetAddresses {
if netAddr.Protocol == crhpv4.ProtocolTCPSiaMux {
return netAddr.Address, true
}
}
return "", false
}

// IsV2 returns whether a host supports V2 or not.
func (h Host) IsV2() bool {
return len(h.V2NetAddresses) > 0
}

// HostMetrics represents averages of scanned information from hosts.
type HostMetrics struct {
ActiveHosts uint64 `json:"activeHosts"`
Expand Down

0 comments on commit a0c70f7

Please sign in to comment.