internal

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: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CreateAddressTable = `` /* 284-byte string literal not displayed */

	// InsertAddressRow inserts a address block row without checking for unique
	// index conflicts. This should only be used before the unique indexes are
	// created or there may be constraint violations (errors).
	InsertAddressRow = insertAddressRow + `RETURNING id;`

	// UpsertAddressRow is an upsert (insert or update on conflict), returning
	// the inserted/updated address row id.
	UpsertAddressRow = insertAddressRow + `ON CONFLICT (tx_vin_vout_row_id, address, is_funding) DO UPDATE
		SET matching_tx_hash = $2, tx_hash = $3, tx_vin_vout_index = $4,
		block_time = $7, valid_mainchain = $9 RETURNING id;`

	// InsertAddressRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with addresses' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertAddressRowOnConflictDoNothing = `WITH inserting AS (` +
		insertAddressRow +
		`	ON CONFLICT (tx_vin_vout_row_id, address, is_funding) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM inserting
		UNION  ALL
		SELECT id FROM addresses
		WHERE  address = $1 AND is_funding = $8 AND tx_vin_vout_row_id = $5 -- only executed if no INSERT
		LIMIT  1;`

	// IndexAddressTableOnVoutID creates the unique index uix_addresses_vout_id
	// on (tx_vin_vout_row_id, address, is_funding).
	IndexAddressTableOnVoutID = `CREATE UNIQUE INDEX uix_addresses_vout_id
		ON addresses(tx_vin_vout_row_id, address, is_funding);`
	DeindexAddressTableOnVoutID = `DROP INDEX uix_addresses_vout_id;`

	// IndexBlockTimeOnTableAddress creates a sorted index on block_time, which
	// accelerates queries with ORDER BY block_time LIMIT n OFFSET m.
	IndexBlockTimeOnTableAddress = `CREATE INDEX block_time_index
		ON addresses(block_time DESC NULLS LAST);`
	DeindexBlockTimeOnTableAddress = `DROP INDEX block_time_index;`

	IndexMatchingTxHashOnTableAddress = `CREATE INDEX matching_tx_hash_index
	    ON addresses(matching_tx_hash);`
	DeindexMatchingTxHashOnTableAddress = `DROP INDEX matching_tx_hash_index;`

	IndexAddressTableOnAddress = `CREATE INDEX uix_addresses_address
		ON addresses(address);`
	DeindexAddressTableOnAddress = `DROP INDEX uix_addresses_address;`

	IndexAddressTableOnTxHash = `CREATE INDEX uix_addresses_funding_tx
		ON addresses(tx_hash);`
	DeindexAddressTableOnTxHash = `DROP INDEX uix_addresses_funding_tx;`

	SelectAddressAllByAddress = `SELECT ` + addrsColumnNames + ` FROM addresses WHERE address=$1 ORDER BY block_time DESC;`
	SelectAddressRecvCount    = `SELECT COUNT(*) FROM addresses WHERE address=$1 AND valid_mainchain = TRUE;`

	SelectAddressesAllTxn = `` /* 300-byte string literal not displayed */

	SelectAddressUnspentCountANDValue = `` /* 142-byte string literal not displayed */

	SelectAddressSpentCountANDValue = `` /* 141-byte string literal not displayed */

	SelectAddressesMergedSpentCount = `SELECT COUNT( DISTINCT tx_hash ) FROM addresses
		WHERE address = $1 AND is_funding = FALSE AND valid_mainchain = TRUE;`

	// SelectAddressSpentUnspentCountAndValue gets the number and combined spent
	// and unspent outpoints for the given address. The key is the "GROUP BY
	// is_funding, matching_tx_hash=”" part of the statement that gets the data
	// for the combinations of is_funding (boolean) and matching_tx_hash=”
	// (boolean). There should never be any with is_funding=true where
	// matching_tx_hash is empty, thus there are three rows in the output. For
	// example, the first row is the spending transactions that must have
	// matching_tx_hash set, the second row the the funding transactions for the
	// first row (notice the equal count and sum), and the third row are the
	// unspent outpoints that are is_funding=true but with an empty
	// matching_tx_hash:
	//
	// count  |      sum       | is_funding | all_empty_matching | no_empty_matching
	// --------+----------------+------------+--------------------+--------------------
	//   45150 | 12352318108368 | f          | f                  | t
	//   45150 | 12352318108368 | t          | f                  | t
	//  229145 | 55875634749104 | t          | t                  | f
	// (3 rows)
	//
	// Since part of the grouping is on "matching_tx_hash = ”", what is
	// logically "any" empty matching is actually no_empty_matching.
	SelectAddressSpentUnspentCountAndValue = `` /* 335-byte string literal not displayed */

	SelectAddressUnspentWithTxn = `` /* 502-byte string literal not displayed */

	SelectAddressLimitNByAddress = `SELECT ` + addrsColumnNames + ` FROM addresses
		WHERE address=$1 AND valid_mainchain = TRUE
		ORDER BY block_time DESC LIMIT $2 OFFSET $3;`

	SelectAddressMergedDebitView = `` /* 317-byte string literal not displayed */

	SelectAddressDebitsLimitNByAddress = `SELECT ` + addrsColumnNames + `
		FROM addresses WHERE address=$1 AND is_funding = FALSE AND valid_mainchain = TRUE
		ORDER BY block_time DESC LIMIT $2 OFFSET $3;`

	SelectAddressCreditsLimitNByAddress = `SELECT ` + addrsColumnNames + `
		FROM addresses WHERE address=$1 AND is_funding = TRUE AND valid_mainchain = TRUE
		ORDER BY block_time DESC LIMIT $2 OFFSET $3;`

	SelectAddressIDsByFundingOutpoint = `` /* 132-byte string literal not displayed */

	SelectAddressIDByVoutIDAddress = `SELECT id FROM addresses WHERE address=$1 AND
	    tx_vin_vout_row_id=$2 AND is_funding = TRUE;`

	SelectAddressOldestTxBlockTime = `SELECT block_time FROM addresses WHERE
		address=$1 ORDER BY block_time LIMIT 1;`

	// SelectAddressTxTypesByAddress gets the transaction type histogram for the
	// given address using block time binning with bin size of block_time.
	// Regular transactions are grouped into (SentRtx and ReceivedRtx), SSTx
	// defines tickets, SSGen defines votes, and SSRtx defines revocations.
	SelectAddressTxTypesByAddress = `` /* 473-byte string literal not displayed */

	SelectAddressAmountFlowByAddress = `` /* 248-byte string literal not displayed */

	SelectAddressUnspentAmountByAddress = `` /* 181-byte string literal not displayed */

	// SetAddressMatchingTxHashForOutpoint sets the matching tx hash (a spending
	// transaction) for the addresses rows corresponding to the specified
	// outpoint (tx_hash:tx_vin_vout_index), a funding tx row.
	SetAddressMatchingTxHashForOutpoint = `UPDATE addresses SET matching_tx_hash=$1
		WHERE tx_hash=$2 AND is_funding = TRUE AND tx_vin_vout_index=$3`  // not terminated with ;

	// AssignMatchingTxHashForOutpoint is like
	// SetAddressMatchingTxHashForOutpoint except that it only updates rows
	// where matching_tx_hash is not already set.
	AssignMatchingTxHashForOutpoint = SetAddressMatchingTxHashForOutpoint + ` AND matching_tx_hash='';`

	SetAddressMainchainForVoutIDs = `UPDATE addresses SET valid_mainchain=$1
		WHERE is_funding = TRUE AND tx_vin_vout_row_id=$2;`

	SetAddressMainchainForVinIDs = `UPDATE addresses SET valid_mainchain=$1
		WHERE is_funding = FALSE AND tx_vin_vout_row_id=$2;`

	SetTxTypeOnAddressesByVinAndVoutIDs = `UPDATE addresses SET tx_type=$1 WHERE
		tx_vin_vout_row_id=$2 AND is_funding=$3;`

	// SelectAddressesGloballyInvalid selects the row ids of the addresses table
	// corresponding to transactions that should have valid_mainchain set to
	// false according to the transactions table. Should is defined as any
	// occurrence of a given transaction (hash) being flagged as is_valid AND
	// is_mainchain.
	SelectAddressesGloballyInvalid = `` /* 403-byte string literal not displayed */

	// UpdateAddressesGloballyInvalid sets valid_mainchain=false on address rows
	// identified by the SelectAddressesGloballyInvalid query (ids of
	// globally_invalid subquery table) as requiring this flag set, but which do
	// not already have it set (incorrectly_valid).
	UpdateAddressesGloballyInvalid = `UPDATE addresses SET valid_mainchain=false
		FROM (
			SELECT id FROM
			(
				` + SelectAddressesGloballyInvalid + `
			) AS invalid_ids
			WHERE invalid_ids.valid_mainchain=true
		) AS incorrectly_valid
		WHERE incorrectly_valid.id=addresses.id;`

	// UpdateAddressesFundingMatchingHash sets matching_tx_hash as per the vins
	// table. This is needed to fix partially updated addresses table entries
	// that were affected by stake invalidation.
	UpdateAddressesFundingMatchingHash = `` /* 276-byte string literal not displayed */

	// UpdateValidMainchainFromTransactions sets valid_mainchain in all rows of
	// the addresses table according to the transactions table, unlike
	// UpdateAddressesGloballyInvalid that does it selectively for only the
	// incorrectly set addresses table rows.  This is much slower.
	UpdateValidMainchainFromTransactions = `` /* 157-byte string literal not displayed */

)
View Source
const (
	CreateBlockTable = `` /* 585-byte string literal not displayed */

	// InsertBlockRow inserts a new block row without checking for unique index
	// conflicts. This should only be used before the unique indexes are created
	// or there may be constraint violations (errors).
	InsertBlockRow = insertBlockRow + `RETURNING id;`

	// UpsertBlockRow is an upsert (insert or update on conflict), returning
	// the inserted/updated block row id.
	UpsertBlockRow = insertBlockRow + `ON CONFLICT (hash) DO UPDATE 
		SET is_valid = $4, is_mainchain = $5 RETURNING id;`

	// InsertBlockRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with blocks' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertBlockRowOnConflictDoNothing = `WITH ins AS (` +
		insertBlockRow +
		`	ON CONFLICT (hash) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM ins
		UNION  ALL
		SELECT id FROM blocks
		WHERE  hash = $1 -- only executed if no INSERT
		LIMIT  1;`

	// IndexBlockTableOnHash creates the unique index uix_block_hash on (hash).
	IndexBlockTableOnHash   = `CREATE UNIQUE INDEX uix_block_hash ON blocks(hash);`
	DeindexBlockTableOnHash = `DROP INDEX uix_block_hash;`

	// IndexBlocksTableOnHeight creates the index uix_block_height on (height).
	// This is not unique because of side chains.
	IndexBlocksTableOnHeight   = `CREATE INDEX uix_block_height ON blocks(height);`
	DeindexBlocksTableOnHeight = `DROP INDEX uix_block_height;`

	SelectBlockByTimeRangeSQL = `SELECT hash, height, size, time, numtx
		FROM blocks WHERE time BETWEEN $1 and $2 ORDER BY time DESC LIMIT $3;`
	SelectBlockByTimeRangeSQLNoLimit = `SELECT hash, height, size, time, numtx
		FROM blocks WHERE time BETWEEN $1 and $2 ORDER BY time DESC;`
	SelectBlockHashByHeight = `SELECT hash FROM blocks WHERE height = $1 AND is_mainchain = true;`
	SelectBlockHeightByHash = `SELECT height FROM blocks WHERE hash = $1;`

	RetrieveBestBlock          = `SELECT * FROM blocks ORDER BY height DESC LIMIT 0, 1;`
	RetrieveBestBlockHeightAny = `SELECT id, hash, height FROM blocks
		ORDER BY height DESC LIMIT 1;`
	RetrieveBestBlockHeight = `SELECT id, hash, height FROM blocks
		WHERE is_mainchain = true ORDER BY height DESC LIMIT 1;`

	// SelectBlocksTicketsPrice selects the ticket price and difficulty for the
	// first block in a stake difficulty window.
	SelectBlocksTicketsPrice = `SELECT sbits, time, difficulty FROM blocks WHERE height % $1 = 0 ORDER BY time;`

	SelectWindowsByLimit = `` /* 365-byte string literal not displayed */

	SelectBlocksBlockSize = `SELECT time, size, numtx, height FROM blocks ORDER BY time;`

	SelectBlocksPreviousHash = `SELECT previous_hash FROM blocks WHERE hash = $1;`

	SelectBlocksHashes = `SELECT hash FROM blocks ORDER BY id;`

	SelectBlockVoteCount = `SELECT voters FROM blocks WHERE hash = $1;`

	SelectSideChainBlocks = `` /* 171-byte string literal not displayed */

	SelectSideChainTips = `` /* 177-byte string literal not displayed */

	SelectBlockStatus = `` /* 151-byte string literal not displayed */

	SelectBlockFlags = `SELECT is_valid, is_mainchain
		FROM blocks
		WHERE hash = $1;`

	SelectDisapprovedBlocks = `` /* 171-byte string literal not displayed */

	UpdateLastBlockValid = `UPDATE blocks SET is_valid = $2 WHERE id = $1;`
	UpdateBlockMainchain = `UPDATE blocks SET is_mainchain = $2 WHERE hash = $1 RETURNING previous_hash;`

	// block_chain table. The primary key is not a SERIAL, but rather the row ID
	// of the block in the blocks table.
	CreateBlockPrevNextTable = `` /* 164-byte string literal not displayed */

	// InsertBlockPrevNext includes the primary key, which should be the row ID
	// of the corresponding block in the blocks table.
	InsertBlockPrevNext = `` /* 134-byte string literal not displayed */

	SelectBlockChainRowIDByHash = `SELECT block_db_id FROM block_chain WHERE this_hash = $1;`

	UpdateBlockNext       = `UPDATE block_chain SET next_hash = $2 WHERE block_db_id = $1;`
	UpdateBlockNextByHash = `UPDATE block_chain SET next_hash = $2 WHERE this_hash = $1;`
)
View Source
const (
	IndexExists = `SELECT 1
FROM   pg_class c
JOIN   pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relname = $1 AND n.nspname = $2;`

	IndexIsUnique = `` /* 167-byte string literal not displayed */

)
View Source
const (
	CreateTicketsTable = `` /* 397-byte string literal not displayed */

	// InsertTicketRow inserts a new ticket row without checking for unique
	// index conflicts. This should only be used before the unique indexes are
	// created or there may be constraint violations (errors).
	InsertTicketRow = insertTicketRow + `RETURNING id;`

	// UpsertTicketRow is an upsert (insert or update on conflict), returning
	// the inserted/updated ticket row id. is_mainchain is updated as this might
	// be a reorganization.
	UpsertTicketRow = insertTicketRow + `ON CONFLICT (tx_hash, block_hash) DO UPDATE 
		SET is_mainchain = $13 RETURNING id;`

	// InsertTicketRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with tickets' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertTicketRowOnConflictDoNothing = `WITH ins AS (` +
		insertTicketRow +
		`	ON CONFLICT (tx_hash, block_hash) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM ins
		UNION  ALL
		SELECT id FROM tickets
		WHERE  tx_hash = $1 AND block_hash = $2 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteTicketsDuplicateRows removes rows that would violate the unique
	// index uix_ticket_hashes_index. This should be run prior to creating the
	// index.
	DeleteTicketsDuplicateRows = `` /* 185-byte string literal not displayed */

	// IndexTicketsTableOnHashes creates the unique index
	// uix_ticket_hashes_index on (tx_hash, block_hash).
	IndexTicketsTableOnHashes = `CREATE UNIQUE INDEX uix_ticket_hashes_index
		ON tickets(tx_hash, block_hash);`
	DeindexTicketsTableOnHashes = `DROP INDEX uix_ticket_hashes_index;`

	// IndexTicketsTableOnTxDbID creates the unique index that ensures only one
	// row in the tickets table may refer to a certain row of the transactions
	// table. This is not the same as being unique on transaction hash, since
	// the transactions table also has a unique constraint is on (tx_hash,
	// block_hash) that allows a transaction appearing in multiple blocks (e.g.
	// side chains and/or invalidated blocks) to have multiple rows in the
	// transactions table.
	IndexTicketsTableOnTxDbID = `CREATE UNIQUE INDEX uix_ticket_ticket_db_id
		ON tickets(purchase_tx_db_id);`
	DeindexTicketsTableOnTxDbID = `DROP INDEX uix_ticket_ticket_db_id;`

	IndexTicketsTableOnPoolStatus = `CREATE INDEX uix_tickets_pool_status ON 
		tickets(pool_status);`
	DeindexTicketsTableOnPoolStatus = `DROP INDEX uix_tickets_pool_status;`

	SelectTicketsInBlock        = `SELECT * FROM tickets WHERE block_hash = $1;`
	SelectTicketsTxDbIDsInBlock = `SELECT purchase_tx_db_id FROM tickets WHERE block_hash = $1;`
	SelectTicketsForAddress     = `SELECT * FROM tickets WHERE stakesubmission_address = $1;`

	SelectTicketIDHeightByHash = `SELECT id, block_height FROM tickets` + forTxHashMainchainFirst
	SelectTicketIDByHash       = `SELECT id FROM tickets` + forTxHashMainchainFirst
	SelectTicketStatusByHash   = `SELECT id, spend_type, pool_status FROM tickets` + forTxHashMainchainFirst

	SelectUnspentTickets = `SELECT id, tx_hash FROM tickets
		WHERE spend_type = 0 AND is_mainchain = true;`

	SelectTicketsForPriceAtLeast = `SELECT * FROM tickets WHERE price >= $1;`
	SelectTicketsForPriceAtMost  = `SELECT * FROM tickets WHERE price <= $1;`

	SelectTicketsByPrice = `` /* 316-byte string literal not displayed */

	SelectTicketsByPurchaseDate = `` /* 386-byte string literal not displayed */

	SelectTicketSpendTypeByBlock = `` /* 204-byte string literal not displayed */

	SetTicketSpendingInfoForHash = `` /* 136-byte string literal not displayed */

	SetTicketSpendingInfoForTicketDbID = `UPDATE tickets
		SET spend_type = $4, spend_height = $2, spend_tx_db_id = $3, pool_status = $5
		WHERE id = $1;`
	SetTicketSpendingInfoForTxDbID = `` /* 126-byte string literal not displayed */

	SetTicketPoolStatusForTicketDbID = `UPDATE tickets SET pool_status = $2 WHERE id = $1;`
	SetTicketPoolStatusForHash       = `UPDATE tickets SET pool_status = $2 WHERE tx_hash = $1;`

	UpdateTicketsMainchainAll = `` /* 136-byte string literal not displayed */

	UpdateTicketsMainchainByBlock = `UPDATE tickets
		SET is_mainchain=$1 
		WHERE block_hash=$2;`

	CreateVotesTable = `` /* 338-byte string literal not displayed */

	// InsertVoteRow inserts a new vote row without checking for unique index
	// conflicts. This should only be used before the unique indexes are created
	// or there may be constraint violations (errors).
	InsertVoteRow = insertVoteRow + `RETURNING id;`

	// UpsertVoteRow is an upsert (insert or update on conflict), returning the
	// inserted/updated vote row id. is_mainchain is updated as this might be a
	// reorganization.
	UpsertVoteRow = insertVoteRow + `ON CONFLICT (tx_hash, block_hash) DO UPDATE 
		SET is_mainchain = $12 RETURNING id;`

	// InsertVoteRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with votes' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertVoteRowOnConflictDoNothing = `WITH ins AS (` +
		insertVoteRow +
		`	ON CONFLICT (tx_hash, block_hash) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM ins
		UNION  ALL
		SELECT id FROM votes
		WHERE  tx_hash = $2 AND block_hash = $3 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteVotesDuplicateRows removes rows that would violate the unique index
	// uix_votes_hashes_index. This should be run prior to creating the index.
	DeleteVotesDuplicateRows = `` /* 181-byte string literal not displayed */

	// IndexVotesTableOnHashes creates the unique index uix_votes_hashes_index
	// on (tx_hash, block_hash).
	IndexVotesTableOnHashes = `CREATE UNIQUE INDEX uix_votes_hashes_index
		ON votes(tx_hash, block_hash);`
	DeindexVotesTableOnHashes = `DROP INDEX uix_votes_hashes_index;`

	IndexVotesTableOnBlockHash = `CREATE INDEX uix_votes_block_hash
		ON votes(block_hash);`
	DeindexVotesTableOnBlockHash = `DROP INDEX uix_votes_block_hash;`

	IndexVotesTableOnCandidate = `CREATE INDEX uix_votes_candidate_block
		ON votes(candidate_block_hash);`
	DeindexVotesTableOnCandidate = `DROP INDEX uix_votes_candidate_block;`

	IndexVotesTableOnVoteVersion = `CREATE INDEX uix_votes_vote_version
		ON votes(version);`
	DeindexVotesTableOnVoteVersion = `DROP INDEX uix_votes_vote_version;`

	SelectAllVoteDbIDsHeightsTicketHashes = `SELECT id, height, ticket_hash FROM votes;`
	SelectAllVoteDbIDsHeightsTicketDbIDs  = `SELECT id, height, ticket_tx_db_id FROM votes;`

	UpdateVotesMainchainAll = `` /* 134-byte string literal not displayed */

	UpdateVotesMainchainByBlock = `UPDATE votes
		SET is_mainchain=$1 
		WHERE block_hash=$2;`

	CreateMissesTable = `` /* 173-byte string literal not displayed */

	// InsertMissRow inserts a new misss row without checking for unique index
	// conflicts. This should only be used before the unique indexes are created
	// or there may be constraint violations (errors).
	InsertMissRow = insertMissRow + `RETURNING id;`

	// UpsertMissRow is an upsert (insert or update on conflict), returning
	// the inserted/updated miss row id.
	UpsertMissRow = insertMissRow + `ON CONFLICT (ticket_hash, block_hash) DO UPDATE 
		SET ticket_hash = $4, block_hash = $2 RETURNING id;`

	// InsertMissRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with misses' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertMissRowOnConflictDoNothing = `WITH ins AS (` +
		insertMissRow +
		`	ON CONFLICT (ticket_hash, block_hash) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM ins
		UNION  ALL
		SELECT id FROM misses
		WHERE  block_hash = $2 AND ticket_hash = $4 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteMissesDuplicateRows removes rows that would violate the unique
	// index uix_misses_hashes_index. This should be run prior to creating the
	// index.
	DeleteMissesDuplicateRows = `` /* 187-byte string literal not displayed */

	// IndexMissesTableOnHashes creates the unique index uix_misses_hashes_index
	// on (ticket_hash, block_hash).
	IndexMissesTableOnHashes = `CREATE UNIQUE INDEX uix_misses_hashes_index
		ON misses(ticket_hash, block_hash);`
	DeindexMissesTableOnHashes = `DROP INDEX uix_misses_hashes_index;`

	SelectMissesInBlock = `SELECT ticket_hash FROM misses WHERE block_hash = $1;`

	CreateAgendasTable = `` /* 239-byte string literal not displayed */

	InsertAgendaRow = insertAgendaRow + `RETURNING id;`

	UpsertAgendaRow = insertAgendaRow + `ON CONFLICT (agenda_id, agenda_vote_choice, tx_hash, block_height) DO UPDATE 
		SET block_time = $5 RETURNING id;`

	// IndexAgendasTableOnAgendaID creates the unique index
	// uix_agendas_agenda_id on (agenda_id, agenda_vote_choice, tx_hash,
	// block_height).
	IndexAgendasTableOnAgendaID = `CREATE UNIQUE INDEX uix_agendas_agenda_id
		ON agendas(agenda_id, agenda_vote_choice, tx_hash, block_height);`
	DeindexAgendasTableOnAgendaID = `DROP INDEX uix_agendas_agenda_id;`

	IndexAgendasTableOnBlockTime = `CREATE INDEX uix_agendas_block_time
		ON agendas(block_time);`
	DeindexAgendasTableOnBlockTime = `DROP INDEX uix_agendas_block_time;`

	SelectAgendasAgendaVotesByTime = `SELECT block_time AS timestamp,
			count(CASE WHEN agenda_vote_choice = $1 THEN 1 ELSE NULL END) AS yes,
			count(CASE WHEN agenda_vote_choice = $2 THEN 1 ELSE NULL END) AS abstain,
			count(CASE WHEN agenda_vote_choice = $3 THEN 1 ELSE NULL END) AS no,
			count(*) AS total
		 FROM agendas
		WHERE agenda_id = $4
		  AND block_height <= (` + agendaLockinBlock + `)
		GROUP BY timestamp ORDER BY timestamp;`

	SelectAgendasAgendaVotesByHeight = `SELECT block_height,
			count(CASE WHEN agenda_vote_choice = $1 THEN 1 ELSE NULL END) AS yes,
			count(CASE WHEN agenda_vote_choice = $2 THEN 1 ELSE NULL END) AS abstain,
			count(CASE WHEN agenda_vote_choice = $3 THEN 1 ELSE NULL END) AS no,
			count(*) AS total
		 FROM agendas
		WHERE agenda_id = $4
		  AND block_height <= (` + agendaLockinBlock + `)
		GROUP BY block_height;`

	SelectAgendasLockedIn   = `SELECT block_height FROM agendas WHERE locked_in = true AND agenda_id = $1 LIMIT 1;`
	SelectAgendasHardForked = `SELECT block_height FROM agendas WHERE hard_forked = true AND agenda_id = $1 LIMIT 1;`
	SelectAgendasActivated  = `SELECT block_height FROM agendas WHERE activated = true AND agenda_id = $1 LIMIT 1;`
)
View Source
const (
	CreateTransactionTable = `` /* 453-byte string literal not displayed */

	// InsertTxRow inserts a new transaction row without checking for unique
	// index conflicts. This should only be used before the unique indexes are
	// created or there may be constraint violations (errors).
	InsertTxRow = insertTxRow + `RETURNING id;`

	// UpsertTxRow is an upsert (insert or update on conflict), returning the
	// inserted/updated transaction row id.
	UpsertTxRow = insertTxRow + `ON CONFLICT (tx_hash, block_hash) DO UPDATE 
		SET is_valid = $20, is_mainchain = $21 RETURNING id;`

	// InsertTxRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with transactions' unique tx index, while returning the row id
	// of either the inserted row or the existing row that causes the conflict.
	// The complexity of this statement is necessary to avoid an unnecessary
	// UPSERT, which would have performance consequences. The row is not locked.
	InsertTxRowOnConflictDoNothing = `WITH ins AS (` +
		insertTxRow +
		`	ON CONFLICT (tx_hash, block_hash) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM ins
		UNION  ALL
		SELECT id FROM transactions
		WHERE  tx_hash = $8 AND block_hash = $1 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteTxDuplicateRows removes rows that would violate the unique index
	// uix_tx_hashes. This should be run prior to creating the index.
	DeleteTxDuplicateRows = `` /* 191-byte string literal not displayed */

	// IndexTransactionTableOnHashes creates the unique index uix_tx_hashes on
	// (tx_hash, block_hash).
	IndexTransactionTableOnHashes = `CREATE UNIQUE INDEX uix_tx_hashes
		 ON transactions(tx_hash, block_hash);`
	DeindexTransactionTableOnHashes = `DROP INDEX uix_tx_hashes;`

	// Investigate removing this. block_hash is already indexed. It would be
	// unique with just (block_hash, block_index). And tree is likely not
	// important to index.  NEEDS TESTING BEFORE REMOVAL.
	IndexTransactionTableOnBlockIn = `CREATE UNIQUE INDEX uix_tx_block_in
		ON transactions(block_hash, block_index, tree);`
	DeindexTransactionTableOnBlockIn = `DROP INDEX uix_tx_block_in;`

	SelectTxByHash = `` /* 126-byte string literal not displayed */

	SelectTxsByBlockHash = `SELECT id, tx_hash, block_index, tree, block_time
		FROM transactions WHERE block_hash = $1;`

	SelectTxBlockTimeByHash = `` /* 130-byte string literal not displayed */

	SelectTxsPerDay = `SELECT to_timestamp(time)::date as date, count(*) FROM transactions
		GROUP BY date ORDER BY date;`

	SelectFullTxByHash = `` /* 330-byte string literal not displayed */

	SelectFullTxsByHash = `` /* 320-byte string literal not displayed */

	SelectTxnsVinsByBlock = `SELECT vin_db_ids, is_valid, is_mainchain
		FROM transactions WHERE block_hash = $1;`

	SelectTxnsVinsVoutsByBlock = `SELECT vin_db_ids, vout_db_ids, is_mainchain
		FROM transactions WHERE block_hash = $1;`

	SelectTxsVinsAndVoutsIDs = `SELECT tx_type, vin_db_ids, vout_db_ids
		FROM transactions
		WHERE block_height BETWEEN $1 AND $2;`

	SelectRegularTxnsVinsVoutsByBlock = `SELECT vin_db_ids, vout_db_ids, is_mainchain
		FROM transactions WHERE block_hash = $1 AND tree = 0;`

	SelectTxsBlocks = `` /* 173-byte string literal not displayed */

	UpdateRegularTxnsValidMainchainByBlock = `UPDATE transactions
		SET is_valid=$1, is_mainchain=$2 
		WHERE block_hash=$3 and tree=0;`

	UpdateRegularTxnsValidByBlock = `UPDATE transactions
		SET is_valid=$1 
		WHERE block_hash=$2 and tree=0;`

	UpdateTxnsMainchainByBlock = `UPDATE transactions
		SET is_mainchain=$1 
		WHERE block_hash=$2
		RETURNING id;`

	UpdateTxnsValidMainchainAll = `` /* 196-byte string literal not displayed */

	UpdateRegularTxnsValidAll = `` /* 142-byte string literal not displayed */

	UpdateTxnsMainchainAll = `` /* 141-byte string literal not displayed */

	SelectTicketsByType = `` /* 226-byte string literal not displayed */

)
View Source
const (
	CreateVinTable = `` /* 271-byte string literal not displayed */

	// InsertVinRow inserts a new vin row without checking for unique index
	// conflicts. This should only be used before the unique indexes are created
	// or there may be constraint violations (errors).
	InsertVinRow = insertVinRow + `RETURNING id;`

	// UpsertVinRow is an upsert (insert or update on conflict), returning the
	// inserted/updated vin row id.
	UpsertVinRow = insertVinRow + `ON CONFLICT (tx_hash, tx_index, tx_tree) DO UPDATE
		SET is_valid = $8, is_mainchain = $9, block_time = $10,
			prev_tx_hash = $4, prev_tx_index = $5, prev_tx_tree = $6
		RETURNING id;`

	// InsertVinRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with vins' unique tx index, while returning the row id of either
	// the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertVinRowOnConflictDoNothing = `WITH inserting AS (` +
		insertVinRow +
		`	ON CONFLICT (tx_hash, tx_index, tx_tree) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM inserting
		UNION  ALL
		SELECT id FROM vins
		WHERE  tx_hash = $1 AND tx_index = $2 AND tx_tree = $3 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteVinsDuplicateRows removes rows that would violate the unique index
	// uix_vin. This should be run prior to creating the index.
	DeleteVinsDuplicateRows = `` /* 186-byte string literal not displayed */

	IndexVinTableOnVins = `CREATE UNIQUE INDEX uix_vin
		ON vins(tx_hash, tx_index, tx_tree);`
	DeindexVinTableOnVins = `DROP INDEX uix_vin;`

	IndexVinTableOnPrevOuts = `CREATE INDEX uix_vin_prevout
		ON vins(prev_tx_hash, prev_tx_index);`
	DeindexVinTableOnPrevOuts = `DROP INDEX uix_vin_prevout;`

	SelectVinIDsALL = `SELECT id FROM vins;`
	CountVinsRows   = `SELECT reltuples::BIGINT AS estimate FROM pg_class WHERE relname='vins';`

	SetTxTypeOnVinsByVinIDs = `UPDATE vins SET tx_type=$1 WHERE id=$2;`

	SelectSpendingTxsByPrevTx                = `SELECT id, tx_hash, tx_index, prev_tx_index FROM vins WHERE prev_tx_hash=$1;`
	SelectSpendingTxsByPrevTxWithBlockHeight = `` /* 285-byte string literal not displayed */

	SelectSpendingTxByPrevOut = `` /* 152-byte string literal not displayed */

	SelectFundingTxsByTx        = `SELECT id, prev_tx_hash FROM vins WHERE tx_hash=$1;`
	SelectFundingTxByTxIn       = `SELECT id, prev_tx_hash FROM vins WHERE tx_hash=$1 AND tx_index=$2;`
	SelectFundingOutpointByTxIn = `SELECT id, prev_tx_hash, prev_tx_index, prev_tx_tree FROM vins
		WHERE tx_hash=$1 AND tx_index=$2;`

	SelectFundingOutpointByVinID     = `SELECT prev_tx_hash, prev_tx_index, prev_tx_tree FROM vins WHERE id=$1;`
	SelectFundingOutpointIndxByVinID = `SELECT prev_tx_index FROM vins WHERE id=$1;`
	SelectFundingTxByVinID           = `SELECT prev_tx_hash FROM vins WHERE id=$1;`
	SelectSpendingTxByVinID          = `SELECT tx_hash, tx_index, tx_tree FROM vins WHERE id=$1;`
	SelectAllVinInfoByID             = `` /* 158-byte string literal not displayed */

	SelectVinVoutPairByID = `SELECT tx_hash, tx_index, prev_tx_hash, prev_tx_index FROM vins WHERE id = $1;`

	SetIsValidIsMainchainByTxHash = `UPDATE vins SET is_valid = $1, is_mainchain = $2
		WHERE tx_hash = $3 AND block_time = $4 AND tx_tree = $5;`
	SetIsValidIsMainchainByVinID = `UPDATE vins SET is_valid = $2, is_mainchain = $3
		WHERE id = $1;`
	SetIsValidByTxHash = `UPDATE vins SET is_valid = $1
		WHERE tx_hash = $2 AND block_time = $3 AND tx_tree = $4;`
	SetIsValidByVinID = `UPDATE vins SET is_valid = $2
		WHERE id = $1;`
	SetIsMainchainByTxHash = `UPDATE vins SET is_mainchain = $1
		WHERE tx_hash = $2 AND block_time = $3 AND tx_tree = $4;`
	SetIsMainchainByVinID = `UPDATE vins SET is_mainchain = $2
		WHERE id = $1;`

	// SetVinsTableCoinSupplyUpgrade does not set is_mainchain because that upgrade comes after this one
	SetVinsTableCoinSupplyUpgrade = `UPDATE vins SET is_valid = $1, block_time = $3, value_in = $4
		WHERE tx_hash = $5 AND tx_index = $6 AND tx_tree = $7;`

	// SelectCoinSupply fetches the coin supply as of the latest block, where
	// sum represents the generated coins for all stakebase and only
	// stake-validated coinbase transactions.
	SelectCoinSupply = `` /* 244-byte string literal not displayed */

	CreateVinType = `` /* 169-byte string literal not displayed */

	CreateVoutTable = `` /* 231-byte string literal not displayed */

	// InsertVoutRow inserts a new vout row without checking for unique index
	// conflicts. This should only be used before the unique indexes are created
	// or there may be constraint violations (errors).
	InsertVoutRow = insertVoutRow + `RETURNING id;`

	// UpsertVoutRow is an upsert (insert or update on conflict), returning the
	// inserted/updated vout row id.
	UpsertVoutRow = insertVoutRow + `ON CONFLICT (tx_hash, tx_index, tx_tree) DO UPDATE
		SET version = $5 RETURNING id;`

	// InsertVoutRowOnConflictDoNothing allows an INSERT with a DO NOTHING on
	// conflict with vouts' unique tx index, while returning the row id of
	// either the inserted row or the existing row that causes the conflict. The
	// complexity of this statement is necessary to avoid an unnecessary UPSERT,
	// which would have performance consequences. The row is not locked.
	InsertVoutRowOnConflictDoNothing = `WITH inserting AS (` +
		insertVoutRow +
		`	ON CONFLICT (tx_hash, tx_index, tx_tree) DO NOTHING -- no lock on row
			RETURNING id
		)
		SELECT id FROM inserting
		UNION  ALL
		SELECT id FROM vouts
		WHERE  tx_hash = $1 AND tx_index = $2 AND tx_tree = $3 -- only executed if no INSERT
		LIMIT  1;`

	// DeleteVoutDuplicateRows removes rows that would violate the unique index
	// uix_vout_txhash_ind. This should be run prior to creating the index.
	DeleteVoutDuplicateRows = `` /* 188-byte string literal not displayed */

	// IndexVoutTableOnTxHashIdx creates the unique index uix_vout_txhash_ind on
	// (tx_hash, tx_index, tx_tree).
	IndexVoutTableOnTxHashIdx = `CREATE UNIQUE INDEX uix_vout_txhash_ind
		ON vouts(tx_hash, tx_index, tx_tree);`
	DeindexVoutTableOnTxHashIdx = `DROP INDEX uix_vout_txhash_ind;`

	SelectAddressByTxHash = `SELECT script_addresses, value FROM vouts
		WHERE tx_hash = $1 AND tx_index = $2 AND tx_tree = $3;`

	SelectPkScriptByID       = `SELECT version, pkscript FROM vouts WHERE id=$1;`
	SelectPkScriptByOutpoint = `SELECT version, pkscript FROM vouts WHERE tx_hash=$1 and tx_index=$2;`
	SelectVoutIDByOutpoint   = `SELECT id FROM vouts WHERE tx_hash=$1 and tx_index=$2;`
	SelectVoutByID           = `SELECT * FROM vouts WHERE id=$1;`

	RetrieveVoutValue  = `SELECT value FROM vouts WHERE tx_hash=$1 and tx_index=$2;`
	RetrieveVoutValues = `SELECT value, tx_index, tx_tree FROM vouts WHERE tx_hash=$1;`

	CreateVoutType = `` /* 145-byte string literal not displayed */

)

Variables

View Source
var (
	SelectAllRevokes = fmt.Sprintf(`SELECT id, tx_hash, block_height, vin_db_ids[0] `+
		`FROM transactions WHERE tx_type = %d;`, stake.TxTypeSSRtx)

	SelectTicketsOutputCountByAllBlocks = fmt.Sprintf(`SELECT block_height,
		SUM(CASE WHEN num_vout = 3 THEN 1 ELSE 0 END) as solo,
		SUM(CASE WHEN num_vout = 5 THEN 1 ELSE 0 END) as pooled
		FROM transactions WHERE tx_type = %d GROUP BY block_height
		ORDER BY block_height;`, stake.TxTypeSStx)

	SelectTicketsOutputCountByTPWindow = fmt.Sprintf(`SELECT
		floor(block_height/144) as count,
		SUM(CASE WHEN num_vout = 3 THEN 1 ELSE 0 END) as solo,
		SUM(CASE WHEN num_vout = 5 THEN 1 ELSE 0 END) as pooled
		FROM transactions WHERE tx_type = %d
		GROUP BY count ORDER BY count;`, stake.TxTypeSStx)
)

Functions

func MakeAddressRowInsertStatement

func MakeAddressRowInsertStatement(checked, updateOnConflict bool) string

MakeAddressRowInsertStatement returns the appropriate addresses insert statement for the desired conflict checking and handling behavior. For checked=false, no ON CONFLICT checks will be performed, and the value of updateOnConflict is ignored. This should only be used prior to creating the unique indexes as these constraints will cause an errors if an inserted row violates a constraint. For updateOnConflict=true, an upsert statement will be provided that UPDATEs the conflicting row. For updateOnConflict=false, the statement will either insert or do nothing, and return the inserted (new) or conflicting (unmodified) row id.

func MakeAgendaInsertStatement

func MakeAgendaInsertStatement(checked bool) string

func MakeBlockInsertStatement

func MakeBlockInsertStatement(block *dbtypes.Block, checked bool) string

func MakeMissInsertStatement

func MakeMissInsertStatement(checked, updateOnConflict bool) string

MakeTicketInsertStatement returns the appropriate misses insert statement for the desired conflict checking and handling behavior. See the description of MakeTicketInsertStatement for details.

func MakeTicketInsertStatement

func MakeTicketInsertStatement(checked, updateOnConflict bool) string

MakeTicketInsertStatement returns the appropriate tickets insert statement for the desired conflict checking and handling behavior. For checked=false, no ON CONFLICT checks will be performed, and the value of updateOnConflict is ignored. This should only be used prior to creating the unique indexes as these constraints will cause an errors if an inserted row violates a constraint. For updateOnConflict=true, an upsert statement will be provided that UPDATEs the conflicting row. For updateOnConflict=false, the statement will either insert or do nothing, and return the inserted (new) or conflicting (unmodified) row id.

func MakeTxInsertStatement

func MakeTxInsertStatement(checked, updateOnConflict bool) string

MakeTxInsertStatement returns the appropriate transaction insert statement for the desired conflict checking and handling behavior. For checked=false, no ON CONFLICT checks will be performed, and the value of updateOnConflict is ignored. This should only be used prior to creating the unique indexes as these constraints will cause an errors if an inserted row violates a constraint. For updateOnConflict=true, an upsert statement will be provided that UPDATEs the conflicting row. For updateOnConflict=false, the statement will either insert or do nothing, and return the inserted (new) or conflicting (unmodified) row id.

func MakeVinCopyInStatement

func MakeVinCopyInStatement() string

func MakeVinInsertStatement

func MakeVinInsertStatement(checked, updateOnConflict bool) string

MakeVinInsertStatement returns the appropriate vins insert statement for the desired conflict checking and handling behavior. For checked=false, no ON CONFLICT checks will be performed, and the value of updateOnConflict is ignored. This should only be used prior to creating the unique indexes as these constraints will cause an errors if an inserted row violates a constraint. For updateOnConflict=true, an upsert statement will be provided that UPDATEs the conflicting row. For updateOnConflict=false, the statement will either insert or do nothing, and return the inserted (new) or conflicting (unmodified) row id.

func MakeVoteInsertStatement

func MakeVoteInsertStatement(checked, updateOnConflict bool) string

MakeTicketInsertStatement returns the appropriate votes insert statement for the desired conflict checking and handling behavior. See the description of MakeTicketInsertStatement for details.

func MakeVoutCopyInStatement

func MakeVoutCopyInStatement() string

func MakeVoutInsertStatement

func MakeVoutInsertStatement(checked, updateOnConflict bool) string

MakeVoutInsertStatement returns the appropriate vouts insert statement for the desired conflict checking and handling behavior. For checked=false, no ON CONFLICT checks will be performed, and the value of updateOnConflict is ignored. This should only be used prior to creating the unique indexes as these constraints will cause an errors if an inserted row violates a constraint. For updateOnConflict=true, an upsert statement will be provided that UPDATEs the conflicting row. For updateOnConflict=false, the statement will either insert or do nothing, and return the inserted (new) or conflicting (unmodified) row id.

Types

This section is empty.

Jump to

Keyboard shortcuts

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