# -*- coding: utf-8 -*-
"""
@Time : 2020/7/10 17:22
@Athor : LinXiao
@功能 :
"""
# ------------------------------
import json
import os
from copy import deepcopy
from pprint import pprint
from loguru import logger
SAVE_PATH = "/backup/btc/btc_outputList_tx"
def file_path(dirname):
file_list=[]
for root, _, files in os.walk(dirname):
for f in files:
file_path=os.path.join(root, f)
file_list.append(file_path)
# pprint(file_list)
return file_list
#清洗新的btc
def get_outputList(tx):
voutValues=0
vinValues=0
outputValue=0
vout=tx["vout"] # list
vin=tx["vin"] # list
for vi in vin:
outputList=[]
if vi['id'] == 'coinbase':
for vout_data in vout:
if vout_data["address"] != "":
dic={
"address": vout_data["address"][0],
"value": vout_data["value"]
}
outputValue=dic["value"]
outputList.append(dic)
# pprint(f"coinbase outputList is: {outputList}")
# pprint(f"coinbase outputValue is: {outputValue}")
# return outputValue, outputList
btc_data={
"outputList": outputList,
"voutValues": outputValue,
"vinValues": 0,
"outputValue": dic["value"],
"fee": dic["value"],
}
# pprint(btc_data)
tx.update(btc_data)
# pprint(tx)
return tx
else:
# 判断vin中的地址是否在vout中出现,若出现在vout中,则删除掉vout中的这笔交易
vout1=deepcopy(vout)
for vi in vin:
vinValues+=vi["value"]
for vo in vout1:
if vi["address"] == vo["address"]:
vout1.remove(vo)
for vo1 in vout1:
output={
"address": vo1["address"][0],
"value": vo1["value"]
}
outputValue+=vo1["value"]
outputList.append(output)
for vot in vout:
voutValues+=vot["value"]
fee=vinValues - voutValues
btc_data={
"outputList": outputList,
"voutValues": voutValues,
"vinValues": vinValues,
"outputValue": outputValue,
"fee": fee,
}
tx.update(btc_data)
return tx
# 读文件
def read_in_line(file_path):
with open(file_path, "r") as f:
while True:
line = f.readline()
if line:
yield line
else:
return # 如果读取到文件末尾,则退出
# 存为json文件
def save_json(tx,path):
# 先将字典对象转化为可写入文本的字符串
item=json.dumps(tx)
try:
if not os.path.exists(path):
with open(path, "w", encoding='utf-8') as f:
f.write(item + ",\n")
else:
with open(path, "a", encoding='utf-8') as f:
f.write(item + ",\n")
except Exception as e:
print(e)
def get_btc_data(dirname):
file_list=file_path(dirname)
for path in file_list:
with open(path, 'r') as f:
while True:
data=f.readline()
if not data:
break
tx=json.loads(data)
btc_data = get_outputList(tx)
# save_path = r"D:\Project\etc\Chain-Storage\src\core\test"
save_path = SAVE_PATH
name = path.split("/")[-1]
new_path=f"{save_path}/new_{name}"
save_json(btc_data,new_path)
logger.info(f"write {name} success")
if __name__ == '__main__':
# dirname=r"D:\Project\etc\Chain-Storage\src\core\test"
# 241 服务器
dirname=r"/backup/btc/btc_new_tx"
# dirname=r"/backup/btc/test"
get_btc_data(dirname)
网友评论