-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtest.py
92 lines (77 loc) · 2.6 KB
/
speedtest.py
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
#!/usr/bin/env python
# Copyright 2019 TokenAnalyst
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# WS client Python 3.x to benchmark block propagation latency of TokenAnalyst to Blockchain.com
import json
import asyncio
import logging
import websockets
import os
import sys
import queue
import asyncio
import time
logging.basicConfig(level=logging.INFO)
API_KEY = os.environ['API_KEY']
# Blockchain.com Protocol
def bcom_block_converter(payload):
data = json.loads(payload)
if(data['op'] == 'block'):
ts = data['x']['time']
blockNumber = data['x']['height']
return(block(ts, blockNumber))
else:
None
blockchain = {
"id": "Blockchain.com",
"url": "wss://ws.blockchain.info/inv",
"subscribe_json": json.dumps({"op":"blocks_sub"}),
"block_converter": bcom_block_converter
}
# TokenAnalyst Protocol
def ta_block_converter(payload):
data = json.loads(payload)
if(data['event'] == 'data'):
ts = data['data']['timestamp']
blockNumber = data['data']['blockNumber']
return(block(ts, blockNumber))
else:
None
ta = {
"id": "TokenAnalyst",
"url": "wss://ws.tokenanalyst.io",
"subscribe_json": json.dumps({"event":"subscribe","channel":"btc_confirmed_exchange_flows","id":"0","key":API_KEY}),
"block_converter": ta_block_converter
}
def block(timestamp, blocknumber):
return({'seen':int(time.time()*1000), 'blockNumber':blocknumber, 'timestamp': timestamp*1000})
async def subscribe(protocol):
uri = protocol['url']
highest_block = 0
socket = await websockets.connect(uri)
await socket.send(protocol['subscribe_json'])
async for msg in socket:
block = protocol['block_converter'](msg)
if(block != None and block['blockNumber'] > highest_block): #dedup
highest_block = block['blockNumber']
print({
"id":protocol['id'],
"blockNumber": block['blockNumber'],
"timestamp": block['timestamp'],
"seen":block['seen']})
async def run_both():
F1 = subscribe(ta)
F2 = subscribe(blockchain)
await asyncio.gather(F1, F2)
asyncio.run(run_both())