Skip to content

Commit

Permalink
fix(feature-activation): add missing metadata migration (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco authored Jan 29, 2024
1 parent f651e2a commit b6748ee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
10 changes: 10 additions & 0 deletions hathor/transaction/base_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.')
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion hathor/transaction/storage/transaction_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit b6748ee

Please sign in to comment.