Randomise transactions on Debug.

This commit is contained in:
Alejandro Cabeza Romero 2025-10-16 13:35:50 +02:00
parent 9fba016060
commit f3aaff84c6
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD

View File

@ -1,6 +1,9 @@
import logging
import os
import random
from typing import List
from typing import Any, List, Self
from pydantic.config import ExtraValues
from sqlalchemy import Column
from sqlmodel import Field
@ -10,6 +13,15 @@ from node.models.transactions import Transaction
from utils.random import random_hash
def _is_debug__randomize_transactions():
is_debug = os.getenv("DEBUG", "False").lower() == "true"
is_debug__randomize_transactions = os.getenv("DEBUG__RANDOMIZE_TRANSACTIONS", "False").lower() == "true"
return is_debug and is_debug__randomize_transactions
logger = logging.getLogger(__name__)
class Public(NbeSchema):
aged_root: str
epoch_nonce: str
@ -84,7 +96,27 @@ class Block(TimestampedModel, table=True):
return f"Block(slot={self.slot})"
def __repr__(self) -> str:
return f"<Block(id={self.id}, created_at={self.created_at}, slot={self.slot}, parent={self.header['parent_block']})>"
return f"<Block(id={self.id}, created_at={self.created_at}, slot={self.slot}, parent={self.header["parent_block"]})>"
@classmethod
def model_validate_json(
cls,
json_data: str | bytes | bytearray,
*,
strict: bool | None = None,
extra: ExtraValues | None = None,
context: Any | None = None,
by_alias: bool | None = None,
by_name: bool | None = None,
) -> Self:
self = super().model_validate_json(
json_data, strict=strict, extra=extra, context=context, by_alias=by_alias, by_name=by_name
)
if _is_debug__randomize_transactions():
logger.debug("DEBUG and DEBUG__RANDOMIZE_TRANSACTIONS is enabled, randomizing Block's transactions.")
n = 0 if random.randint(0, 1) <= 0.5 else random.randint(1, 10)
self.transactions = [Transaction.from_random() for _ in range(n)]
return self
@classmethod
def from_random(cls, slot_from: int = 1, slot_to: int = 100) -> "Block":