splitticket

package
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2019 License: ISC Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SplitTicketSrvService is the _service_ part of an SRV record that is
	// looked up prior to connecting to the split ticket service
	SplitTicketSrvService = "split-tickets-grpc"

	// SplitTicketSrvProto is the protocol to use when looking up an SRV
	// record for the split ticket service
	SplitTicketSrvProto = "tcp"

	// MinimumSplitInputConfirms is how many confirmations any utxo being used
	// in the split transaction must have before being allowed in
	MinimumSplitInputConfirms = 2

	// MaximumSplitInputs is the maximum number of inputs into a split
	// transaction that a single participant should provide.
	//
	// Sending too many inputs might cause a DoS in either the server or the
	// participants, so servers should limit participants to this number of
	// inputs. Participants need to consolidate their funds if they have more
	// utxos than this.
	//
	// As of 2018-11-09 only ~3% of all mainnet txs used more than 20 inputs, so
	// this should be reasonable.
	MaximumSplitInputs = 20

	// TxFeeRate is the expected transaction fee rate for the split and ticket
	// transactions of participants of split tickets.
	//
	// Measured as Atoms/KB. 1e5 = 0.001 DCR
	TxFeeRate dcrutil.Amount = 1e5
)
View Source
const (
	// TicketTxInitialSize is the initial size estimate for the ticket
	// transaction. It includes the tx header + the txout for the ticket
	// voting address
	TicketTxInitialSize = 12 +
		1 + 1 + 1 +
		8 + 2 + 1 + 26 // ticket submission TxOut = amount + version + script

	// TicketParticipantSize is the size estimate for each additional
	// participant in a split ticket purchase (the txIn + 2 txOuts)
	TicketParticipantSize = 32 + 4 + 1 + 4 +
		8 + 4 + 4 +
		1 + 108 +
		8 + 2 + 1 + 32 +
		8 + 2 + 1 + 26 // Stake Change TxOut = amount + version + script
)
View Source
const (
	// SecretNbHashSize is the size of the hash of a secret number
	SecretNbHashSize = 32

	// SecretNbSize is the number of random bytes of a secret number
	SecretNbSize = 8

	// LotteryCommitmentHashSize is the size in bytes of the lottery commitment
	LotteryCommitmentHashSize = 32
)
View Source
const (
	// MaxPoolFeeRateTestnet is the maximum observed pool fee rate in
	// testnet/simnet (%)
	MaxPoolFeeRateTestnet = 7.5

	// MaxPoolFeeRateMainnet is the maximum observed pool fee rate in mainnet (%)
	MaxPoolFeeRateMainnet = 5.0

	// MaximumTicketExpiry is the maximum expiry (in blocks) expected in a ticket
	// transaction
	MaximumTicketExpiry = 16

	// CommitmentLimits is the limit used in ticket commitments
	CommitmentLimits = uint16(0x5800)
)
View Source
const (
	// RedeemPoolVotingScriptSize is the maximum size of a scriptSig used to redeem
	// a ticket tx (via vote or revocation) when using a stakepool.
	RedeemPoolVotingScriptSize = 1 + 73 + 1 + 73
)
View Source
const VoterLotteryPkScriptSize = 1 + 1 + 32

VoterLotteryPkScriptSize is the number of bytes in the output script of the output that stores the voter lottery commitment. The entry is: OP_RETURN OP_DATA32 [32-byte data]

Variables

This section is empty.

Functions

func CalcLotteryResult added in v0.5.1

func CalcLotteryResult(secretNbs []SecretNumber,
	contribAmounts []dcrutil.Amount, mainchainHash *chainhash.Hash) (
	dcrutil.Amount, int)

CalcLotteryResult discovers the the selected coin and selected index (ie, the results of the voter selection lottery) given the input data. Note that len(secretNbs) MUST be equal to len(contribAmounts), otherwise the result may be undefined.

func CalcLotteryResultHash added in v0.5.1

func CalcLotteryResultHash(secretNbs []SecretNumber,
	mainchainHash *chainhash.Hash) []byte

CalcLotteryResultHash calculates the hash result of the lottery. This value is interpreted as a 256 bit uint, such that its value (mod total contribution amount) is the coin that should belong to the voter.

func CheckOnlySignedInSplit

func CheckOnlySignedInSplit(split *wire.MsgTx, outpoints []wire.OutPoint) error

CheckOnlySignedInSplit checks whether the only signed inputs (identified by their respective outpoints) of the ticket are the expected ones.

This is used by individual participants to ensure they are only signing the outpoints they previously specified for a particular session.

