txhelpers

package
v3.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2019 License: ISC Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AddressTypeP2PK = iota
	AddressTypeP2PKH
	AddressTypeP2SH
	AddressTypeOther
	AddressTypeUnknown
)
View Source
const (

	// DefaultStakeDbName is the default database name
	DefaultStakeDbName = "ffldb_stake"
)

Variables

View Source
var CoinbaseFlags = "/dcrd/"
View Source
var CoinbaseScript = append([]byte{0x00, 0x00}, []byte(CoinbaseFlags)...)

Functions

func AddressValidation added in v3.1.0

func AddressValidation(address string, params *chaincfg.Params) (dcrutil.Address, AddressType, AddressError)

AddressValidation performs several validation checks on the given address string. Initially, decoding as a Decred address is attempted. If it fails to decode, btcutil is used to try decoding it as a Bitcoin address. If both decoding fails, AddressErrorDecodeFailed is returned with AddressTypeUnknown. If the address is a Bitcoin address, AddressErrorBitcoin is returned with AddressTypeBitcoin. If the address decoded successfully as a Decred address, it is checked against the specified network. If it is the wrong network, AddressErrorWrongNet is returned with AddressTypeUnknown. If the address is the correct network, the address type is obtained. A final check is performed to determine if the address is the zero pubkey hash address, in which case AddressErrorZeroAddress is returned with the determined address type. If it is another address, AddressErrorNoError is returned with the determined address type.

func BlockConsumesOutpointWithAddresses

func BlockConsumesOutpointWithAddresses(block *dcrutil.Block, addrs map[string]TxAction,
	c RawTransactionGetter, params *chaincfg.Params) map[string][]*dcrutil.Tx

BlockConsumesOutpointWithAddresses checks the specified block to see if it includes transactions that spend from outputs created using any of the addresses in addrs. The TxAction for each address is not important, but it would logically be TxMined. Both regular and stake transactions are checked. The RPC client is used to get the PreviousOutPoint for each TxIn of each transaction in the block, from which the address is obtained from the PkScript of that output. chaincfg Params is required to decode the script.

func BlockReceivesToAddresses

func BlockReceivesToAddresses(block *dcrutil.Block, addrs map[string]TxAction,
	params *chaincfg.Params) map[string][]*dcrutil.Tx

BlockReceivesToAddresses checks a block for transactions paying to the specified addresses, and creates a map of addresses to a slice of dcrutil.Tx involving the address.

func BuildStakeTree

func BuildStakeTree(blocks map[int64]*dcrutil.Block, netParams *chaincfg.Params,
	nodeClient *rpcclient.Client, poolRequiredHeight int64, DBName ...string) (database.DB, []int64, error)

BuildStakeTree returns a database with a stake tree

func CalcMeanVotingBlocks

func CalcMeanVotingBlocks(params *chaincfg.Params) int64

CalcMeanVotingBlocks computes the average number of blocks a ticket will be live before voting. The expected block (aka mean) of the probability distribution is given by:

sum(B * P(B)), B=1 to 40960

Where B is the block number and P(B) is the probability of voting at block B. For more information see: https://github.com/decred/dcrdata/v3/issues/471#issuecomment-390063025

func DetermineTxTypeString

func DetermineTxTypeString(msgTx *wire.MsgTx) string

DetermineTxTypeString returns a string representing the transaction type given a wire.MsgTx struct

func FeeInfoBlock

func FeeInfoBlock(block *dcrutil.Block) *dcrjson.FeeInfoBlock

FeeInfoBlock computes ticket fee statistics for the tickets included in the specified block.

func FeeRateInfoBlock

func FeeRateInfoBlock(block *dcrutil.Block) *dcrjson.FeeInfoBlock

FeeRateInfoBlock computes ticket fee rate statistics for the tickets included in the specified block.

func FilterHashSlice

func FilterHashSlice(s []chainhash.Hash, doRemove func(h chainhash.Hash) bool) []chainhash.Hash

FilterHashSlice removes elements from the specified if the doRemove function evaluates to true for a given element. For example, given a slice of hashes called blackList that should be removed from the slice hashList:

hashList = FilterHashSlice(hashList, func(h chainhash.Hash) bool {
 return HashInSlice(h, blackList)
})

func GenesisTxHash added in v3.1.0

func GenesisTxHash(params *chaincfg.Params) chainhash.Hash

GenesisTxHash returns the hash of the single coinbase transaction in the genesis block of the specified network. This transaction is hard coded, and the pubkey script for its one output only decodes for simnet.

func GetDifficultyRatio

func GetDifficultyRatio(bits uint32, params *chaincfg.Params) float64

