Skip to content

Commit dc55893

Browse files
committed
refactor(verification): refactor verify_sum
1 parent 1171e10 commit dc55893

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

hathor/transaction/token_creation_tx.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
from struct import error as StructError, pack
1616
from typing import Any, Optional
1717

18+
from typing_extensions import override
19+
1820
from hathor.transaction.base_transaction import TxInput, TxOutput, TxVersion
1921
from hathor.transaction.storage import TransactionStorage # noqa: F401
20-
from hathor.transaction.transaction import Transaction
22+
from hathor.transaction.transaction import TokenInfo, Transaction
2123
from hathor.transaction.util import VerboseCallback, int_to_bytes, unpack, unpack_len
24+
from hathor.types import TokenUid
2225

2326
# Signal bits (B), version (B), inputs len (B), outputs len (B)
2427
_FUNDS_FORMAT_STRING = '!BBBB'
@@ -213,6 +216,16 @@ def to_json_extended(self) -> dict[str, Any]:
213216
json['tokens'] = []
214217
return json
215218

219+
@override
220+
def get_token_info_from_inputs(self) -> dict[TokenUid, TokenInfo]:
221+
token_dict = super().get_token_info_from_inputs()
222+
223+
# we add the created token's info to token_dict, as the creation tx allows for mint/melt
224+
assert self.hash is not None
225+
token_dict[self.hash] = TokenInfo(0, True, True)
226+
227+
return token_dict
228+
216229

217230
def decode_string_utf8(encoded: bytes, key: str) -> str:
218231
""" Raises StructError in case it's not a valid utf-8 string

hathor/verification/token_creation_transaction_verifier.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from hathor.transaction import Transaction
1516
from hathor.transaction.exceptions import InvalidToken, TransactionDataError
1617
from hathor.transaction.token_creation_tx import TokenCreationTransaction
17-
from hathor.transaction.transaction import TokenInfo, Transaction
1818
from hathor.transaction.util import clean_token_string
19+
from hathor.util import not_none
1920
from hathor.verification.transaction_verifier import TransactionVerifier
2021

2122

@@ -39,20 +40,14 @@ def verify_sum(self, tx: Transaction) -> None:
3940
:raises InputOutputMismatch: if sum of inputs is not equal to outputs and there's no mint/melt
4041
"""
4142
assert isinstance(tx, TokenCreationTransaction)
42-
token_dict = tx.get_token_info_from_inputs()
43-
44-
# we add the created token's info to token_dict, as the creation tx allows for mint/melt
45-
assert tx.hash is not None
46-
token_dict[tx.hash] = TokenInfo(0, True, True)
47-
48-
self.update_token_info_from_outputs(tx, token_dict=token_dict)
43+
token_dict = self.get_complete_token_info(tx)
4944

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

55-
self.verify_authorities_and_deposit(token_dict)
50+
super().verify_sum(tx)
5651

5752
def verify_token_info(self, tx: TokenCreationTransaction) -> None:
5853
""" Validates token info

hathor/verification/transaction_verifier.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ def verify_sum(self, tx: Transaction) -> None:
186186
:raises InvalidToken: when there's an error in token operations
187187
:raises InputOutputMismatch: if sum of inputs is not equal to outputs and there's no mint/melt
188188
"""
189-
token_dict = tx.get_token_info_from_inputs()
190-
self.update_token_info_from_outputs(tx, token_dict=token_dict)
189+
token_dict = self.get_complete_token_info(tx)
191190
self.verify_authorities_and_deposit(token_dict)
192191

193192
def verify_reward_locked(self, tx: Transaction) -> None:
@@ -290,3 +289,12 @@ def update_token_info_from_outputs(self, tx: Transaction, *, token_dict: dict[To
290289
# for regular outputs, just subtract from the total amount
291290
sum_tokens = token_info.amount + tx_output.value
292291
token_dict[token_uid] = TokenInfo(sum_tokens, token_info.can_mint, token_info.can_melt)
292+
293+
def get_complete_token_info(self, tx: Transaction) -> dict[TokenUid, TokenInfo]:
294+
"""
295+
Get a complete token info dict, including data from both inputs and outputs.
296+
"""
297+
token_dict = tx.get_token_info_from_inputs()
298+
self.update_token_info_from_outputs(tx, token_dict=token_dict)
299+
300+
return token_dict

0 commit comments

Comments
 (0)