Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: check for negative balances #1148

Merged
merged 3 commits into from Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 13 additions & 32 deletions packages/core-database-postgres/lib/connection.js
Expand Up @@ -113,16 +113,26 @@ module.exports = class PostgresConnection extends ConnectionInterface {
if (!lastBlock) {
errors.push('Last block is not available')
} else {
const numberOfBlocks = await this.__numberOfBlocks()
const { count: numberOfBlocks } = await this.db.blocks.count()

// Last block height equals the number of stored blocks
if (lastBlock.data.height !== +numberOfBlocks) {
errors.push(`Last block height: ${lastBlock.data.height.toLocaleString()}, number of stored blocks: ${numberOfBlocks}`)
}
}

const blockStats = await this.__blockStats()
const transactionStats = await this.__transactionStats()
const blockStats = await this.db.blocks.statistics()
const transactionStats = await this.db.transactions.statistics()
const { count: negativeBalances } = await this.db.wallets.findNegativeBalances()
const { count: negativeVoteBalances } = await this.db.wallets.findNegativeVoteBalances()

if (+negativeBalances > 1) {
errors.push(`Expected 1 wallet with a negative balance but found ${negativeBalances}`)
}

if (+negativeVoteBalances !== 0) {
errors.push(`Expected 0 wallets with a negative vote balance but found ${negativeVoteBalances}`)
}

// Number of stored transactions equals the sum of block.numberOfTransactions in the database
if (blockStats.numberOfTransactions !== transactionStats.count) {
Expand Down Expand Up @@ -665,33 +675,4 @@ module.exports = class PostgresConnection extends ConnectionInterface {
}
})
}

/**
* This auxiliary method returns the number of blocks of the blockchain and
* is used to verify it
* @return {Number}
*/
async __numberOfBlocks () {
const { count } = await this.db.blocks.count()

return count
}

/**
* This auxiliary method returns some stats about the blocks that are
* used to verify the blockchain
* @return {Promise}
*/
async __blockStats () {
return this.db.blocks.statistics()
}

/**
* This auxiliary method returns some stats about the transactions that are
* used to verify the blockchain
* @return {Promise}
*/
async __transactionStats () {
return this.db.transactions.statistics()
}
}
@@ -1,4 +1,5 @@
SELECT SUM ("number_of_transactions") AS "numberOfTransactions",
SUM ("total_fee") AS "totalFee",
SUM ("total_amount") AS "totalAmount"
SUM ("total_amount") AS "totalAmount",
COUNT (DISTINCT "height") AS "count"
FROM blocks
4 changes: 3 additions & 1 deletion packages/core-database-postgres/lib/queries/index.js
Expand Up @@ -44,6 +44,8 @@ module.exports = {
},
wallets: {
all: loadQueryFile(__dirname, './wallets/all.sql'),
findByAddress: loadQueryFile(__dirname, './wallets/find-by-address.sql')
findByAddress: loadQueryFile(__dirname, './wallets/find-by-address.sql'),
findNegativeBalances: loadQueryFile(__dirname, './wallets/find-negative-balances.sql'),
findNegativeVoteBalances: loadQueryFile(__dirname, './wallets/find-negative-vote-balances.sql')
}
}
@@ -0,0 +1,3 @@
SELECT COUNT (DISTINCT "address") AS "count"
FROM wallets
WHERE balance < 0;
@@ -0,0 +1,3 @@
SELECT COUNT (DISTINCT "address") AS "count"
FROM wallets
WHERE vote_balance < 0;
16 changes: 16 additions & 0 deletions packages/core-database-postgres/lib/repositories/wallets.js
Expand Up @@ -20,6 +20,22 @@ module.exports = class WalletsRepository extends Repository {
return this.db.oneOrNone(sql.findByAddress, { address })
}

/**
* Find all wallets that have a negative balance.
* @return {Promise}
*/
async findNegativeBalances () {
return this.db.oneOrNone(sql.findNegativeBalances)
}

/**
* Find all wallets that have a negative vote balance.
* @return {Promise}
*/
async findNegativeVoteBalances () {
return this.db.oneOrNone(sql.findNegativeVoteBalances)
}

/**
* Create or update a record matching the attributes, and fill it with values.
* @param {Object} wallet
Expand Down