Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Feb 1, 2024
1 parent 53cdf0b commit a11178b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
23 changes: 12 additions & 11 deletions rhp/v4/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import (
)

var (
defaultOptions = Options{
defaultOptions = options{
DialTimeout: time.Minute,
IdleTimeout: 30 * time.Second,
RPCTimeout: 5 * time.Minute,
}
)

type (
Option func(o *Options)
// An Option is used to configure the client during creation.
Option func(o *options)

// Options are used to configure the client during creation.
Options struct {
// options are used to configure the client during creation.
options struct {
DialTimeout time.Duration // timeout for dialing a new connection
IdleTimeout time.Duration // timeout for idle connections before recreating them
RPCTimeout time.Duration // timeout for RPCs
Expand All @@ -52,21 +53,21 @@ type (

// WithDialTimeout overwrites the default dial timeout of 1 minute.
func WithDialTimeout(d time.Duration) Option {
return func(opts *Options) {
return func(opts *options) {
opts.DialTimeout = d
}
}

// WithIdleTimeout overwrites the default idle timeout of 30 seconds.
func WithIdleTimeout(d time.Duration) Option {
return func(opts *Options) {
return func(opts *options) {
opts.IdleTimeout = d
}
}

// WithRPCTimeout overwrites the default RPC timeout of 5 minutes.
func WithRPCTimeout(d time.Duration) Option {
return func(opts *Options) {
return func(opts *options) {
opts.RPCTimeout = d
}
}
Expand Down Expand Up @@ -215,7 +216,7 @@ func (c *Client) FormContract(ctx context.Context, hp rhpv4.HostPrices, contract
}
panic("incomplete rpc - missing outputs")
// TODO: verify host signatures
return rpc.Contract, nil
// return rpc.Contract, nil
}

// RenewContract renews a contract with the host, immediately unlocking
Expand Down Expand Up @@ -247,7 +248,7 @@ func (c *Client) PinSectors(ctx context.Context, contract types.V2FileContract,
if len(gaps) > 0 {
actions[i] = rhpv4.WriteAction{}
panic("incomplete type")
gaps = gaps[1:]
// gaps = gaps[1:]
} else {

Check warning on line 252 in rhp/v4/client.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 1.21)

superfluous-else: if block ends with call to panic function, so drop this else and outdent its block (revive)
actions[i] = rhpv4.WriteAction{
Type: rhpv4.ActionAppend,
Expand Down Expand Up @@ -389,7 +390,7 @@ func (c *Client) WriteSector(ctx context.Context, hp rhpv4.HostPrices, data []by
return types.Hash256{}, fmt.Errorf("root mismatch")
}
panic("unfinished rpc - missing payment")
return rpc.Root, nil
// return rpc.Root, nil
}

// SectorRoots returns 'length' roots of a contract starting at the given
Expand All @@ -405,7 +406,7 @@ func (c *Client) SectorRoots(ctx context.Context, hp rhpv4.HostPrices, offset, l
}
// TODO: verify proof
panic("unfinished rpc - missing payment")
return rpc.Roots, nil
// return rpc.Roots, nil
}

// AccountBalance returns the balance of a given account.
Expand Down
4 changes: 4 additions & 0 deletions rhp/v4/host/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
confirmedRevisionNumber uint64
}

// A MemContractStore manages the state of file contracts in memory.
MemContractStore struct {
log *zap.Logger

Expand All @@ -32,6 +33,7 @@ type (
}
)

// ProcessChainApplyUpdate implements the chain.Subscriber interface
func (ms *MemContractStore) ProcessChainApplyUpdate(cau *chain.ApplyUpdate, mayCommit bool) error {
ms.updates = append(ms.updates, cau)

Expand All @@ -54,6 +56,7 @@ func (ms *MemContractStore) ProcessChainApplyUpdate(cau *chain.ApplyUpdate, mayC
return nil
}

// ProcessChainRevertUpdate implements the chain.Subscriber interface
func (ms *MemContractStore) ProcessChainRevertUpdate(cru *chain.RevertUpdate) error {
if len(ms.updates) != 0 && ms.updates[len(ms.updates)-1].State.Index == cru.State.Index {
ms.updates = ms.updates[:len(ms.updates)-1]
Expand All @@ -63,6 +66,7 @@ func (ms *MemContractStore) ProcessChainRevertUpdate(cru *chain.RevertUpdate) er
panic("implement me")
}

// Revision returns the current revision of the contract.
func (ms *MemContractStore) Revision() types.V2FileContractRevision {
panic("implement me")
}
Expand Down
19 changes: 15 additions & 4 deletions rhp/v4/host/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,21 @@ import (
)

var (
ErrNotEnoughFunds = wallet.ErrNotEnoughFunds
ErrNotEnoughStorage = errors.New("not enough storage")
ErrSectorNotFound = errors.New("sector not found")
// ErrNotEnoughFunds is returned when a transaction cannot be funded.
ErrNotEnoughFunds = wallet.ErrNotEnoughFunds
// ErrNotEnoughStorage is returned when a sector cannot be stored because
// the host has run out of space
ErrNotEnoughStorage = errors.New("not enough storage")
// ErrSectorNotFound is returned when a sector cannot be deleted because it
// does not exist.
ErrSectorNotFound = errors.New("sector not found")
// ErrPriceTableExpired is returned when the renter sent an expired price
// table.
ErrPriceTableExpired = errors.New("price table expired")
// ErrOffsetOutOfBounds is returned when a renter requests a sector with an
// offset that is out of bounds.
ErrOffsetOutOfBounds = errors.New("offset out of bounds")
ErrContractExists = errors.New("contract already exists")
//ErrContractExists is returned when a renter tries to form a contract with
// a host that already has a contract with the renter.
ErrContractExists = errors.New("contract already exists")
)
3 changes: 2 additions & 1 deletion rhp/v4/host/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ type (
Settings Settings
}

// An Option sets options such as the host's default settings for the server.
Option func(*config)
)

// WithLogger sets the logger for the server.
// WithSettings sets the logger for the server.
func WithSettings(s Settings) Option { return func(c *config) { c.Settings = s } }
8 changes: 8 additions & 0 deletions rhp/v4/host/rhp.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,23 @@ func (s *Server) Serve(t Transport, log *zap.Logger) error {
}
}

// UpdateSettings updates the host's internal settings.
func (s *Server) UpdateSettings(settings Settings) error {
s.config.Settings = settings
return nil
}

// SetMaxSectors sets the maximum number of sectors the host can store.
func (s *Server) SetMaxSectors(n uint64) {
s.sectors.maxSectors = n
}

// NewServer creates a new reference host server with the given private key and chain
// manager. The server will use the provided options to configure its internal
// settings.
//
// A transport must be set up and then called with the Serve method to start
// an RHP session.
func NewServer(privKey types.PrivateKey, cm ChainManager, opts ...Option) *Server {
cfg := config{
Settings: Settings{
Expand Down
6 changes: 6 additions & 0 deletions rhp/v4/host/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,32 @@ import (
"go.sia.tech/core/types"
)

// A MemSectorStore manages the state of sectors in memory.
type MemSectorStore struct {
maxSectors uint64

mu sync.Mutex
sectors map[types.Hash256][rhp.SectorSize]byte
}

// NewMemSectorStore creates a new MemSectorStore with a maximum number of
// sectors.
func NewMemSectorStore(maxSectors uint64) *MemSectorStore {
return &MemSectorStore{
maxSectors: maxSectors,
sectors: make(map[types.Hash256][rhp.SectorSize]byte),
}
}

// Read retrieves a sector from the store.
func (m *MemSectorStore) Read(root types.Hash256) ([rhp.SectorSize]byte, bool) {
m.mu.Lock()
defer m.mu.Unlock()
sector, ok := m.sectors[root]
return sector, ok
}

// Write stores a sector in the store.
func (m *MemSectorStore) Write(root types.Hash256, sector [rhp.SectorSize]byte) error {
m.mu.Lock()
defer m.mu.Unlock()
Expand All @@ -39,6 +44,7 @@ func (m *MemSectorStore) Write(root types.Hash256, sector [rhp.SectorSize]byte)
return nil
}

// Delete removes a sector from the store.
func (m *MemSectorStore) Delete(root types.Hash256) error {
m.mu.Lock()
defer m.mu.Unlock()
Expand Down

0 comments on commit a11178b

Please sign in to comment.