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

use the host's block height when setting an expiry on EA withdrawal messages #912

Merged
merged 6 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 1 addition & 3 deletions worker/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"context"
"errors"
"testing"
"time"

rhpv2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/core/types"
)

func TestHost(t *testing.T) {
c := newMockContract(types.FileContractID{1})
h := newMockHost(types.PublicKey{1}, newTestHostPriceTable(time.Now()), c)
h := newMockHost(true)
sector, root := newMockSector()

// upload the sector
Expand Down
2 changes: 1 addition & 1 deletion worker/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SHARDS:
}

// migrate the shards
err = w.uploadManager.MigrateShards(ctx, s, shardIndices, shards, contractSet, allowed, bh, lockingPriorityUpload, mem)
err = w.uploadManager.UploadShards(ctx, s, shardIndices, shards, contractSet, allowed, bh, lockingPriorityUpload, mem)
if err != nil {
return 0, surchargeApplied, fmt.Errorf("failed to upload slab for migration: %w", err)
}
Expand Down
118 changes: 73 additions & 45 deletions worker/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
)

type (
contractsMap map[types.PublicKey]api.ContractMetadata

mockContract struct {
rev types.FileContractRevision
rev types.FileContractRevision
metadata api.ContractMetadata

mu sync.Mutex
sectors map[types.Hash256]*[rhpv2.SectorSize]byte
Expand All @@ -43,8 +42,11 @@ type (
hptBlockChan chan struct{}
}

mockHosts []*mockHost
mockContracts []*mockContract

mockHostManager struct {
hosts map[types.PublicKey]Host
hosts map[types.PublicKey]*mockHost
}

mockMemory struct{}
Expand Down Expand Up @@ -72,8 +74,6 @@ type (

dl *downloadManager
ul *uploadManager

contracts contractsMap
}
)

Expand All @@ -94,14 +94,38 @@ var (
errSectorOutOfBounds = errors.New("sector out of bounds")
)

func (c contractsMap) values() []api.ContractMetadata {
var contracts []api.ContractMetadata
for _, contract := range c {
contracts = append(contracts, contract)
var mockContractCntr = 0
peterjan marked this conversation as resolved.
Show resolved Hide resolved

func newFileContractID() (fcid types.FileContractID) {
fcid = types.FileContractID{byte(mockContractCntr)}
mockContractCntr++
return
}

var mockHostCntr = 0

func newHostKey() (hk types.PublicKey) {
hk = types.PublicKey{byte(mockHostCntr)}
mockHostCntr++
return
}

func (hosts mockHosts) contracts() mockContracts {
contracts := make([]*mockContract, len(hosts))
for i, host := range hosts {
contracts[i] = host.c
}
return contracts
}

func (contracts mockContracts) metadata() []api.ContractMetadata {
metadata := make([]api.ContractMetadata, len(contracts))
for i, contract := range contracts {
metadata[i] = contract.metadata
}
return metadata
}

func (m *mockMemory) Release() {}
func (m *mockMemory) ReleaseSome(uint64) {}

Expand Down Expand Up @@ -340,6 +364,15 @@ func (h *mockHost) FundAccount(ctx context.Context, balance types.Currency, rev
}

func (h *mockHost) RenewContract(ctx context.Context, rrr api.RHPRenewRequest) (_ rhpv2.ContractRevision, _ []types.Transaction, _ types.Currency, err error) {
h.mu.Lock()
defer h.mu.Unlock()

curr := h.c.metadata
update := newMockContract(h.hk)
update.metadata.RenewedFrom = curr.ID
update.metadata.WindowStart = curr.WindowEnd
update.metadata.WindowEnd = update.metadata.WindowStart + (curr.WindowEnd - curr.WindowStart)
h.c = update
return
}

Expand Down Expand Up @@ -377,34 +410,33 @@ func (cs *mockContractStore) KeepaliveContract(ctx context.Context, fcid types.F
return nil
}

func newMockHosts(n int) []*mockHost {
hosts := make([]*mockHost, n)
for i := range hosts {
hosts[i] = newMockHost(types.PublicKey{byte(i)}, newTestHostPriceTable(time.Now().Add(time.Minute)), nil)
func newMockHost(contract bool) *mockHost {
host := &mockHost{
hk: newHostKey(),
hpt: newTestHostPriceTable(time.Now().Add(time.Minute)),
}
return hosts
}

func newMockHost(hk types.PublicKey, hpt hostdb.HostPriceTable, c *mockContract) *mockHost {
return &mockHost{
hk: hk,
c: c,

hpt: hpt,
if contract {
host.c = newMockContract(host.hk)
}
return host
}

func newMockContracts(hosts []*mockHost) []*mockContract {
contracts := make([]*mockContract, len(hosts))
for i := range contracts {
contracts[i] = newMockContract(types.FileContractID{byte(i)})
hosts[i].c = contracts[i]
func newMockHosts(n int, contract bool) (hosts mockHosts) {
for i := 0; i < n; i++ {
hosts = append(hosts, newMockHost(contract))
}
return contracts
return
}

func newMockContract(fcid types.FileContractID) *mockContract {
func newMockContract(hk types.PublicKey) *mockContract {
fcid := newFileContractID()
return &mockContract{
metadata: api.ContractMetadata{
ID: fcid,
HostKey: hk,
WindowStart: 0,
WindowEnd: 10,
},
rev: types.FileContractRevision{ParentID: fcid},
sectors: make(map[types.Hash256]*[rhpv2.SectorSize]byte),
}
Expand All @@ -419,7 +451,7 @@ func newMockContractStore(contracts []*mockContract) *mockContractStore {
}

func newMockHostManager(hosts []*mockHost) *mockHostManager {
hm := &mockHostManager{hosts: make(map[types.PublicKey]Host)}
hm := &mockHostManager{hosts: make(map[types.PublicKey]*mockHost)}
for _, h := range hosts {
hm.hosts[h.hk] = h
}
Expand All @@ -439,38 +471,34 @@ func newMockSector() (*[rhpv2.SectorSize]byte, types.Hash256) {
}

func newMockWorker(numHosts int) *mockWorker {
// create hosts and contracts
hosts := newMockHosts(numHosts)
contracts := newMockContracts(hosts)
// create hosts
hosts := newMockHosts(numHosts, true)

// create dependencies
cs := newMockContractStore(contracts)
cs := newMockContractStore(hosts.contracts())
hm := newMockHostManager(hosts)
os := newMockObjectStore()
mm := &mockMemoryManager{}

dl := newDownloadManager(context.Background(), hm, mm, os, 0, 0, zap.NewNop().Sugar())
ul := newUploadManager(context.Background(), hm, mm, os, cs, 0, 0, time.Minute, zap.NewNop().Sugar())

// create contract metadata
metadatas := make(contractsMap)
for _, h := range hosts {
metadatas[h.hk] = api.ContractMetadata{
ID: h.c.rev.ParentID,
HostKey: h.hk,
}
}

return &mockWorker{
cs: cs,
hm: hm,
mm: mm,
os: os,

dl: dl,
ul: ul,
}
}

contracts: metadatas,
func (w *mockWorker) contracts() (metadatas []api.ContractMetadata) {
for _, h := range w.hm.hosts {
metadatas = append(metadatas, h.c.metadata)
}
return
}

func newTestHostPriceTable(expiry time.Time) hostdb.HostPriceTable {
Expand Down
11 changes: 6 additions & 5 deletions worker/pricetables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ func newMockHostStore(hosts []*hostdb.HostInfo) *mockHostStore {
}

func TestPriceTables(t *testing.T) {
// create two price tables
// create two price tables, a valid one and one that expired
expiredPT := newTestHostPriceTable(time.Now())
validPT := newTestHostPriceTable(time.Now().Add(time.Minute))

// create a mock host with that returns a valid price table
hk1 := types.PublicKey{1}
h1 := newMockHost(hk1, validPT, nil)
// create a mock host that has a valid price table
h1 := newMockHost(false)
h1.hpt = validPT
hk1 := h1.hk

// create a hostdb host with an expired pt
// create a hostdb entry for that host that returns the expired price table
hdb1 := &hostdb.HostInfo{
Host: hostdb.Host{
PublicKey: hk1,
Expand Down
Loading