swap

package
v0.1.1-beta1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: BlueOak-1.0.0 Imports: 26 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info.

Types

type AuthManager

type AuthManager interface {
	Route(string, func(account.AccountID, *msgjson.Message) *msgjson.Error)
	Auth(user account.AccountID, msg, sig []byte) error
	Sign(...msgjson.Signable) error
	Send(account.AccountID, *msgjson.Message) error
	Request(account.AccountID, *msgjson.Message, func(comms.Link, *msgjson.Message)) error
	RequestWithTimeout(user account.AccountID, req *msgjson.Message, handlerFunc func(comms.Link, *msgjson.Message),
		expireTimeout time.Duration, expireFunc func()) error
	SwapSuccess(user account.AccountID, mmid db.MarketMatchID, value uint64, refTime time.Time)
	Inaction(user account.AccountID, misstep auth.NoActionStep, mmid db.MarketMatchID, matchValue uint64, refTime time.Time, oid order.OrderID)
	RecordCancel(user account.AccountID, oid, target order.OrderID, t time.Time)
	RecordCompletedOrder(user account.AccountID, oid order.OrderID, t time.Time)
}

AuthManager handles client-related actions, including authorization and communications.

type BackupFile

type BackupFile struct {
	// Name is the full path to the backup file.
	Name string
	// Stamp is the millisecond unix epoch time stamp of the state.
	Stamp int64
}

BackupFile is a located swap state file path and it's millisecond time stamp.

func LatestStateFile

func LatestStateFile(dir string) (*BackupFile, error)

LatestStateFile attempts to locate the swap state file with the most recent time stamp (in the file name) from the given directory. The *BackupFile return will be nil if the directory was read successfully but no matching files were found.

type Config

type Config struct {
	// DataDir is the folder where swap state is stored.
	DataDir string
	// AllowPartialRestore indicates if it is acceptable to load only part of
	// the State if the Swapper's asset configuration lacks assets required to
	// load all elements of State.
	AllowPartialRestore bool
	// Assets is a map to all the asset information, including the asset backends,
	// used by this Swapper.
	Assets map[uint32]*LockableAsset
	// AuthManager is the auth manager for client messaging and authentication.
	AuthManager AuthManager
	// A database backend.
	Storage Storage
	// BroadcastTimeout is how long the Swapper will wait for expected swap
	// transactions following new blocks.
	BroadcastTimeout time.Duration
	// LockTimeTaker is the locktime Swapper will use for auditing taker swaps.
	LockTimeTaker time.Duration
	// LockTimeTaker is the locktime Swapper will use for auditing maker swaps.
	LockTimeMaker time.Duration
	// IgnoreState indicates that the swapper should not load the latest state
	// from file.
	IgnoreState bool
	// StatePath is a path to a swap state file from which the swapper state
	// will be loaded. If StatePath is not set, and IgnoreState if false, the
	// most recent stored state file will be loaded. StatePath supercedes
	// IgnoreState.
	StatePath string
	// UnbookHook informs the DEX manager that the specified order should be
	// removed from the order book.
	UnbookHook func(lo *order.LimitOrder) bool
}

Config is the swapper configuration settings. A Config instance is the only argument to the Swapper constructor.

type LockableAsset

type LockableAsset struct {
	*asset.BackedAsset
	coinlock.CoinLocker // should be *coinlock.AssetCoinLocker
}

LockableAsset pairs an Asset with a CoinLocker.

type State

type State struct {
	// Version is State format's binary version. This field should be present in
	// any revision of the State struct.
	Version byte
	// Assets lists the IDs of the assets required to fully restore the the data
	// for all of the active matches.
	Assets []uint32
	// Swapper.matches
	MatchTrackers map[order.MatchID]*matchTrackerData
	// Swapper.orders.orderMatches
	OrderMatches map[order.OrderID]*orderSwapStat
	// Swapper.liveWaiters
	LiveWaiters map[waiterKey]*handlerArgs
}

State contains the Swapper's state data, which is used to restart the Swapper without interrupting active swaps or waiting for long duration operations to complete before stopping.

func LoadStateFile

func LoadStateFile(fileName string) (*State, error)

LoadStateFile attempts to load and decode a swap state file.

type Storage

type Storage interface {
	db.SwapArchiver
	LastErr() error
	Fatal() <-chan struct{}
	InsertMatch(match *order.Match) error
	CancelOrder(*order.LimitOrder) error
	RevokeOrder(order.Order) (cancelID order.OrderID, t time.Time, err error)
	SetOrderCompleteTime(ord order.Order, compTimeMs int64) error
	GetStateHash() ([]byte, error)
	SetStateHash([]byte) error
}

Storage updates match data in what is presumably a database.

type Swapper

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

Swapper handles order matches by handling authentication and inter-party communications between clients, or 'users'. The Swapper authenticates users (vua AuthManager) and validates transactions as they are reported.

func NewSwapper

func NewSwapper(cfg *Config) (*Swapper, error)

NewSwapper is a constructor for a Swapper.

func (*Swapper) CheckUnspent

func (s *Swapper) CheckUnspent(asset uint32, coinID []byte) error

CheckUnspent attempts to verify a coin ID for a given asset by retrieving the corresponding asset.Coin. If the coin is not found or spent, an asset.CoinNotFoundError is returned.

func (*Swapper) LockCoins

func (s *Swapper) LockCoins(asset uint32, coins map[order.OrderID][]order.CoinID)

LockCoins locks coins of a given asset. The OrderID is used for tracking.

func (*Swapper) LockOrdersCoins

func (s *Swapper) LockOrdersCoins(orders []order.Order)

LockOrdersCoins locks the backing coins for the provided orders.

func (*Swapper) Negotiate

func (s *Swapper) Negotiate(matchSets []*order.MatchSet, finalSwap map[order.OrderID]bool)

Negotiate takes ownership of the matches and begins swap negotiation. For reliable identification of completed orders when redeem acks are received and processed by processAck, BeginMatchAndNegotiate should be called prior to matching and order status/amount updates, and EndMatchAndNegotiate should be called after Negotiate. This locking sequence allows for orders that may already be involved in active swaps to remain unmodified by the Matcher/Market until new matches are recorded by the Swapper in Negotiate. If this is not done, it is possible that an order may be flagged as completed if a swap A completes after Matching and creation of swap B but before Negotiate has a chance to record the new swap.

func (*Swapper) Run

func (s *Swapper) Run(ctx context.Context)

Run is the main Swapper loop. It's primary purpose is to update transaction confirmations when new blocks are mined, and to trigger inaction checks.

func (*Swapper) UserSwappingAmt

func (s *Swapper) UserSwappingAmt(user account.AccountID, base, quote uint32) (amt, count uint64)

UserSwappingAmt gets the total amount in active swaps for a user in a specified market. This helps the market compute a user's order size limit.

Jump to

Keyboard shortcuts

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