work on proof of work

master
Kevin Froman 2018-05-05 16:50:15 -05:00
parent 268cd10f34
commit 7d9936e55c
3 changed files with 41 additions and 3 deletions

View File

@ -561,6 +561,12 @@ class OnionrCommunicate:
blockMeta2 = {'type': ''} blockMeta2 = {'type': ''}
pass pass
blockContent = blockContent[blockContent.rfind(b'}') + 1:] blockContent = blockContent[blockContent.rfind(b'}') + 1:]
if not self.verifyPow(blockContent, blockMeta2):
logger.warn(i + " has invalid or insufficient proof of work token, deleting")
self._core.removeBlock(i)
continue
try: try:
blockMetadata['sig'] blockMetadata['sig']
blockMeta2['id'] blockMeta2['id']
@ -656,6 +662,31 @@ class OnionrCommunicate:
peerTryCount += 1 peerTryCount += 1
return retVal return retVal
def verifyPow(self, blockContent, metadata):
'''
Verifies the proof of work associated with a block
'''
retData = False
try:
metadata['pow']
token = metadata['pow']
except KeyError:
return False
dataLen = len(blockContent)
expectedHash = self._crypto.blake2bHash(blockContent)
difficulty = 0
if token == expectedHash:
difficulty = math.floor(dataLen/1000000)
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
puzzle = mainHash[0:difficulty]
if token[0:difficulty] == puzzle:
logger.info('Validated block pow')
retData = True
return retData
def urlencode(self, data): def urlencode(self, data):
''' '''

View File

@ -638,7 +638,6 @@ class Core:
if powToken != False: if powToken != False:
break break
time.sleep(0.3) time.sleep(0.3)
try: try:
data.decode() data.decode()

View File

@ -17,7 +17,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import nacl.signing, nacl.encoding, nacl.public, nacl.secret, os, binascii, base64, hashlib, logger import nacl.signing, nacl.encoding, nacl.public, nacl.hash, nacl.secret, os, binascii, base64, hashlib, logger
class OnionrCrypto: class OnionrCrypto:
def __init__(self, coreInstance): def __init__(self, coreInstance):
@ -209,4 +209,12 @@ class OnionrCrypto:
hasher.update(pubkey + prev) hasher.update(pubkey + prev)
prev = hasher.hexdigest() prev = hasher.hexdigest()
result = prev result = prev
return result return result
def sha3Hash(self, data):
hasher = hashlib.sha3_256()
hasher.update(data)
return hasher.hexdigest()
def blake2bHash(self, data):
return nacl.hash.blake2b(data)