-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathBlockSummary.js
More file actions
184 lines (169 loc) · 5.58 KB
/
BlockSummary.js
File metadata and controls
184 lines (169 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { BcThing } from './BcThing'
import Tx from './Tx'
import BlockTrace from './BlockTrace'
import { BlockAddresses } from './BlockAddresses'
import { isBlockHash } from '../../lib/utils'
import { isAddress } from '@rsksmart/rsk-utils'
import config from '../../lib/config'
export class BlockSummary extends BcThing {
constructor (hashOrNumber, { nod3, log, initConfig }) {
super({ nod3, initConfig, log, name: 'Summary' })
this.hashOrNumber = hashOrNumber
this.Addresses = undefined
this.data = {
block: null,
transactions: [],
internalTransactions: [],
addresses: [],
tokenAddresses: [],
events: []
}
}
async fetch (forceFetch, skipDb) {
try {
let { fetched } = this
if (fetched && !forceFetch) {
return this.getData()
}
let blockData = await this.getBlockData()
const { hash } = blockData
if (!skipDb) {
let dbData = await this.getBlockSummaryFromDb(hash)
if (dbData && dbData.data) {
this.setData(dbData.data)
this.fetched = true
return this.getData()
}
}
let Addresses = await this.getAddresses()
let Txs = await this.createTxs(blockData, Addresses)
let txsData = await this.fetchItems(Txs)
let transactions = txsData.map(d => d.tx)
this.setData({ transactions })
let events = [].concat(...txsData.map(d => d.events))
let internalTransactions = [].concat(...txsData.map(d => d.internalTransactions))
let tokenAddresses = [].concat(...txsData.map(d => d.tokenAddresses))
let suicides = [].concat(...txsData.map(d => d.suicides))
let addresses = await Addresses.fetch()
this.setData({ internalTransactions, events, addresses, tokenAddresses, suicides })
this.fetched = true
return this.getData()
} catch (err) {
return Promise.reject(err)
}
}
async createTxs (blockData, addresses) {
try {
let { nod3, initConfig, log } = this
let { timestamp, transactions, hash } = blockData
let blockTrace = new BlockTrace(hash, { nod3, log, initConfig })
blockTrace = await blockTrace.fetchFromNode()
let txs = []
let receipts = []
const txsChunks = []
for (let i = 0; i <= transactions.length; i += config.blocks.batchRequestSize) {
txsChunks.push(transactions.slice(i, i + config.blocks.batchRequestSize))
}
for (const txChunk of txsChunks) {
txs.push(...(await nod3.batchRequest(txChunk.map(hash => ['eth.getTransactionByHash', hash]))))
receipts.push(...(await nod3.batchRequest(txChunk.map(hash => ['eth.getTransactionReceipt', hash]))))
}
return transactions.map(hash => {
let txData = txs.find(tx => tx.hash === hash)
let receipt = receipts.find(re => re.transactionHash === hash)
return new Tx(hash, timestamp, { txData, receipt, blockTrace, blockData, addresses, nod3, initConfig })
})
} catch (err) {
return Promise.reject(err)
}
}
async getBlockData () {
try {
let { block } = this.getData()
if (block) return block
let { nod3, hashOrNumber } = this
block = await getBlock(hashOrNumber, false, nod3)
if (block) {
this.setData({ block })
return block
}
} catch (err) {
return Promise.reject(err)
}
}
async fetchItems (items) {
const fetched = []
for (const item of items) {
fetched.push(await item.fetch())
}
return fetched
}
async getAddresses () {
try {
let { Addresses, nod3, initConfig } = this
if (!Addresses) {
let blockData = await this.getBlockData()
Addresses = new BlockAddresses(blockData, { nod3, initConfig })
let { miner } = blockData
let options = { block: blockData }
Addresses.add(miner, options)
let summariesAddresses = await this.getSummariesAddresses()
for (let address of summariesAddresses) {
if (isAddress(address)) Addresses.add(address, options)
}
this.Addresses = Addresses
}
return Addresses
} catch (err) {
return Promise.reject(err)
}
}
async getSummariesAddresses () {
try {
const { number } = await this.getBlockData()
const summaries = await this.getBlockSummariesByNumber(number)
const addresses = [...new Set([].concat(...summaries.map(({ addresses }) => addresses)))]
return addresses
} catch (err) {
return Promise.reject(err)
}
}
async getBlockSummaryFromDb (hash) {
try {
if (!isBlockHash(hash)) throw new Error(`Invalid blockHash ${hash}`)
let data = await this.repository.findOne({ hash }, {})
return data
} catch (err) {
return Promise.reject(err)
}
}
async getBlockSummariesByNumber (number) {
try {
const blockNumber = parseInt(number)
if (isNaN(blockNumber)) throw new Error(`Invalid blockNumber ${number}`)
let res = await this.repository.find({ blockNumber }, {})
return res
} catch (err) {
return Promise.reject(err)
}
}
}
export async function getBlock (hashOrNumber, txArr = false, nod3) {
try {
let blockData = await nod3.eth.getBlock(hashOrNumber, txArr)
if (blockData) blockData._received = Date.now()
return blockData
} catch (err) {
return Promise.reject(err)
}
}
export async function deleteBlockSummaryFromDb (hash) {
try {
if (!isBlockHash(hash)) throw new Error(`Invalid blockHash ${hash}`)
let res = this.repository.deleteOne({ hash })
return res
} catch (err) {
return Promise.reject(err)
}
}
export default BlockSummary