diff --git a/api/client.go b/api/client.go index c347f5c..1c31f6f 100644 --- a/api/client.go +++ b/api/client.go @@ -17,6 +17,17 @@ type Client struct { n *consensus.Network // for ConsensusTipState } +func (c *Client) getNetwork() (*consensus.Network, error) { + if c.n == nil { + var err error + c.n, err = c.ConsensusNetwork() + if err != nil { + return nil, err + } + } + return c.n, nil +} + // State returns information about the current state of the walletd daemon. func (c *Client) State() (resp StateResponse, err error) { err = c.c.GET("/state", &resp) @@ -36,6 +47,8 @@ func (c *Client) TxpoolTransactions() (txns []types.Transaction, v2txns []types. return resp.Transactions, resp.V2Transactions, err } +// TxpoolParents returns the parents of a transaction that are currently in the +// transaction pool. func (c *Client) TxpoolParents(txn types.Transaction) (resp []types.Transaction, err error) { err = c.c.POST("/txpool/parents", txn, &resp) return @@ -61,22 +74,14 @@ func (c *Client) ConsensusTip() (resp types.ChainIndex, err error) { return } +// ConsensusIndex returns the consensus index at the specified height. func (c *Client) ConsensusIndex(height uint64) (resp types.ChainIndex, err error) { err = c.c.GET(fmt.Sprintf("/consensus/index/%d", height), &resp) return } -func (c *Client) getNetwork() (*consensus.Network, error) { - if c.n == nil { - var err error - c.n, err = c.ConsensusNetwork() - if err != nil { - return nil, err - } - } - return c.n, nil -} - +// ConsensusUpdates returns at most n consensus updates that have occurred since +// the specified index func (c *Client) ConsensusUpdates(index types.ChainIndex, limit int) ([]chain.RevertUpdate, []chain.ApplyUpdate, error) { // index.String() is a short-hand representation. We need the full text indexBuf, err := index.MarshalText() @@ -85,7 +90,7 @@ func (c *Client) ConsensusUpdates(index types.ChainIndex, limit int) ([]chain.Re } var resp ConsensusUpdatesResponse - if err = c.c.GET(fmt.Sprintf("/consensus/updates/%s?limit=%d", indexBuf, limit), &resp); err != nil { + if err := c.c.GET(fmt.Sprintf("/consensus/updates/%s?limit=%d", indexBuf, limit), &resp); err != nil { return nil, nil, err } diff --git a/api/opts.go b/api/opts.go index d4ee3ab..3db63de 100644 --- a/api/opts.go +++ b/api/opts.go @@ -1,5 +1,6 @@ package api +// A ServerOption is a functional option type for configuring the API server. type ServerOption func(*server) // WithChainManager sets the chain manager used by the server. diff --git a/config/config.go b/config/config.go index a757d85..b24901d 100644 --- a/config/config.go +++ b/config/config.go @@ -9,6 +9,7 @@ type ( Password string `yaml:"password,omitempty"` } + // Syncer contains the configuration for the p2p syncer. Syncer struct { Address string `yaml:"address,omitempty"` Peers []string `yaml:"peers,omitempty"` @@ -16,6 +17,8 @@ type ( Bootstrap bool `yaml:"bootstrap,omitempty"` } + // A RemoteConsensus contains the configuration for connecting to a remote + // consensus database. RemoteConsensus struct { Address string `yaml:"address,omitempty"` Password string `yaml:"password,omitempty"` diff --git a/internal/remote/chain.go b/internal/remote/chain.go index 30ef9eb..8eccd9c 100644 --- a/internal/remote/chain.go +++ b/internal/remote/chain.go @@ -27,8 +27,8 @@ type ( // UpdatesSince returns any consensus updates since the given index, up to a // maximum of max updates. -func (cm *ChainManager) UpdatesSince(index types.ChainIndex, max int) ([]chain.RevertUpdate, []chain.ApplyUpdate, error) { - return cm.client.ConsensusUpdates(index, max) +func (cm *ChainManager) UpdatesSince(index types.ChainIndex, limit int) ([]chain.RevertUpdate, []chain.ApplyUpdate, error) { + return cm.client.ConsensusUpdates(index, limit) } // BestIndex returns the chain index for the given height, or false if the height @@ -51,7 +51,6 @@ func (cm *ChainManager) Tip() types.ChainIndex { cm.mu.Lock() defer cm.mu.Unlock() return cm.state.Index - } // TipState returns the current state of the consensus set. @@ -123,7 +122,6 @@ func (cm *ChainManager) updateState() error { cm.fee = fee cm.mu.Unlock() return nil - } // OnReorg registers a callback to be called when a reorganization occurs. @@ -147,6 +145,7 @@ func (cm *ChainManager) OnReorg(fn func(types.ChainIndex)) (cancel func()) { } } +// NewChainManager creates a new ChainManager. func NewChainManager(client *api.Client, log *zap.Logger, opts ...ChainManagerOption) (*ChainManager, error) { cm := &ChainManager{ client: client, diff --git a/internal/remote/options.go b/internal/remote/options.go index 3bc1f38..ad5f9d0 100644 --- a/internal/remote/options.go +++ b/internal/remote/options.go @@ -2,8 +2,10 @@ package remote import "time" +// ChainManagerOption is a functional option type for configuring a ChainManager. type ChainManagerOption func(*ChainManager) +// WithRefreshInterval sets the polling interval used by the chain manager. func WithRefreshInterval(interval time.Duration) ChainManagerOption { return func(cm *ChainManager) { cm.refreshInterval = interval diff --git a/internal/remote/syncer.go b/internal/remote/syncer.go index cf02867..66d806f 100644 --- a/internal/remote/syncer.go +++ b/internal/remote/syncer.go @@ -10,6 +10,7 @@ import ( "go.uber.org/zap" ) +// A Syncer is a syncer.Syncer that uses a remote API to broadcast transactions. type Syncer struct { client *api.Client log *zap.Logger @@ -41,6 +42,8 @@ func (s *Syncer) BroadcastTransactionSet(txns []types.Transaction) { } } +// BroadcastV2TransactionSet broadcasts a set of v2 transactions using the remote +// API func (s *Syncer) BroadcastV2TransactionSet(index types.ChainIndex, txns []types.V2Transaction) { if err := s.client.TxpoolBroadcast(nil, txns); err != nil { s.log.Error("failed to broadcast v2 transaction set", zap.Error(err))