Created
August 5, 2015 08:08
-
-
Save anch0vy/d5af734ab0737482bbd9 to your computer and use it in GitHub Desktop.
ch0vycoin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
import pickle | |
import struct | |
from math import log,e | |
from hashlib import md5 | |
startBlock = 'cat' | |
def mine(block,diff): | |
''' | |
코인을 캐는 함수 | |
block: 가장 최신 block, 이걸 기반으로 코인을 캐게 됨 | |
diff: 문제 난이도 | |
md5(block + 어떤문자열) < diff 일경우 코인을 캔걸로 인정이 됨 | |
''' | |
n = 0 | |
hash = md5(block) | |
while True: | |
hash_ = hash.copy() | |
hash_.update(struct.pack('Q',n)) | |
if int(hash_.hexdigest(),16) < diff: | |
return hash_.hexdigest(),block + struct.pack('Q',n) | |
else: | |
n+=1 | |
def getDict(hash,block,beforeDiff,beforeTime): | |
dic = {} | |
dic['time'] = time.time() | |
dic['md5'] = hash | |
dic['block'] = block | |
dic['diff'] = beforeDiff - int(1.0 / (dic['time'] - beforeTime) * 0x10000000000000000000000000) | |
''' | |
간단하게 이번 코인을 캐는데 걸린시간의 역수를 구해서 적당한 정수를 곱한뒤에 이전 난이도에서 빼는식으로 함 | |
만약 적당한 정수를 작게잡으면 난이도가 천천히 높아질거고 높게잡으면 난이도가 가파르게 올라갈거임 | |
''' | |
return dic | |
def main(): | |
blockChain = [] | |
diff = 0xffffffffffffffffffffffffffffffff | |
dic = {} #젤 처음 사용할 용도 | |
dic['time'] = time.time() | |
dic['md5'] = md5(startBlock).hexdigest() | |
dic['diff'] = diff | |
dic['block'] = startBlock | |
blockChain.append(dic) | |
t = time.time() | |
while True: | |
hash , newBlock = mine(blockChain[-1]['block'],blockChain[-1]['diff']) | |
tmp = getDict(hash,newBlock,blockChain[-1]['diff'],blockChain[-1]['time']) | |
blockChain.append(tmp) | |
if time.time() - t > 3: | |
t = time.time() | |
print tmp['md5'],'%33s'%hex(tmp['diff'])[2:],'%-2f'%(tmp['time'] - blockChain[-2]['time']) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment