Skip to content

Commit

Permalink
Fix disableSubnetValidatorTx
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Oct 3, 2024
1 parent 9b378cf commit 2b624ab
Show file tree
Hide file tree
Showing 20 changed files with 680 additions and 82 deletions.
17 changes: 11 additions & 6 deletions vms/platformvm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package executor
import (
"context"
"errors"
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/snowman"
Expand Down Expand Up @@ -125,7 +126,7 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {

recommendedPChainHeight, err := m.ctx.ValidatorState.GetMinimumHeight(context.TODO())
if err != nil {
return err
return fmt.Errorf("failed to fetch P-chain height: %w", err)
}
err = executor.VerifyWarpMessages(
context.TODO(),
Expand All @@ -135,12 +136,12 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
tx.Unsigned,
)
if err != nil {
return err
return fmt.Errorf("failed verifying warp messages: %w", err)
}

stateDiff, err := state.NewDiff(m.preferred, m)
if err != nil {
return err
return fmt.Errorf("failed creating state diff: %w", err)
}

nextBlkTime, _, err := state.NextBlockTime(
Expand All @@ -149,21 +150,25 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
m.txExecutorBackend.Clk,
)
if err != nil {
return err
return fmt.Errorf("failed selecting next block time: %w", err)
}

_, err = executor.AdvanceTimeTo(m.txExecutorBackend, stateDiff, nextBlkTime)
if err != nil {
return err
return fmt.Errorf("failed to advance the chain time: %w", err)
}

feeCalculator := state.PickFeeCalculator(m.txExecutorBackend.Config, stateDiff)
return tx.Unsigned.Visit(&executor.StandardTxExecutor{
err = tx.Unsigned.Visit(&executor.StandardTxExecutor{
Backend: m.txExecutorBackend,
State: stateDiff,
FeeCalculator: feeCalculator,
Tx: tx,
})
if err != nil {
return fmt.Errorf("failed execution: %w", err)
}
return nil
}

func (m *manager) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error {
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/network/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (g *gossipMempool) Add(tx *txs.Tx) error {

if err := g.txVerifier.VerifyTx(tx); err != nil {
g.Mempool.MarkDropped(txID, err)
return err
return fmt.Errorf("failed verification: %w", err)
}

if err := g.Mempool.Add(tx); err != nil {
Expand Down
1 change: 1 addition & 0 deletions vms/platformvm/txs/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ func RegisterEtnaTypes(targetCodec linearcodec.Codec) error {
targetCodec.RegisterType(&RegisterSubnetValidatorTx{}),
targetCodec.RegisterType(&SetSubnetValidatorWeightTx{}),
targetCodec.RegisterType(&IncreaseBalanceTx{}),
targetCodec.RegisterType(&DisableSubnetValidatorTx{}),
)
}
2 changes: 1 addition & 1 deletion vms/platformvm/txs/disable_subnet_validator_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DisableSubnetValidatorTx struct {
// Metadata, inputs and outputs
BaseTx `serialize:"true"`
// ID corresponding to the validator
ValidationID ids.ID `json:"validationID"`
ValidationID ids.ID `serialize:"true" json:"validationID"`
// Authorizes this validator to be disabled
DisableAuth verify.Verifiable `serialize:"true" json:"disableAuthorization"`
}
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/txs/executor/standard_tx_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ func (e *StandardTxExecutor) DisableSubnetValidatorTx(tx *txs.DisableSubnetValid

sov, err := e.State.GetSubnetOnlyValidator(tx.ValidationID)
if err != nil {
return err
return fmt.Errorf("couldn't load SoV for %s: %w", tx.ValidationID, err)
}

var disableOwner message.PChainOwner
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/message/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Parse(bytes []byte) (Payload, error) {
return p, nil
}

func initialize(p Payload) error {
func Initialize(p Payload) error {
bytes, err := Codec.Marshal(CodecVersion, &p)
if err != nil {
return fmt.Errorf("couldn't marshal %T payload: %w", p, err)
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/message/register_subnet_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewRegisterSubnetValidator(
DisableOwner: disableOwner,
Weight: weight,
}
return msg, initialize(msg)
return msg, Initialize(msg)
}

// ParseRegisterSubnetValidator parses bytes into an initialized
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/message/subnet_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewSubnetConversion(id ids.ID) (*SubnetConversion, error) {
msg := &SubnetConversion{
ID: id,
}
return msg, initialize(msg)
return msg, Initialize(msg)
}

// ParseSubnetConversion parses bytes into an initialized SubnetConversion.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewSubnetValidatorRegistration(
ValidationID: validationID,
Registered: registered,
}
return msg, initialize(msg)
return msg, Initialize(msg)
}

// ParseSubnetValidatorRegistration parses bytes into an initialized
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/message/subnet_validator_weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewSubnetValidatorWeight(
Nonce: nonce,
Weight: weight,
}
return msg, initialize(msg)
return msg, Initialize(msg)
}

// ParseSubnetValidatorWeight parses bytes into an initialized
Expand Down
2 changes: 1 addition & 1 deletion wallet/subnet/primary/examples/convert-subnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
uri := "http://localhost:9700"
kc := secp256k1fx.NewKeychain(key)
subnetID := ids.FromStringOrPanic("2DeHa7Qb6sufPkmQcFWG2uCd4pBPv9WB6dkzroiMQhd1NSRtof")
chainID := ids.FromStringOrPanic("E8nTR9TtRwfkS7XFjTYUYHENQ91mkPMtDUwwCeu7rNgBBtkqu")
chainID := ids.FromStringOrPanic("2BMFrJ9xeh5JdwZEx6uuFcjfZC2SV2hdbMT8ee5HrvjtfJb5br")
addressHex := ""
weight := units.Schmeckle

Expand Down
54 changes: 54 additions & 0 deletions wallet/subnet/primary/examples/disable-subnet-validator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"log"
"time"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
validationID := ids.FromStringOrPanic("9FAftNgNBrzHUMMApsSyV6RcFiL9UmCbvsCu28xdLV2mQ7CMo")

ctx := context.Background()

// MakeWallet fetches the available UTXOs owned by [kc] on the network that
// [uri] is hosting and registers [subnetID].
walletSyncStartTime := time.Now()
wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
URI: uri,
AVAXKeychain: kc,
EthKeychain: kc,
ValidationIDs: []ids.ID{validationID},
})
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()

disableSubnetValidatorStartTime := time.Now()
disableSubnetValidatorTx, err := pWallet.IssueDisableSubnetValidatorTx(
validationID,
)
if err != nil {
log.Fatalf("failed to issue disable subnet validator transaction: %s\n", err)
}
log.Printf("disabled %s with %s in %s\n",
validationID,
disableSubnetValidatorTx.ID(),
time.Since(disableSubnetValidatorStartTime),
)
}
56 changes: 56 additions & 0 deletions wallet/subnet/primary/examples/increase-balance/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"log"
"time"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
validationID := ids.FromStringOrPanic("9FAftNgNBrzHUMMApsSyV6RcFiL9UmCbvsCu28xdLV2mQ7CMo")
balance := uint64(2)

ctx := context.Background()

// MakeWallet fetches the available UTXOs owned by [kc] on the network that
// [uri] is hosting and registers [subnetID].
walletSyncStartTime := time.Now()
wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
URI: uri,
AVAXKeychain: kc,
EthKeychain: kc,
})
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()

