syncer

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BalanceChanges

func BalanceChanges(
	ctx context.Context,
	asserter *asserter.Asserter,
	block *types.Block,
	orphan bool,
	handler Handler,
) ([]*storage.BalanceChange, error)

BalanceChanges returns all balance changes for a particular block. All balance changes for a particular account are summed into a single storage.BalanceChanges struct. If a block is being orphaned, the opposite of each balance change is returned.

func NextSyncableRange

func NextSyncableRange(
	ctx context.Context,
	s Syncer,
	endIndex int64,
) (int64, int64, bool, error)

NextSyncableRange returns the next range of indexes to sync based on what the last processed block in storage is and the contents of the network status response.

func Sync

func Sync(
	ctx context.Context,
	cancel context.CancelFunc,
	s Syncer,
	startIndex int64,
	endIndex int64,
) error

Sync cycles endlessly until there is an error or the requested range is synced.

Types

type BaseHandler

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

BaseHandler logs processed blocks and reconciles modified balances.

func (*BaseHandler) AccountExempt added in v0.1.4

func (h *BaseHandler) AccountExempt(
	ctx context.Context,
	account *types.AccountIdentifier,
	currency *types.Currency,
) bool

AccountExempt returns a boolean indicating if the provided account and currency are exempt from balance tracking and reconciliation.

func (*BaseHandler) BlockProcessed

func (h *BaseHandler) BlockProcessed(
	ctx context.Context,
	block *types.Block,
	reorg bool,
	balanceChanges []*storage.BalanceChange,
) error

BlockProcessed is called by the syncer after each block is processed. TODO: refactor to BlockAdded and BlockRemoved

type Handler

type Handler interface {
	// TODO: change to BlockAdded and BlockRemoved
	BlockProcessed(
		ctx context.Context,
		block *types.Block,
		orphan bool,
		changes []*storage.BalanceChange,
	) error

	AccountExempt(
		ctx context.Context,
		account *types.AccountIdentifier,
		current *types.Currency,
	) bool
}

Handler is called at various times during the sync cycle to handle different events. It is common to write logs or perform reconciliation in the sync handler.

func NewBaseHandler

func NewBaseHandler(
	logger *logger.Logger,
	reconciler reconciler.Reconciler,
	exemptAccounts []*reconciler.AccountCurrency,
) Handler

NewBaseHandler constructs a basic Handler.

type StatefulSyncer

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

StatefulSyncer contains the logic that orchestrates block fetching, storage, and reconciliation. The stateful syncer is useful for creating an application where durability and consistency of data is important. The stateful syncer supports re-orgs out-of the box.

func NewStateful

func NewStateful(
	network *types.NetworkIdentifier,
	storage *storage.BlockStorage,
	fetcher *fetcher.Fetcher,
	handler Handler,
) *StatefulSyncer

NewStateful returns a new Syncer.

func (*StatefulSyncer) CurrentIndex

func (s *StatefulSyncer) CurrentIndex(
	ctx context.Context,
) (int64, error)

CurrentIndex returns the next index to sync.

func (*StatefulSyncer) Fetcher

func (s *StatefulSyncer) Fetcher(
	ctx context.Context,
) *fetcher.Fetcher

Fetcher returns the syncer fetcher.

func (*StatefulSyncer) Network

Network returns the syncer network.

func (*StatefulSyncer) SetStartIndex

func (s *StatefulSyncer) SetStartIndex(
	ctx context.Context,
	startIndex int64,
) error

SetStartIndex initializes the genesisBlock and attempts to set the newHeadIndex.

func (*StatefulSyncer) SyncRange

func (s *StatefulSyncer) SyncRange(
	ctx context.Context,
	startIndex int64,
	endIndex int64,
) error

SyncRange syncs blocks from startIndex to endIndex, inclusive. This function handles re-orgs that may occur while syncing as long as the genesisIndex is not orphaned.

type StatelessSyncer

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

StatelessSyncer contains the logic that orchestrates stateless block fetching and reconciliation. The stateless syncer is useful for performing a quick check over a range of blocks without needed to sync all blocks up to the start of the range (a common pattern when debugging). It is important to note that the stateless syncer does not support reorgs nor does it save where it is on restart.

func NewStateless

func NewStateless(
	network *types.NetworkIdentifier,
	fetcher *fetcher.Fetcher,
	handler Handler,
) *StatelessSyncer

NewStateless returns a new Syncer.

func (*StatelessSyncer) CurrentIndex

func (s *StatelessSyncer) CurrentIndex(
	ctx context.Context,
) (int64, error)

CurrentIndex returns the next index to sync.

func (*StatelessSyncer) Fetcher

func (s *StatelessSyncer) Fetcher(
	ctx context.Context,
) *fetcher.Fetcher

Fetcher returns the syncer fetcher.

func (*StatelessSyncer) Network

Network returns the syncer network.

func (*StatelessSyncer) SetStartIndex

func (s *StatelessSyncer) SetStartIndex(
	ctx context.Context,
	startIndex int64,
) error

SetStartIndex initializes the current block index with the genesis block index if it is -1.

func (*StatelessSyncer) SyncRange

func (s *StatelessSyncer) SyncRange(
	ctx context.Context,
	startIndex int64,
	endIndex int64,
) error

SyncRange syncs blocks from startIndex to endIndex, inclusive. This function does NOT handle re-orgs. If you want re-org support, checkout the StatefulSyncer.

type Syncer

type Syncer interface {
	SetStartIndex(
		ctx context.Context,
		startIndex int64,
	) error

	CurrentIndex(
		ctx context.Context,
	) (int64, error)

	SyncRange(
		ctx context.Context,
		rangeStart int64,
		rangeEnd int64,
	) error

	Network(
		ctx context.Context,
	) *types.NetworkIdentifier

	Fetcher(
		ctx context.Context,
	) *fetcher.Fetcher
}

Syncer defines an interface for syncing some range of blocks.

Jump to

Keyboard shortcuts

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