Skip to content

Commit

Permalink
Relax ValidatorPublicKey Check in UnlockStake Txn (#1374)
Browse files Browse the repository at this point in the history
* Relax ValidatorPublicKey Check in UnlockStake Txn

* Update derived key check function
  • Loading branch information
tholonious committed Jun 20, 2024
1 parent da08f6e commit aa6b5bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 25 deletions.
18 changes: 6 additions & 12 deletions lib/block_view_stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2109,11 +2109,8 @@ func (bav *UtxoView) IsValidUnlockStakeMetadata(transactorPkBytes []byte, metada
}

// Validate ValidatorPublicKey.
validatorEntry, err := bav.GetValidatorByPublicKey(metadata.ValidatorPublicKey)
if err != nil {
return errors.Wrapf(err, "UtxoView.IsValidUnlockStakeMetadata: ")
}
if validatorEntry == nil || validatorEntry.isDeleted {
validatorPKIDEntry := bav.GetPKIDForPublicKey(metadata.ValidatorPublicKey.ToBytes())
if validatorPKIDEntry == nil || validatorPKIDEntry.isDeleted {
return errors.Wrapf(RuleErrorInvalidValidatorPKID, "UtxoView.IsValidUnlockStakeMetadata: ")
}

Expand Down Expand Up @@ -2148,7 +2145,7 @@ func (bav *UtxoView) IsValidUnlockStakeMetadata(transactorPkBytes []byte, metada

// Validate LockedStakeEntries exist.
lockedStakeEntries, err := bav.GetLockedStakeEntriesInRange(
validatorEntry.ValidatorPKID, transactorPKIDEntry.PKID, metadata.StartEpochNumber, metadata.EndEpochNumber,
validatorPKIDEntry.PKID, transactorPKIDEntry.PKID, metadata.StartEpochNumber, metadata.EndEpochNumber,
)
existsLockedStakeEntries := false
for _, lockedStakeEntry := range lockedStakeEntries {
Expand Down Expand Up @@ -3113,17 +3110,14 @@ func (bav *UtxoView) _checkUnlockStakeTxnSpendingLimitAndUpdateDerivedKey(
// this derived key is allowed to perform.

// Convert ValidatorPublicKey to ValidatorPKID.
validatorEntry, err := bav.GetValidatorByPublicKey(txMeta.ValidatorPublicKey)
if err != nil {
return derivedKeyEntry, errors.Wrapf(err, "_checkUnlockStakeTxnSpendingLimitAndUpdateDerivedKey: ")
}
if validatorEntry == nil || validatorEntry.isDeleted {
validatorPKIDEntry := bav.GetPKIDForPublicKey(txMeta.ValidatorPublicKey.ToBytes())
if validatorPKIDEntry == nil || validatorPKIDEntry.isDeleted {
return derivedKeyEntry, errors.Wrapf(RuleErrorInvalidValidatorPKID, "UtxoView._checkUnlockStakeTxnSpendingLimitAndUpdateDerivedKey: ")
}

// Check spending limit for this validator.
// If not found, check spending limit for any validator.
for _, validatorPKID := range []*PKID{validatorEntry.ValidatorPKID, &ZeroPKID} {
for _, validatorPKID := range []*PKID{validatorPKIDEntry.PKID, &ZeroPKID} {
// Retrieve DerivedKeyEntry.TransactionSpendingLimit.
stakeLimitKey := MakeStakeLimitKey(validatorPKID)
spendingLimit, exists := derivedKeyEntry.TransactionSpendingLimitTracker.UnlockStakeLimitMap[stakeLimitKey]
Expand Down
13 changes: 0 additions & 13 deletions lib/block_view_stake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,6 @@ func _testStaking(t *testing.T, flushToDB bool) {
GlobalDeSoParams.EncoderMigrationHeights = GetEncoderMigrationHeights(&params.ForkHeights)
GlobalDeSoParams.EncoderMigrationHeightsList = GetEncoderMigrationHeightsList(&params.ForkHeights)
}
{
// RuleErrorInvalidValidatorPKID
unlockStakeMetadata := &UnlockStakeMetadata{
ValidatorPublicKey: NewPublicKey(m2PkBytes),
StartEpochNumber: currentEpochNumber,
EndEpochNumber: currentEpochNumber,
}
_, err = _submitUnlockStakeTxn(
testMeta, m1Pub, m1Priv, unlockStakeMetadata, nil, flushToDB,
)
require.Error(t, err)
require.Contains(t, err.Error(), RuleErrorInvalidValidatorPKID)
}
{
// RuleErrorInvalidUnlockStakeEpochRange
unlockStakeMetadata := &UnlockStakeMetadata{
Expand Down

0 comments on commit aa6b5bc

Please sign in to comment.