pegnet

package
v0.3.1-0...-218028b Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2019 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultPad = 1
)
View Source
const QueryLimit = 50

QueryLimit is the amount of transactions to return in one query

Variables

View Source
var (
	InsufficientBalanceErr = errors.New("insufficient balance")
	PFCTOneWayError        = errors.New("pFCT conversions are one way only at this height, they cannot be a conversion destination")
)

Functions

func FormatTxID

func FormatTxID(index int, hash string) string

FormatTxID constructs a txid from an entryhash and its index

func FormatTxIDWithPad

func FormatTxIDWithPad(pad, index int, hash string) string

FormatTxIDWithPad constructs a txid from an entryhash and its index. It will pad the index such that it is of at least 'pad' characters in lenght. pad = 2 -> 01-entryhash pad = 3 -> 001-entryhash

func SplitTxID

func SplitTxID(txid string) (index int, batchHash string, err error)

SplitTxID splits a TxID into it's parts. TxID format : [TxIndex]-[BatchHash]

1-c99dedea0e4e0c40118fe7e4d515b23cc0489269c8cef187b4f15a4ccbd880be

func VerifyTransactionHash

func VerifyTransactionHash(hash string) (index int, batchHash string, err error)

VerifyTransactionHash checks if a given hash or txid is valid. There are 2 types of transaction hashes:

  • batches, 64 hex characters indicates a batch of transactions. All burns and coinbases are considered batches of length 1.
  • txid, [TxIndex]-[BatchHash] indicates a single transaction in a batch.

All hashes in pure hash format (64 hex characters) will return an index of -1, meaning the hash indicates a batch of transactions.

All hashes in txid format, [TxIndex]-[BatchHash] will return an index number >= 0.

Types

type BalancePair

type BalancePair struct {
	Address *factom.FAAddress
	Balance uint64
}

type BalancesPair

type BalancesPair struct {
	Address  *factom.FAAddress
	Balances []uint64
}

type BlockSync

type BlockSync struct {
	Synced uint32
}

type HistoryAction

type HistoryAction int32

HistoryAction are the different types of actions inside the history

const (
	// Invalid is used for debugging
	Invalid HistoryAction = iota
	// Transfer is a 1:n transfer of pegged assets from one address to another
	Transfer
	// Conversion is a conversion of pegged assets
	Conversion
	// Coinbase is a miner reward payout
	Coinbase
	// FCTBurn is a pFCT payout for burning FCT on factom
	FCTBurn
)

type HistoryQueryOptions

type HistoryQueryOptions struct {
	Offset     int
	Desc       bool
	Transfer   bool
	Conversion bool
	Coinbase   bool
	FCTBurn    bool
	Asset      string

	// UseTxIndex is set if specifying a specific tx in the batch.
	// Because 0 is a valid tx index, we want the uninitialized value
	// to be "off"
	UseTxIndex bool
	TxIndex    int
}

HistoryQueryOptions contains the data of what to query for the query builder

type HistoryTransaction

type HistoryTransaction struct {
	Hash      *factom.Bytes32 `json:"hash"`
	TxID      string          `json:"txid"` // [TxIndex]-[BatchHash]
	Height    int64           `json:"height"`
	Timestamp time.Time       `json:"timestamp"`
	Executed  int32           `json:"executed"`
	TxIndex   int             `json:"txindex"`
	TxAction  HistoryAction   `json:"txaction"`

	FromAddress *factom.FAAddress          `json:"fromaddress"`
	FromAsset   string                     `json:"fromasset"`
	FromAmount  int64                      `json:"fromamount"`
	ToAsset     string                     `json:"toasset,omitempty"`
	ToAmount    int64                      `json:"toamount,omitempty"`
	Outputs     []HistoryTransactionOutput `json:"outputs,omitempty"`
}

HistoryTransaction is a flattened entry of the history table structure. It contains several actions: transfers, conversions, coinbases, and fct burns

type HistoryTransactionOutput

type HistoryTransactionOutput struct {
	Address factom.FAAddress `json:"address"`
	Amount  int64            `json:"amount"`
}

HistoryTransactionOutput is an entry of a transfer's outputs

type PEGPricingPhase

type PEGPricingPhase int
const (
	PEGPriceIsZero     PEGPricingPhase // PEG == 0
	PEGPriceIsEquation                 // PEG == MarketCap / Peg Supply
	PEGPriceIsFloating                 // PEG == ExchRate
)

type Pegnet

type Pegnet struct {
	Config *viper.Viper

	// This is the sqlite db to store state
	DB *sql.DB
}

func New

func New(conf *viper.Viper) *Pegnet

func (*Pegnet) AddToBalance

func (p *Pegnet) AddToBalance(tx *sql.Tx, adr *factom.FAAddress, ticker fat2.PTicker, value uint64) (int64, error)

AddToBalance adds value to the typed balance of adr, creating a new row in "pn_addresses" if it does not exist. If successful, the row id is returned.

func (*Pegnet) CreateTableAddresses

func (p *Pegnet) CreateTableAddresses() error

func (*Pegnet) DoesTransactionExist