Only safe to be called on splits that have passed the CheckSplit function.

func CheckParticipantInSplit

func CheckParticipantInSplit(split *wire.MsgTx, splitAddress dcrutil.Address,
	commitAmount, ticketFee dcrutil.Amount, splitChange *wire.TxOut,
	params *chaincfg.Params) error

CheckParticipantInSplit verifies that the given split transaction records the given output address for ticket participation and the specified change.

This is to be used by each individual participant to verify if they are present in the split transaction.

func CheckParticipantInTicket

func CheckParticipantInTicket(split, ticket *wire.MsgTx, amount,
	fee dcrutil.Amount, commitmentAddr, splitAddr dcrutil.Address,
	splitChange *wire.TxOut, index uint32,
	splitInputs []wire.OutPoint, params *chaincfg.Params) error

CheckParticipantInTicket checks whether the participant at `index` on the ticket is present with the given parameters.

This function checks whether the input and commitment output of the ith participant is being committed to the correct address and amount.

This is only safe to be called on tickets that have passed the CheckTicket function.

func CheckParticipantSessionPoolFee added in v0.6.0

func CheckParticipantSessionPoolFee(numParticipants int, ticketPrice dcrutil.Amount,
	contribAmount, partPoolFee, partFee dcrutil.Amount, blockHeight int,
	poolFeePerc float64, net *chaincfg.Params) error

CheckParticipantSessionPoolFee checks whether the pool fee paid by a given participant is fair, assuming a given ticket price, poolFeeRate and contribution amount.

func CheckRevocation

func CheckRevocation(ticket, revocation *wire.MsgTx, params *chaincfg.Params) error

CheckRevocation checks whether the revocation for the given ticket respects the rules for split ticket buying. The ticket must have passed the CheckTicket function.

func CheckSelectedVoter

func CheckSelectedVoter(secretNbs []SecretNumber,
	secretNbHashes []SecretNumberHash,
	amounts []dcrutil.Amount, voteScripts [][]byte,
	ticket *wire.MsgTx, mainchainHash *chainhash.Hash) error

CheckSelectedVoter checks whether all the provided information validates into the correct voter in the script.

The purpose of this function is to ensure that the provided ticket is the one that is supposed to be published into the network, given what is known about a split ticket matching session. The parameters are the following:

- secretNbs is the list of secret numbers provided by each individual participant - secretNbHashes is the list of secret number hashes provided by each individual participant *before* the split transaction was funded - amounts is the list of individual participation amounts - voteScripts is the list of voting scripts for each individual participant - ticket is the ticket transaction - mainchainHash is the hash of the block at the start of the matching session

Note that the lists must be in the correct order, otherwise the lottery choice will not be consistent.

func CheckSignedSplit

func CheckSignedSplit(split *wire.MsgTx, utxos UtxoMap, params *chaincfg.Params) error

CheckSignedSplit validates that the given signed split transaction is valid according to split ticket matcher rules. Only safe to be called on split transactions that passed CheckSplit

func CheckSignedTicket

func CheckSignedTicket(split, ticket *wire.MsgTx, params *chaincfg.Params) error

CheckSignedTicket validates whether the given signed ticket can be spent on the network. Only safe to be called on tickets that passed CheckTicket().

func CheckSplit

func CheckSplit(split *wire.MsgTx, utxos UtxoMap,
	secretHashes []SecretNumberHash, mainchainHash *chainhash.Hash,
	currentBlockHeight uint32, params *chaincfg.Params) error

CheckSplit validates that the given split transaction respects the rules for the split ticket matching service

func CheckSplitLotteryCommitment added in v0.5.1

func CheckSplitLotteryCommitment(split *wire.MsgTx,
	secretHashes []SecretNumberHash, amounts []dcrutil.Amount,
	voteAddresses []dcrutil.Address, mainchainHash *chainhash.Hash) error

CheckSplitLotteryCommitment verifies whether the split transaction contains the correct voter commitment lottery, given the information necessary to derive it. Only safe to be called on transactions that have passed CheckSplit().

func CheckTicket

func CheckTicket(split, ticket *wire.MsgTx, ticketPrice,
	partTicketFee dcrutil.Amount, partsAmounts []dcrutil.Amount,
	currentBlockHeight uint32, params *chaincfg.Params) error

CheckTicket validates that the given ticket respects the rules for the split ticket matching service. Split must have passed the CheckSplit() function. This function can be called on buyers, to ensure that no other participant or the matcher service are trying to trick the buyer into a malicious split ticket session, wasting time or (more importantly) funds.