GetDifficultyRatio returns the proof-of-work difficulty as a multiple of the minimum difficulty using the passed bits field from the header of a block.

func HashInSlice

func HashInSlice(h chainhash.Hash, list []chainhash.Hash) bool

HashInSlice determines if a hash exists in a slice of hashes.

func IncludesStakeTx

func IncludesStakeTx(txHash *chainhash.Hash, block *dcrutil.Block) (int, int8)

IncludesStakeTx checks if a block contains a stake transaction hash

func IncludesTx

func IncludesTx(txHash *chainhash.Hash, block *dcrutil.Block) (int, int8)

IncludesTx checks if a block contains a transaction hash

func IsStakeTx

func IsStakeTx(msgTx *wire.MsgTx) bool

IsStakeTx indicates if the input MsgTx is a stake transaction.

func IsZeroHashP2PHKAddress added in v3.1.0

func IsZeroHashP2PHKAddress(checkAddressString string, params *chaincfg.Params) bool

IsZeroHashP2PHKAddress checks if the given address is the dummy (zero pubkey hash) address. See https://github.com/decred/dcrdata/v3/issues/358 for details.

func MedianAmount

func MedianAmount(s []dcrutil.Amount) dcrutil.Amount

MedianAmount gets the median Amount from a slice of Amounts

func MedianCoin

func MedianCoin(s []float64) float64

MedianCoin gets the median DCR from a slice of float64s

func MsgTxFromHex

func MsgTxFromHex(txhex string) (*wire.MsgTx, error)

MsgTxFromHex returns a wire.MsgTx struct built from the transaction hex string

func OutPointAddresses

func OutPointAddresses(outPoint *wire.OutPoint, c RawTransactionGetter,
	params *chaincfg.Params) ([]string, dcrutil.Amount, error)

OutPointAddresses gets the addresses paid to by a transaction output.

func OutPointAddressesFromString

func OutPointAddressesFromString(txid string, index uint32, tree int8,
	c RawTransactionGetter, params *chaincfg.Params) ([]string, error)

OutPointAddressesFromString is the same as OutPointAddresses, but it takes the outpoint as the tx string, vout index, and tree.

func RevokedTicketsInBlock

func RevokedTicketsInBlock(bl *dcrutil.Block) []chainhash.Hash

RevokedTicketsInBlock finds all the revoked tickets in the block.

func RewardsAtBlock

func RewardsAtBlock(blockIdx int64, votes uint16, p *chaincfg.Params) (work, stake, tax int64)

RewardsAtBlock computes the PoW, PoS (per vote), and project fund subsidies at for the specified block index, assuming a certain number of votes.

func SSGenVoteBits

func SSGenVoteBits(tx *wire.MsgTx) (uint16, error)

SSGenVoteBits returns the VoteBits of txOut[1] of a ssgen tx

func SSGenVoteChoices

func SSGenVoteChoices(tx *wire.MsgTx, params *chaincfg.Params) (BlockValidation, uint32, uint16, []*VoteChoice, error)

SSGenVoteChoices gets a ssgen's vote choices (block validity and any agendas). The vote's stake version, to which the vote choices correspond, and vote bits are also returned. Note that []*VoteChoice may be an empty slice if there are no consensus deployments for the transaction's vote version. The error value may be non-nil if the tx is not a valid ssgen.

func SSTXInBlock

func SSTXInBlock(block *dcrutil.Block) []*dcrutil.Tx

SSTXInBlock gets a slice containing all of the SSTX mined in a block

func TicketTxnsInBlock

func TicketTxnsInBlock(bl *dcrutil.Block) ([]chainhash.Hash, []*dcrutil.Tx)

TicketTxnsInBlock finds all the new tickets in the block.

func TicketsInBlock

func TicketsInBlock(bl *dcrutil.Block) ([]chainhash.Hash, []*wire.MsgTx)

TicketsInBlock finds all the new tickets in the block.

func TicketsSpentInBlock

func TicketsSpentInBlock(bl *dcrutil.Block) []chainhash.Hash

TicketsSpentInBlock finds all the tickets spent in the block.

func TotalOutFromMsgTx

func TotalOutFromMsgTx(msgTx *wire.MsgTx) dcrutil.Amount

TotalOutFromMsgTx computes the total value out of a MsgTx

func TotalVout

func TotalVout(vouts []dcrjson.Vout) dcrutil.Amount

TotalVout computes the total value of a slice of dcrjson.Vout

func TxConsumesOutpointWithAddress

func TxConsumesOutpointWithAddress(msgTx *wire.MsgTx, addr string,
	c VerboseTransactionGetter, params *chaincfg.Params) (prevOuts []PrevOut, prevTxs []*TxWithBlockData)