func (p *Pegnet) DoesTransactionExist(entryhash factom.Bytes32) (bool, error)

func (*Pegnet) Init

func (p *Pegnet) Init() error

func (*Pegnet) InsertCoinbase

func (p *Pegnet) InsertCoinbase(tx *sql.Tx, winner *grader.GradingOPR, addr []byte, timestamp time.Time) error

InsertCoinbase inserts the payouts from mining into the history system. There is one transaction per winning OPR, with the entry hash pointing to that specific opr

func (*Pegnet) InsertFCTBurn

func (p *Pegnet) InsertFCTBurn(tx *sql.Tx, fBlockHash *factom.Bytes32, burn *factom.FactoidTransaction, height uint32) error

InsertFCTBurn inserts a payout for an FCT burn into the system. Note that from_asset and to_asset are hardcoded

func (*Pegnet) InsertGradeBlock

func (p *Pegnet) InsertGradeBlock(tx *sql.Tx, eblock *factom.EBlock, graded grader.GradedBlock) error

func (*Pegnet) InsertRates

func (p *Pegnet) InsertRates(tx *sql.Tx, height uint32, rates []opr.AssetUint, phase PEGPricingPhase) error

InsertRates adds all asset rates as rows, computing the rate for PEG if necessary

func (*Pegnet) InsertSynced

func (p *Pegnet) InsertSynced(tx *sql.Tx, bs *BlockSync) error

func (*Pegnet) InsertTransactionBatchHolding

func (p *Pegnet) InsertTransactionBatchHolding(tx *sql.Tx, txBatch *fat2.TransactionBatch, height uint64, eblockKeyMR *factom.Bytes32) (int64, error)

InsertTransactionBatchHolding inserts a row into "pn_transaction_batch_holding" that stores the entry data at its entry hash, along with the contextual information of the block height and eblock_keymr. If successful, the row id for the new row in "pn_transaction_batch_holding" is returned.

Note: It is assumed that the entry stored has already been validated as a fat2 transaction batch that contains at least one conversion. It is only put into holding to be executed against future asset exchange rates.

func (*Pegnet) InsertTransactionHistoryTxBatch

func (p *Pegnet) InsertTransactionHistoryTxBatch(tx *sql.Tx, blockorder int, txbatch *fat2.TransactionBatch, height uint32) error

InsertTransactionHistoryTxBatch inserts a transaction from the transaction chain into the history system

func (*Pegnet) InsertTransactionRelation

func (p *Pegnet) InsertTransactionRelation(tx *sql.Tx, adr factom.FAAddress, entryHash *factom.Bytes32, txIndex uint64, to bool, isConversion bool) (int64, error)

InsertTransactionRelation inserts a row into "pnaddress_transactions" relating the adrID with the entryHash with the given transaction direction, to. If successful, the row id for the new row in "pn_address_transactions" is returned.

If isConversion is true, the to field will automatically be set to true.

func (*Pegnet) IsReplayTransaction

func (p *Pegnet) IsReplayTransaction(tx *sql.Tx, entryHash *factom.Bytes32) (bool, error)

IsReplayTransaction returns true if there exist any transaction relations in the "pn_address_transactions" table.

func (*Pegnet) SelectAllBalances

func (p *Pegnet) SelectAllBalances() ([]BalancesPair, error)

SelectPendingBalances returns a map of all valid PTickers and their associated balances for the given address. If the address is not in the database, the map will contain 0 for all valid PTickers. This works on the pending tx

func (*Pegnet) SelectBalance

func (p *Pegnet) SelectBalance(adr *factom.FAAddress, ticker fat2.PTicker) (uint64, error)

SelectBalance returns the balance of an individual token type for the given address. If the address is not in the database, 0 will be returned.

func (*Pegnet) SelectBalances

func (p *Pegnet) SelectBalances(adr *factom.FAAddress) (map[fat2.PTicker]uint64, error)

SelectBalances returns a map of all valid PTickers and their associated balances for the given address. If the address is not in the database, the map will contain 0 for all valid PTickers.

func (*Pegnet) SelectIssuances

func (p *Pegnet) SelectIssuances() (map[fat2.PTicker]uint64, error)

func (*Pegnet) SelectMostRecentRatesBeforeHeight

func (p *Pegnet) SelectMostRecentRatesBeforeHeight(ctx context.Context, tx QueryAble, height uint32) (map[fat2.PTicker]uint64, uint32, error)

func (*Pegnet) SelectPendingBalance

func (p *Pegnet) SelectPendingBalance(tx *sql.Tx, adr *factom.FAAddress, ticker fat2.PTicker) (uint64, error)

SelectPendingBalance returns the balance of an individual token type for the given address, in the context of a given sql transaction. If the address is not in the database or will not be in the database after the tx is committed, 0 will be returned.

func (*Pegnet) SelectPendingBalances

func (p *Pegnet) SelectPendingBalances(tx *sql.Tx, adr *factom.FAAddress) (map[fat2.PTicker]uint64, error)

SelectPendingBalances returns a map of all valid PTickers and their associated balances for the given address. If the address is not in the database, the map will contain 0 for all valid PTickers. This works on the pending tx

