Skip to content

Commit

Permalink
convert header hashing to standard binary instead of string, update m…
Browse files Browse the repository at this point in the history
…ining pool
  • Loading branch information
pdxwebdev committed May 4, 2021
1 parent 9064f76 commit aaa8273
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
28 changes: 25 additions & 3 deletions yadacoin/core/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ async def generate(
block.signature = TU.generate_signature(block.hash, private_key)
return block

def little_hash(self):
little_hex = bytearray.fromhex(self.hash)
little_hex.reverse()

str_little = ''.join(format(x, '02x') for x in little_hex)

return str_little

def generate_header(self):
if int(self.version) < 3:
return str(self.version) + \
Expand Down Expand Up @@ -321,13 +329,27 @@ def get_coinbase(self):
def generate_hash_from_header(self, height, header, nonce):
if not hasattr(Block, 'pyrx'):
Block.pyrx = pyrx.PyRX()
header = header.format(nonce=nonce)
if height >= CHAIN.RANDOMX_FORK:
seed_hash = binascii.unhexlify('4181a493b397a733b083639334bc32b407915b9a82b7917ac361816f0a1f5d4d') #sha256(yadacoin65000)
seed_hash = binascii.unhexlify('4181a493b397a733b083639334bc32b407915b9a82b7917ac361816f0a1f5d4d') #sha256(yadacoin65000)
if height >= CHAIN.BLOCK_V5_FORK:
bh = Block.pyrx.get_rx_hash(
header.encode().replace(
b'{nonce}',
binascii.unhexlify(
nonce
)
),
seed_hash,
height
)
hh = binascii.hexlify(bh).decode()
return hh
elif height >= CHAIN.RANDOMX_FORK:
header = header.format(nonce=nonce)
bh = Block.pyrx.get_rx_hash(header, seed_hash, height)
hh = binascii.hexlify(bh).decode()
return hh
else:
header = header.format(nonce=nonce)
return hashlib.sha256(hashlib.sha256(header.encode('utf-8')).digest()).digest()[::-1].hex()

def verify(self):
Expand Down
5 changes: 4 additions & 1 deletion yadacoin/core/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ async def get_inputs(inputs):
target_block_time = CHAIN.target_block_time(self.config.network)

checks_passed = False
if (int(block.hash, 16) < target):
if (block.index >= CHAIN.BLOCK_V5_FORK) and int(block.little_hash(), 16) < target:
self.config.app_log.warning('5')
checks_passed = True
elif (int(block.hash, 16) < target):
self.config.app_log.warning('6')
checks_passed = True
elif (block.special_min and int(block.hash, 16) < special_target):
Expand Down
7 changes: 6 additions & 1 deletion yadacoin/core/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ class CHAIN(object):
POW_FORK_V2 = 60000
MINING_AND_TXN_REFORM_FORK = 60000
POW_FORK_V3 = 61110

BLOCK_V5_FORK = 176000
RANDOMX_FORK = 65000
FORK_10_MIN_BLOCK = 65500
SPECIAL_MIN_FORK = 38600

RETARGET_PERIOD = 2016 # blocks
TWO_WEEKS = 1209600 # seconds
Expand Down Expand Up @@ -129,6 +132,8 @@ def special_target(cls, block_height: int, target:int, delta_t:int, network: str
def get_version_for_height(cls, height: int):
if int(height) <= 14484:
return 1
elif int(height) >= cls.BLOCK_V5_FORK:
return 5
elif int(height) >= cls.POW_FORK_V3:
return 4
elif int(height) > cls.POW_FORK_V2 and int(height) < cls.POW_FORK_V3:
Expand Down Expand Up @@ -405,7 +410,7 @@ async def get_target(cls, height, last_block, block, extra_blocks=None) -> int:
else:
block_to_check = block
delta_t = int(block.time) - int(last_block.time)
if block.index >= 38600 and delta_t > max_block_time and block.special_min:
if block.index >= CHAIN.SPECIAL_MIN_FORK and delta_t > max_block_time and block.special_min:
special_target = CHAIN.special_target(block.index, block.target, delta_t, get_config().network)
return special_target

Expand Down
34 changes: 26 additions & 8 deletions yadacoin/core/miningpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ async def on_miner_nonce(self, nonce: str, address: str='') -> bool:
return False


if (int(block_candidate.target) + 0x0000F00000000000000000000000000000000000000000000000000000000000) > int(hash1, 16):
if (
(int(block_candidate.target) + 0x0000F00000000000000000000000000000000000000000000000000000000000) > int(hash1, 16) or
(
block_candidate.index >= CHAIN.BLOCK_V5_FORK and
(int(block_candidate.target) + 0x0000F00000000000000000000000000000000000000000000000000000000000) > int(block_candidate.little_hash(), 16)
)
):
# submit share only now, not to slow down if we had a block
self.app_log.warning('{} {}'.format(hash1, address))
await self.mongo.async_db.shares.update_one({
Expand All @@ -115,13 +121,25 @@ async def on_miner_nonce(self, nonce: str, address: str='') -> bool:
}
}, upsert=True)

if int(block_candidate.target) > int(block_candidate.hash, 16):
if (
int(block_candidate.target) > int(block_candidate.hash, 16) or
(
block_candidate.index >= CHAIN.BLOCK_V5_FORK and
int(block_candidate.target) > int(block_candidate.little_hash(), 16)
)
):
# accept winning block
await self.accept_block(block_candidate)
# Conversion to dict is important, or the object may change
self.app_log.debug('block ok')
self.app_log.error('^^ ^^ ^^')
elif block_candidate.special_min and (int(block_candidate.special_target) > int(block_candidate.hash, 16)):
elif (
block_candidate.special_min and (int(block_candidate.special_target) > int(block_candidate.hash, 16)) or
(
block_candidate.index >= CHAIN.BLOCK_V5_FORK and
block_candidate.special_min and (int(block_candidate.special_target) > int(block_candidate.little_hash(), 16))
)
):
# accept winning block
await self.accept_block(block_candidate)
# Conversion to dict is important, or the object may change
Expand Down Expand Up @@ -190,10 +208,10 @@ async def block_template(self):
difficulty = int(self.max_target / self.block_factory.target)
seed_hash = '4181a493b397a733b083639334bc32b407915b9a82b7917ac361816f0a1f5d4d' #sha256(yadacoin65000)
res = {
'job_id': self.block_factory.header.replace('{nonce}', '{00}'),
'difficulty': difficulty,
'target': hex(int(self.block_factory.target))[2:].rjust(64, '0')[:16],
'blocktemplate_blob': self.block_factory.header.replace('{nonce}', '{000000}'),
'blockhashing_blob': self.block_factory.header.replace('{nonce}', '{000000}'),
'target': '0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', #hex(int(self.block_factory.target))[2:].rjust(64, '0')[:14],
'blob': self.block_factory.header.replace('{nonce}', '{00}').encode().hex(),
'seed_hash': seed_hash,
'height': self.config.LatestBlock.block.index + 1, # This is the height of the one we are mining
}
Expand All @@ -204,7 +222,7 @@ async def set_target(self, to_time):
await self.set_target_from_last_non_special_min(self.config.LatestBlock.block)
# todo: keep block target at normal target, for header and block info.
# Only tweak target at validation time, and don't include special_min into header
if self.block_factory.index >= 38600: # TODO: use a CHAIN constant
if self.block_factory.index >= CHAIN.SPECIAL_MIN_FORK: # TODO: use a CHAIN constant
# print("test target", int(to_time), self.last_block_time)
if self.block_factory.target == 0:
# If the node is started when the current block is special_min, then we have a 0 target
Expand All @@ -219,7 +237,7 @@ async def set_target(self, to_time):
self.block_factory.time = int(to_time)
else:
self.block_factory.special_min = False
elif self.block_factory.index < 38600: # TODO: use a CHAIN constant
elif self.block_factory.index < CHAIN.SPECIAL_MIN_FORK: # TODO: use a CHAIN constant
if (int(to_time) - self.last_block_time) > self.target_block_time:
self.block_factory.target = self.max_target
self.block_factory.special_min = True
Expand Down
12 changes: 6 additions & 6 deletions yadacoin/tcpsocket/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ async def block_checker(cls):
cls.config = get_config()
if cls.current_index != cls.config.LatestBlock.block.index:
job = await cls.config.mp.block_template()
job['job_id'] = job['blocktemplate_blob']
job['blob'] = job['blocktemplate_blob']
job['job_id'] = job['job_id']
job['blob'] = job['blob']
cls.current_index = cls.config.LatestBlock.block.index
result = {
'id': job['blocktemplate_blob'],
'id': job['job_id'],
'job': job
}
rpc_data = {
Expand Down Expand Up @@ -126,10 +126,10 @@ async def login(self, body, stream):
stream.peer = Peer(body['params'].get('login'))
self.config.app_log.info(f'Connected to Miner: {stream.peer.to_json()}')
StratumServer.inbound_streams[Miner.__name__][stream.peer] = stream
job['job_id'] = job['blocktemplate_blob']
job['blob'] = job['blocktemplate_blob']
job['job_id'] = job['job_id']
job['blob'] = job['blob']
result = {
'id': job['blocktemplate_blob'],
'id': job['blob'],
'job': job
}
rpc_data = {
Expand Down

0 comments on commit aaa8273

Please sign in to comment.