diff --git a/rhp/v4/client.go b/rhp/v4/client.go index 6e6e719..5ded8d9 100644 --- a/rhp/v4/client.go +++ b/rhp/v4/client.go @@ -14,7 +14,7 @@ import ( ) var ( - defaultOptions = Options{ + defaultOptions = options{ DialTimeout: time.Minute, IdleTimeout: 30 * time.Second, RPCTimeout: 5 * time.Minute, @@ -22,10 +22,11 @@ var ( ) 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 @@ -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 } } @@ -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 @@ -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 { actions[i] = rhpv4.WriteAction{ Type: rhpv4.ActionAppend, @@ -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 @@ -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. diff --git a/rhp/v4/host/contracts.go b/rhp/v4/host/contracts.go index 8835bb3..88b3c32 100644 --- a/rhp/v4/host/contracts.go +++ b/rhp/v4/host/contracts.go @@ -22,6 +22,7 @@ type ( confirmedRevisionNumber uint64 } + // A MemContractStore manages the state of file contracts in memory. MemContractStore struct { log *zap.Logger @@ -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) @@ -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] @@ -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") } diff --git a/rhp/v4/host/errors.go b/rhp/v4/host/errors.go index 8eee2b8..5045628 100644 --- a/rhp/v4/host/errors.go +++ b/rhp/v4/host/errors.go @@ -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") ) diff --git a/rhp/v4/host/options.go b/rhp/v4/host/options.go index 06adbe9..60fbaeb 100644 --- a/rhp/v4/host/options.go +++ b/rhp/v4/host/options.go @@ -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 } } diff --git a/rhp/v4/host/rhp.go b/rhp/v4/host/rhp.go index 6e2f770..5359715 100644 --- a/rhp/v4/host/rhp.go +++ b/rhp/v4/host/rhp.go @@ -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{ diff --git a/rhp/v4/host/storage.go b/rhp/v4/host/storage.go index fcbc8fe..f761f15 100644 --- a/rhp/v4/host/storage.go +++ b/rhp/v4/host/storage.go @@ -8,6 +8,7 @@ import ( "go.sia.tech/core/types" ) +// A MemSectorStore manages the state of sectors in memory. type MemSectorStore struct { maxSectors uint64 @@ -15,6 +16,8 @@ type MemSectorStore struct { 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, @@ -22,6 +25,7 @@ func NewMemSectorStore(maxSectors uint64) *MemSectorStore { } } +// 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() @@ -29,6 +33,7 @@ func (m *MemSectorStore) Read(root types.Hash256) ([rhp.SectorSize]byte, bool) { 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() @@ -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()