Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(verification): implement verify_minted_tokens [part 3/9] #832

Merged
Merged
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
7 changes: 2 additions & 5 deletions hathor/verification/token_creation_transaction_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from hathor.transaction import Transaction
from hathor.transaction.exceptions import InvalidToken, TransactionDataError
from hathor.transaction.token_creation_tx import TokenCreationTransaction
from hathor.transaction.util import clean_token_string
Expand All @@ -29,26 +28,24 @@ def verify(self, tx: TokenCreationTransaction, *, reject_locked_reward: bool = T
We also overload verify_sum to make some different checks
"""
super().verify(tx, reject_locked_reward=reject_locked_reward)
self.verify_minted_tokens(tx)
self.verify_token_info(tx)

def verify_sum(self, tx: Transaction) -> None:
def verify_minted_tokens(self, tx: TokenCreationTransaction) -> None:
""" Besides all checks made on regular transactions, a few extra ones are made:
- only HTR tokens on the inputs;
- new tokens are actually being minted;

:raises InvalidToken: when there's an error in token operations
:raises InputOutputMismatch: if sum of inputs is not equal to outputs and there's no mint/melt
"""
assert isinstance(tx, TokenCreationTransaction)
token_dict = tx.get_complete_token_info()

# make sure tokens are being minted
token_info = token_dict[not_none(tx.hash)]
if token_info.amount <= 0:
raise InvalidToken('Token creation transaction must mint new tokens')

super().verify_sum(tx)

def verify_token_info(self, tx: TokenCreationTransaction) -> None:
""" Validates token info
"""
Expand Down
6 changes: 6 additions & 0 deletions tests/tx/test_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ def test_token_creation_transaction_verify(self) -> None:
verify_reward_locked_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_reward_locked)

verify_token_info_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_token_info)
verify_minted_tokens_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_minted_tokens)

with (
patch.object(TokenCreationTransactionVerifier, 'verify_pow', verify_pow_wrapped),
Expand All @@ -795,6 +796,7 @@ def test_token_creation_transaction_verify(self) -> None:
patch.object(TokenCreationTransactionVerifier, 'verify_sum', verify_sum_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_reward_locked', verify_reward_locked_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_token_info', verify_token_info_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_minted_tokens', verify_minted_tokens_wrapped),
):
self.manager.verification_service.verify(tx)

Expand All @@ -813,6 +815,7 @@ def test_token_creation_transaction_verify(self) -> None:

# TokenCreationTransaction methods
verify_token_info_wrapped.assert_called_once()
verify_minted_tokens_wrapped.assert_called_once()

def test_token_creation_transaction_validate_basic(self) -> None:
tx = self._get_valid_token_creation_tx()
Expand Down Expand Up @@ -907,6 +910,7 @@ def test_token_creation_transaction_validate_full(self) -> None:
verify_reward_locked_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_reward_locked)

verify_token_info_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_token_info)
verify_minted_tokens_wrapped = Mock(wraps=self.verifiers.token_creation_tx.verify_minted_tokens)

with (
patch.object(TokenCreationTransactionVerifier, 'verify_parents_basic', verify_parents_basic_wrapped),
Expand All @@ -924,6 +928,7 @@ def test_token_creation_transaction_validate_full(self) -> None:
patch.object(TokenCreationTransactionVerifier, 'verify_sum', verify_sum_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_reward_locked', verify_reward_locked_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_token_info', verify_token_info_wrapped),
patch.object(TokenCreationTransactionVerifier, 'verify_minted_tokens', verify_minted_tokens_wrapped),
):
self.manager.verification_service.validate_full(tx)

Expand All @@ -944,6 +949,7 @@ def test_token_creation_transaction_validate_full(self) -> None:

# TokenCreationTransaction methods
verify_token_info_wrapped.assert_called_once()
verify_minted_tokens_wrapped.assert_called_once()


class SyncV1VerificationTest(unittest.SyncV1Params, BaseVerificationTest):
Expand Down