increaseBalanceStartTime := time.Now()
increaseBalanceTx, err := pWallet.IssueIncreaseBalanceTx(
validationID,
balance,
)
if err != nil {
log.Fatalf("failed to issue increase balance transaction: %s\n", err)
}
log.Printf("increased balance of validationID %s by %d with %s in %s\n",
validationID,
balance,
increaseBalanceTx.ID(),
time.Since(increaseBalanceStartTime),
)
}
20 changes: 10 additions & 10 deletions wallet/subnet/primary/examples/register-subnet-validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/warp"
Expand All @@ -30,9 +29,9 @@ func main() {
uri := "http://localhost:9710"
kc := secp256k1fx.NewKeychain(key)
subnetID := ids.FromStringOrPanic("2DeHa7Qb6sufPkmQcFWG2uCd4pBPv9WB6dkzroiMQhd1NSRtof")
chainID := ids.FromStringOrPanic("21G9Uqg2R7hYa81kZK7oMCgstsHEiNGbkpB9kdBLUeWx3wWMsV")
chainID := ids.FromStringOrPanic("2BMFrJ9xeh5JdwZEx6uuFcjfZC2SV2hdbMT8ee5HrvjtfJb5br")
addressHex := ""
weight := units.Schmeckle
weight := uint64(1)

address, err := hex.DecodeString(addressHex)
if err != nil {
Expand Down Expand Up @@ -79,9 +78,11 @@ func main() {
addressedCallPayload, err := message.NewRegisterSubnetValidator(
subnetID,
nodeID,
weight,
nodePoP.PublicKey,
uint64(time.Now().Add(5*time.Minute).Unix()),
message.PChainOwner{},
message.PChainOwner{},
weight,
)
if err != nil {
log.Fatalf("failed to create RegisterSubnetValidator message: %s\n", err)
Expand Down Expand Up @@ -128,17 +129,16 @@ func main() {
log.Fatalf("failed to create Warp message: %s\n", err)
}

convertSubnetStartTime := time.Now()
addValidatorTx, err := pWallet.IssueRegisterSubnetValidatorTx(
registerSubnetValidatorStartTime := time.Now()
registerSubnetValidatorTx, err := pWallet.IssueRegisterSubnetValidatorTx(
units.Avax,
nodePoP.ProofOfPossession,
&secp256k1fx.OutputOwners{},
warp.Bytes(),
)
if err != nil {
log.Fatalf("failed to issue add subnet validator transaction: %s\n", err)
log.Fatalf("failed to issue register subnet validator transaction: %s\n", err)
}

var validationID ids.ID = hashing.ComputeHash256Array(addressedCallPayload.Bytes())
log.Printf("added new subnet validator %s to subnet %s with txID %s as validationID %s in %s\n", nodeID, subnetID, addValidatorTx.ID(), validationID, time.Since(convertSubnetStartTime))
validationID := addressedCallPayload.ValidationID()
log.Printf("registered new subnet validator %s to subnet %s with txID %s as validationID %s in %s\n", nodeID, subnetID, registerSubnetValidatorTx.ID(), validationID, time.Since(registerSubnetValidatorStartTime))
}
Loading

0 comments on commit 2b624ab

Please sign in to comment.