|
| 1 | +from pydantic import root_validator |
| 2 | + |
| 3 | +from node.blockchain.mixins.crypto import SignableMixin, validate_signature_helper |
| 4 | +from node.blockchain.mixins.validatable import ValidatableMixin |
| 5 | +from node.core.exceptions import ValidationError |
| 6 | +from node.core.utils.cryptography import derive_public_key |
| 7 | +from node.core.utils.types import non_negative_int |
| 8 | + |
| 9 | +from ..types import AccountNumber, Hash, NodeRole, Signature |
| 10 | +from .base import BaseModel |
| 11 | + |
| 12 | + |
| 13 | +class BlockConfirmationMessage(BaseModel, SignableMixin): |
| 14 | + number: non_negative_int |
| 15 | + hash: Hash # noqa: A003 |
| 16 | + |
| 17 | + |
| 18 | +class BlockConfirmation(ValidatableMixin, BaseModel): |
| 19 | + signer: AccountNumber |
| 20 | + signature: Signature |
| 21 | + message: BlockConfirmationMessage |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def create(cls, number, hash_, signing_key): |
| 25 | + message = BlockConfirmationMessage(number=number, hash=hash_) |
| 26 | + signer = derive_public_key(signing_key) |
| 27 | + signature = message.make_signature(signing_key) |
| 28 | + |
| 29 | + return cls(signer=signer, signature=signature, message=message) |
| 30 | + |
| 31 | + @root_validator |
| 32 | + def validate_signature(cls, values): |
| 33 | + return validate_signature_helper(values) |
| 34 | + |
| 35 | + def get_number(self): |
| 36 | + return self.message.number |
| 37 | + |
| 38 | + def get_hash(self): |
| 39 | + return self.message.hash |
| 40 | + |
| 41 | + def validate_signer(self, blockchain_facade): |
| 42 | + if not blockchain_facade.has_blocks(): |
| 43 | + return |
| 44 | + |
| 45 | + cv_identifiers = (node.identifier for node in blockchain_facade.yield_nodes({NodeRole.CONFIRMATION_VALIDATOR})) |
| 46 | + if self.signer not in cv_identifiers: |
| 47 | + raise ValidationError('Invalid block signer') |
| 48 | + |
| 49 | + def validate_business_logic(self): |
| 50 | + pass |
| 51 | + |
| 52 | + def validate_blockchain_state_dependent(self, blockchain_facade): |
| 53 | + self.validate_signer(blockchain_facade) |
0 commit comments