Skip to content

Commit

Permalink
pow 算法实现
Browse files Browse the repository at this point in the history
  • Loading branch information
magic.chen authored and magic.chen committed Jul 9, 2018
1 parent ff65af4 commit 8d5feb9
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 15 deletions.
8 changes: 8 additions & 0 deletions consensus/dpos.py
@@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
This Document is Created by At 2018/7/9
"""


8 changes: 8 additions & 0 deletions consensus/pos.py
@@ -0,0 +1,8 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
This Document is Created by At 2018/7/9
"""


44 changes: 35 additions & 9 deletions consensus/proof_of_work.py
Expand Up @@ -4,8 +4,8 @@
"""
This Document is Created by At 2018/6/29
"""
import copy
from settings import maxNonce
import hashlib
from settings import maxNonce, targetBits


class ProofOfWork:
Expand All @@ -20,26 +20,52 @@ def __init__(self, block, nonce=0):
raise TypeError

self.block = block
self.target = 1 << (256-targetBits)

def prepare_data(self):
"""
准备计算的数据
:return:
"""
pre_data = copy.deepcopy(self.block)
pre_data.pop('Hash')

data = {
""
}
timestamp = hex(self.block["TimeStamp"])[0]

data = "".join([
self.block["PrevBlockHash"],
self.block["Data"],
timestamp,
hex(targetBits),
hex(self.nonce)
])

return data

def run(self):
print("Mining a new block...")

hash_v = ""
while self.nonce < maxNonce:
data = self.prepare_data()
print(data)

hash_v = hashlib.sha256(data.encode("utf-8")).hexdigest()

print("-----> is mining ... %s" % hash_v)

if int(hash_v, 16) <= self.target:
break
else:
self.nonce += 1
print("\n")

return hash_v

def validate(self):
pass
data = self.prepare_data()
hash_v = hashlib.sha256(data.encode('utf-8')).hexdigest()

if hash_v <= self.target:
return True
return False



18 changes: 18 additions & 0 deletions consensus/proof_of_work_test.py
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
This Document is Created by At 2018/7/9
"""

from consensus.proof_of_work import ProofOfWork
from core.blockchain.block import Block


if __name__ == '__main__':
b = Block()

genesis_block = b.new_block("Magic test", "")

pow = ProofOfWork(genesis_block)
pow.run()
49 changes: 46 additions & 3 deletions core/blockchain/block.py
Expand Up @@ -5,14 +5,57 @@
This Document is Created by At 2018/7/7
"""

import time
import hashlib
from consensus.proof_of_work import ProofOfWork


class Block:
"""
doc 区块
block = {
"Timestamp": "",
"Data": "",
"PrevBlockHash": "",
"Hash": "",
"Nonce": ""
}
"""

def __init__(self):
pass
self.block = dict()

def new_block(self, data, prev_hash):
block = {
"TimeStamp": int(time.time()),
"Data": data,
"PrevBlockHash": prev_hash,
}

pow = ProofOfWork(block)

b_hash = pow.run()
block["Hash"] = b_hash

self.block = block
return block

def set_hash(self, block):
"""
v1
# timestamp = hex(block["TimeStamp"])[0]
#
# data = "".join([block["PrevBlockHash"], block["Data"], timestamp])
#
# return hashlib.sha256(data.encode("utf-8")).hexdigest()
:param block:
:return:
"""
# use pow to set the hash value
timestamp = hex(block["TimeStamp"])[0]

def new_block(self):
pass
data = "".join([block["PrevBlockHash"], block["Data"], timestamp])

return hashlib.sha256(data.encode("utf-8")).hexdigest()
19 changes: 19 additions & 0 deletions core/blockchain/block_test.py
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
This Document is Created by At 2018/7/9
"""

from core.blockchain.block import Block

if __name__ == '__main__':
b = Block()

b.new_block("Magic", "")

block = b.block

new_block = b.new_block("This is a test block", b.block["Hash"])

assert new_block["PrevBlockHash"] == block["Hash"]
6 changes: 3 additions & 3 deletions core/blockchain/blockchain.py
Expand Up @@ -21,17 +21,17 @@
"""

from datetime import datetime
import time


class BlockChain:
"""
doc New blockchain
"""
def __init__(self):
pass
self.blockchain = []

def new_blockchain(self):
def add_block(self):
pass


Expand Down
1 change: 1 addition & 0 deletions settings.py
Expand Up @@ -6,3 +6,4 @@
"""

maxNonce = 1 << 63 - 1
targetBits = 16

0 comments on commit 8d5feb9

Please sign in to comment.