아직은 많이 조악한 내가 만든 블록체인 코드
언어/python2018. 3. 14. 15:29
"""
python version : python3.x
작성자 : 김준현
"""
import datetime as dtime # 블록의 생성 시간 리턴
import hashlib as hash # 블록의 해시 값 - sha 256
#---------------------------------------------------------------------------------------
class Block:
def __init__(self, index, prev_hash, bits):
self.version = str(4901).encode(encoding='utf-8') # 블록 버전 정보 (4byte)
self.index = str(index).encode(encoding='utf-8') # Genesis 블록은 0
self.prev_hash = str(prev_hash).encode(encoding='utf-8') # 이전 노드의 해시 (32byte)
self.timestamp_ = str(dtime.datetime.now()).encode(encoding='utf-8')# 블록 생성시간 정보 (4byte)
self.bits = str(bits).encode(encoding='utf-8')# 증명의 난이도
self.block_hash = str(self.retHash()).encode(encoding='utf-8') # 블록 해시 (32byte)
def retHash(self):
sha256 = hash.sha256() # 객체 생성
sha256.update(self.version + # 블록 버전 정보
self.index +
self.prev_hash + # 이전 블록의 해시
self.timestamp_ + # 블록 생성시간 정보
self.bits) # 증명의 난이도
return sha256.hexdigest()
==================================================================
Genesis block
#----------------------------------------------------
import blockClass as Bclass
import InsertMongoDB_HASH
#----------------------------------------------------
GenBlock = Bclass.Block(0, "None", 0)
print (GenBlock.version.decode(encoding='utf-8'))
print (GenBlock.timestamp_.decode(encoding='utf-8'))
print (GenBlock.prev_hash.decode(encoding='utf-8'))
print (GenBlock.block_hash.decode(encoding='utf-8'))
print (GenBlock.index.decode(encoding='utf-8'))
InsertMongoDB_HASH.Insert_blockInformation(GenBlock)
==================================================================
일반 블록
#----------------------------------------------------
import blockClass as Bclass
import InsertMongoDB_HASH
import RetPrevHash
import RetIndex
#----------------------------------------------------
temp_index = RetIndex.ret_index()
t = str(temp_index-1)
temp_prev_hash = RetPrevHash.ret_prev_block_hash(t)
normalBlock = Bclass.Block(temp_index, temp_prev_hash, 0) # index, prev_hash, bits
print (normalBlock.version.decode(encoding='utf-8'))
print (normalBlock.timestamp_.decode(encoding='utf-8'))
print (normalBlock.prev_hash.decode(encoding='utf-8'))
print (normalBlock.block_hash.decode(encoding='utf-8'))
print (normalBlock.index.decode(encoding='utf-8'))
#----------------------------------------------------
# // Block information mongodb insert
InsertMongoDB_HASH.Insert_blockInformation(normalBlock)==========================================================
import pymongo
from pymongo import MongoClient
import pprint
#-----------------------------------------------
def ret_index():
Client = MongoClient('localhost', 27017) # 객체 생성
db = Client["test01"]
# print (db)
# print (db.collection_names())
collection = db.users
dictValue = collection.find()
s = [ n for n in dictValue ]
#print (s[len(s)-1])
return int(s[len(s)-1]["index"])+1
===========================================================
from pymongo import MongoClient
def Insert_blockInformation(block):
client = MongoClient('localhost', 27017) # 객체 생성
db = client.test01
users = db.users
doc = {'_version' : block.version.decode(encoding='utf-8'),
'_timestamp': block.timestamp_.decode(encoding='utf-8'),
'prev_hash' : block.prev_hash.decode(encoding='utf-8'),
'block_hash': block.block_hash.decode(encoding='utf-8'),
'index' : block.index.decode(encoding='utf-8')}
try:
users.insert(doc)
except:
print("insert faild")==========================================================
import pymongo
from pymongo import MongoClient
import pprint
#-----------------------------------------------
def ret_prev_block_hash(index):
Client = MongoClient('localhost', 27017) # 객체 생성
db = Client["test01"]
# print (db)
# print (db.collection_names())
collection = db.users
dictValue = collection.find({'index':index})
print (dictValue[0]['block_hash'])
return dictValue[0]['block_hash']
'언어 > python' 카테고리의 다른 글
억지스럽다 ㅋㅋㅋㅋ 결과를 dict 형태로 만들기 (0) | 2018.03.20 |
---|---|
프로세스 킬 코드 파이썬2.x (0) | 2018.03.15 |
블록체인 코드 (0) | 2018.03.13 |
파일 타입 확인 _ 테스트 _CentOS7 (0) | 2018.03.04 |
YARA _01 (0) | 2018.03.03 |