func CheckTicketPoolFeeRate added in v0.5.2

func CheckTicketPoolFeeRate(split, ticket *wire.MsgTx, poolFeeRate float64,
	currentBlockHeight uint32, params *chaincfg.Params) error

CheckTicketPoolFeeRate checks whether the pool fee recorded in the given ticket is acceptable by a voting pool using the given poolFeeRate as subsidy requirement and at most 1% more than the poolFeeRate. Only safe to be called on tickets that passed CheckTicket.

func CheckTicketScriptMatchAddresses

func CheckTicketScriptMatchAddresses(voteAddress, poolAddress dcrutil.Address,
	votePkScript, poolPkScript []byte, params *chaincfg.Params) error

CheckTicketScriptMatchAddresses checks whether the voteaddress is actually present in the vote pk script and if the pool address is present in the poolPkScript

func CreateUnsignedRevocation added in v0.5.1

func CreateUnsignedRevocation(ticketHash *chainhash.Hash,
	ticketPurchase *wire.MsgTx, feePerKB dcrutil.Amount) (*wire.MsgTx, error)

CreateUnsignedRevocation creates an unsigned revocation transaction that revokes a missed or expired ticket. Revocations must carry a relay fee and this function can error if the revocation contains no suitable output to decrease the estimated relay fee from.

This is based on the dcrwallet code, copied here due to it not being originally exported.

func FindRevocationTxFee added in v0.5.0

func FindRevocationTxFee(ticket, revocation *wire.MsgTx) (dcrutil.Amount, error)

FindRevocationTxFee finds the revocation transaction fee, assuming the ticket is correct. Only safe to be called on ticket and revocation transactions that have passed their respective check functions.

func FindTicketTxFee

func FindTicketTxFee(splitTx, ticket *wire.MsgTx) (dcrutil.Amount, error)

FindTicketTxFee finds the ticket transaction fee, assuming the split ticket is correct. Only safe to be called on split and ticket transactions that have passed their respective check functions.

func FindTxFee

func FindTxFee(tx *wire.MsgTx, utxos UtxoMap) (dcrutil.Amount, error)

FindTxFee finds the total transaction fee paid on the the given transaction This function does **not** check for overflows.

func RevocationFeeRate added in v0.5.1

func RevocationFeeRate(params *chaincfg.Params) dcrutil.Amount

RevocationFeeRate is the fee rate in Atoms/KB of the revocation tx for a given network.

func SelectContributionAmounts

func SelectContributionAmounts(maxAmounts []dcrutil.Amount, ticketPrice,
	partFee, poolFee dcrutil.Amount) ([]dcrutil.Amount, []dcrutil.Amount, error)

SelectContributionAmounts decides how to split the ticket priced at ticketPrice such that each ith participant contributes at most maxAmount[i], pays a participation fee partFee and everyone pays total poolFee.

In order to call this function sum(maxAmounts) must be > (ticketPrice + sum(partFee)) and the maxAmounts **MUST** be ordered in ascending order of amount.

The algorithm tries to split the purchasing amounts between the participants in such a way as to average the contribution % of the participants, while ensuring that the ticket can be bought.

This function returns the corresponding commitment amounts for each participant (ie, what the sstxcommitment output will be for each participant) and the proportional pool fee amount.

func SessionFeeEstimate added in v0.5.2

func SessionFeeEstimate(numParticipants int) dcrutil.Amount

SessionFeeEstimate returns an estimate for the fees of a session with the given number of participants.

Note that the calculation is done from SessionParticipantFee in order to be certain that all participants will pay an integer and equal amount of fees.

func SessionParticipantFee added in v0.5.2

func SessionParticipantFee(numParticipants int) dcrutil.Amount

SessionParticipantFee returns the fee that a single participant of a ticket split tx with the given number of participants should pay

func SessionPoolFee added in v0.6.0

func SessionPoolFee(numParticipants int, ticketPrice dcrutil.Amount,
	blockHeight int, poolFeePerc float64, net *chaincfg.Params) dcrutil.Amount

SessionPoolFee returns the estimate for pool fee contribution for a split ticket session, given the parameters.

func StakeDiffChangeDistance

func StakeDiffChangeDistance(blockHeight uint32, params *chaincfg.Params) int32

StakeDiffChangeDistance returns the distance (in blocks) to the closest stake diff change block (either in the past or the future, whichever is closest).

func TargetTicketExpirationBlock added in v0.5.2

func TargetTicketExpirationBlock(curBlockHeight, maxExpiry uint32,
	params *chaincfg.Params) uint32

