fetcher

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

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/guapcrypto/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

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

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

	// DefaultIdleConnTimeout is the maximum amount of time an idle
	// (keep-alive) connection will remain idle before closing
	// itself.
	DefaultIdleConnTimeout = 30 * time.Second

	// DefaultMaxConnections limits the number of concurrent
	// connections we will attempt to make. Most OS's have a
	// default connection limit of 128, so we set the default
	// below that.
	DefaultMaxConnections = 120
)

Variables

View Source
var (
	// ErrNoNetworks is returned when there are no
	// networks available for syncing.
	ErrNoNetworks = errors.New("no networks available")

	// ErrNetworkMissing is returned during asserter initialization
	// when the provided *types.NetworkIdentifier is not in the
	// *types.NetworkListResponse.
	ErrNetworkMissing = errors.New("network missing")

	// ErrRequestFailed is returned when a request fails.
	ErrRequestFailed = errors.New("request failed")

	// ErrExhaustedRetries is returned when a request with retries
	// fails because it was attempted too many times.
	ErrExhaustedRetries = errors.New("retries exhausted")

	// ErrCouldNotAcquireSemaphore is returned when acquiring
	// the connection semaphore returns an error.
	ErrCouldNotAcquireSemaphore = errors.New("could not acquire semaphore")
)

Functions

func CheckNetworkListForNetwork

func CheckNetworkListForNetwork(
	networkList *types.NetworkListResponse,
	networkIdentifier *types.NetworkIdentifier,
) (bool, []*types.NetworkIdentifier)

CheckNetworkListForNetwork returns a boolean indicating if a *types.NetworkIdentifier is present in the list of supported networks.

func Err

func Err(err error) bool

Err takes an error as an argument and returns whether or not the error is one thrown by the fetcher package

Types

type Backoff

type Backoff struct {
	// contains filtered or unexported fields
}

Backoff wraps backoff.BackOff so we can access the retry count (which is private on backoff.BackOff).

type Error

type Error struct {
	Err       error        `json:"err"`
	ClientErr *types.Error `json:"client_err"`

	// Retry is a boolean that indicates if the request should be retried.
	// It is the combination of the *types.Error.Retriable status and a
	// collection of transient errors.
	Retry bool `json:"retry"`
}

Error wraps the two possible types of error responses returned by the Rosetta Client

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(
	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,
	block *types.PartialBlockIdentifier,
	currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *Error)

AccountBalance returns the validated response from the AccountBalance method. If a block is provided, a historical lookup is performed.

func (*Fetcher) AccountBalanceRetry

func (f *Fetcher) AccountBalanceRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	account *types.AccountIdentifier,
	block *types.PartialBlockIdentifier,
	currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Amount, map[string]interface{}, *Error)

AccountBalanceRetry retrieves the validated AccountBalance with a specified number of retries and max elapsed time. If a block is provided, a historical lookup is performed.

func (*Fetcher) AccountCoins

func (f *Fetcher) AccountCoins(
	ctx context.Context,
	network *types.NetworkIdentifier,
	account *types.AccountIdentifier,
	includeMempool bool,
	currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Coin, map[string]interface{}, *Error)

AccountCoins returns the validated response from the AccountCoins method.

func (*Fetcher) AccountCoinsRetry

func (f *Fetcher) AccountCoinsRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	account *types.AccountIdentifier,
	includeMempool bool,
	currencies []*types.Currency,
) (*types.BlockIdentifier, []*types.Coin, map[string]interface{}, *Error)

AccountCoinsRetry retrieves the validated AccountCoins 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) 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) Call

func (f *Fetcher) Call(
	ctx context.Context,
	network *types.NetworkIdentifier,
	method string,
	parameters map[string]interface{},
) (map[string]interface{}, bool, *Error)

Call returns the validated response from the Call method.

func (*Fetcher) CallRetry

func (f *Fetcher) CallRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	method string,
	parameters map[string]interface{},
) (map[string]interface{}, bool, *Error)

CallRetry invokes the /call endpoint with a specified number of retries and max elapsed time.

func (*Fetcher) ConstructionCombine

func (f *Fetcher) ConstructionCombine(
	ctx context.Context,
	network *types.NetworkIdentifier,
	unsignedTransaction string,
	signatures []*types.Signature,
) (string, *Error)

ConstructionCombine creates a network-specific transaction from an unsigned transaction and an array of provided signatures.

The signed transaction returned from this method will be sent to the `/construction/submit` endpoint by the caller.

func (*Fetcher) ConstructionDerive

func (f *Fetcher) ConstructionDerive(
	ctx context.Context,
	network *types.NetworkIdentifier,
	publicKey *types.PublicKey,
	metadata map[string]interface{},
) (*types.AccountIdentifier, map[string]interface{}, *Error)

ConstructionDerive returns the network-specific address associated with a public key.

Blockchains that require an on-chain action to create an account should not implement this method.

func (*Fetcher) ConstructionHash

func (f *Fetcher) ConstructionHash(
	ctx context.Context,
	network *types.NetworkIdentifier,
	signedTransaction string,
) (*types.TransactionIdentifier, *Error)

ConstructionHash returns the network-specific transaction hash for a signed transaction.

