Skip to content

Commit

Permalink
feat(mining): make sure block template weight will increase the score
Browse files Browse the repository at this point in the history
  • Loading branch information
jansegre committed Apr 27, 2023
1 parent c500362 commit 56c79b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from hathor.transaction.exceptions import TxValidationError
from hathor.transaction.storage import TransactionStorage
from hathor.transaction.storage.exceptions import TransactionDoesNotExist
from hathor.util import EnvironmentInfo, LogDuration, Random, Reactor, not_none
from hathor.util import EnvironmentInfo, LogDuration, Random, Reactor, calculate_min_significant_weight, not_none
from hathor.wallet import BaseWallet

settings = HathorSettings()
Expand Down Expand Up @@ -846,8 +846,12 @@ def _make_block_template(self, parent_block: Block, parent_txs: 'ParentTxs', cur
else:
timestamp_max = timestamp_abs_max
timestamp = min(max(current_timestamp, timestamp_min), timestamp_max)
weight = daa.calculate_next_weight(parent_block, timestamp)
parent_block_metadata = parent_block.get_metadata()
# this is the min weight to cause an increase of twice the WEIGHT_TOL, we make sure to generate a template with
# at least this weight (note that the user of the API can set its own weight, the block sumit API will also
# protect agains a weight that is too small but using WEIGHT_TOL instead of 2*WEIGHT_TOL)
min_significant_weight = calculate_min_significant_weight(parent_block_metadata.score, 2 * settings.WEIGHT_TOL)
weight = max(daa.calculate_next_weight(parent_block, timestamp), min_significant_weight)
height = parent_block_metadata.height + 1
parents = [parent_block.hash] + parent_txs.must_include
parents_any = parent_txs.can_include
Expand Down Expand Up @@ -907,6 +911,12 @@ def submit_block(self, blk: Block, fails_silently: bool = True) -> bool:
if parent_hash not in tips:
self.log.warn('submit_block(): Ignoring block: parent not a tip', blk=blk.hash_hex)
return False
parent_block = self.tx_storage.get_transaction(parent_hash)
parent_block_metadata = parent_block.get_metadata()
# this is the smallest weight that won't cause the score to increase, anything equal or smaller is bad
min_insignificant_weight = calculate_min_significant_weight(parent_block_metadata.score, settings.WEIGHT_TOL)
if blk.weight <= min_insignificant_weight:
self.log.warn('submit_block(): insignificant weight? accepted anyway', blk=blk.hash_hex, weight=blk.weight)
return self.propagate_tx(blk, fails_silently=fails_silently)

def push_tx(self, tx: Transaction, allow_non_standard_script: bool = False,
Expand Down
6 changes: 6 additions & 0 deletions hathor/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,9 @@ def get_environment_info(args: str, peer_id: Optional[str]) -> EnvironmentInfo:

def get_hathor_core_version():
return hathor.__version__


def calculate_min_significant_weight(score: float, tol: float) -> float:
""" This function will return the min significant weight to increase score by tol.
"""
return score + math.log2(2 ** tol - 1)

0 comments on commit 56c79b0

Please sign in to comment.