TargetTicketExpirationBlock calculates the expected expiration block for a ticket, given the current block height and a maximum expiry value.

The calculated value is guaranteed to be < maxExpiry, but may be significantly less if the current block height is close to a change in stake difficulty.

func TicketSizeEstimate added in v0.6.0

func TicketSizeEstimate(numParticipants int) int

TicketSizeEstimate returns the size estimate for the ticket transaction for the given number of participants

Types

type LotteryCommitmentHash added in v0.5.1

type LotteryCommitmentHash [LotteryCommitmentHashSize]byte

LotteryCommitmentHash is the hash that commits all participants of a split ticket session to a given lottery result, which decides the voter of the session. This commitment is added to the split tx as an OP_RETURN, in order for the lottery results to be accountable (ie, a single participant can prove whether the posted ticket was the one agreed upon by the rules of the lottery).

func CalcLotteryCommitmentHash added in v0.5.1

func CalcLotteryCommitmentHash(secretNbHashes []SecretNumberHash,
	amounts []dcrutil.Amount, voteAddresses []dcrutil.Address,
	mainchainHash *chainhash.Hash) *LotteryCommitmentHash

CalcLotteryCommitmentHash calculates the lottery commitment hash, given all required information.

The lottery commitment hash is calculated as follows:

H(secretNbHashes || amounts || voteAddresses)

The mainchainHash[16:] is used as salt to the calculation. The individual items are concatenated in slice order.

The number of secrets and amounts **MUST** be the same, otherwise the result is undefined

type SecretNumber

type SecretNumber []byte

SecretNumber is the secret number that each individual participant chooses.

func RandomSecretNumber added in v0.7.5

func RandomSecretNumber() SecretNumber

RandomSecretNumber returns a cryptografically random secret number for use in split ticket transactions.

func (SecretNumber) Format added in v0.7.5

func (nb SecretNumber) Format(f fmt.State, c rune)

Format fulfills the fmt.Formatter interface

func (SecretNumber) Hash

func (nb SecretNumber) Hash(mainchainHash *chainhash.Hash) SecretNumberHash

Hash gives the hash of the secret number, given the hash of a block to use as salt. This is usually called with the block id of the mainchain tip.

func (SecretNumber) String added in v0.7.5

func (nb SecretNumber) String() string

type SecretNumberHash

type SecretNumberHash [SecretNbHashSize]byte

SecretNumberHash represents the hash of a secret number

func (SecretNumberHash) Equals

func (h SecretNumberHash) Equals(other SecretNumberHash) bool

Equals checks whether the hashes are equal.

func (SecretNumberHash) String added in v0.4.7

func (h SecretNumberHash) String() string

String converts the secret hash to a string representation.

type UtxoEntry

type UtxoEntry struct {
	PkScript      []byte
	Value         dcrutil.Amount
	Version       uint16
	Confirmations int64
}

UtxoEntry is an entry of the utxo set of the network

type UtxoMap

type UtxoMap map[wire.OutPoint]UtxoEntry

UtxoMap is an auxilary type for the split transaction checks.

func UtxoMapFromDcrdata added in v0.7.0

func UtxoMapFromDcrdata(dcrdataURL string, tx *wire.MsgTx) (UtxoMap, error)

UtxoMapFromDcrdata queries the dcrdata server for the outpoints of the given transaction and returns an utxo map for use in validation functions.

func UtxoMapFromNetwork

func UtxoMapFromNetwork(client *rpcclient.Client, tx *wire.MsgTx) (UtxoMap, error)

UtxoMapFromNetwork queries a daemon connected via rpc for the outpoints of the given transaction and returns an utxo map for use in validation functions.

func UtxoMapOutpointsFromDcrdata added in v0.7.0

func UtxoMapOutpointsFromDcrdata(dcrdataURL string, outpoints []*wire.OutPoint) (UtxoMap, error)

UtxoMapOutpointsFromDcrdata queries the dcrdata server for the outpoints of the given transaction and returns an utxo map for use in validation functions.

func UtxoMapOutpointsFromNetwork

func UtxoMapOutpointsFromNetwork(client *rpcclient.Client, outpoints []*wire.OutPoint) (UtxoMap, error)

UtxoMapOutpointsFromNetwork queries a daemon connected via rpc for the outpoints of the given transaction and returns an utxo map for use in validation functions.

type VoterSelectionValidationError

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

VoterSelectionValidationError are errors generated inside the voter validation check function. These are considered critical errors of the ticket matching process.

func (VoterSelectionValidationError) Error

Jump to

Keyboard shortcuts

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