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 <charon@lethean.io>
This commit is contained in:
Claude 2026-03-16 20:51:54 +00:00
parent 18ceb7fa26
commit 9631efa5a8
No known key found for this signature in database
GPG key ID: AF404715446AEB41
2 changed files with 61 additions and 0 deletions

View file

@ -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
}

View file

@ -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")
}
}