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:
parent
18ceb7fa26
commit
9631efa5a8
2 changed files with 61 additions and 0 deletions
|
|
@ -93,3 +93,15 @@ func IsHardForkActive(forks []HardFork, version uint8, height uint64) bool {
|
||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
49
config/hardfork_activation_test.go
Normal file
49
config/hardfork_activation_test.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue