Files
proto/p/txs/add_permissionless_delegator_tx.go
zeekay f90e6bb5bb refactor(p,x): generic math.Add/Mul, Codec-free sort, LockOut.Bytes
- safemath.Add64/Mul64 → Add/Mul (Go generics; same wire/bounds
  behaviour, fewer per-width copies). Sweep across p/{reward,utxo,txs}
  and the x-side test helpers.
- lux.SortTransferableOutputs / SyntacticVerifier UTXO/Output helpers:
  drop the threaded Codec arg; sort key is now Bytes()+assetID, no
  codec roundtrip per compare. Callers in p/utxo/handler.go,
  p/txs/{add_*,base_tx,builder,executor/*}, p/state, and
  x/txs/executor/syntactic_verifier follow.
- stakeable.LockOut: implement Bytes() returning
  0xFF || BE(Locktime) || inner.Bytes(). 0xFF marker keeps locked
  outputs sorted strictly AFTER unlocked fxs primitives, preserving
  the legacy ordering invariant the SyntacticVerify expects.
- Update test helpers in p/block/{builder,executor}, p/network, p/txs
  to construct outputs without the codec arg and rely on the new sort.
2026-06-04 17:05:03 -07:00

151 lines
4.4 KiB
Go

// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"context"
"fmt"
"github.com/luxfi/constants"
"github.com/luxfi/crypto/bls"
"github.com/luxfi/ids"
safemath "github.com/luxfi/math"
"github.com/luxfi/proto/p/fx"
"github.com/luxfi/runtime"
lux "github.com/luxfi/utxo"
"github.com/luxfi/utxo/secp256k1fx"
"github.com/luxfi/vm/components/verify"
)
var (
_ DelegatorTx = (*AddPermissionlessDelegatorTx)(nil)
_ ScheduledStaker = (*AddPermissionlessDelegatorTx)(nil)
)
// AddPermissionlessDelegatorTx is an unsigned addPermissionlessDelegatorTx
type AddPermissionlessDelegatorTx struct {
// Metadata, inputs and outputs
BaseTx `serialize:"true"`
// Describes the validator
Validator `serialize:"true" json:"validator"`
// ID of the chain this validator is validating
Chain ids.ID `serialize:"true" json:"chainID"`
// Where to send staked tokens when done validating
StakeOuts []*lux.TransferableOutput `serialize:"true" json:"stake"`
// Where to send staking rewards when done validating
DelegationRewardsOwner fx.Owner `serialize:"true" json:"rewardsOwner"`
}
// InitRuntime sets the FxID fields in the inputs and outputs of this
// [AddPermissionlessDelegatorTx]. Also sets the [ctx] to the given [vm.ctx] so
// that the addresses can be json marshalled into human readable format
func (tx *AddPermissionlessDelegatorTx) InitRuntime(rt *runtime.Runtime) {
tx.BaseTx.InitRuntime(rt)
for _, out := range tx.StakeOuts {
out.FxID = secp256k1fx.ID
out.InitRuntime(rt)
}
// Owner doesn't have InitRuntime method
}
func (tx *AddPermissionlessDelegatorTx) ChainID() ids.ID {
return tx.Chain
}
func (tx *AddPermissionlessDelegatorTx) NodeID() ids.NodeID {
return tx.Validator.NodeID
}
func (*AddPermissionlessDelegatorTx) PublicKey() (*bls.PublicKey, bool, error) {
return nil, false, nil
}
func (tx *AddPermissionlessDelegatorTx) PendingPriority() Priority {
if tx.Chain == constants.PrimaryNetworkID {
return PrimaryNetworkDelegatorBanffPendingPriority
}
return NetPermissionlessDelegatorPendingPriority
}
func (tx *AddPermissionlessDelegatorTx) CurrentPriority() Priority {
if tx.Chain == constants.PrimaryNetworkID {
return PrimaryNetworkDelegatorCurrentPriority
}
return NetPermissionlessDelegatorCurrentPriority
}
func (tx *AddPermissionlessDelegatorTx) Stake() []*lux.TransferableOutput {
return tx.StakeOuts
}
func (tx *AddPermissionlessDelegatorTx) RewardsOwner() fx.Owner {
return tx.DelegationRewardsOwner
}
// SyntacticVerify returns nil iff [tx] is valid
func (tx *AddPermissionlessDelegatorTx) SyntacticVerify(rt *runtime.Runtime) error {
switch {
case tx == nil:
return ErrNilTx
case tx.SyntacticallyVerified: // already passed syntactic verification
return nil
case len(tx.StakeOuts) == 0: // Ensure there is provided stake
return errNoStake
}
if err := tx.BaseTx.SyntacticVerify(rt); err != nil {
return fmt.Errorf("failed to verify BaseTx: %w", err)
}
if err := verify.All(&tx.Validator, tx.DelegationRewardsOwner); err != nil {
return fmt.Errorf("failed to verify validator or rewards owner: %w", err)
}
for _, out := range tx.StakeOuts {
if err := out.Verify(); err != nil {
return fmt.Errorf("failed to verify output: %w", err)
}
}
firstStakeOutput := tx.StakeOuts[0]
stakedAssetID := firstStakeOutput.AssetID()
totalStakeWeight := firstStakeOutput.Output().Amount()
for _, out := range tx.StakeOuts[1:] {
newWeight, err := safemath.Add(totalStakeWeight, out.Output().Amount())
if err != nil {
return err
}
totalStakeWeight = newWeight
assetID := out.AssetID()
if assetID != stakedAssetID {
return fmt.Errorf("%w: %q and %q", errMultipleStakedAssets, stakedAssetID, assetID)
}
}
switch {
case !lux.IsSortedTransferableOutputs(tx.StakeOuts):
return errOutputsNotSorted
case totalStakeWeight != tx.Wght:
return fmt.Errorf("%w, delegator weight %d total stake weight %d",
errDelegatorWeightMismatch,
tx.Wght,
totalStakeWeight,
)
}
// cache that this is valid
tx.SyntacticallyVerified = true
return nil
}
func (tx *AddPermissionlessDelegatorTx) Visit(visitor Visitor) error {
return visitor.AddPermissionlessDelegatorTx(tx)
}
// InitializeWithContext initializes the transaction with consensus context
func (tx *AddPermissionlessDelegatorTx) InitializeWithContext(ctx context.Context) error {
// Initialize any context-dependent fields here
return nil
}