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

fix add validator issues #2396

Merged
merged 5 commits into from
Dec 3, 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
6 changes: 3 additions & 3 deletions cmd/blockchaincmd/add_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ func CallAddValidator(
if err != nil {
return err
}
balanceAVAX, err := promptValidatorBalance(availableBalance)
balance, err = promptValidatorBalance(availableBalance)
if err != nil {
return err
}
// convert to nanoAVAX
balance = balanceAVAX * units.Avax
}
// convert to nanoAVAX
balance *= units.Avax

if remainingBalanceOwnerAddr == "" {
remainingBalanceOwnerAddr, err = getKeyForChangeOwner(network)
Expand Down
155 changes: 106 additions & 49 deletions pkg/validatormanager/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func PoAValidatorManagerInitializeValidatorRegistration(
}

func ValidatorManagerGetSubnetValidatorRegistrationMessage(
rpcURL string,
network models.Network,
aggregatorLogLevel logging.Level,
aggregatorQuorumPercentage uint64,
Expand All @@ -162,34 +163,62 @@ func ValidatorManagerGetSubnetValidatorRegistrationMessage(
balanceOwners warpMessage.PChainOwner,
disableOwners warpMessage.PChainOwner,
weight uint64,
alreadyInitialized bool,
) (*warp.Message, ids.ID, error) {
addressedCallPayload, err := warpMessage.NewRegisterL1Validator(
subnetID,
nodeID,
blsPublicKey,
expiry,
balanceOwners,
disableOwners,
weight,
var (
registerSubnetValidatorUnsignedMessage *warp.UnsignedMessage
validationID ids.ID
err error
)
if err != nil {
return nil, ids.Empty, err
}
validationID := addressedCallPayload.ValidationID()
registerSubnetValidatorAddressedCall, err := warpPayload.NewAddressedCall(
managerAddress.Bytes(),
addressedCallPayload.Bytes(),
)
if err != nil {
return nil, ids.Empty, err
}
registerSubnetValidatorUnsignedMessage, err := warp.NewUnsignedMessage(
network.ID,
blockchainID,
registerSubnetValidatorAddressedCall.Bytes(),
)
if err != nil {
return nil, ids.Empty, err
if alreadyInitialized {
validationID, err = GetRegisteredValidator(
rpcURL,
managerAddress,
nodeID,
)
if err != nil {
return nil, ids.Empty, err
}
unsignedMessageBytes, err := GetRegistrationMessage(
rpcURL,
validationID,
)
if err != nil {
return nil, ids.Empty, err
}
registerSubnetValidatorUnsignedMessage, err = warp.ParseUnsignedMessage(unsignedMessageBytes)
if err != nil {
return nil, ids.Empty, err
}
} else {
addressedCallPayload, err := warpMessage.NewRegisterL1Validator(
subnetID,
nodeID,
blsPublicKey,
expiry,
balanceOwners,
disableOwners,
weight,
)
if err != nil {
return nil, ids.Empty, err
}
validationID = addressedCallPayload.ValidationID()
registerSubnetValidatorAddressedCall, err := warpPayload.NewAddressedCall(
managerAddress.Bytes(),
addressedCallPayload.Bytes(),
)
if err != nil {
return nil, ids.Empty, err
}
registerSubnetValidatorUnsignedMessage, err = warp.NewUnsignedMessage(
network.ID,
blockchainID,
registerSubnetValidatorAddressedCall.Bytes(),
)
if err != nil {
return nil, ids.Empty, err
}
}
signatureAggregator, err := interchain.NewSignatureAggregator(
network,
Expand Down Expand Up @@ -291,7 +320,7 @@ func ValidatorManagerGetPChainSubnetValidatorRegistrationWarpMessage(
}
var justificationBytes []byte
if !registered {
justificationBytes, err = GetRegistrationMessage(rpcURL, validationID, subnetID)
justificationBytes, err = GetRegistrationJustification(rpcURL, validationID, subnetID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -355,6 +384,7 @@ func InitValidatorRegistration(
return nil, ids.Empty, err
}
managerAddress := common.HexToAddress(validatorManagerSDK.ProxyContractAddress)
alreadyInitialized := false
if initWithPos {
ux.Logger.PrintLineSeparator()
ux.Logger.PrintToUser("Initializing a validator registration with PoS validator manager")
Expand All @@ -379,6 +409,7 @@ func InitValidatorRegistration(
return nil, ids.Empty, evm.TransactionError(tx, err, "failure initializing validator registration")
}
ux.Logger.PrintToUser("the validator registration was already initialized. Proceeding to the next step")
alreadyInitialized = true
}
} else {
managerAddress = common.HexToAddress(validatorManagerSDK.ProxyContractAddress)
Expand All @@ -398,6 +429,7 @@ func InitValidatorRegistration(
return nil, ids.Empty, evm.TransactionError(tx, err, "failure initializing validator registration")
}
ux.Logger.PrintToUser("the validator registration was already initialized. Proceeding to the next step")
alreadyInitialized = true
}
}
aggregatorLogLevel, err := logging.ToLevel(aggregatorLogLevelStr)
Expand All @@ -419,6 +451,7 @@ func InitValidatorRegistration(

ux.Logger.PrintToUser(fmt.Sprintf("Validator weight: %d", weight))
return ValidatorManagerGetSubnetValidatorRegistrationMessage(
rpcURL,
network,
aggregatorLogLevel,
0,
Expand All @@ -433,6 +466,7 @@ func InitValidatorRegistration(
balanceOwners,
disableOwners,
weight,
alreadyInitialized,
)
}

Expand Down Expand Up @@ -494,23 +528,7 @@ func FinishValidatorRegistration(
func GetRegistrationMessage(
rpcURL string,
validationID ids.ID,
subnetID ids.ID,
) ([]byte, error) {
const numBootstrapValidatorsToSearch = 100
for validationIndex := uint32(0); validationIndex < numBootstrapValidatorsToSearch; validationIndex++ {
bootstrapValidationID := subnetID.Append(validationIndex)
if bootstrapValidationID == validationID {
justification := platformvm.L1ValidatorRegistrationJustification{
Preimage: &platformvm.L1ValidatorRegistrationJustification_ConvertSubnetToL1TxData{
ConvertSubnetToL1TxData: &platformvm.SubnetIDIndex{
SubnetId: subnetID[:],
Index: validationIndex,
},
},
}
return proto.Marshal(&justification)
}
}
client, err := evm.GetClient(rpcURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -545,12 +563,7 @@ func GetRegistrationMessage(
reg, err := warpMessage.ParseRegisterL1Validator(addressedCall.Payload)
if err == nil {
if reg.ValidationID() == validationID {
justification := platformvm.L1ValidatorRegistrationJustification{
Preimage: &platformvm.L1ValidatorRegistrationJustification_RegisterL1ValidatorMessage{
RegisterL1ValidatorMessage: addressedCall.Payload,
},
}
return proto.Marshal(&justification)
return msg.Bytes(), nil
}
}
}
Expand All @@ -559,3 +572,47 @@ func GetRegistrationMessage(
}
return nil, fmt.Errorf("validation id %s not found on warp events", validationID)
}

func GetRegistrationJustification(
rpcURL string,
validationID ids.ID,
subnetID ids.ID,
) ([]byte, error) {
const numBootstrapValidatorsToSearch = 100
for validationIndex := uint32(0); validationIndex < numBootstrapValidatorsToSearch; validationIndex++ {
bootstrapValidationID := subnetID.Append(validationIndex)
if bootstrapValidationID == validationID {
justification := platformvm.L1ValidatorRegistrationJustification{
Preimage: &platformvm.L1ValidatorRegistrationJustification_ConvertSubnetToL1TxData{
ConvertSubnetToL1TxData: &platformvm.SubnetIDIndex{
SubnetId: subnetID[:],
Index: validationIndex,
},
},
}
return proto.Marshal(&justification)
}
}
msg, err := GetRegistrationMessage(
rpcURL,
validationID,
)
if err != nil {
return nil, err
}
parsed, err := warp.ParseUnsignedMessage(msg)
if err != nil {
return nil, err
}
payload := parsed.Payload
addressedCall, err := warpPayload.ParseAddressedCall(payload)
if err != nil {
return nil, err
}
justification := platformvm.L1ValidatorRegistrationJustification{
Preimage: &platformvm.L1ValidatorRegistrationJustification_RegisterL1ValidatorMessage{
RegisterL1ValidatorMessage: addressedCall.Payload,
},
}
return proto.Marshal(&justification)
}
Loading