Skip to content

Commit

Permalink
Merge pull request #511 from khaihkd/master
Browse files Browse the repository at this point in the history
 optimize get list tx, list block
  • Loading branch information
khaihkd authored Dec 5, 2018
2 parents cb06b4a + b89d7e2 commit 526a88f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
4 changes: 2 additions & 2 deletions client/components/TableReward.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
<template
slot="lockBalance"
slot-scope="props">
{{ formatUnit(parseFloat(props.item.lockBalance)) }}
{{ formatNumber(props.item.lockBalance) }}
</template>
<template
slot="reward"
slot-scope="props">
{{ formatUnit(parseFloat(props.item.reward)) }}
{{ formatNumber(props.item.reward) }}
</template>
<template
slot="validator"
Expand Down
19 changes: 0 additions & 19 deletions client/components/TableTx.vue
Original file line number Diff line number Diff line change
Expand Up @@ -270,26 +270,7 @@ export default {
self.page.txsCount = self.realTotal
}
let listHash = []
data.items.forEach(function (item) {
if (typeof item.status === 'undefined') {
listHash.push(item.hash)
}
})
let listStatus = null
if (listHash.length > 0) {
listStatus = await self.$axios.get(`/api/txs/list/status?hash=${listHash.join(',')}`)
}
data.items.forEach(async (item, index, array) => {
if (listStatus !== null) {
Object.keys(listStatus.data).forEach(function (key) {
if (key === item.hash) {
item.status = listStatus.data[key]
}
})
}
if (index === array.length - 1) {
self.items = array
Expand Down
16 changes: 0 additions & 16 deletions client/pages/blocks/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,6 @@ export default {
self.currentPage = data.currentPage
self.pages = data.pages
let listNumber = []
data.items.forEach(function (item) {
listNumber.push(item.number)
})
let listFinality = await self.$axios.get(`/api/blocks/list/finality?numbers=${listNumber.join(',')}`)
self.items.forEach(async (item) => {
if (listFinality !== null) {
Object.keys(listFinality.data).forEach(function (key) {
if (key === item.number) {
item.finality = listFinality.data[key]
}
})
}
})
// Hide loading.
this.loading = false
Expand Down
26 changes: 26 additions & 0 deletions server/src/api/BlockController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BlockController.get('/blocks', async (req, res, next) => {
}
let items = []
let blocks = await db.Block.find({ number: { $in: listBlkNum } })
let finalityBlock = []

if (blocks.length === perPage) {
items = blocks
Expand All @@ -33,6 +34,9 @@ BlockController.get('/blocks', async (req, res, next) => {
for (let i = 0; i < blocks.length; i++) {
items.push(blocks[i])
existBlock.push(blocks[i].number)
if (blocks[i].finality < 50) {
finalityBlock.push(blocks[i].number)
}
}
let notExistBlock = []
if (existBlock.length === 0) {
Expand All @@ -50,6 +54,17 @@ BlockController.get('/blocks', async (req, res, next) => {
})
await Promise.all(map)
}
let finality = []
let map2 = finalityBlock.map(async function (number) {
let b = await web3.eth.getBlock(number)
if (b) {
finality.push({
block: number,
finality: b.finality
})
}
})

let result = []
for (let i = 0; i < listBlkNum.length; i++) {
for (let j = 0; j < items.length; j++) {
Expand All @@ -59,12 +74,23 @@ BlockController.get('/blocks', async (req, res, next) => {
}
}

for (let i = 0; i < finality.length; i++) {
for (let j = 0; j < result.length; j++) {
if (finality[i].block === result[j].number) {
result[j].finality = finality[i].finality
break
}
}
}

let limitedRecords = config.get('LIMITED_RECORDS')
let newTotal = maxBlockNumber > limitedRecords ? limitedRecords : maxBlockNumber
let pages = Math.ceil(maxBlockNumber / perPage)
if (pages > 500) {
pages = 500
}
await Promise.all(map2)

let data = {
realTotal: maxBlockNumber,
total: newTotal,
Expand Down
28 changes: 26 additions & 2 deletions server/src/api/TxController.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,14 @@ TxController.get('/txs', async (req, res) => {
pages: pages,
items: items
}
const web3 = await Web3Util.getWeb3()
// If exist blockNumber & not found txs on db (or less than) will get txs on chain
if (blockNumber) {
let block = await db.Block.findOne({ number: blockNumber })
let blockTx = await db.Tx.countDocuments({ blockNumber: blockNumber })

const offset = page > 1 ? (page - 1) * perPage : 0
if (block.e_tx > blockTx) {
const web3 = await Web3Util.getWeb3()

const _block = await web3.eth.getBlock(blockNumber)

const trans = _block.transactions
Expand Down Expand Up @@ -172,6 +171,31 @@ TxController.get('/txs', async (req, res) => {
await Promise.all(map1)
data.items = newItem
}
let status = []
for (let i = 0; i < data.items.length; i++) {
if (!data.items[i].hasOwnProperty('status')) {
status.push({ hash: data.items[i].hash })
}
}
if (status.length > 0) {
let map = status.map(async function (s) {
let receipt = await web3.eth.getTransactionReceipt(s.hash)
if (receipt) {
s.status = receipt.status
} else {
s.status = null
}
})
await Promise.all(map)
console.log(status)
for (let i = 0; i < status.length; i++) {
for (let j = 0; j < data.items.length; j++) {
if (status[i].hash === data.items[j].hash) {
data.items[j].status = status[i].status
}
}
}
}

return res.json(data)
} catch (e) {
Expand Down
10 changes: 5 additions & 5 deletions server/src/commands/updateSpecialAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const updateSpecialAccount = async () => {
let map1 = candidates.data.map(async (candidate) => {
let hash = candidate.candidate.toLowerCase()
console.info('process candidate', hash)
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }] })
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }], isPending: false })
let minedBlock = await db.Block.countDocuments({ signer: hash })
let rewardCount = await db.Reward.countDocuments({ address: hash })
let logCount = await db.Log.countDocuments({ address: hash })
Expand All @@ -36,7 +36,7 @@ const updateSpecialAccount = async () => {
}, { upsert: true })

let owner = candidate.owner.toLowerCase()
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }] })
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }], isPending: false })
let minedBlockOwner = await db.Block.countDocuments({ signer: owner })
let rewardCountOwner = await db.Reward.countDocuments({ address: owner })
let logCountOwner = await db.Log.countDocuments({ address: owner })
Expand All @@ -54,9 +54,9 @@ const updateSpecialAccount = async () => {
let map2 = accounts.map(async (acc) => {
let hash = acc.hash.toLowerCase()
console.info('process account', hash)
let txCount = await db.Tx.countDocuments({ from: hash })
txCount += await db.Tx.countDocuments({ to: hash })
txCount += await db.Tx.countDocuments({ contractAddress: hash })
let txCount = await db.Tx.countDocuments({ from: hash, isPending: false })
txCount += await db.Tx.countDocuments({ to: hash, isPending: false })
txCount += await db.Tx.countDocuments({ contractAddress: hash, isPending: false })
let logCount = await db.Log.countDocuments({ address: hash })
await db.SpecialAccount.updateOne({ hash: hash }, {
transactionCount: txCount,
Expand Down
10 changes: 5 additions & 5 deletions server/src/queues/updateSpecialAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ consumer.task = async function (job, done) {
let hash = candidate.candidate.toLowerCase()

console.info('process candidate', hash)
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }] })
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }], isPending: false })
let minedBlock = await db.Block.countDocuments({ signer: hash })
let rewardCount = await db.Reward.countDocuments({ address: hash })
let logCount = await db.Log.countDocuments({ address: hash })
Expand All @@ -42,7 +42,7 @@ consumer.task = async function (job, done) {
}, { upsert: true })

let owner = candidate.owner.toLowerCase()
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }] })
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }], isPending: false })
let minedBlockOwner = await db.Block.countDocuments({ signer: owner })
let rewardCountOwner = await db.Reward.countDocuments({ address: owner })
let logCountOwner = await db.Log.countDocuments({ address: owner })
Expand All @@ -59,9 +59,9 @@ consumer.task = async function (job, done) {
console.info('there are %s contract accounts', accounts.length)
let map2 = accounts.map(async (acc) => {
let hash = acc.hash.toLowerCase()
let txCount = await db.Tx.countDocuments({ from: hash })
txCount += await db.Tx.countDocuments({ to: hash })
txCount += await db.Tx.countDocuments({ contractAddress: hash })
let txCount = await db.Tx.countDocuments({ from: hash, isPending: false })
txCount += await db.Tx.countDocuments({ to: hash, isPending: false })
txCount += await db.Tx.countDocuments({ contractAddress: hash, isPending: false })
let logCount = await db.Log.countDocuments({ address: hash })
await db.SpecialAccount.updateOne({ hash: hash }, {
transactionCount: txCount,
Expand Down

0 comments on commit 526a88f

Please sign in to comment.