mirror of
https://github.com/luxfi/proto.git
synced 2026-10-30 15:21:09 +00:00
Package-local identifier only (incl. the p/genesis builder param + its test var); no exported field, json tag, or wire-format change — genesis files are unaffected. Co-authored-by: Hanzo Dev <[email protected]>
141 lines
3.8 KiB
Go
141 lines
3.8 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package txs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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 = (*AddDelegatorTx)(nil)
|
|
_ ScheduledStaker = (*AddDelegatorTx)(nil)
|
|
|
|
errDelegatorWeightMismatch = errors.New("delegator weight is not equal to total stake weight")
|
|
errStakeMustBeLUX = errors.New("stake must be LUX")
|
|
)
|
|
|
|
// AddDelegatorTx is an unsigned addDelegatorTx
|
|
type AddDelegatorTx struct {
|
|
// Metadata, inputs and outputs
|
|
BaseTx `serialize:"true"`
|
|
// Describes the delegatee
|
|
Validator `serialize:"true" json:"validator"`
|
|
// 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
|
|
// [UnsignedAddDelegatorTx]. Also sets the [ctx] to the given [vm.ctx] so that
|
|
// the addresses can be json marshalled into human readable format
|
|
func (tx *AddDelegatorTx) 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 (*AddDelegatorTx) ChainID() ids.ID {
|
|
return constants.PrimaryNetworkID
|
|
}
|
|
|
|
func (tx *AddDelegatorTx) NodeID() ids.NodeID {
|
|
return tx.Validator.NodeID
|
|
}
|
|
|
|
func (*AddDelegatorTx) PublicKey() (*bls.PublicKey, bool, error) {
|
|
return nil, false, nil
|
|
}
|
|
|
|
func (*AddDelegatorTx) PendingPriority() Priority {
|
|
return PrimaryNetworkDelegatorApricotPendingPriority
|
|
}
|
|
|
|
func (*AddDelegatorTx) CurrentPriority() Priority {
|
|
return PrimaryNetworkDelegatorCurrentPriority
|
|
}
|
|
|
|
func (tx *AddDelegatorTx) Stake() []*lux.TransferableOutput {
|
|
return tx.StakeOuts
|
|
}
|
|
|
|
func (tx *AddDelegatorTx) RewardsOwner() fx.Owner {
|
|
return tx.DelegationRewardsOwner
|
|
}
|
|
|
|
// SyntacticVerify returns nil iff [tx] is valid
|
|
func (tx *AddDelegatorTx) SyntacticVerify(rt *runtime.Runtime) error {
|
|
switch {
|
|
case tx == nil:
|
|
return ErrNilTx
|
|
case tx.SyntacticallyVerified: // already passed syntactic verification
|
|
return nil
|
|
}
|
|
|
|
if err := tx.BaseTx.SyntacticVerify(rt); err != nil {
|
|
return err
|
|
}
|
|
if err := verify.All(&tx.Validator, tx.DelegationRewardsOwner); err != nil {
|
|
return fmt.Errorf("failed to verify validator or rewards owner: %w", err)
|
|
}
|
|
|
|
totalStakeWeight := uint64(0)
|
|
for _, out := range tx.StakeOuts {
|
|
if err := out.Verify(); err != nil {
|
|
return fmt.Errorf("output verification failed: %w", err)
|
|
}
|
|
newWeight, err := safemath.Add(totalStakeWeight, out.Output().Amount())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
totalStakeWeight = newWeight
|
|
|
|
assetID := out.AssetID()
|
|
utxoAssetID := rt.UTXOAssetID
|
|
if assetID != utxoAssetID {
|
|
return fmt.Errorf("%w but is %q", errStakeMustBeLUX, 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 *AddDelegatorTx) Visit(visitor Visitor) error {
|
|
return visitor.AddDelegatorTx(tx)
|
|
}
|
|
|
|
// InitializeWithContext initializes the transaction with consensus context
|
|
func (tx *AddDelegatorTx) InitializeWithContext(ctx context.Context) error {
|
|
// Initialize any context-dependent fields here
|
|
return nil
|
|
}
|