Skip to content

Commit 526a88f

Browse files
authored
Merge pull request #511 from khaihkd/master
optimize get list tx, list block
2 parents cb06b4a + b89d7e2 commit 526a88f

File tree

7 files changed

+64
-49
lines changed

7 files changed

+64
-49
lines changed

client/components/TableReward.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
<template
3434
slot="lockBalance"
3535
slot-scope="props">
36-
{{ formatUnit(parseFloat(props.item.lockBalance)) }}
36+
{{ formatNumber(props.item.lockBalance) }}
3737
</template>
3838
<template
3939
slot="reward"
4040
slot-scope="props">
41-
{{ formatUnit(parseFloat(props.item.reward)) }}
41+
{{ formatNumber(props.item.reward) }}
4242
</template>
4343
<template
4444
slot="validator"

client/components/TableTx.vue

-19
Original file line numberDiff line numberDiff line change
@@ -270,26 +270,7 @@ export default {
270270
self.page.txsCount = self.realTotal
271271
}
272272
273-
let listHash = []
274-
data.items.forEach(function (item) {
275-
if (typeof item.status === 'undefined') {
276-
listHash.push(item.hash)
277-
}
278-
})
279-
let listStatus = null
280-
if (listHash.length > 0) {
281-
listStatus = await self.$axios.get(`/api/txs/list/status?hash=${listHash.join(',')}`)
282-
}
283-
284273
data.items.forEach(async (item, index, array) => {
285-
if (listStatus !== null) {
286-
Object.keys(listStatus.data).forEach(function (key) {
287-
if (key === item.hash) {
288-
item.status = listStatus.data[key]
289-
}
290-
})
291-
}
292-
293274
if (index === array.length - 1) {
294275
self.items = array
295276

client/pages/blocks/index.vue

-16
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,6 @@ export default {
142142
self.currentPage = data.currentPage
143143
self.pages = data.pages
144144
145-
let listNumber = []
146-
data.items.forEach(function (item) {
147-
listNumber.push(item.number)
148-
})
149-
let listFinality = await self.$axios.get(`/api/blocks/list/finality?numbers=${listNumber.join(',')}`)
150-
151-
self.items.forEach(async (item) => {
152-
if (listFinality !== null) {
153-
Object.keys(listFinality.data).forEach(function (key) {
154-
if (key === item.number) {
155-
item.finality = listFinality.data[key]
156-
}
157-
})
158-
}
159-
})
160-
161145
// Hide loading.
162146
this.loading = false
163147

server/src/api/BlockController.js

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ BlockController.get('/blocks', async (req, res, next) => {
2525
}
2626
let items = []
2727
let blocks = await db.Block.find({ number: { $in: listBlkNum } })
28+
let finalityBlock = []
2829

2930
if (blocks.length === perPage) {
3031
items = blocks
@@ -33,6 +34,9 @@ BlockController.get('/blocks', async (req, res, next) => {
3334
for (let i = 0; i < blocks.length; i++) {
3435
items.push(blocks[i])
3536
existBlock.push(blocks[i].number)
37+
if (blocks[i].finality < 50) {
38+
finalityBlock.push(blocks[i].number)
39+
}
3640
}
3741
let notExistBlock = []
3842
if (existBlock.length === 0) {
@@ -50,6 +54,17 @@ BlockController.get('/blocks', async (req, res, next) => {
5054
})
5155
await Promise.all(map)
5256
}
57+
let finality = []
58+
let map2 = finalityBlock.map(async function (number) {
59+
let b = await web3.eth.getBlock(number)
60+
if (b) {
61+
finality.push({
62+
block: number,
63+
finality: b.finality
64+
})
65+
}
66+
})
67+
5368
let result = []
5469
for (let i = 0; i < listBlkNum.length; i++) {
5570
for (let j = 0; j < items.length; j++) {
@@ -59,12 +74,23 @@ BlockController.get('/blocks', async (req, res, next) => {
5974
}
6075
}
6176

77+
for (let i = 0; i < finality.length; i++) {
78+
for (let j = 0; j < result.length; j++) {
79+
if (finality[i].block === result[j].number) {
80+
result[j].finality = finality[i].finality
81+
break
82+
}
83+
}
84+
}
85+
6286
let limitedRecords = config.get('LIMITED_RECORDS')
6387
let newTotal = maxBlockNumber > limitedRecords ? limitedRecords : maxBlockNumber
6488
let pages = Math.ceil(maxBlockNumber / perPage)
6589
if (pages > 500) {
6690
pages = 500
6791
}
92+
await Promise.all(map2)
93+
6894
let data = {
6995
realTotal: maxBlockNumber,
7096
total: newTotal,

server/src/api/TxController.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ TxController.get('/txs', async (req, res) => {
109109
pages: pages,
110110
items: items
111111
}
112+
const web3 = await Web3Util.getWeb3()
112113
// If exist blockNumber & not found txs on db (or less than) will get txs on chain
113114
if (blockNumber) {
114115
let block = await db.Block.findOne({ number: blockNumber })
115116
let blockTx = await db.Tx.countDocuments({ blockNumber: blockNumber })
116117

117118
const offset = page > 1 ? (page - 1) * perPage : 0
118119
if (block.e_tx > blockTx) {
119-
const web3 = await Web3Util.getWeb3()
120-
121120
const _block = await web3.eth.getBlock(blockNumber)
122121

123122
const trans = _block.transactions
@@ -172,6 +171,31 @@ TxController.get('/txs', async (req, res) => {
172171
await Promise.all(map1)
173172
data.items = newItem
174173
}
174+
let status = []
175+
for (let i = 0; i < data.items.length; i++) {
176+
if (!data.items[i].hasOwnProperty('status')) {
177+
status.push({ hash: data.items[i].hash })
178+
}
179+
}
180+
if (status.length > 0) {
181+
let map = status.map(async function (s) {
182+
let receipt = await web3.eth.getTransactionReceipt(s.hash)
183+
if (receipt) {
184+
s.status = receipt.status
185+
} else {
186+
s.status = null
187+
}
188+
})
189+
await Promise.all(map)
190+
console.log(status)
191+
for (let i = 0; i < status.length; i++) {
192+
for (let j = 0; j < data.items.length; j++) {
193+
if (status[i].hash === data.items[j].hash) {
194+
data.items[j].status = status[i].status
195+
}
196+
}
197+
}
198+
}
175199

176200
return res.json(data)
177201
} catch (e) {

server/src/commands/updateSpecialAccount.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const updateSpecialAccount = async () => {
2424
let map1 = candidates.data.map(async (candidate) => {
2525
let hash = candidate.candidate.toLowerCase()
2626
console.info('process candidate', hash)
27-
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }] })
27+
let txCount = await db.Tx.countDocuments({ $or: [{ from: hash }, { to: hash }], isPending: false })
2828
let minedBlock = await db.Block.countDocuments({ signer: hash })
2929
let rewardCount = await db.Reward.countDocuments({ address: hash })
3030
let logCount = await db.Log.countDocuments({ address: hash })
@@ -36,7 +36,7 @@ const updateSpecialAccount = async () => {
3636
}, { upsert: true })
3737

3838
let owner = candidate.owner.toLowerCase()
39-
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }] })
39+
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }], isPending: false })
4040
let minedBlockOwner = await db.Block.countDocuments({ signer: owner })
4141
let rewardCountOwner = await db.Reward.countDocuments({ address: owner })
4242
let logCountOwner = await db.Log.countDocuments({ address: owner })
@@ -54,9 +54,9 @@ const updateSpecialAccount = async () => {
5454
let map2 = accounts.map(async (acc) => {
5555
let hash = acc.hash.toLowerCase()
5656
console.info('process account', hash)
57-
let txCount = await db.Tx.countDocuments({ from: hash })
58-
txCount += await db.Tx.countDocuments({ to: hash })
59-
txCount += await db.Tx.countDocuments({ contractAddress: hash })
57+
let txCount = await db.Tx.countDocuments({ from: hash, isPending: false })
58+
txCount += await db.Tx.countDocuments({ to: hash, isPending: false })
59+
txCount += await db.Tx.countDocuments({ contractAddress: hash, isPending: false })
6060
let logCount = await db.Log.countDocuments({ address: hash })
6161
await db.SpecialAccount.updateOne({ hash: hash }, {
6262
transactionCount: txCount,

server/src/queues/updateSpecialAccount.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ consumer.task = async function (job, done) {
3030
let hash = candidate.candidate.toLowerCase()
3131

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

4444
let owner = candidate.owner.toLowerCase()
45-
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }] })
45+
let txCountOwner = await db.Tx.countDocuments({ $or: [{ from: owner }, { to: owner }], isPending: false })
4646
let minedBlockOwner = await db.Block.countDocuments({ signer: owner })
4747
let rewardCountOwner = await db.Reward.countDocuments({ address: owner })
4848
let logCountOwner = await db.Log.countDocuments({ address: owner })
@@ -59,9 +59,9 @@ consumer.task = async function (job, done) {
5959
console.info('there are %s contract accounts', accounts.length)
6060
let map2 = accounts.map(async (acc) => {
6161
let hash = acc.hash.toLowerCase()
62-
let txCount = await db.Tx.countDocuments({ from: hash })
63-
txCount += await db.Tx.countDocuments({ to: hash })
64-
txCount += await db.Tx.countDocuments({ contractAddress: hash })
62+
let txCount = await db.Tx.countDocuments({ from: hash, isPending: false })
63+
txCount += await db.Tx.countDocuments({ to: hash, isPending: false })
64+
txCount += await db.Tx.countDocuments({ contractAddress: hash, isPending: false })
6565
let logCount = await db.Log.countDocuments({ address: hash })
6666
await db.SpecialAccount.updateOne({ hash: hash }, {
6767
transactionCount: txCount,

0 commit comments

Comments
 (0)