-
Notifications
You must be signed in to change notification settings - Fork 2
/
apply_batch.py
102 lines (91 loc) · 3.43 KB
/
apply_batch.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
93
94
95
96
97
98
99
100
101
102
import io
import json
import os
import sys
import time
import jwt
from tqdm import tqdm
from utils import send_json_rpc
class PayloadApplier:
def __init__(
self,
engine_url,
jwt_secret_path,
payload_dir,
start,
end,
batch_size,
target_safe_number,
target_safe_hash,
target_finalized_number,
target_finalized_hash,
canyon_time,
ecotone_time,
logging=False,
):
self.engine_url = engine_url
with open(jwt_secret_path, 'r') as f:
self.jwt_secret = f.readline().strip()
self.payload_dir = payload_dir
self.start = start
self.end = end
self.batch_size = batch_size
self.target_safe_number = target_safe_number
self.target_safe_hash = target_safe_hash
self.target_finalized_number = target_finalized_number
self.target_finalized_hash = target_finalized_hash
self.canyon_time = canyon_time
self.ecotone_time = ecotone_time
self.logging = logging
def _get_jwt_token(self):
auth_payload = {
'iat': int(time.time())
}
self.jwt_token = jwt.encode(
auth_payload,
bytes.fromhex(self.jwt_secret[2:] if self.jwt_secret.startswith('0x') else self.jwt_secret)
)
def apply(self, block_number):
with open(os.path.join(self.payload_dir, f'{hex(block_number)}.json'), 'r') as f:
payload = json.load(f)
timestamp = int(payload['timestamp'], 16)
version = 3 if timestamp >= self.ecotone_time else 2 if timestamp >= self.canyon_time else 1
send_json_rpc(self.engine_url, f'engine_newPayloadV{version}', params=payload, token=self.jwt_token)
if block_number < self.end and block_number % self.batch_size < self.batch_size - 1:
return
while True:
res = send_json_rpc(
self.engine_url,
f'engine_forkchoiceUpdatedV{version}',
params=[
{
'headBlockHash': payload['blockHash'],
'safeBlockHash': payload['blockHash'] if block_number < self.target_safe_number else self.target_safe_hash,
'finalizedBlockHash': payload['blockHash'] if block_number < self.target_finalized_number else self.target_finalized_hash,
},
],
token=self.jwt_token,
)
if res['payloadStatus']['status'] == 'SYNCING':
time.sleep(0.1)
continue
break
def job(self, block_number):
for _ in range(3):
try:
self.apply(block_number)
return
except Exception as e:
self._get_jwt_token()
exit()
def run(self):
self._get_jwt_token()
pbar = tqdm(range(self.start, self.end + 1), total=self.end - self.start + 1, file=io.StringIO() if self.logging else sys.stdout)
logged_at = 0
for block_number in pbar:
self.job(block_number)
now = time.time()
if self.logging and now > logged_at + 10:
data = pbar.format_dict
print(f'applying payload | {data["n"]}/{data["total"]} | elapsed: {time.strftime("%H:%M:%S", time.gmtime(data["elapsed"]))}')
logged_at = now