-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(verification): create vertex verifiers
- Loading branch information
Showing
10 changed files
with
227 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.profiler import get_cpu_profiler | ||
from hathor.transaction import Block | ||
from hathor.verification.vertex_verifier import VertexVerifier | ||
|
||
cpu = get_cpu_profiler() | ||
|
||
|
||
class BlockVerifier(VertexVerifier): | ||
__slots__ = () | ||
|
||
def verify_basic(self, block: Block, *, skip_block_weight_verification: bool = False) -> None: | ||
"""Partially run validations, the ones that need parents/inputs are skipped.""" | ||
if not skip_block_weight_verification: | ||
block.verify_weight() | ||
block.verify_reward() | ||
|
||
@cpu.profiler(key=lambda _, block: 'block-verify!{}'.format(block.hash.hex())) | ||
def verify(self, block: Block) -> None: | ||
""" | ||
(1) confirms at least two pending transactions and references last block | ||
(2) solves the pow with the correct weight (done in HathorManager) | ||
(3) creates the correct amount of tokens in the output (done in HathorManager) | ||
(4) all parents must exist and have timestamp smaller than ours | ||
(5) data field must contain at most BLOCK_DATA_MAX_SIZE bytes | ||
""" | ||
# TODO Should we validate a limit of outputs? | ||
if block.is_genesis: | ||
# TODO do genesis validation | ||
return | ||
|
||
block.verify_without_storage() | ||
|
||
# (1) and (4) | ||
block.verify_parents() | ||
|
||
block.verify_height() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.verification.block_verifier import BlockVerifier | ||
|
||
|
||
class MergeMinedBlockVerifier(BlockVerifier): | ||
__slots__ = () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.profiler import get_cpu_profiler | ||
from hathor.transaction import Transaction | ||
from hathor.verification.vertex_verifier import VertexVerifier | ||
|
||
cpu = get_cpu_profiler() | ||
|
||
|
||
class TransactionVerifier(VertexVerifier): | ||
__slots__ = () | ||
|
||
def verify_basic(self, tx: Transaction) -> None: | ||
"""Partially run validations, the ones that need parents/inputs are skipped.""" | ||
if tx.is_genesis: | ||
# TODO do genesis validation? | ||
return | ||
tx.verify_parents_basic() | ||
tx.verify_weight() | ||
tx.verify_without_storage() | ||
|
||
@cpu.profiler(key=lambda _, tx: 'tx-verify!{}'.format(tx.hash.hex())) | ||
def verify(self, tx: Transaction, *, reject_locked_reward: bool = True) -> None: | ||
""" Common verification for all transactions: | ||
(i) number of inputs is at most 256 | ||
(ii) number of outputs is at most 256 | ||
(iii) confirms at least two pending transactions | ||
(iv) solves the pow (we verify weight is correct in HathorManager) | ||
(v) validates signature of inputs | ||
(vi) validates public key and output (of the inputs) addresses | ||
(vii) validate that both parents are valid | ||
(viii) validate input's timestamps | ||
(ix) validate inputs and outputs sum | ||
""" | ||
if tx.is_genesis: | ||
# TODO do genesis validation | ||
return | ||
tx.verify_without_storage() | ||
tx.verify_sigops_input() | ||
tx.verify_inputs() # need to run verify_inputs first to check if all inputs exist | ||
tx.verify_parents() | ||
tx.verify_sum() | ||
if reject_locked_reward: | ||
tx.verify_reward_locked() |
Oops, something went wrong.