Files
node/vms/platformvm/client_permissionless_validator.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

192 lines
5.9 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package platformvm
import (
"github.com/go-json-experiment/json"
"github.com/luxfi/address"
"github.com/luxfi/ids"
"github.com/luxfi/node/vms/platformvm/api"
"github.com/luxfi/node/vms/platformvm/signer"
)
// ClientStaker is the representation of a staker sent via client.
type ClientStaker struct {
// the txID of the transaction that added this staker.
TxID ids.ID
// the Unix time when they start staking
StartTime uint64
// the Unix time when they are done staking
EndTime uint64
// the validator weight when sampling validators
Weight uint64
// the node ID of the staker
NodeID ids.NodeID
}
// ClientOwner is the repr. of a reward owner sent over client
type ClientOwner struct {
Locktime uint64
Threshold uint32
Addresses []ids.ShortID
}
type ClientL1Validator struct {
ValidationID *ids.ID
RemainingBalanceOwner *ClientOwner
DeactivationOwner *ClientOwner
MinNonce *uint64
Balance *uint64
}
// ClientPermissionlessValidator is the repr. of a permissionless validator sent
// over client
type ClientPermissionlessValidator struct {
ClientStaker
ClientL1Validator
ValidationRewardOwner *ClientOwner
DelegationRewardOwner *ClientOwner
PotentialReward *uint64
AccruedDelegateeReward *uint64
DelegationFee float32
// Uptime is deprecated for Net Validators.
// It will be available only for Primary Network Validators.
Uptime *float32
// Connected is deprecated for Net Validators.
// It will be available only for Primary Network Validators.
Connected *bool
Signer *signer.ProofOfPossession
// The delegators delegating to this validator
DelegatorCount *uint64
DelegatorWeight *uint64
Delegators []ClientDelegator
}
// ClientDelegator is the repr. of a delegator sent over client
type ClientDelegator struct {
ClientStaker
RewardOwner *ClientOwner
PotentialReward *uint64
}
func apiStakerToClientStaker(validator api.Staker) ClientStaker {
return ClientStaker{
TxID: validator.TxID,
StartTime: uint64(validator.StartTime),
EndTime: uint64(validator.EndTime),
Weight: uint64(validator.Weight),
NodeID: validator.NodeID,
}
}
func apiOwnerToClientOwner(rewardOwner *api.Owner) (*ClientOwner, error) {
if rewardOwner == nil {
return nil, nil
}
addrs, err := address.ParseToIDs(rewardOwner.Addresses)
return &ClientOwner{
Locktime: uint64(rewardOwner.Locktime),
Threshold: uint32(rewardOwner.Threshold),
Addresses: addrs,
}, err
}
func getClientPermissionlessValidators(validatorsSliceIntf []interface{}) ([]ClientPermissionlessValidator, error) {
clientValidators := make([]ClientPermissionlessValidator, len(validatorsSliceIntf))
for i, validatorMapIntf := range validatorsSliceIntf {
validatorMapJSON, err := json.Marshal(validatorMapIntf)
if err != nil {
return nil, err
}
var apiValidator api.PermissionlessValidator
err = json.Unmarshal(validatorMapJSON, &apiValidator)
if err != nil {
return nil, err
}
clientValidator, err := getClientPrimaryOrNetValidator(apiValidator)
if err != nil {
return nil, err
}
// If the validator is a L1 validator, we need to set the L1 fields as well
if apiValidator.ValidationID != nil {
l1Validator, err := getClientL1Validator(apiValidator)
if err != nil {
return nil, err
}
clientValidator.ClientL1Validator = l1Validator
}
clientValidators[i] = clientValidator
}
return clientValidators, nil
}
func getClientL1Validator(apiValidator api.PermissionlessValidator) (ClientL1Validator, error) {
remainingBalanceOwner, err := apiOwnerToClientOwner(apiValidator.RemainingBalanceOwner)
if err != nil {
return ClientL1Validator{}, err
}
deactivationOwner, err := apiOwnerToClientOwner(apiValidator.DeactivationOwner)
if err != nil {
return ClientL1Validator{}, err
}
return ClientL1Validator{
ValidationID: apiValidator.ValidationID,
RemainingBalanceOwner: remainingBalanceOwner,
DeactivationOwner: deactivationOwner,
MinNonce: (*uint64)(apiValidator.MinNonce),
Balance: (*uint64)(apiValidator.Balance),
}, nil
}
func getClientPrimaryOrNetValidator(apiValidator api.PermissionlessValidator) (ClientPermissionlessValidator, error) {
validationRewardOwner, err := apiOwnerToClientOwner(apiValidator.ValidationRewardOwner)
if err != nil {
return ClientPermissionlessValidator{}, err
}
delegationRewardOwner, err := apiOwnerToClientOwner(apiValidator.DelegationRewardOwner)
if err != nil {
return ClientPermissionlessValidator{}, err
}
var clientDelegators []ClientDelegator
if apiValidator.Delegators != nil {
clientDelegators = make([]ClientDelegator, len(*apiValidator.Delegators))
for j, apiDelegator := range *apiValidator.Delegators {
rewardOwner, err := apiOwnerToClientOwner(apiDelegator.RewardOwner)
if err != nil {
return ClientPermissionlessValidator{}, err
}
clientDelegators[j] = ClientDelegator{
ClientStaker: apiStakerToClientStaker(apiDelegator.Staker),
RewardOwner: rewardOwner,
PotentialReward: (*uint64)(apiDelegator.PotentialReward),
}
}
}
return ClientPermissionlessValidator{
ClientStaker: apiStakerToClientStaker(apiValidator.Staker),
ValidationRewardOwner: validationRewardOwner,
DelegationRewardOwner: delegationRewardOwner,
PotentialReward: (*uint64)(apiValidator.PotentialReward),
AccruedDelegateeReward: (*uint64)(apiValidator.AccruedDelegateeReward),
DelegationFee: float32(apiValidator.DelegationFee),
Uptime: (*float32)(apiValidator.Uptime),
Connected: apiValidator.Connected,
Signer: apiValidator.Signer,
DelegatorCount: (*uint64)(apiValidator.DelegatorCount),
DelegatorWeight: (*uint64)(apiValidator.DelegatorWeight),
Delegators: clientDelegators,
}, nil
}