fetcher

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2020 License: Apache-2.0 Imports: 11 Imported by: 18

README

Fetcher

GoDoc

The Fetcher package provides a simplified client interface to communicate with a Rosetta server. It also provides automatic retries and concurrent block fetches.

If you want a lower-level interface to communicate with a Rosetta server, check out the Client.

Installation

go get github.com/coinbase/rosetta-sdk-go/fetcher

Create a Fetcher

Creating a basic Fetcher is as easy as providing a context and Rosetta server URL:

fetcher := fetcher.New(ctx, serverURL)

It is also possible to provide a series of optional arguments that override default behavior. For example, it is possible to override the concurrency for making block requests:

fetcher := fetcher.New(ctx, serverURL, fetcher.WithBlockConcurrency(10))

More Examples

Check out the examples to see how easy it is to connect to a Rosetta server.

Documentation

Index

Constants

View Source
const (
	// DefaultElapsedTime is the default limit on time
	// spent retrying a fetch.
	DefaultElapsedTime = 1 * time.Minute

	// DefaultRetries is the default number of times to
	// attempt a retry on a failed request.
	DefaultRetries = 10

	// DefaultBlockConcurrency is the default number of
	// blocks a Fetcher will try to get concurrently.
	DefaultBlockConcurrency = 8

	// DefaultHTTPTimeout is the default timeout for
	// HTTP requests.
	DefaultHTTPTimeout = 10 * time.Second

	// DefaultTransactionConcurrency is the default
	// number of transactions a Fetcher will try to
	// get concurrently when populating a block (if
	// transactions are not included in the original
	// block fetch).
	DefaultTransactionConcurrency = 8

	// DefaultUserAgent is the default userAgent
	// to populate on requests to a Rosetta server.
	DefaultUserAgent = "rosetta-sdk-go"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockAndLatency

type BlockAndLatency struct {
	Block   *types.Block
	Latency float64
}

BlockAndLatency is utilized to track the latency of concurrent block fetches.

type Fetcher

type Fetcher struct {
	// Asserter is a public variable because
	// it can be used to determine if a retrieved
	// types.Operation is successful and should
	// be applied.
	Asserter *asserter.Asserter
	// contains filtered or unexported fields
}

Fetcher contains all logic to communicate with a Rosetta Server.

func New

func New(
	ctx context.Context,
	serverAddress string,
	options ...Option,
) *Fetcher

New constructs a new Fetcher with provided options.

func (*Fetcher) AccountBalance

func (f *Fetcher) AccountBalance(
	ctx context.Context,
	network *types.NetworkIdentifier,
	account *types.AccountIdentifier,
) (*types.BlockIdentifier, []*types.Amount, error)

AccountBalance returns the validated response from the AccountBalance method.

func (*Fetcher) AccountBalanceRetry

func (f *Fetcher) AccountBalanceRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	account *types.AccountIdentifier,
) (*types.BlockIdentifier, []*types.Amount, error)

AccountBalanceRetry retrieves the validated AccountBalance with a specified number of retries and max elapsed time.

func (*Fetcher) Block

func (f *Fetcher) Block(
	ctx context.Context,
	network *types.NetworkIdentifier,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.Block, error)

Block returns the validated response from the block method. This function will automatically fetch any transactions that were not returned by the call to fetch the block.

func (*Fetcher) BlockRange

func (f *Fetcher) BlockRange(
	ctx context.Context,
	network *types.NetworkIdentifier,
	startIndex int64,
	endIndex int64,
) (map[int64]*BlockAndLatency, error)

BlockRange concurrently fetches blocks from startIndex to endIndex, inclusive. Blocks returned by this method may not contain a path from the endBlock to the startBlock over Block.ParentBlockIdentifers if a re-org occurs during the fetch. This should be handled gracefully by any callers.

func (*Fetcher) BlockRetry

func (f *Fetcher) BlockRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.Block, error)

BlockRetry retrieves a validated Block with a specified number of retries and max elapsed time.

func (*Fetcher) ConstructionMetadata

func (f *Fetcher) ConstructionMetadata(
	ctx context.Context,
	network *types.NetworkIdentifier,
	options *map[string]interface{},
) (*map[string]interface{}, error)

ConstructionMetadata returns the validated response from the ConstructionMetadata method.

func (*Fetcher) ConstructionSubmit

func (f *Fetcher) ConstructionSubmit(
	ctx context.Context,
	signedTransaction string,
) (*types.TransactionIdentifier, *map[string]interface{}, error)

ConstructionSubmit returns the validated response from the ConstructionSubmit method.

func (*Fetcher) InitializeAsserter

func (f *Fetcher) InitializeAsserter(
	ctx context.Context,
) (
	*types.NetworkIdentifier,
	*types.NetworkStatusResponse,
	error,
)

