Files
node/vms/platformvm/config/config_test.go
T
Hanzo AI c3b398bc7b json: migrate every encoding/json import to json/v2 (go-json-experiment)
External (HTTP / JSON-RPC) is the only place JSON is legitimate. Every
existing encoding/json import in node/ moves to github.com/go-json-experiment/json
(v2 root, not the v1 sub-package). NewEncoder/NewDecoder rewrite to
MarshalWrite/UnmarshalRead. MarshalIndent rewrites to Marshal with
jsontext.WithIndent. json.RawMessage rewrites to jsontext.Value.
*json.SyntaxError rewrites to *jsontext.SyntacticError.

81 files migrated. LLM.md captures the rule + v1->v2 delta table.

Known v2 semantic deltas surfaced by existing tests (followups, not regressions):
- [N]byte fields with no MarshalJSON now marshal as base64 string (v1 marshalled
  as JSON array of byte numbers). Affects vms/platformvm/txs/*_test.go fixtures
  with embedded BLS proofOfPossession.
- time.Duration has no v2 default representation; configs that wire-format
  Duration as nanoseconds (vms/{xvm,platformvm}/config, config/spec) need to
  switch to string-form Duration or carry an explicit option. v2 root does not
  re-export FormatDurationAsNano.
- v2 enforces strict UTF-8 (vms/chainadapter/messaging fixture has non-UTF-8).
- json.MarshalWrite does not append a trailing '\n' (v1 NewEncoder.Encode did);
  service/auth/auth_test.go expectation updated.
- nil []byte round-trips to empty (not nil); config_test deep-equal fixtures
  surface this.

All affected sites are at the API boundary; ZAP wire envelope already covers
the internal data paths (state, P2P, consensus, MPC, threshold). Internal
JSON sites that should move to ZAP next (separate work):
- vms/da/store.go            (DA blob/cert storage as JSON)
- vms/platformvm/airdrop     (airdrop claims as JSON in db)
- vms/chainadapter/appchain  (SQLite materializer schema/data blobs)
- vms/chainadapter/messaging (conversation codec)
- staking/kms.go             (KMS HTTP client — external technically, leave)
- utils/{bimap,ips}          (small marshaler shims — low priority)
2026-06-06 22:26:02 -07:00

130 lines
3.8 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package config
import (
"github.com/go-json-experiment/json"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/luxfi/ids"
"github.com/luxfi/math/set"
)
// Requires all values in a struct to be initialized
func verifyInitializedStruct(tb testing.TB, s interface{}) {
tb.Helper()
require := require.New(tb)
structType := reflect.TypeOf(s)
require.Equal(reflect.Struct, structType.Kind())
v := reflect.ValueOf(s)
for i := 0; i < v.NumField(); i++ {
fieldType := structType.Field(i)
field := v.Field(i)
// Skip fields with json:"-" tag as they aren't part of serialization
jsonTag := fieldType.Tag.Get("json")
if jsonTag == "-" {
continue
}
require.True(field.IsValid(), "invalid field: ", fieldType.Name)
require.False(field.IsZero(), "zero field: ", fieldType.Name)
}
}
func TestConfigUnmarshal(t *testing.T) {
t.Run("default values from empty json", func(t *testing.T) {
require := require.New(t)
b := []byte(`{}`)
ec, err := GetConfig(b)
require.NoError(err)
require.Equal(&Default, ec)
})
t.Run("default values from empty bytes", func(t *testing.T) {
require := require.New(t)
b := []byte(``)
ec, err := GetConfig(b)
require.NoError(err)
require.Equal(&Default, ec)
})
t.Run("mix default and extracted values from json", func(t *testing.T) {
require := require.New(t)
b := []byte(`{"block-cache-size":1}`)
ec, err := GetConfig(b)
require.NoError(err)
expected := Default
expected.BlockCacheSize = 1
require.Equal(&expected, ec)
})
t.Run("all values extracted from json", func(t *testing.T) {
require := require.New(t)
trackedChains := set.NewSet[ids.ID](1)
trackedChains.Add(ids.ID{1, 2, 3})
expected := &Config{
Network: Network{
MaxValidatorSetStaleness: 1,
TargetGossipSize: 2,
PushGossipPercentStake: .3,
PushGossipNumValidators: 4,
PushGossipNumPeers: 5,
PushRegossipNumValidators: 6,
PushRegossipNumPeers: 7,
PushGossipDiscardedCacheSize: 8,
PushGossipMaxRegossipFrequency: 9,
PushGossipFrequency: 10,
PullGossipPollSize: 11,
PullGossipFrequency: 12,
PullGossipThrottlingPeriod: 13,
PullGossipThrottlingLimit: 14,
ExpectedBloomFilterElements: 15,
ExpectedBloomFilterFalsePositiveProbability: 16,
MaxBloomFilterFalsePositiveProbability: 17,
},
BlockCacheSize: 1,
TxCacheSize: 2,
TransformedNetTxCacheSize: 3,
RewardUTXOsCacheSize: 5,
ChainCacheSize: 6,
ChainDBCacheSize: 7,
BlockIDCacheSize: 8,
FxOwnerCacheSize: 9,
NetToL1ConversionCacheSize: 10,
L1WeightsCacheSize: 11,
L1InactiveValidatorsCacheSize: 12,
L1ChainIDNodeIDCacheSize: 13,
ChecksumsEnabled: true,
SybilProtectionEnabled: true,
TrackedChains: trackedChains,
MempoolPruneFrequency: time.Minute,
TxFee: 14,
CreateAssetTxFee: 15,
CreateNetworkTxFee: 16,
CreateChainTxFee: 17,
AddNetworkValidatorFee: 18,
AddNetworkDelegatorFee: 19,
}
verifyInitializedStruct(t, *expected)
verifyInitializedStruct(t, expected.Network)
b, err := json.Marshal(expected)
require.NoError(err)
actual, err := GetConfig(b)
require.NoError(err)
require.Equal(expected, actual)
})
}