-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy patheth_streaming_test.js
More file actions
221 lines (194 loc) · 7.37 KB
/
eth_streaming_test.js
File metadata and controls
221 lines (194 loc) · 7.37 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const WebSocket = require('ws')
const conf = require('./config')
const helpers = require('./helpers')
const { assert } = require('chai')
const { Web3 } = require('web3')
const web3 = conf.web3
it('streaming of blocks, transactions, logs using filters', async () => {
// this is a failsafe if socket is kept open since test node process won't finish otherwise
setTimeout(() => process.exit(1), 1000 * 25)
let deployed = await helpers.deployContract('storage')
let contractAddress = deployed.receipt.contractAddress
let repeatA = 10
const testValues = [
{ A: 1, B: 2 },
{ A: -1, B: -2 },
{ A: repeatA, B: 200 },
{ A: repeatA, B: 300 },
{ A: repeatA, B: 400 },
]
let ws = new Web3('ws://127.0.0.1:8545')
// wait for subscription for a bit
await new Promise((res, rej) => setTimeout(() => res(), 1000))
// subscribe to new blocks being produced by transaction submissions below
let blocksHeaders = []
let subBlocks = await ws.eth.subscribe('newBlockHeaders')
subBlocks.on('error', async (err) => {
assert.fail(err.message)
})
subBlocks.on('data', async (block) => {
blocksHeaders.push(block) // add received tx hash
if (blocksHeaders.length === testValues.length) {
subBlocks.unsubscribe()
}
})
// subscribe to all new transaction events being produced by transaction submissions below
let txHashes = []
let subTx = await ws.eth.subscribe('pendingTransactions')
subTx.on('error', async (err) => {
assert.fail(err.message)
})
subTx.on('data', async (tx) => {
txHashes.push(tx) // add received tx hash
if (txHashes.length === testValues.length) {
subTx.unsubscribe()
}
})
// subscribe to events being emitted by a deployed contract and transaction interactions below
let logs = []
let subLog = await ws.eth.subscribe('logs', {
address: contractAddress,
})
subLog.on('error', async err => {
assert.fail(err.message)
})
subLog.on('data', async (log) => {
logs.push(log)
if (logs.length === testValues.length) {
subLog.unsubscribe()
}
})
let socket = new WebSocket('ws://127.0.0.1:8545')
// give some time for the connection to open
await new Promise((res) => setTimeout(() => res(), 1500))
let payload = `
{
"jsonrpc": "2.0",
"id": 2,
"method": "eth_subscribe",
"params": [
"transactionReceipts",
{
"transactionHashes": []
}
]
}
`
socket.send(payload)
// subscribe to all new receipts being produced by transaction submissions below
let receipts = []
socket.onmessage = (event) => {
let response = JSON.parse(event.data)
if (response.method == 'eth_subscription') {
receipts = receipts.concat(response.params.result)
}
}
let sentHashes = []
// produce events by submitting transactions
for (const { A, B } of testValues) {
let res = await helpers.signAndSend({
from: conf.eoa.address,
to: contractAddress,
data: deployed.contract.methods.sum(A, B).encodeABI(),
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
assert.equal(res.receipt.status, conf.successStatus)
sentHashes.push(res.receipt.transactionHash) // add sent hash
}
// wait for subscription for a bit
await new Promise((res, rej) => setTimeout(() => res(), 1000))
// check that transaction hashes we received when submitting transactions above
// match array of transaction hashes received from subscriptions
assert.deepEqual(txHashes, sentHashes)
assert.lengthOf(blocksHeaders, testValues.length)
for (let blockHeader of blocksHeaders) {
let block = await web3.eth.getBlock(blockHeader.number)
assert.equal(blockHeader.number, block.number)
assert.equal(blockHeader.hash, block.hash)
assert.equal(blockHeader.parentHash, block.parentHash)
assert.equal(blockHeader.nonce, block.nonce)
assert.equal(blockHeader.sha3Uncles, block.sha3Uncles)
assert.equal(blockHeader.logsBloom, block.logsBloom)
assert.equal(blockHeader.transactionsRoot, block.transactionsRoot)
assert.equal(blockHeader.stateRoot, block.stateRoot)
assert.equal(blockHeader.receiptsRoot, block.receiptsRoot)
assert.equal(blockHeader.miner, block.miner)
assert.equal(blockHeader.extraData, block.extraData)
assert.equal(blockHeader.gasLimit, block.gasLimit)
assert.equal(blockHeader.gasUsed, block.gasUsed)
assert.equal(blockHeader.timestamp, block.timestamp)
assert.equal(blockHeader.difficulty, block.difficulty)
}
assert.lengthOf(logs, testValues.length)
for (let log of logs) {
let matchingLogs = await web3.eth.getPastLogs({
address: log.address,
blockHash: log.blockHash
})
assert.lengthOf(matchingLogs, 1)
assert.deepEqual(log, matchingLogs[0])
}
assert.equal(10, receipts.length)
for (let txHash of sentHashes) {
let txReceipt = await helpers.callRPCMethod(
'eth_getTransactionReceipt',
[txHash]
)
for (let rcp of receipts) {
if (rcp.transactionHash == txHash) {
assert.deepEqual(rcp, txReceipt.body['result'])
}
}
}
let signedTx = await conf.eoa.signTransaction({
from: conf.eoa.address,
to: contractAddress,
data: deployed.contract.methods.sum(7, 7).encodeABI(),
gas: 1_000_000,
gasPrice: conf.minGasPrice
})
receipts = []
let subID = null
socket.onmessage = (event) => {
let response = JSON.parse(event.data)
if (response.id == 12) {
subID = response.result
}
if (response.method == 'eth_subscription' && response.params.subscription == subID) {
receipts = receipts.concat(response.params.result)
}
}
// Check that the subscription will notify the caller only for the given
// set of tx hashes.
payload = `
{
"jsonrpc": "2.0",
"id": 12,
"method": "eth_subscribe",
"params": [
"transactionReceipts",
{
"transactionHashes": [
"0x7b45084668258f29cfc525494d00ea5171766d1d43436e41cea930380d96bf67",
"0xed970aa258b677d5e772125dd4342f38e5ccf4dec685d38fc5f04f18eff1a939",
"${signedTx.transactionHash}"
]
}
]
}
`
socket.send(payload)
// send transaction and make sure interaction was success
let txReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction)
assert.equal(txReceipt.status, conf.successStatus)
assert.equal(txReceipt.transactionHash, signedTx.transactionHash)
await new Promise((res, rej) => setTimeout(() => res(), 1500))
socket.close(1000, 'finished testing')
assert.equal(1, receipts.length)
let expectedReceipt = await helpers.callRPCMethod(
'eth_getTransactionReceipt',
[signedTx.transactionHash]
)
assert.deepEqual(receipts[0], expectedReceipt.body['result'])
})