一、下载安装包、解压
litecoin的官方网站为 https://litecoin.org/ 首页最下面有各个系统的下载地址,我们选择下载linux 64位程序,截止到 2020.4.22
最新软件版本为 0.17.1
1、下载 选择 litecoin-0.17.1-x86_64-linux-gnu.tar.gz 适用于centos7的安装包
wget https://download.litecoin.org/litecoin-0.17.1/linux/litecoin-0.17.1-x86_64-linux-gnu.tar.gz
2、解压
tar -zxvf litecoin-0.17.1-x86_64-linux-gnu.tar.gz
二、配置litecoin
1、创建数据存储路径,/data/litecoin/litecoin_data
mkdir -p /data/litecoin/litecoin_data
2、创建配置文件到路径,/data/litecoin/litecoin.conf
vim /data/litecoin/litecoin.conf
3、向配置文件添加内容
点击i切换到编辑模式,输入以下内容:
# rpc 用户名 rpcuser=litecoinrpc # rpc 密码 rpcpassword=litecoin202001
rpcallowip=0.0.0.0/0 # 端口 rpcport=9046 # 是否是服务器 server=1 # 允许后台运行 daemon=1 txindex=1 # 非测试网络 testnet=0 dbcache=8000 # 区块存储位置 datadir=/data/litecoin/litecoin_data #paytxfee=0.00 # rpc 请求超时时间 #rpctimeout=30 # 不挖矿 #gen=0 # 通知到账
4、先按ESC键,输入:x保存编辑并退出
三、运行litecoin
1、进入安装目录,进入litecoin-0.17.1/bin/
cd litecoin-0.17.1/bin/
2、查看版本号(可以不执行)
./litecoind --version
3、将可执行文件拷贝至环境目录
sudo cp litecoin-cli /usr/bin/
sudo cp litecoind /usr/bin/
4、启动运行
sudo litecoind -conf=/data/litecoin/litecoin.conf
出现 Litecoin server starting 说明莱特币正在同步节点.
四、查看同步信息
1、执行命令查看
sudo litecoin-cli -conf=/data/litecoin/litecoin.conf getblockchaininfo
2、结果中 blocks 为目前同步区块数
3、停止服务
sudo litecoin-cli -conf=/data/litecoin/litecoin.conf stop
原文:https://blog.csdn.net/Mrhan210/article/details/104258182
五、解析交易数据并入库
注意:如果是外网主机,需要设置防火墙端口,此端口跟前面config文件设置的端口一致,
import json import logging import time import pymysql
import request //钱包配置文件设置的用户名和密码 rpcusername = 'litecoinrpc' rpcpassword = 'litecoin202001' //钱包所在电脑的IP和rpc的接口 url = '0.0.0.0:0000'" # 计算uae的authorization headers = { 'ua': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36' } auth = (rpcusername,rpcpassword) //json-rpc命令 def getblockhash(num): while True: try: r = requests.post(url,json={ "method": "getblockhash", "params": [num], "id": "foo" } ,timeout=10,headers=headers,auth=auth) response = json.loads(r.text) result = response['result'] break except Exception as e: print("连接中。。。") time.sleep(0.1) return result def getblock(hash): while True: try: r = requests.post(url, json={ "method": "getblock", "params": [hash], "id": "foo" }, timeout=10, headers=headers,auth=auth) response = json.loads(r.text) result = response['result'] break except Exception as e: time.sleep(0.1) return result def getrawtransaction(hash): while True: try: r = requests.post(url, json={ "method": "getrawtransaction", "params": [hash], "id": "foo" }, timeout=10, headers=headers,auth=auth) if r.status_code == 200: response = json.loads(r.text) result = response['result'] break except Exception as e: time.sleep(0.1) return result def decoderawtransaction(info): while True: try: r = requests.post(url, json={ "method": "decoderawtransaction", "params": [info], "id": "foo" }, timeout=10, headers=headers,auth=auth) response = json.loads(r.text) result = response['result'] break except Exception as e: time.sleep(0.1) return result //要存入数据库的地址 while True: try: db = pymysql.connect('localhost','root','123456','litecoin') cursor = db.cursor() if db: break except Exception as e: time.sleep(5) //日志文件 logging.basicConfig(filename='litecoin_log.txt', filemode="a", format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%d-%M-%Y %H:%M:%S", level=logging.INFO) //解析入库 def analysis_block(height): try: block_hash = getblockhash(height) block_info = getblock(block_hash) # print(block_info) logging.info('区块高度:{}--success'.format(height)) # block_info = eval(block_info) tx_hash_list = block_info.get('tx') #遍历交易hash列表 for tx in tx_hash_list: tx_row = getrawtransaction(tx) # print(tx_row) tx_info = decoderawtransaction(tx_row) # tx_info = json.loads(tx_info) # print(tx_info) logging.info('交易hash:{}--success'.format(tx)) account_1 = [] account_2 = [] all_price =0.0 vin= tx_info.get('vin') #判断是否为挖矿 coinbase = vin[0].get('coinbase') if coinbase: tx_type = 'miner' account_1_tuple = ['coinbase'] account_1.append(account_1_tuple) account_1=json.dumps(account_1) else: tx_type = 'transfer' # tx_input_out_list = [] for tx_input in vin: vin_vout_index = tx_input.get('vout') txid = tx_input.get('txid') # txid = '6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4' tx__input_row = getrawtransaction(txid) # tx__input_row = os.popen(get_input_tx_row_cmd).read() # print(tx__input_row) tx__input_info = decoderawtransaction(tx__input_row) # tx__input_info = os.popen(tx_input_info_cmd).read() # tx__input_info = json.loads(tx__input_info) tx_input_vout = tx__input_info.get('vout') scriptPubKey = tx_input_vout[vin_vout_index].get('scriptPubKey') account_1_hash = scriptPubKey.get('addresses')[0] account_1_price = tx_input_vout[vin_vout_index].get('value') account_1_tuple = (account_1_hash, account_1_price) account_1.append(account_1_tuple) account_1 = json.dumps(account_1) tx_hash = tx_info.get('hash') vout = tx_info.get('vout') #遍历输入地址列表 for tx_out in vout: account_2_hash_list = tx_out.get('scriptPubKey').get('addresses') if account_2_hash_list: account_2_hash = tx_out.get('scriptPubKey').get('addresses')[0] account_2_price = tx_out.get('value') all_price += float(account_2_price) account_2_tuple = (account_2_hash, account_2_price) account_2.append(account_2_tuple) account_2=json.dumps(account_2) exchange = '' time_stamp = block_info.get('time') timeArray = time.localtime(time_stamp) time_str = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) note = block_info.get('nonce') # print(tx_hash) # print(account_1) # print(account_2) # print(tx_type) # print(all_price) # print(exchange) # print(time_str) # print(note) # print(height,'----------------------------------') risk = 1 if tx_type !='miner' and float(all_price) < 0.000001: risk = 0 sql_transactions = 'insert into transaction(hash,account_1,account_2,type,price,exchange,time,note,risk,timestamp) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)' try: cursor.execute(sql_transactions, (tx_hash,account_1,account_2,tx_type,all_price,exchange,time_str,note,risk,time_stamp,)) db.commit() print(height,"success") except Exception as e: logging.info('error:{}--fail'.format(e)) continue # price = except Exception as e: logging.info('error:{}--fail'.format(e)) if __name__ == '__main__': height = 1 # while True: # if height < 10: # try: while True: analysis_block(height) height+=1 # time.sleep(0.5) # continue # except: # break
附注:简单安装运行litecoin同步节点
原文:https://www.matools.com/blog/190657982