From 9631efa5a8fbcc8cecd8b351e369ff30955ee7ab Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Mar 2026 20:51:54 +0000 Subject: [PATCH] feat(config): add HardforkActivationHeight helper Returns the raw activation height for a given hardfork version. Needed by the pre-hardfork transaction freeze logic. Co-Authored-By: Charon --- config/hardfork.go | 12 ++++++++ config/hardfork_activation_test.go | 49 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 config/hardfork_activation_test.go diff --git a/config/hardfork.go b/config/hardfork.go index 957f64e..4792025 100644 --- a/config/hardfork.go +++ b/config/hardfork.go @@ -93,3 +93,15 @@ func IsHardForkActive(forks []HardFork, version uint8, height uint64) bool { } return false } + +// HardforkActivationHeight returns the activation height for the given +// hardfork version. The fork becomes active at heights strictly greater +// than the returned value. Returns (0, false) if the version is not found. +func HardforkActivationHeight(forks []HardFork, version uint8) (uint64, bool) { + for _, hf := range forks { + if hf.Version == version { + return hf.Height, true + } + } + return 0, false +} diff --git a/config/hardfork_activation_test.go b/config/hardfork_activation_test.go new file mode 100644 index 0000000..6863453 --- /dev/null +++ b/config/hardfork_activation_test.go @@ -0,0 +1,49 @@ +// Copyright (c) 2017-2026 Lethean (https://lt.hn) +// +// Licensed under the European Union Public Licence (EUPL) version 1.2. +// SPDX-License-Identifier: EUPL-1.2 + +package config + +import "testing" + +func TestHardforkActivationHeight_Good(t *testing.T) { + tests := []struct { + name string + forks []HardFork + version uint8 + want uint64 + wantOK bool + }{ + {"mainnet_hf5", MainnetForks, HF5, 999999999, true}, + {"testnet_hf5", TestnetForks, HF5, 200, true}, + {"testnet_hf4", TestnetForks, HF4Zarcanum, 100, true}, + {"mainnet_hf0", MainnetForks, HF0Initial, 0, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := HardforkActivationHeight(tt.forks, tt.version) + if ok != tt.wantOK { + t.Fatalf("HardforkActivationHeight ok = %v, want %v", ok, tt.wantOK) + } + if got != tt.want { + t.Errorf("HardforkActivationHeight = %d, want %d", got, tt.want) + } + }) + } +} + +func TestHardforkActivationHeight_Bad(t *testing.T) { + _, ok := HardforkActivationHeight(MainnetForks, 99) + if ok { + t.Error("HardforkActivationHeight with unknown version should return false") + } +} + +func TestHardforkActivationHeight_Ugly(t *testing.T) { + _, ok := HardforkActivationHeight(nil, HF5) + if ok { + t.Error("HardforkActivationHeight with nil forks should return false") + } +}