Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions minichain/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
logger = logging.getLogger(__name__)


class InvalidProofOfWorkError(ValueError):
pass


def validate_block_link_and_hash(previous_block, block):
if block.previous_hash != previous_block.hash:
raise ValueError(
Expand All @@ -25,6 +29,10 @@ def validate_block_link_and_hash(previous_block, block):
if block.hash != expected_hash:
raise ValueError(f"invalid hash {block.hash}")

if not block.hash.startswith("0" * block.difficulty):
raise InvalidProofOfWorkError(
f"invalid PoW: hash {block.hash} does not satisfy difficulty {block.difficulty}"
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

class Blockchain:
"""
Expand Down Expand Up @@ -130,6 +138,9 @@ def add_block(self, block):
with self._lock:
try:
validate_block_link_and_hash(self.last_block, block)
except InvalidProofOfWorkError as exc:
logger.warning("Block %s rejected: %s", block.index, exc)
return ValidationStatus.INVALID
except ValueError as exc:
logger.warning("Block %s rejected: %s", block.index, exc)
return ValidationStatus.INVALID if "hash" in str(exc) else ValidationStatus.FAILED
Expand Down
37 changes: 37 additions & 0 deletions tests/test_pow_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from minichain import Blockchain, Block
from minichain.validators import ValidationStatus


def _make_block_with_invalid_pow(chain):
block = Block(
index=chain.last_block.index + 1,
previous_hash=chain.last_block.hash,
transactions=[],
difficulty=chain.current_difficulty,
state_root=chain.state.state_root(),
)

while True:
block.hash = block.compute_hash()
if not block.hash.startswith("0" * block.difficulty):
return block
block.nonce += 1


def test_add_block_rejects_invalid_pow():
chain = Blockchain()
block = _make_block_with_invalid_pow(chain)

assert chain.add_block(block) == ValidationStatus.INVALID
assert len(chain.chain) == 1


def test_resolve_conflicts_rejects_invalid_pow():
chain = Blockchain()
block = _make_block_with_invalid_pow(chain)

success, _ = chain.resolve_conflicts([chain.chain[0], block])

assert success is False
assert len(chain.chain) == 1