TxConsumesOutpointWithAddress checks a transaction for inputs that spend an outpoint paying to the given address. Returned are the identified input indexes and the corresponding previous outpoints determined.

func TxFee

func TxFee(msgTx *wire.MsgTx) dcrutil.Amount

TxFee computes and returns the fee for a given tx

func TxFeeRate

func TxFeeRate(msgTx *wire.MsgTx) (dcrutil.Amount, dcrutil.Amount)

TxFeeRate computes and returns the fee rate in DCR/KB for a given tx

func TxInvolvesAddress

func TxInvolvesAddress(msgTx *wire.MsgTx, addr string, c VerboseTransactionGetter,
	params *chaincfg.Params) (outpoints []*wire.OutPoint,
	prevOuts []PrevOut, prevTxs []*TxWithBlockData)

TxInvolvesAddress checks the inputs and outputs of a transaction for involvement of the given address.

func TxIsRegular added in v3.1.0

func TxIsRegular(txType int) bool

TxIsRegular indicates if the transaction type is a regular (non-stake) transaction.

func TxIsRevoke added in v3.1.0

func TxIsRevoke(txType int) bool

TxIsRevoke indicates if the transaction type is a revocation (ssrtx).

func TxIsTicket added in v3.1.0

func TxIsTicket(txType int) bool

TxIsTicket indicates if the transaction type is a ticket (sstx).

func TxIsVote added in v3.1.0

func TxIsVote(txType int) bool

TxIsVote indicates if the transaction type is a vote (ssgen).

func TxPaysToAddress

func TxPaysToAddress(msgTx *wire.MsgTx, addr string,
	params *chaincfg.Params) (outpoints []*wire.OutPoint)

TxPaysToAddress returns a slice of outpoints of a transaction which pay to specified address.

func TxTree

func TxTree(msgTx *wire.MsgTx) int8

TxTree returns for a wire.MsgTx either wire.TxTreeStake or wire.TxTreeRegular depending on the type of transaction.

func TxTypeToString added in v3.1.0

func TxTypeToString(txType int) string

TxTypeToString returns a string representation of the provided transaction type, which corresponds to the txn types defined for stake.TxType type.

func TxhashInSlice

func TxhashInSlice(txs []*dcrutil.Tx, txHash *chainhash.Hash) *dcrutil.Tx

TxhashInSlice searches a slice of *dcrutil.Tx for a transaction with the hash txHash. If found, it returns the corresponding *Tx, otherwise nil.

func UltimateSubsidy

func UltimateSubsidy(params *chaincfg.Params) int64

UltimateSubsidy computes the total subsidy over the entire subsidy distribution period of the network.

func ValidateNetworkAddress added in v3.1.0

func ValidateNetworkAddress(address dcrutil.Address, p *chaincfg.Params) bool

ValidateNetworkAddress checks if the given address is valid on the given network.

func VoteBitsInBlock

func VoteBitsInBlock(block *dcrutil.Block) []stake.VoteVersionTuple

VoteBitsInBlock returns a list of vote bits for the votes in a block

func VoteVersion

func VoteVersion(pkScript []byte) uint32

VoteVersion extracts the vote version from the input pubkey script.

func VotesInBlock

func VotesInBlock(bl *dcrutil.Block) []chainhash.Hash

VotesInBlock finds all the votes in the block.

Types

type AddressError added in v3.1.0

type AddressError error

AddressError is the type of error returned by AddressValidation.

var (
	AddressErrorNoError      AddressError = nil
	AddressErrorZeroAddress  AddressError = errors.New("null address")
	AddressErrorWrongNet     AddressError = errors.New("wrong network")
	AddressErrorDecodeFailed AddressError = errors.New("decoding failed")
	AddressErrorUnknown      AddressError = errors.New("unknown error")
	AddressErrorUnsupported  AddressError = errors.New("recognized, but unsuported address type")
)

type AddressOutpoints

type AddressOutpoints struct {
	Address   string
	Outpoints []*wire.OutPoint
	PrevOuts  []PrevOut
	TxnsStore map[chainhash.Hash]*TxWithBlockData
}

AddressOutpoints collects spendable and spent transactions outpoints paying to a certain address. The transactions referenced by the outpoints are stored for quick access.

func NewAddressOutpoints

func NewAddressOutpoints(address string) *AddressOutpoints

NewAddressOutpoints creates a new AddressOutpoints, initializing the transaction store/cache, and setting the address string.

func (*AddressOutpoints) Merge

func (a *AddressOutpoints) Merge(ao *AddressOutpoints)

