Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vms/platformvm: Add missing txs to txs.Builder #2663

Merged
merged 9 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions vms/platformvm/txs/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
Expand Down Expand Up @@ -124,6 +126,27 @@ type ProposalTxBuilder interface {
changeAddr ids.ShortID,
) (*txs.Tx, error)

// stakeAmount: amount the validator stakes
// startTime: unix time they start validating
// endTime: unix time they stop validating
// nodeID: ID of the node we want to validate with
// pop: the node proof of possession
// rewardAddress: address to send reward to, if applicable
// shares: 10,000 times percentage of reward taken from delegators
// keys: Keys providing the staked tokens
// changeAddr: Address to send change to, if there is any
NewAddPermissionlessValidatorTx(
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
pop *signer.ProofOfPossession,
rewardAddress ids.ShortID,
shares uint32,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)

// stakeAmount: amount the delegator stakes
// startTime: unix time they start delegating
// endTime: unix time they stop delegating
Expand All @@ -141,6 +164,23 @@ type ProposalTxBuilder interface {
changeAddr ids.ShortID,
) (*txs.Tx, error)

// stakeAmount: amount the delegator stakes
// startTime: unix time they start delegating
// endTime: unix time they stop delegating
// nodeID: ID of the node we are delegating to
// rewardAddress: address to send reward to, if applicable
// keys: keys providing the staked tokens
// changeAddr: address to send change to, if there is any
NewAddPermissionlessDelegatorTx(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused in this PR but is used in the follow-up: #2662

stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error)

// weight: sampling weight of the new validator
// startTime: unix time they start delegating
// endTime: unix time they top delegating
Expand Down Expand Up @@ -483,6 +523,57 @@ func (b *builder) NewAddValidatorTx(
return tx, tx.SyntacticVerify(b.ctx)
}

func (b *builder) NewAddPermissionlessValidatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
pop *signer.ProofOfPossession,
rewardAddress ids.ShortID,
shares uint32,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unstakedOuts, stakedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkValidatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the tx
utx := &txs.AddPermissionlessValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: unstakedOuts,
}},
Validator: txs.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: stakeAmount,
},
Subnet: constants.PrimaryNetworkID,
Signer: pop,
StakeOuts: stakedOuts,
ValidatorRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{rewardAddress},
},
DelegatorRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{rewardAddress},
},
DelegationShares: shares,
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}

func (b *builder) NewAddDelegatorTx(
stakeAmount,
startTime,
Expand Down Expand Up @@ -524,6 +615,48 @@ func (b *builder) NewAddDelegatorTx(
return tx, tx.SyntacticVerify(b.ctx)
}

func (b *builder) NewAddPermissionlessDelegatorTx(
stakeAmount,
startTime,
endTime uint64,
nodeID ids.NodeID,
rewardAddress ids.ShortID,
keys []*secp256k1.PrivateKey,
changeAddr ids.ShortID,
) (*txs.Tx, error) {
ins, unlockedOuts, lockedOuts, signers, err := b.Spend(b.state, keys, stakeAmount, b.cfg.AddPrimaryNetworkDelegatorFee, changeAddr)
if err != nil {
return nil, fmt.Errorf("couldn't generate tx inputs/outputs: %w", err)
}
// Create the tx
utx := &txs.AddPermissionlessDelegatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: b.ctx.NetworkID,
BlockchainID: b.ctx.ChainID,
Ins: ins,
Outs: unlockedOuts,
}},
Validator: txs.Validator{
NodeID: nodeID,
Start: startTime,
End: endTime,
Wght: stakeAmount,
},
Subnet: constants.PrimaryNetworkID,
StakeOuts: lockedOuts,
DelegationRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{rewardAddress},
},
}
tx, err := txs.NewSigned(utx, txs.Codec, signers)
if err != nil {
return nil, err
}
return tx, tx.SyntacticVerify(b.ctx)
}

func (b *builder) NewAddSubnetValidatorTx(
weight,
startTime,
Expand Down
66 changes: 14 additions & 52 deletions vms/platformvm/validator_set_property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ import (
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/api"
"github.com/ava-labs/avalanchego/vms/platformvm/block"
"github.com/ava-labs/avalanchego/vms/platformvm/config"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

blockexecutor "github.com/ava-labs/avalanchego/vms/platformvm/block/executor"
txexecutor "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
Expand Down Expand Up @@ -297,60 +294,25 @@ func addSubnetValidator(vm *VM, data *validatorInputData, subnetID ids.ID) (*sta

func addPrimaryValidatorWithBLSKey(vm *VM, data *validatorInputData) (*state.Staker, error) {
addr := keys[0].PublicKey().Address()
utxoHandler := utxo.NewHandler(vm.ctx, &vm.clock, vm.fx)
ins, unstakedOuts, stakedOuts, signers, err := utxoHandler.Spend(
vm.state,
keys,
vm.MinValidatorStake,
vm.Config.AddPrimaryNetworkValidatorFee,
addr, // change Addresss
)
if err != nil {
return nil, fmt.Errorf("could not create inputs/outputs for permissionless validator: %w", err)
}

sk, err := bls.NewSecretKey()
if err != nil {
return nil, fmt.Errorf("could not create secret key: %w", err)
return nil, fmt.Errorf("failed to generate BLS key: %w", err)
}

uPrimaryTx := &txs.AddPermissionlessValidatorTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: vm.ctx.NetworkID,
BlockchainID: vm.ctx.ChainID,
Ins: ins,
Outs: unstakedOuts,
}},
Validator: txs.Validator{
NodeID: data.nodeID,
Start: uint64(data.startTime.Unix()),
End: uint64(data.endTime.Unix()),
Wght: vm.MinValidatorStake,
},
Subnet: constants.PrimaryNetworkID,
Signer: signer.NewProofOfPossession(sk),
StakeOuts: stakedOuts,
ValidatorRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{
addr,
},
},
DelegatorRewardsOwner: &secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{
addr,
},
},
DelegationShares: reward.PercentDenominator,
}
signedTx, err := txs.NewSigned(uPrimaryTx, txs.Codec, signers)
signedTx, err := vm.txBuilder.NewAddPermissionlessValidatorTx(
vm.Config.MinValidatorStake,
uint64(data.startTime.Unix()),
uint64(data.endTime.Unix()),
data.nodeID,
signer.NewProofOfPossession(sk),
addr,
reward.PercentDenominator,
[]*secp256k1.PrivateKey{keys[0], keys[1]},
addr,
)
if err != nil {
return nil, fmt.Errorf("could not create AddPermissionlessValidatorTx with BLS key: %w", err)
}
if err := signedTx.SyntacticVerify(vm.ctx); err != nil {
return nil, fmt.Errorf("failed syntax verification of AddPermissionlessValidatorTx: %w", err)
return nil, fmt.Errorf("could not create AddPermissionlessValidatorTx: %w", err)
}
return internalAddValidator(vm, signedTx)
}
Expand Down
Loading