fixed finding nonces

This commit is contained in:
Ryan Bakkes 2023-11-12 17:17:08 +01:00
parent 886632550d
commit d2263ab105
2 changed files with 16 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import random
REWARD_VALUE = 50.0 REWARD_VALUE = 50.0
leading_zeros = 2 leading_zeros = 2
leading_extra = 1
next_char_limit = 20 next_char_limit = 20
class TxBlock (CBlock): class TxBlock (CBlock):
@ -21,16 +22,6 @@ class TxBlock (CBlock):
def addTx(self, Tx_in): def addTx(self, Tx_in):
self.data.append(Tx_in) self.data.append(Tx_in)
def __count_totals(self):
total_in = 0
total_out = 0
for tx in self.data:
for addr, amt in tx.inputs:
total_in = total_in + amt
for addr, amt in tx.outputs:
total_out = total_out + amt
return total_in, total_out
def is_valid(self): def is_valid(self):
if not super(TxBlock, self).is_valid(): if not super(TxBlock, self).is_valid():
return False return False
@ -42,12 +33,6 @@ class TxBlock (CBlock):
if not self.good_nonce(): if not self.good_nonce():
return False return False
# total_in, total_out = self.__count_totals()
# Tx_Balance = round(total_out - total_in, 10)
# print("Tx_Balance: ", Tx_Balance)
# if Tx_Balance > REWARD_VALUE:
# return False
return True return True
def good_nonce(self): def good_nonce(self):
@ -56,11 +41,22 @@ class TxBlock (CBlock):
digest.update(bytes(str(self.previousHash), 'utf8')) digest.update(bytes(str(self.previousHash), 'utf8'))
digest.update(bytes(str(self.nonce), 'utf8')) digest.update(bytes(str(self.nonce), 'utf8'))
this_hash = digest.finalize() this_hash = digest.finalize()
return this_hash[:leading_zeros] == b'\x00'*leading_zeros
bytes_array = [b'\x00', b'\x01', b'\x02', b'\x03', b'\x04', b'\x05', b'\x06']
if this_hash[:leading_zeros] == b'\x00'*leading_zeros:
for i in range(leading_extra):
if this_hash[:leading_extra + i + 1][leading_extra+i:] in bytes_array:
continue
else:
return False
return True
def find_nonce(self): def find_nonce(self):
for i in range(10000000): for i in range(200000):
self.nonce = i self.nonce = "".join([random.choice("1234567890ABCDEF") for n in range(next_char_limit)])
if self.good_nonce(): if self.good_nonce():
return self.nonce return self.nonce
return None return None

View File

@ -3,7 +3,7 @@ from classes.TxBlock import TxBlock
from helpers import UtilityHelper as utilityHelper from helpers import UtilityHelper as utilityHelper
import time import time
MIN_MINING_TIME = 0 MIN_MINING_TIME = 10
MAX_MINING_TIME = 20 MAX_MINING_TIME = 20
def exploreBlocks(self): def exploreBlocks(self):