Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record Interactions #783

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (wp *workerPool) withWorkers(workerFunc func([]Worker)) {

// Handler returns an HTTP handler that serves the autopilot api.
func (ap *Autopilot) Handler() http.Handler {
return jape.Mux(tracing.TracedRoutes(api.DefaultAutopilotID, map[string]jape.Handler{
return jape.Mux(tracing.TracingMiddleware(api.DefaultAutopilotID, map[string]jape.Handler{
"GET /config": ap.configHandlerGET,
"PUT /config": ap.configHandlerPUT,
"POST /hosts": ap.hostsHandlerPOST,
Expand Down
2 changes: 1 addition & 1 deletion autopilot/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func (c *contractor) runContractChecks(ctx context.Context, w Worker, contracts
toArchive[fcid] = errContractMaxRevisionNumber.Error()
} else if contract.RevisionNumber == math.MaxUint64 {
toArchive[fcid] = errContractMaxRevisionNumber.Error()
} else if contract.State == api.ContractStatePending && cs.BlockHeight-contract.StartHeight > 18 {
} else if contract.State == api.ContractStatePending && cs.BlockHeight-contract.StartHeight > contractConfirmationDeadline {
toArchive[fcid] = errContractNotConfirmed.Error()
}
if _, archived := toArchive[fcid]; archived {
Expand Down
2 changes: 1 addition & 1 deletion bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ type bus struct {

// Handler returns an HTTP handler that serves the bus API.
func (b *bus) Handler() http.Handler {
return jape.Mux(tracing.TracedRoutes("bus", map[string]jape.Handler{
return jape.Mux(tracing.TracingMiddleware("bus", map[string]jape.Handler{
"GET /accounts": b.accountsHandlerGET,
"POST /account/:id": b.accountHandlerGET,
"POST /account/:id/add": b.accountsAddHandlerPOST,
Expand Down
24 changes: 0 additions & 24 deletions hostdb/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@ type hostAnnouncement struct {
Signature types.Signature
}

type ErrorResult struct {
Error string `json:"error,omitempty"`
}

type MetricResultCommon struct {
Address string `json:"address"`
Timestamp time.Time `json:"timestamp"`
Elapsed time.Duration `json:"elapsed"`
}

type ScanResult struct {
ErrorResult
PriceTable rhpv3.HostPriceTable `json:"priceTable,omitempty"`
Settings rhpv2.HostSettings `json:"settings,omitempty"`
}

type PriceTableUpdateResult struct {
ErrorResult
PriceTable HostPriceTable `json:"priceTable,omitempty"`
}

const InteractionTypeScan = "scan"
const InteractionTypePriceTableUpdate = "pricetableupdate"

// ForEachAnnouncement calls fn on each host announcement in a block.
func ForEachAnnouncement(b types.Block, height uint64, fn func(types.PublicKey, Announcement)) {
for _, txn := range b.Transactions {
Expand Down
91 changes: 91 additions & 0 deletions internal/testing/interactions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package testing

import (
"context"
"errors"
"testing"
"time"

rhpv3 "go.sia.tech/core/rhp/v3"
)

func TestInteractions(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

// create a new test cluster
cluster := newTestCluster(t, clusterOptsDefault)
defer cluster.Shutdown()
b := cluster.Bus
w := cluster.Worker
tt := cluster.tt

// add a host
hosts := cluster.AddHosts(1)
h1 := hosts[0]

// wait for contracts
cluster.WaitForContracts()

// shut down the autopilot to prevent it from interfering with the test
cluster.ShutdownAutopilot(context.Background())
time.Sleep(time.Second)

// fetch the host
h, err := b.Host(context.Background(), h1.PublicKey())
tt.OK(err)

// assert the host got scanned at least once
ts := h.Interactions.TotalScans
if ts == 0 {
t.Fatal("expected at least one scan")
}

// assert the price table was set
ptUID := h.PriceTable.UID
if ptUID == (rhpv3.SettingsID{}) {
t.Fatal("expected pt UID to be set")
}

// assert price table gets updated
var ptUpdates int
tt.Retry(100, 100*time.Millisecond, func() error {
// fetch contracts (this registers host interactions)
tt.OKAll(w.Contracts(context.Background(), time.Minute))

// fetch the host
h, err := b.Host(context.Background(), h1.PublicKey())
tt.OK(err)

// make sure it did not get scanned again
if h.Interactions.TotalScans != ts {
t.Fatal("expected no new scans", h.Interactions.TotalScans, ts)
}

// keep track of pt updates
if h.PriceTable.UID != ptUID {
ptUID = h.PriceTable.UID
ptUpdates++
}

// assert the price table was updated
if ptUpdates < 2 {
return errors.New("price table should be updated from time to time")
}
return nil
})

// scan the host manually
tt.OKAll(w.RHPScan(context.Background(), h1.PublicKey(), h.NetAddress, 0))
time.Sleep(3 * testBusFlushInterval)

// fetch the host
h, err = b.Host(context.Background(), h1.PublicKey())
tt.OK(err)

// assert the scan was registered
if ts+1 != h.Interactions.TotalScans {
t.Fatal("expected one new scan")
}
}
38 changes: 0 additions & 38 deletions metrics/metrics.go

This file was deleted.

12 changes: 6 additions & 6 deletions stores/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func (currency) GormDataType() string {
// Scan scan value into currency, implements sql.Scanner interface.
func (c *currency) Scan(value interface{}) error {
var s string
switch value.(type) {
switch value := value.(type) {
case string:
s = value.(string)
s = value
case []byte:
s = string(value.([]byte))
s = string(value)
default:
return fmt.Errorf("failed to unmarshal currency value: %v %t", value, value)
}
Expand Down Expand Up @@ -202,11 +202,11 @@ func (balance) GormDataType() string {
// Scan scan value into balance, implements sql.Scanner interface.
func (hs *balance) Scan(value interface{}) error {
var s string
switch value.(type) {
switch value := value.(type) {
case string:
s = value.(string)
s = value
case []byte:
s = string(value.([]byte))
s = string(value)
default:
return fmt.Errorf("failed to unmarshal balance value: %v %t", value, value)
}
Expand Down
4 changes: 2 additions & 2 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func Init(serviceInstanceId string) (func(ctx context.Context) error, error) {
return provider.Shutdown, nil
}

// TracedHandler attaches a tracing handler to http routes.
func TracedRoutes(component string, routes map[string]jape.Handler) map[string]jape.Handler {
// TracingMiddleware attaches a tracing handler to http routes.
func TracingMiddleware(component string, routes map[string]jape.Handler) map[string]jape.Handler {
adapt := func(route string, h jape.Handler) jape.Handler {
return jape.Adapt(func(h http.Handler) http.Handler {
return otelhttp.NewHandler(h, fmt.Sprintf("%s: %s", component, route))
Expand Down
7 changes: 7 additions & 0 deletions worker/gouging.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,10 @@ func sectorUploadCostRHPv3(pt rhpv3.HostPriceTable) (types.Currency, bool) {
}
return total, false
}

func errToStr(err error) string {
if err != nil {
return err.Error()
}
return ""
}
Loading