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

feat: Given a day of the voting date find the hour that maximizes the probability that the upgrade happens during working hours #12204

Merged
merged 25 commits into from
Oct 28, 2024
Merged
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
64 changes: 61 additions & 3 deletions debug_scripts/estimate_epoch_start_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,54 @@ def find_epoch_for_timestamp(future_epochs, voting_timestamp):
return len(future_epochs)


def find_best_voting_hour(voting_date_str, future_epochs):
# Working hours during which there are available NEAR engineers
WORKING_HOURS_START = 8 # 8:00 UTC
VanBarbascu marked this conversation as resolved.
Show resolved Hide resolved
WORKING_HOURS_END = 22 # 22:00 UTC

valid_hours = []

for hour in range(24):
# Construct datetime for each hour of the voting date
voting_datetime = datetime.strptime(
f"{voting_date_str} {hour:02d}:00:00", '%Y-%m-%d %H:%M:%S')
voting_datetime = pytz.utc.localize(voting_datetime)
voting_timestamp = voting_datetime.timestamp()

# Find the epoch T in which the voting date falls
epoch_T = find_epoch_for_timestamp(future_epochs, voting_timestamp)
if epoch_T <= 0:
continue # Voting date is before the first predicted epoch

# Calculate when the protocol upgrade will happen (start of epoch T+2)
protocol_upgrade_epoch_number = epoch_T + 2
if protocol_upgrade_epoch_number > len(future_epochs):
print(
"Not enough future epochs predicted to determine all the protocol upgrade times."
)
break

protocol_upgrade_timestamp = future_epochs[protocol_upgrade_epoch_number
- 1]
protocol_upgrade_datetime = datetime.fromtimestamp(
protocol_upgrade_timestamp)
upgrade_hour_utc = protocol_upgrade_datetime.hour

if WORKING_HOURS_START <= upgrade_hour_utc < WORKING_HOURS_END:
stedfn marked this conversation as resolved.
Show resolved Hide resolved
valid_hours.append((hour, protocol_upgrade_epoch_number))

if valid_hours:
print(
f"\nVoting hours on {voting_date_str} UTC that result in upgrade during working hours (UTC {WORKING_HOURS_START}:00-{WORKING_HOURS_END}:00):"
)
for (hour, epoch) in valid_hours:
print(f"- {hour:02d}:00, Upgrade Epoch: {epoch}")
else:
print(
"\nNo voting hours on the given date result in an upgrade during working hours."
)


def valid_voting_datetime(s):
try:
dt = datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
Expand Down Expand Up @@ -178,6 +226,8 @@ def main(args):
if args.voting_date:
find_protocol_upgrade_time(args.voting_date, future_epochs,
args.timezone)
elif args.voting_date_day:
find_best_voting_hour(args.voting_date_day, future_epochs)


# Custom action to set the URL based on chain_id
Expand Down Expand Up @@ -222,9 +272,17 @@ def __call__(self, parser, namespace, values, option_string=None):
type=valid_timezone,
default="UTC",
help="Time zone to display times in (e.g., 'America/New_York').")
parser.add_argument("--voting_date",
type=valid_voting_datetime,
help="Voting date in 'YYYY-MM-DD HH:MM:SS' format.")
# Voting date arguments
voting_group = parser.add_mutually_exclusive_group()
voting_group.add_argument(
"--voting_date",
type=valid_voting_datetime,
help="Voting date in 'YYYY-MM-DD HH:MM:SS' format.")
voting_group.add_argument(
"--voting_date_day",
help=
"Voting date (day) in 'YYYY-MM-DD' format to find voting hours resulting in upgrade during working hours."
)

args = parser.parse_args()
main(args)
Loading