Merge concatenates the outpoints of two AddressOutpoints, and merges the transactions.

func (*AddressOutpoints) Update

func (a *AddressOutpoints) Update(Txns []*TxWithBlockData,
	Outpoints []*wire.OutPoint, PrevOutpoint []PrevOut)

Update appends the provided outpoints, and merges the transactions.

type AddressType added in v3.1.0

type AddressType int

type BlockValidation

type BlockValidation struct {
	// Hash is the hash of the block being targeted (in)validated
	Hash chainhash.Hash

	// Height is the height of the block
	Height int64

	// Validity indicates the vote is to validate (true) or invalidate (false)
	// the block.
	Validity bool
}

BlockValidation models the block validation indicated by an ssgen (vote) transaction.

func SSGenVoteBlockValid

func SSGenVoteBlockValid(msgTx *wire.MsgTx) (BlockValidation, uint16, error)

SSGenVoteBlockValid determines if a vote transaction is voting yes or no to a block, and returns the votebits in case the caller wants to check agenda votes. The error return may be ignored if the input transaction is known to be a valid ssgen (vote), otherwise it should be checked.

type BlockWatchedTx

type BlockWatchedTx struct {
	BlockHeight   int64
	TxsForAddress map[string][]*dcrutil.Tx
}

BlockWatchedTx contains, for a certain block, the transactions for certain watched addresses

type PrevOut

type PrevOut struct {
	TxSpending       chainhash.Hash
	InputIndex       int
	PreviousOutpoint *wire.OutPoint
}

PrevOut contains a transaction input's previous outpoint, the Hash of the spending (following) transaction, and input index in the transaction.

type RawTransactionGetter

type RawTransactionGetter interface {
	GetRawTransaction(txHash *chainhash.Hash) (*dcrutil.Tx, error)
}

RawTransactionGetter is an interface satisfied by rpcclient.Client, and required by functions that would otherwise require a rpcclient.Client just for GetRawTransaction.

type ReorgData added in v3.1.0

type ReorgData struct {
	CommonAncestor chainhash.Hash
	OldChainHead   chainhash.Hash
	OldChainHeight int32
	OldChain       []chainhash.Hash
	NewChainHead   chainhash.Hash
	NewChainHeight int32
	NewChain       []chainhash.Hash
	WG             *sync.WaitGroup
}

ReorgData contains details of a chain reorganization, including the full old and new chains, and the common ancestor that should not be included in either chain.

type TxAction

type TxAction int32

TxAction is what is happening to the transaction (mined or inserted into mempool).

const (
	TxMined TxAction = 1 << iota
	TxInserted
)

Valid values for TxAction

type TxWithBlockData

type TxWithBlockData struct {
	Tx          *wire.MsgTx
	BlockHeight int64
	BlockHash   string
	MemPoolTime int64
}

TxWithBlockData contains a MsgTx and the block hash and height in which it was mined and Time it entered MemPool.

func (*TxWithBlockData) Confirmed

func (t *TxWithBlockData) Confirmed() bool

Confirmed indicates if the transaction is confirmed (mined).

func (*TxWithBlockData) Hash

func (t *TxWithBlockData) Hash() chainhash.Hash

Hash returns the chainhash.Hash of the transaction.

type VerboseTransactionGetter

type VerboseTransactionGetter interface {
	GetRawTransactionVerbose(txHash *chainhash.Hash) (*dcrjson.TxRawResult, error)
}

VerboseTransactionGetter is an interface satisfied by rpcclient.Client, and required by functions that would otherwise require a rpcclient.Client just for GetRawTransactionVerbose.

type VoteChoice

type VoteChoice struct {
	// Single unique word identifying the vote.
	ID string `json:"id"`

	// Longer description of what the vote is about.
	Description string `json:"description"`

	// Usable bits for this vote.
	Mask uint16 `json:"mask"`

	// VoteVersion and VoteIndex specify which vote item is referenced by this
	// VoteChoice (i.e. chaincfg.Params.Deployments[VoteVersion][VoteIndex]).
	VoteVersion uint32 `json:"vote_version"`
	VoteIndex   int    `json:"vote_index"`

	// ChoiceIdx indicates the corresponding element in the vote item's []Choice
	ChoiceIdx int `json:"choice_index"`

	// Choice is the selected choice for the specified vote item
	Choice *chaincfg.Choice `json:"choice"`
}

VoteChoice represents the choice made by a vote transaction on a single vote item in an agenda. The ID, Description, and Mask fields describe the vote item for which the choice is being made. Those are the initial fields in chaincfg.Params.Deployments[VoteVersion][VoteIndex].

Jump to

Keyboard shortcuts

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