diff --git a/hathor/transaction/base_transaction.py b/hathor/transaction/base_transaction.py index 532538969..958c59c05 100644 --- a/hathor/transaction/base_transaction.py +++ b/hathor/transaction/base_transaction.py @@ -707,6 +707,7 @@ def update_initial_metadata(self, *, save: bool = True) -> None: self._update_height_metadata() self._update_parents_children_metadata() self._update_reward_lock_metadata() + self._update_feature_activation_bit_counts() if save: assert self.storage is not None self.storage.save_transaction(self, only_metadata=True) @@ -732,6 +733,15 @@ def _update_parents_children_metadata(self) -> None: metadata.children.append(self.hash) self.storage.save_transaction(parent, only_metadata=True) + def _update_feature_activation_bit_counts(self) -> None: + """Update the block's feature_activation_bit_counts.""" + if not self.is_block: + return + from hathor.transaction import Block + assert isinstance(self, Block) + # This method lazily calculates and stores the value in metadata + self.get_feature_activation_bit_counts() + def update_timestamp(self, now: int) -> None: """Update this tx's timestamp diff --git a/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata.py b/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata.py index e231fdf46..20c93b3b5 100644 --- a/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata.py +++ b/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata.py @@ -17,7 +17,6 @@ from structlog import get_logger from hathor.transaction.storage.migrations import BaseMigration -from hathor.util import progress if TYPE_CHECKING: from hathor.transaction.storage import TransactionStorage @@ -33,9 +32,6 @@ def get_db_name(self) -> str: return 'add_feature_activation_bit_counts_metadata' def run(self, storage: 'TransactionStorage') -> None: + # We can skip this migration as it will run again in `add_feature_activation_bit_counts_metadata2`. log = logger.new() - topological_iterator = storage.topological_iterator() - - for vertex in progress(topological_iterator, log=log, total=None): - if vertex.is_block: - vertex.update_initial_metadata() + log.info('Skipping unnecessary migration.') diff --git a/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata2.py b/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata2.py new file mode 100644 index 000000000..eb59daa6b --- /dev/null +++ b/hathor/transaction/storage/migrations/add_feature_activation_bit_counts_metadata2.py @@ -0,0 +1,41 @@ +# 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 typing import TYPE_CHECKING + +from structlog import get_logger + +from hathor.transaction.storage.migrations import BaseMigration +from hathor.util import progress + +if TYPE_CHECKING: + from hathor.transaction.storage import TransactionStorage + +logger = get_logger() + + +class Migration(BaseMigration): + def skip_empty_db(self) -> bool: + return True + + def get_db_name(self) -> str: + return 'add_feature_activation_bit_counts_metadata2' + + def run(self, storage: 'TransactionStorage') -> None: + log = logger.new() + topological_iterator = storage.topological_iterator() + + for vertex in progress(topological_iterator, log=log, total=None): + if vertex.is_block: + vertex.update_initial_metadata() diff --git a/hathor/transaction/storage/transaction_storage.py b/hathor/transaction/storage/transaction_storage.py index 8b441ed31..4a94f98f9 100644 --- a/hathor/transaction/storage/transaction_storage.py +++ b/hathor/transaction/storage/transaction_storage.py @@ -39,6 +39,7 @@ BaseMigration, MigrationState, add_feature_activation_bit_counts_metadata, + add_feature_activation_bit_counts_metadata2, add_min_height_metadata, remove_first_nop_features, ) @@ -89,7 +90,8 @@ class TransactionStorage(ABC): _migration_factories: list[type[BaseMigration]] = [ add_min_height_metadata.Migration, add_feature_activation_bit_counts_metadata.Migration, - remove_first_nop_features.Migration + remove_first_nop_features.Migration, + add_feature_activation_bit_counts_metadata2.Migration, ] _migrations: list[BaseMigration]