forked from hclivess/nado
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollback.py
More file actions
61 lines (47 loc) · 2.3 KB
/
rollback.py
File metadata and controls
61 lines (47 loc) · 2.3 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
import os
import time
import msgpack
from account_ops import reflect_transaction, change_balance, increase_produced_count
from block_ops import load_block_from_hash, set_latest_block_info
from data_ops import get_home
from transaction_ops import unindex_transaction
def rollback_one_block(logger, lock, block_message) -> dict:
#print("rollback triggered for", block_message)
with lock:
try:
previous_block = load_block_from_hash(
block_hash=block_message["parent_hash"], logger=logger
)
for transaction in block_message["block_transactions"]:
unindex_transaction(transaction, logger)
reflect_transaction(transaction, revert=True)
change_balance(
address=block_message["block_creator"],
amount=-block_message["block_reward"],
)
increase_produced_count(address=block_message["block_creator"],
amount=block_message["block_reward"],
revert=True)
set_latest_block_info(block_message=previous_block,
logger=logger)
with open(f"{get_home()}/blocks/block_numbers/index.dat", "wb") as outfile:
msgpack.pack({"last_number": previous_block["block_number"]}, outfile)
block_number = f"{get_home()}/blocks/block_numbers/{block_message['block_number']}.dat"
while os.path.exists(block_number):
try:
os.remove(block_number)
except Exception as e:
logger.error(f"Failed to remove {block_number}: {e}, retrying")
time.sleep(1)
block_data = f"{get_home()}/blocks/{block_message['block_hash']}.block"
while os.path.exists(block_data):
try:
os.remove(block_data)
except Exception as e:
logger.error(f"Failed to remove {block_data}: {e}, retrying")
time.sleep(1)
logger.info(f"Rolled back {block_message['block_hash']} successfully")
except Exception as e:
logger.error(f"Failed to remove block {block_message['block_hash']}: {e}")
finally:
return previous_block