InitializeAsserter creates an Asserter for validating responses. The Asserter is created by fetching the NetworkStatus and NetworkOptions from a Rosetta server.

If there is more than one network supported by the Rosetta server, the first network returned by NetworkList is used as the default network.

This method should be called before making any validated client requests.

func (*Fetcher) Mempool

func (f *Fetcher) Mempool(
	ctx context.Context,
	network *types.NetworkIdentifier,
) ([]*types.TransactionIdentifier, error)

Mempool returns the validated response from the Mempool method.

func (*Fetcher) MempoolTransaction

func (f *Fetcher) MempoolTransaction(
	ctx context.Context,
	network *types.NetworkIdentifier,
	transaction *types.TransactionIdentifier,
) (*types.Transaction, *map[string]interface{}, error)

MempoolTransaction returns the validated response from the MempoolTransaction method.

func (*Fetcher) NetworkList added in v0.1.2

func (f *Fetcher) NetworkList(
	ctx context.Context,
	metadata *map[string]interface{},
) (*types.NetworkListResponse, error)

NetworkList returns the validated response from the NetworList method.

func (*Fetcher) NetworkListRetry added in v0.1.2

func (f *Fetcher) NetworkListRetry(
	ctx context.Context,
	metadata *map[string]interface{},
) (*types.NetworkListResponse, error)

NetworkListRetry retrieves the validated NetworkList with a specified number of retries and max elapsed time.

func (*Fetcher) NetworkOptions added in v0.1.2

func (f *Fetcher) NetworkOptions(
	ctx context.Context,
	network *types.NetworkIdentifier,
	metadata *map[string]interface{},
) (*types.NetworkOptionsResponse, error)

NetworkOptions returns the validated response from the NetworList method.

func (*Fetcher) NetworkOptionsRetry added in v0.1.2

func (f *Fetcher) NetworkOptionsRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	metadata *map[string]interface{},
) (*types.NetworkOptionsResponse, error)

NetworkOptionsRetry retrieves the validated NetworkOptions with a specified number of retries and max elapsed time.

func (*Fetcher) NetworkStatus

func (f *Fetcher) NetworkStatus(
	ctx context.Context,
	network *types.NetworkIdentifier,
	metadata *map[string]interface{},
) (*types.NetworkStatusResponse, error)

NetworkStatus returns the validated response from the NetworkStatus method.

func (*Fetcher) NetworkStatusRetry

func (f *Fetcher) NetworkStatusRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	metadata *map[string]interface{},
) (*types.NetworkStatusResponse, error)

NetworkStatusRetry retrieves the validated NetworkStatus with a specified number of retries and max elapsed time.

func (*Fetcher) UnsafeBlock

func (f *Fetcher) UnsafeBlock(
	ctx context.Context,
	network *types.NetworkIdentifier,
	blockIdentifier *types.PartialBlockIdentifier,
) (*types.Block, error)

UnsafeBlock returns the unvalidated response from the Block method. This function will automatically fetch any transactions that were not returned by the call to fetch the block.

func (*Fetcher) UnsafeTransactions

func (f *Fetcher) UnsafeTransactions(
	ctx context.Context,
	network *types.NetworkIdentifier,
	block *types.BlockIdentifier,
	transactionIdentifiers []*types.TransactionIdentifier,
) ([]*types.Transaction, error)

UnsafeTransactions returns the unvalidated response from the BlockTransaction method. UnsafeTransactions fetches all provided types.TransactionIdentifiers concurrently (with the number of threads specified by txConcurrency). If any fetch fails, this function will return an error.

type Option added in v0.1.2

type Option func(f *Fetcher)

Option is used to overwrite default values in Fetcher construction. Any Option not provided falls back to the default value.

func WithAsserter added in v0.1.2

func WithAsserter(asserter *asserter.Asserter) Option

WithAsserter sets the asserter.Asserter on construction so it does not need to be initialized.

func WithBlockConcurrency added in v0.1.2

func WithBlockConcurrency(concurrency uint64) Option

WithBlockConcurrency overrides the default block concurrency.

func WithClient added in v0.1.2

func WithClient(client *client.APIClient) Option

WithClient overrides the default client.APIClient.

func WithMaxRetries added in v0.1.2

func WithMaxRetries(maxRetries uint64) Option

WithMaxRetries overrides the default number of retries on a request.

func WithRetryElapsedTime added in v0.1.2

func WithRetryElapsedTime(retryElapsedTime time.Duration) Option

WithRetryElapsedTime overrides the default max elapsed time to retry a request.

func WithTransactionConcurrency added in v0.1.2

func WithTransactionConcurrency(concurrency uint64) Option

WithTransactionConcurrency overrides the default transaction concurrency.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL