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

Add chain package #1178

Merged
merged 7 commits into from
Apr 30, 2024
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: 2 additions & 0 deletions api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
ErrContractSetNotFound = errors.New("couldn't find contract set")
)

type ContractState string

type (
// A Contract wraps the contract metadata with the latest contract revision.
Contract struct {
Expand Down
10 changes: 10 additions & 0 deletions chain/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package chain

import (
"go.sia.tech/coreutils/chain"
)

type (
Manager = chain.Manager
HostAnnouncement = chain.HostAnnouncement
)
29 changes: 29 additions & 0 deletions chain/subscriber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package chain

import (
"time"

"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
"go.sia.tech/renterd/api"
)

type (
ChainStore interface {
BeginChainUpdateTx() (ChainUpdateTx, error)
ChainIndex() (types.ChainIndex, error)
}

ChainUpdateTx interface {
Commit() error
Rollback() error

ContractState(fcid types.FileContractID) (api.ContractState, error)
UpdateChainIndex(index types.ChainIndex) error
UpdateContract(fcid types.FileContractID, revisionHeight, revisionNumber, size uint64) error
UpdateContractState(fcid types.FileContractID, state api.ContractState) error
UpdateContractProofHeight(fcid types.FileContractID, proofHeight uint64) error
UpdateFailedContracts(blockHeight uint64) error
UpdateHost(hk types.PublicKey, ha chain.HostAnnouncement, bh uint64, blockID types.BlockID, ts time.Time) error
}
)
20 changes: 17 additions & 3 deletions internal/test/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ func newTestCluster(t *testing.T, opts testClusterOptions) *TestCluster {

if nHosts > 0 {
cluster.AddHostsBlocking(nHosts)
cluster.WaitForPeers()
cluster.WaitForContracts()
cluster.WaitForContractSet(test.ContractSet, nHosts)
cluster.WaitForAccounts()
Expand Down Expand Up @@ -657,6 +658,19 @@ func (c *TestCluster) WaitForContractSetContracts(set string, n int) {
})
}

func (c *TestCluster) WaitForPeers() {
c.tt.Helper()
c.tt.Retry(300, 100*time.Millisecond, func() error {
peers, err := c.Bus.SyncerPeers(context.Background())
if err != nil {
return err
} else if len(peers) == 0 {
return errors.New("no peers found")
}
return nil
})
}

func (c *TestCluster) RemoveHost(host *Host) {
c.tt.Helper()
c.tt.OK(host.Close())
Expand Down Expand Up @@ -687,15 +701,15 @@ func (c *TestCluster) AddHost(h *Host) {
c.hosts = append(c.hosts, h)

// Fund host from bus.
fundAmt := types.Siacoins(100e3)
fundAmt := types.Siacoins(25e3)
var scos []types.SiacoinOutput
for i := 0; i < 10; i++ {
scos = append(scos, types.SiacoinOutput{
Value: fundAmt,
Value: fundAmt.Div64(10),
Address: h.WalletAddress(),
})
}
c.tt.OK(c.Bus.SendSiacoins(context.Background(), scos, false))
c.tt.OK(c.Bus.SendSiacoins(context.Background(), scos, true))

// Mine transaction.
c.MineBlocks(1)
Expand Down
25 changes: 4 additions & 21 deletions internal/test/e2e/pruning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/internal/test"
"go.uber.org/zap/zapcore"
)

func TestHostPruning(t *testing.T) {
Expand All @@ -21,14 +20,11 @@ func TestHostPruning(t *testing.T) {
}

// create a new test cluster
opts := clusterOptsDefault
opts.logger = newTestLoggerCustom(zapcore.DebugLevel)
cluster := newTestCluster(t, opts)
cluster := newTestCluster(t, testClusterOptions{hosts: 1})
defer cluster.Shutdown()

// convenience variables
b := cluster.Bus
w := cluster.Worker
a := cluster.Autopilot
tt := cluster.tt

Expand All @@ -48,26 +44,13 @@ func TestHostPruning(t *testing.T) {
tt.OK(b.RecordHostScans(context.Background(), his))
}

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

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

// scan the host (lastScan needs to be > 0 for downtime to start counting)
tt.OKAll(w.RHPScan(context.Background(), h1.PublicKey(), h.NetAddress, 0))

// block the host
tt.OK(b.UpdateHostBlocklist(context.Background(), []string{h1.PublicKey().String()}, nil, false))
// shut down the worker manually, this will flush any interactions
cluster.ShutdownWorker(context.Background())

// remove it from the cluster manually
h1 := cluster.hosts[0]
cluster.RemoveHost(h1)

// shut down the worker manually, this will flush any interactions
cluster.ShutdownWorker(context.Background())

// record 9 failed interactions, right before the pruning threshold, and
// wait for the autopilot loop to finish at least once
recordFailedInteractions(9, h1.PublicKey())
Expand Down
Loading
Loading