func (*Pegnet) SelectPendingRates

func (p *Pegnet) SelectPendingRates(ctx context.Context, tx *sql.Tx, height uint32) (map[fat2.PTicker]uint64, error)

func (*Pegnet) SelectPreviousWinners

func (p *Pegnet) SelectPreviousWinners(ctx context.Context, height uint32) ([]string, error)

func (*Pegnet) SelectRates

func (p *Pegnet) SelectRates(ctx context.Context, height uint32) (map[fat2.PTicker]uint64, error)

func (*Pegnet) SelectRatesByKeyMR

func (p *Pegnet) SelectRatesByKeyMR(ctx context.Context, keymr *factom.Bytes32) (map[fat2.PTicker]uint64, error)

func (*Pegnet) SelectRichList

func (p *Pegnet) SelectRichList(ticker fat2.PTicker, count int) ([]BalancePair, error)

SelectRichList returns the balance of all addresses for a given ticker

func (*Pegnet) SelectSynced

func (p *Pegnet) SelectSynced(ctx context.Context) (*BlockSync, error)

func (*Pegnet) SelectTransactionBatchesInHoldingAtHeight

func (p *Pegnet) SelectTransactionBatchesInHoldingAtHeight(height uint64) ([]*fat2.TransactionBatch, error)

SelectTransactionBatchesInHoldingAtHeight selects all fat2.TransactionBatch entries that are in holding at the given height. It should be assumed that a TransactionBatch in the database has already returned nil for TransactionBatch.Validate() and also that TransactionBatch.HasConversions() returns true.

func (*Pegnet) SelectTransactionHistoryActionsByAddress

func (p *Pegnet) SelectTransactionHistoryActionsByAddress(addr *factom.FAAddress, options HistoryQueryOptions) ([]HistoryTransaction, int, error)

SelectTransactionHistoryActionsByAddress uses the lookup table to retrieve all transactions that have the specified address in either inputs or outputs

func (*Pegnet) SelectTransactionHistoryActionsByHash

func (p *Pegnet) SelectTransactionHistoryActionsByHash(hash *factom.Bytes32, options HistoryQueryOptions) ([]HistoryTransaction, int, error)

SelectTransactionHistoryActionsByHash returns the specified amount of transactions based on the hash. Hash can be an entry hash from the opr and transaction chains, or a transaction hash from an fblock.

func (*Pegnet) SelectTransactionHistoryActionsByHeight

func (p *Pegnet) SelectTransactionHistoryActionsByHeight(height uint32, options HistoryQueryOptions) ([]HistoryTransaction, int, error)

SelectTransactionHistoryActionsByHeight returns all transactions that were **entered** at the specified height.

func (*Pegnet) SelectTransactionHistoryActionsByTxID

func (p *Pegnet) SelectTransactionHistoryActionsByTxID(hash *factom.Bytes32, options HistoryQueryOptions) ([]HistoryTransaction, int, error)

SelectTransactionHistoryActionsByTxID uses the lookup table to retrieve all transactions that have the specified txid. A TxID is an entryhash + a transaction index

func (*Pegnet) SelectTransactionHistoryStatus

func (p *Pegnet) SelectTransactionHistoryStatus(hash *factom.Bytes32) (uint32, uint32, error)

SelectTransactionHistoryStatus returns the status of a transaction: `-1` for a failed transaction, `0` for a pending transactions, `height` for the block in which it was applied otherwise

func (*Pegnet) SetTransactionHistoryConvertedAmount

func (p *Pegnet) SetTransactionHistoryConvertedAmount(tx *sql.Tx, txbatch *fat2.TransactionBatch, index int, amount int64) error

SetTransactionHistoryConvertedAmount updates a conversion with the actual conversion value. This is done in the same SQL Transaction as updating its executed status

func (*Pegnet) SetTransactionHistoryExecuted

func (p *Pegnet) SetTransactionHistoryExecuted(tx *sql.Tx, txbatch *fat2.TransactionBatch, executed int64) error

SetTransactionHistoryExecuted updates a transaction's executed status

func (*Pegnet) SetTransactionHistoryPEGConvertedRequestAmount

func (p *Pegnet) SetTransactionHistoryPEGConvertedRequestAmount(tx *sql.Tx, txbatch *fat2.TransactionBatch, index int, pegAmount, refundAmount int64) error

SetTransactionHistoryPEGConvertedRequestAmount updates a peg conversion request with the actual amount of PEG received and the refund amount. The refund amount will appear as an output. This is done in the same SQL Transaction as updating its executed status

func (*Pegnet) SubFromBalance

func (p *Pegnet) SubFromBalance(tx *sql.Tx, adr *factom.FAAddress, ticker fat2.PTicker, value uint64) (id int64, txError, err error)

SubFromBalance subtracts value from the typed balance of adr, creating a new row in "pn_addresses" if it does not exist and value is 0. If successful, the row id is returned, otherwise 0. If subtracting sub would result in a negative balance, txErr is not nil and starts with "insufficient balance".

type QueryAble

type QueryAble interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

QueryAble is so we can swap db and tx interactions

Jump to

Keyboard shortcuts

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