func (*Fetcher) ConstructionMetadata

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

ConstructionMetadata returns the validated response from the ConstructionMetadata method.

func (*Fetcher) ConstructionParse

func (f *Fetcher) ConstructionParse(
	ctx context.Context,
	network *types.NetworkIdentifier,
	signed bool,
	transaction string,
) ([]*types.Operation, []*types.AccountIdentifier, map[string]interface{}, *Error)

ConstructionParse is called on both unsigned and signed transactions to understand the intent of the formulated transaction.

This is run as a sanity check before signing (after `/construction/payloads`) and before broadcast (after `/construction/combine`).

func (*Fetcher) ConstructionPayloads

func (f *Fetcher) ConstructionPayloads(
	ctx context.Context,
	network *types.NetworkIdentifier,
	operations []*types.Operation,
	metadata map[string]interface{},
	publicKeys []*types.PublicKey,
) (string, []*types.SigningPayload, *Error)

ConstructionPayloads is called with an array of operations and the response from `/construction/metadata`. It returns an unsigned transaction blob and a collection of payloads that must be signed by particular addresses using a certain SignatureType.

The array of operations provided in transaction construction often times can not specify all "effects" of a transaction (consider invoked transactions in Ethereum). However, they can deterministically specify the "intent" of the transaction, which is sufficient for construction. For this reason, parsing the corresponding transaction in the Data API (when it lands on chain) will contain a superset of whatever operations were provided during construction.

func (*Fetcher) ConstructionPreprocess

func (f *Fetcher) ConstructionPreprocess(
	ctx context.Context,
	network *types.NetworkIdentifier,
	operations []*types.Operation,
	metadata map[string]interface{},
) (map[string]interface{}, []*types.AccountIdentifier, *Error)

ConstructionPreprocess is called prior to `/construction/payloads` to construct a request for any metadata that is needed for transaction construction given (i.e. account nonce).

The request returned from this method will be used by the caller (in a different execution environment) to call the `/construction/metadata` endpoint.

func (*Fetcher) ConstructionSubmit

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

ConstructionSubmit returns the validated response from the ConstructionSubmit method.

func (*Fetcher) EventsBlocks

func (f *Fetcher) EventsBlocks(
	ctx context.Context,
	network *types.NetworkIdentifier,
	offset *int64,
	limit *int64,
) (int64, []*types.BlockEvent, *Error)

EventsBlocks returns the validated response from the EventsBlocks method.

func (*Fetcher) EventsBlocksRetry

func (f *Fetcher) EventsBlocksRetry(
	ctx context.Context,
	network *types.NetworkIdentifier,
	offset *int64,
	limit *int64,
) (int64, []*types.BlockEvent, *Error)

EventsBlocksRetry retrieves the valided EventsBlocks with a specified number of retries and max elapsed time.

func (*Fetcher) InitializeAsserter

func (f *Fetcher) InitializeAsserter(
	ctx context.Context,
	networkIdentifier *types.NetworkIdentifier,
) (
	*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 a *types.NetworkIdentifier is provided, it will be used to fetch status and options. Not providing a *types.NetworkIdentifier will result in the first network returned by NetworkList to be used.

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

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

NetworkList returns the validated response from the NetworkList method.

func (*Fetcher) NetworkListRetry

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

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

NetworkOptions returns the validated response from the NetworkOptions method.

func (*Fetcher) NetworkOptionsRetry

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) RequestFailedError

func (f *Fetcher) RequestFailedError(
	rosettaErr *types.Error,
	err error,
	message string,
) *Error

RequestFailedError creates a new *Error and asserts the provided rosettaErr was provided in /network/options.

func (*Fetcher) SearchTransactions

func (f *Fetcher) SearchTransactions(
	ctx context.Context,
	request *types.SearchTransactionsRequest,
) (*int64, []*types.BlockTransaction, *Error)

SearchTransactions returns the validated response from the SearchTransactions method.

func (*Fetcher) SearchTransactionsRetry

func (f *Fetcher) SearchTransactionsRetry(
	ctx context.Context,
	request *types.SearchTransactionsRequest,
) (*int64, []*types.BlockTransaction, *Error)

SearchTransactionsRetry retrieves the valided SearchTransactions 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

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

func WithAsserter(asserter *asserter.Asserter) Option

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

func WithClient

func WithClient(client *client.APIClient) Option

WithClient overrides the default client.APIClient.

func WithInsecureTLS

func WithInsecureTLS() Option

WithInsecureTLS overrides the default TLS security settings to allow insecure certificates on an HTTPS connection.

This should ONLY be used when debugging a Rosetta API implementation. Using this option can lead to a man-in-the-middle attack!!

func WithMaxConnections

func WithMaxConnections(connections int) Option

WithMaxConnections limits the number of concurrent requests the fetcher will attempt at once.

func WithMaxRetries

func WithMaxRetries(maxRetries uint64) Option

WithMaxRetries overrides the default number of retries on a request.

func WithRetryElapsedTime

func WithRetryElapsedTime(retryElapsedTime time.Duration) Option

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

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout overrides the default HTTP timeout.

Jump to

Keyboard shortcuts

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