diff --git a/lib/block_view.go b/lib/block_view.go index 10979be89..ad541719f 100644 --- a/lib/block_view.go +++ b/lib/block_view.go @@ -137,9 +137,10 @@ type UtxoView struct { // It contains the snapshot value of the GlobalParamsEntry at the given SnapshotAtEpochNumber. SnapshotGlobalParamEntries map[uint64]*GlobalParamsEntry - // SnapshotValidatorEntries is a map of to a ValidatorEntry. - // It contains the snapshot value of a ValidatorEntry at the given SnapshotAtEpochNumber. - SnapshotValidatorEntries map[SnapshotValidatorMapKey]*ValidatorEntry + // SnapshotValidatorSet is a map of to a ValidatorEntry. + // It contains the snapshot value of every ValidatorEntry that makes up the validator set at + // the given SnapshotAtEpochNumber. + SnapshotValidatorSet map[SnapshotValidatorSetMapKey]*ValidatorEntry // SnapshotGlobalActiveStakeAmountNanos is a map of SnapshotAtEpochNumber to a GlobalActiveStakeAmountNanos. // It contains the snapshot value of the GlobalActiveStakeAmountNanos at the given SnapshotAtEpochNumber. @@ -259,8 +260,8 @@ func (bav *UtxoView) _ResetViewMappingsAfterFlush() { // SnapshotGlobalParamEntries bav.SnapshotGlobalParamEntries = make(map[uint64]*GlobalParamsEntry) - // SnapshotValidatorEntries - bav.SnapshotValidatorEntries = make(map[SnapshotValidatorMapKey]*ValidatorEntry) + // SnapshotValidatorSet + bav.SnapshotValidatorSet = make(map[SnapshotValidatorSetMapKey]*ValidatorEntry) // SnapshotGlobalActiveStakeAmountNanos bav.SnapshotGlobalActiveStakeAmountNanos = make(map[uint64]*uint256.Int) @@ -559,9 +560,9 @@ func (bav *UtxoView) CopyUtxoView() (*UtxoView, error) { newView.SnapshotGlobalParamEntries[epochNumber] = globalParamsEntry.Copy() } - // Copy the SnapshotValidatorEntries - for mapKey, validatorEntry := range bav.SnapshotValidatorEntries { - newView.SnapshotValidatorEntries[mapKey] = validatorEntry.Copy() + // Copy the SnapshotValidatorSet + for mapKey, validatorEntry := range bav.SnapshotValidatorSet { + newView.SnapshotValidatorSet[mapKey] = validatorEntry.Copy() } // Copy the SnapshotGlobalActiveStakeAmountNanos diff --git a/lib/block_view_flush.go b/lib/block_view_flush.go index b414e5fc4..4cf1e724d 100644 --- a/lib/block_view_flush.go +++ b/lib/block_view_flush.go @@ -2,11 +2,12 @@ package lib import ( "fmt" + "reflect" + "github.com/btcsuite/btcd/btcec" "github.com/dgraph-io/badger/v3" "github.com/golang/glog" "github.com/pkg/errors" - "reflect" ) func (bav *UtxoView) FlushToDb(blockHeight uint64) error { @@ -164,7 +165,7 @@ func (bav *UtxoView) FlushToDbWithTxn(txn *badger.Txn, blockHeight uint64) error if err := bav._flushSnapshotGlobalParamsEntryToDbWithTxn(txn, blockHeight); err != nil { return err } - if err := bav._flushSnapshotValidatorEntriesToDbWithTxn(txn, blockHeight); err != nil { + if err := bav._flushSnapshotValidatorSetToDbWithTxn(txn, blockHeight); err != nil { return err } if err := bav._flushSnapshotGlobalActiveStakeAmountNanosToDbWithTxn(txn, blockHeight); err != nil { diff --git a/lib/db_utils.go b/lib/db_utils.go index 7d97ed759..157d75010 100644 --- a/lib/db_utils.go +++ b/lib/db_utils.go @@ -533,14 +533,16 @@ type DBPrefixes struct { // Prefix, -> *GlobalParamsEntry PrefixSnapshotGlobalParamsEntry []byte `prefix_id:"[85]" is_state:"true"` - // PrefixSnapshotValidatorByPKID: Retrieve a snapshot ValidatorEntry by . + // PrefixSnapshotValidatorSetByPKID: Retrieve a ValidatorEntry from a snapshot validator set by + // . // Prefix, , -> *ValidatorEntry - PrefixSnapshotValidatorByPKID []byte `prefix_id:"[86]" is_state:"true"` + PrefixSnapshotValidatorSetByPKID []byte `prefix_id:"[86]" is_state:"true"` - // PrefixSnapshotValidatorByStatusAndStake: Retrieve stake-ordered active ValidatorEntries by SnapshotAtEpochNumber. + // PrefixSnapshotValidatorSetByStake: Retrieve stake-ordered ValidatorEntries from a snapshot validator set + // by SnapshotAtEpochNumber. // Prefix, , , -> nil // Note: we parse the ValidatorPKID from the key and the value is nil to save space. - PrefixSnapshotValidatorByStake []byte `prefix_id:"[87]" is_state:"true"` + PrefixSnapshotValidatorSetByStake []byte `prefix_id:"[87]" is_state:"true"` // PrefixSnapshotGlobalActiveStakeAmountNanos: Retrieve a snapshot GlobalActiveStakeAmountNanos by SnapshotAtEpochNumber. // Prefix, -> *uint256.Int @@ -776,10 +778,10 @@ func StatePrefixToDeSoEncoder(prefix []byte) (_isEncoder bool, _encoder DeSoEnco } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotGlobalParamsEntry) { // prefix_id:"[85]" return true, &GlobalParamsEntry{} - } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotValidatorByPKID) { + } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotValidatorSetByPKID) { // prefix_id:"[86]" return true, &ValidatorEntry{} - } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotValidatorByStake) { + } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotValidatorSetByStake) { // prefix_id:"[87]" return false, nil } else if bytes.Equal(prefix, Prefixes.PrefixSnapshotGlobalActiveStakeAmountNanos) { diff --git a/lib/pos_epoch_complete_hook.go b/lib/pos_epoch_complete_hook.go index b24c2e3de..b3983ad7c 100644 --- a/lib/pos_epoch_complete_hook.go +++ b/lib/pos_epoch_complete_hook.go @@ -80,7 +80,7 @@ func (bav *UtxoView) RunEpochCompleteHook(blockHeight uint64) error { return errors.Wrapf(err, "RunEpochCompleteHook: error retrieving top ValidatorEntries: ") } for _, validatorEntry := range validatorSet { - bav._setSnapshotValidatorEntry(validatorEntry, currentEpochEntry.EpochNumber) + bav._setSnapshotValidatorSetEntry(validatorEntry, currentEpochEntry.EpochNumber) } // Snapshot the current GlobalActiveStakeAmountNanos. diff --git a/lib/pos_epoch_complete_hook_test.go b/lib/pos_epoch_complete_hook_test.go index f2b665bf5..a41f44b72 100644 --- a/lib/pos_epoch_complete_hook_test.go +++ b/lib/pos_epoch_complete_hook_test.go @@ -149,9 +149,9 @@ func TestRunEpochCompleteHook(t *testing.T) { _assertEmptyValidatorSnapshots := func() { // Test SnapshotValidatorByPKID is nil. for _, pkid := range validatorPKIDs { - snapshotValidatorEntry, err := utxoView().GetSnapshotValidatorByPKID(pkid) + snapshotValidatorSetEntry, err := utxoView().GetSnapshotValidatorSetEntryByPKID(pkid) require.NoError(t, err) - require.Nil(t, snapshotValidatorEntry) + require.Nil(t, snapshotValidatorSetEntry) } // Test SnapshotTopActiveValidatorsByStake is empty. @@ -297,9 +297,9 @@ func TestRunEpochCompleteHook(t *testing.T) { // Test SnapshotValidatorByPKID is populated. for _, pkid := range validatorPKIDs { - snapshotValidatorEntry, err := utxoView().GetSnapshotValidatorByPKID(pkid) + snapshotValidatorSetEntry, err := utxoView().GetSnapshotValidatorSetEntryByPKID(pkid) require.NoError(t, err) - require.NotNil(t, snapshotValidatorEntry) + require.NotNil(t, snapshotValidatorSetEntry) } // Test SnapshotTopActiveValidatorsByStake is populated. @@ -345,7 +345,7 @@ func TestRunEpochCompleteHook(t *testing.T) { _runOnEpochCompleteHook() // Snapshot m5 still has 600 staked. - validatorEntry, err = utxoView().GetSnapshotValidatorByPKID(m5PKID) + validatorEntry, err = utxoView().GetSnapshotValidatorSetEntryByPKID(m5PKID) require.NoError(t, err) require.NotNil(t, validatorEntry) require.Equal(t, validatorEntry.TotalStakeAmountNanos.Uint64(), uint64(600)) @@ -354,7 +354,7 @@ func TestRunEpochCompleteHook(t *testing.T) { _runOnEpochCompleteHook() // Snapshot m5 now has 800 staked. - validatorEntry, err = utxoView().GetSnapshotValidatorByPKID(m5PKID) + validatorEntry, err = utxoView().GetSnapshotValidatorSetEntryByPKID(m5PKID) require.NoError(t, err) require.NotNil(t, validatorEntry) require.Equal(t, validatorEntry.TotalStakeAmountNanos.Uint64(), uint64(800)) @@ -397,9 +397,9 @@ func TestRunEpochCompleteHook(t *testing.T) { // Test snapshotting changing validator set. // m0 unregisters as a validator. - snapshotValidatorEntries, err := utxoView().GetSnapshotValidatorSetByStake(10) + snapshotValidatorSet, err := utxoView().GetSnapshotValidatorSetByStake(10) require.NoError(t, err) - require.Len(t, snapshotValidatorEntries, 7) + require.Len(t, snapshotValidatorSet, 7) _, err = _submitUnregisterAsValidatorTxn(testMeta, m0Pub, m0Priv, true) require.NoError(t, err) @@ -408,17 +408,17 @@ func TestRunEpochCompleteHook(t *testing.T) { _runOnEpochCompleteHook() // m0 is still in the snapshot validator set. - snapshotValidatorEntries, err = utxoView().GetSnapshotValidatorSetByStake(10) + snapshotValidatorSet, err = utxoView().GetSnapshotValidatorSetByStake(10) require.NoError(t, err) - require.Len(t, snapshotValidatorEntries, 7) + require.Len(t, snapshotValidatorSet, 7) // Run OnEpochCompleteHook(). _runOnEpochCompleteHook() // m0 is dropped from the snapshot validator set. - snapshotValidatorEntries, err = utxoView().GetSnapshotValidatorSetByStake(10) + snapshotValidatorSet, err = utxoView().GetSnapshotValidatorSetByStake(10) require.NoError(t, err) - require.Len(t, snapshotValidatorEntries, 6) + require.Len(t, snapshotValidatorSet, 6) } { // Test jailing inactive validators. @@ -446,9 +446,9 @@ func TestRunEpochCompleteHook(t *testing.T) { } getNumSnapshotActiveValidators := func() int { - snapshotValidatorEntries, err := utxoView().GetSnapshotValidatorSetByStake(10) + snapshotValidatorSet, err := utxoView().GetSnapshotValidatorSetByStake(10) require.NoError(t, err) - return len(snapshotValidatorEntries) + return len(snapshotValidatorSet) } getCurrentValidator := func(validatorPKID *PKID) *ValidatorEntry { diff --git a/lib/pos_snapshot_entries.go b/lib/pos_snapshot_entries.go index 6ae7c6ca8..4fea4508f 100644 --- a/lib/pos_snapshot_entries.go +++ b/lib/pos_snapshot_entries.go @@ -198,36 +198,36 @@ func DBPutSnapshotGlobalParamsEntryWithTxn( } // -// SnapshotValidatorEntry +// SnapshotValidatorSet // -type SnapshotValidatorMapKey struct { +type SnapshotValidatorSetMapKey struct { SnapshotAtEpochNumber uint64 ValidatorPKID PKID } -func (bav *UtxoView) GetSnapshotValidatorByPKID(pkid *PKID) (*ValidatorEntry, error) { +func (bav *UtxoView) GetSnapshotValidatorSetEntryByPKID(pkid *PKID) (*ValidatorEntry, error) { // Calculate the SnapshotEpochNumber. snapshotAtEpochNumber, err := bav.GetSnapshotEpochNumber() if err != nil { - return nil, errors.Wrapf(err, "GetSnapshotValidatorByPKID: problem calculating SnapshotEpochNumber: ") + return nil, errors.Wrapf(err, "GetSnapshotValidatorSetEntryByPKID: problem calculating SnapshotEpochNumber: ") } // Check the UtxoView first. - mapKey := SnapshotValidatorMapKey{SnapshotAtEpochNumber: snapshotAtEpochNumber, ValidatorPKID: *pkid} - if validatorEntry, exists := bav.SnapshotValidatorEntries[mapKey]; exists { + mapKey := SnapshotValidatorSetMapKey{SnapshotAtEpochNumber: snapshotAtEpochNumber, ValidatorPKID: *pkid} + if validatorEntry, exists := bav.SnapshotValidatorSet[mapKey]; exists { return validatorEntry, nil } // If we don't have it in the UtxoView, check the db. - validatorEntry, err := DBGetSnapshotValidatorByPKID(bav.Handle, bav.Snapshot, pkid, snapshotAtEpochNumber) + validatorEntry, err := DBGetSnapshotValidatorSetEntryByPKID(bav.Handle, bav.Snapshot, pkid, snapshotAtEpochNumber) if err != nil { return nil, errors.Wrapf( err, - "GetSnapshotValidatorByPKID: problem retrieving ValidatorEntry from db: ", + "GetSnapshotValidatorSetEntryByPKID: problem retrieving ValidatorEntry from db: ", ) } if validatorEntry != nil { // Cache the result in the UtxoView. - bav._setSnapshotValidatorEntry(validatorEntry, snapshotAtEpochNumber) + bav._setSnapshotValidatorSetEntry(validatorEntry, snapshotAtEpochNumber) } return validatorEntry, nil } @@ -236,40 +236,40 @@ func (bav *UtxoView) GetSnapshotValidatorSetByStake(limit uint64) ([]*ValidatorE // Calculate the SnapshotEpochNumber. snapshotAtEpochNumber, err := bav.GetSnapshotEpochNumber() if err != nil { - return nil, errors.Wrapf(err, "GetSnapshotValidatorSetByStake: problem calculating SnapshotEpochNumber: ") + return nil, errors.Wrapf(err, "GetSnapshotValidatorSetEntriesByStake: problem calculating SnapshotEpochNumber: ") } // Create a slice of all UtxoView ValidatorEntries to prevent pulling them from the db. var utxoViewValidatorEntries []*ValidatorEntry - for mapKey, validatorEntry := range bav.SnapshotValidatorEntries { + for mapKey, validatorEntry := range bav.SnapshotValidatorSet { if mapKey.SnapshotAtEpochNumber == snapshotAtEpochNumber { utxoViewValidatorEntries = append(utxoViewValidatorEntries, validatorEntry) } } - // Pull top N active ValidatorEntries from the database (not present in the UtxoView). + // Pull top N ValidatorEntries from the database (not present in the UtxoView). // Note that we will skip validators that are present in the view because we pass // utxoViewValidatorEntries to the function. - dbValidatorEntries, err := DBGetSnapshotTopActiveValidatorsByStake( + dbValidatorEntries, err := DBGetSnapshotValidatorSetByStake( bav.Handle, bav.Snapshot, limit, snapshotAtEpochNumber, utxoViewValidatorEntries, ) if err != nil { - return nil, errors.Wrapf(err, "GetSnapshotValidatorSetByStake: error retrieving entries from db: ") + return nil, errors.Wrapf(err, "GetSnapshotValidatorSetEntriesByStake: error retrieving entries from db: ") } // Cache top N active ValidatorEntries from the db in the UtxoView. for _, validatorEntry := range dbValidatorEntries { // We only pull ValidatorEntries from the db that are not present in the // UtxoView. As a sanity check, we double-check that the ValidatorEntry // is not already in the UtxoView here. - mapKey := SnapshotValidatorMapKey{ + mapKey := SnapshotValidatorSetMapKey{ SnapshotAtEpochNumber: snapshotAtEpochNumber, ValidatorPKID: *validatorEntry.ValidatorPKID, } - if _, exists := bav.SnapshotValidatorEntries[mapKey]; !exists { - bav._setSnapshotValidatorEntry(validatorEntry, snapshotAtEpochNumber) + if _, exists := bav.SnapshotValidatorSet[mapKey]; !exists { + bav._setSnapshotValidatorSetEntry(validatorEntry, snapshotAtEpochNumber) } } // Pull !isDeleted, active ValidatorEntries from the UtxoView with stake > 0. var validatorEntries []*ValidatorEntry - for mapKey, validatorEntry := range bav.SnapshotValidatorEntries { + for mapKey, validatorEntry := range bav.SnapshotValidatorSet { if mapKey.SnapshotAtEpochNumber == snapshotAtEpochNumber && !validatorEntry.isDeleted && validatorEntry.Status() == ValidatorStatusActive && @@ -294,68 +294,68 @@ func (bav *UtxoView) GetSnapshotValidatorSetByStake(limit uint64) ([]*ValidatorE return validatorEntries[0:upperBound], nil } -func (bav *UtxoView) _setSnapshotValidatorEntry(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) { +func (bav *UtxoView) _setSnapshotValidatorSetEntry(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) { if validatorEntry == nil { - glog.Errorf("_setSnapshotValidatorEntry: called with nil entry, this should never happen") + glog.Errorf("_setSnapshotValidatorSetEntry: called with nil entry, this should never happen") return } - mapKey := SnapshotValidatorMapKey{ + mapKey := SnapshotValidatorSetMapKey{ SnapshotAtEpochNumber: snapshotAtEpochNumber, ValidatorPKID: *validatorEntry.ValidatorPKID, } - bav.SnapshotValidatorEntries[mapKey] = validatorEntry.Copy() + bav.SnapshotValidatorSet[mapKey] = validatorEntry.Copy() } -func (bav *UtxoView) _deleteSnapshotValidatorEntry(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) { +func (bav *UtxoView) _deleteSnapshotValidatorSetEntry(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) { // This function shouldn't be called with nil. if validatorEntry == nil { - glog.Errorf("_deleteSnapshotValidatorEntry: called with nil entry, this should never happen") + glog.Errorf("_deleteSnapshotValidatorSetEntry: called with nil entry, this should never happen") return } // Create a tombstone entry. tombstoneEntry := *validatorEntry tombstoneEntry.isDeleted = true // Set the mappings to the point to the tombstone entry. - bav._setSnapshotValidatorEntry(&tombstoneEntry, snapshotAtEpochNumber) + bav._setSnapshotValidatorSetEntry(&tombstoneEntry, snapshotAtEpochNumber) } -func (bav *UtxoView) _flushSnapshotValidatorEntriesToDbWithTxn(txn *badger.Txn, blockHeight uint64) error { - // Delete all SnapshotValidatorEntries from the db that are in the UtxoView. - for mapKey, validatorEntry := range bav.SnapshotValidatorEntries { +func (bav *UtxoView) _flushSnapshotValidatorSetToDbWithTxn(txn *badger.Txn, blockHeight uint64) error { + // Delete all SnapshotValidatorSet entries from the db that are in the UtxoView. + for mapKey, validatorEntry := range bav.SnapshotValidatorSet { if validatorEntry == nil { return fmt.Errorf( - "_flushSnapshotValidatorEntriesToDb: found nil entry for EpochNumber %d, this should never happen", + "_flushSnapshotValidatorSetToDbWithTxn: found nil entry for EpochNumber %d, this should never happen", mapKey.SnapshotAtEpochNumber, ) } - if err := DBDeleteSnapshotValidatorEntryWithTxn( + if err := DBDeleteSnapshotValidatorSetEntryWithTxn( txn, bav.Snapshot, &mapKey.ValidatorPKID, mapKey.SnapshotAtEpochNumber, ); err != nil { return errors.Wrapf( err, - "_flushSnapshotValidatorEntriesToDb: problem deleting ValidatorEntry for EpochNumber %d: ", + "_flushSnapshotValidatorSetToDbWithTxn: problem deleting ValidatorEntry for EpochNumber %d: ", mapKey.SnapshotAtEpochNumber, ) } } - // Set all !isDeleted SnapshotValidatorEntries into the db from the UtxoView. - for mapKey, validatorEntry := range bav.SnapshotValidatorEntries { + // Set all !isDeleted SnapshotValidatorSet into the db from the UtxoView. + for mapKey, validatorEntry := range bav.SnapshotValidatorSet { if validatorEntry == nil { return fmt.Errorf( - "_flushSnapshotValidatorEntriesToDb: found nil entry for EpochNumber %d, this should never happen", + "_flushSnapshotValidatorSetToDbWithTxn: found nil entry for EpochNumber %d, this should never happen", mapKey.SnapshotAtEpochNumber, ) } if validatorEntry.isDeleted { - // Skip any deleted SnapshotValidatorEntries. + // Skip any deleted SnapshotValidatorSet. continue } - if err := DBPutSnapshotValidatorEntryWithTxn( + if err := DBPutSnapshotValidatorSetEntryWithTxn( txn, bav.Snapshot, validatorEntry, mapKey.SnapshotAtEpochNumber, blockHeight, ); err != nil { return errors.Wrapf( err, - "_flushSnapshotValidatorEntriesToDb: problem setting ValidatorEntry for EpochNumber %d: ", + "_flushSnapshotValidatorSetToDbWithTxn: problem setting ValidatorEntry for EpochNumber %d: ", mapKey.SnapshotAtEpochNumber, ) } @@ -363,58 +363,58 @@ func (bav *UtxoView) _flushSnapshotValidatorEntriesToDbWithTxn(txn *badger.Txn, return nil } -func DBKeyForSnapshotValidatorByPKID(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) []byte { - key := append([]byte{}, Prefixes.PrefixSnapshotValidatorByPKID...) +func DBKeyForSnapshotValidatorSetByPKID(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) []byte { + key := append([]byte{}, Prefixes.PrefixSnapshotValidatorSetByPKID...) key = append(key, EncodeUint64(snapshotAtEpochNumber)...) key = append(key, validatorEntry.ValidatorPKID.ToBytes()...) return key } -func DBKeyForSnapshotValidatorByStake(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) []byte { - key := append([]byte{}, Prefixes.PrefixSnapshotValidatorByStake...) +func DBKeyForSnapshotValidatorSetByStake(validatorEntry *ValidatorEntry, snapshotAtEpochNumber uint64) []byte { + key := append([]byte{}, Prefixes.PrefixSnapshotValidatorSetByStake...) key = append(key, EncodeUint64(snapshotAtEpochNumber)...) key = append(key, FixedWidthEncodeUint256(validatorEntry.TotalStakeAmountNanos)...) key = append(key, validatorEntry.ValidatorPKID.ToBytes()...) return key } -func DBGetSnapshotValidatorByPKID(handle *badger.DB, snap *Snapshot, pkid *PKID, snapshotAtEpochNumber uint64) (*ValidatorEntry, error) { +func DBGetSnapshotValidatorSetEntryByPKID(handle *badger.DB, snap *Snapshot, pkid *PKID, snapshotAtEpochNumber uint64) (*ValidatorEntry, error) { var ret *ValidatorEntry err := handle.View(func(txn *badger.Txn) error { var innerErr error - ret, innerErr = DBGetSnapshotValidatorByPKIDWithTxn(txn, snap, pkid, snapshotAtEpochNumber) + ret, innerErr = DBGetSnapshotValidatorSetEntryByPKIDWithTxn(txn, snap, pkid, snapshotAtEpochNumber) return innerErr }) return ret, err } -func DBGetSnapshotValidatorByPKIDWithTxn( +func DBGetSnapshotValidatorSetEntryByPKIDWithTxn( txn *badger.Txn, snap *Snapshot, pkid *PKID, snapshotAtEpochNumber uint64, ) (*ValidatorEntry, error) { // Retrieve ValidatorEntry from db. - key := DBKeyForSnapshotValidatorByPKID(&ValidatorEntry{ValidatorPKID: pkid}, snapshotAtEpochNumber) + key := DBKeyForSnapshotValidatorSetByPKID(&ValidatorEntry{ValidatorPKID: pkid}, snapshotAtEpochNumber) validatorBytes, err := DBGetWithTxn(txn, snap, key) if err != nil { // We don't want to error if the key isn't found. Instead, return nil. if err == badger.ErrKeyNotFound { return nil, nil } - return nil, errors.Wrapf(err, "DBGetSnapshotValidatorByPKID: problem retrieving ValidatorEntry") + return nil, errors.Wrapf(err, "DBGetSnapshotValidatorSetEntryByPKIDWithTxn: problem retrieving ValidatorEntry") } // Decode ValidatorEntry from bytes. validatorEntry := &ValidatorEntry{} rr := bytes.NewReader(validatorBytes) if exist, err := DecodeFromBytes(validatorEntry, rr); !exist || err != nil { - return nil, errors.Wrapf(err, "DBGetSnapshotValidatorByPKID: problem decoding ValidatorEntry") + return nil, errors.Wrapf(err, "DBGetSnapshotValidatorSetEntryByPKIDWithTxn: problem decoding ValidatorEntry") } return validatorEntry, nil } -func DBGetSnapshotTopActiveValidatorsByStake( +func DBGetSnapshotValidatorSetByStake( handle *badger.DB, snap *Snapshot, limit uint64, @@ -426,17 +426,17 @@ func DBGetSnapshotTopActiveValidatorsByStake( // Convert ValidatorEntriesToSkip to ValidatorEntryKeysToSkip. validatorKeysToSkip := NewSet([]string{}) for _, validatorEntryToSkip := range validatorEntriesToSkip { - validatorKeysToSkip.Add(string(DBKeyForSnapshotValidatorByStake(validatorEntryToSkip, snapshotAtEpochNumber))) + validatorKeysToSkip.Add(string(DBKeyForSnapshotValidatorSetByStake(validatorEntryToSkip, snapshotAtEpochNumber))) } // Retrieve top N active ValidatorEntry keys by stake. - key := append([]byte{}, Prefixes.PrefixSnapshotValidatorByStake...) + key := append([]byte{}, Prefixes.PrefixSnapshotValidatorSetByStake...) key = append(key, EncodeUint64(snapshotAtEpochNumber)...) keysFound, _, err := EnumerateKeysForPrefixWithLimitOffsetOrder( handle, key, int(limit), nil, true, validatorKeysToSkip, ) if err != nil { - return nil, errors.Wrapf(err, "DBGetSnapshotTopActiveValidatorsByStake: problem retrieving top validators: ") + return nil, errors.Wrapf(err, "DBGetSnapshotValidatorSetByStake: problem retrieving top validators: ") } // For each key found, parse the ValidatorPKID from the key, @@ -447,12 +447,12 @@ func DBGetSnapshotTopActiveValidatorsByStake( // Convert PKIDBytes to PKID. validatorPKID := &PKID{} if err = validatorPKID.FromBytes(bytes.NewReader(validatorPKIDBytes)); err != nil { - return nil, errors.Wrapf(err, "DBGetSnapshotTopActiveValidatorsByStake: problem reading ValidatorPKID: ") + return nil, errors.Wrapf(err, "DBGetSnapshotValidatorSetByStake: problem reading ValidatorPKID: ") } // Retrieve ValidatorEntry by PKID. - validatorEntry, err := DBGetSnapshotValidatorByPKID(handle, snap, validatorPKID, snapshotAtEpochNumber) + validatorEntry, err := DBGetSnapshotValidatorSetEntryByPKID(handle, snap, validatorPKID, snapshotAtEpochNumber) if err != nil { - return nil, errors.Wrapf(err, "DBGetSnapshotTopActiveValidatorsByStake: problem retrieving validator by PKID: ") + return nil, errors.Wrapf(err, "DBGetSnapshotValidatorSetByStake: problem retrieving validator by PKID: ") } validatorEntries = append(validatorEntries, validatorEntry) } @@ -460,7 +460,7 @@ func DBGetSnapshotTopActiveValidatorsByStake( return validatorEntries, nil } -func DBPutSnapshotValidatorEntryWithTxn( +func DBPutSnapshotValidatorSetEntryWithTxn( txn *badger.Txn, snap *Snapshot, validatorEntry *ValidatorEntry, @@ -469,67 +469,67 @@ func DBPutSnapshotValidatorEntryWithTxn( ) error { if validatorEntry == nil { // This should never happen but is a sanity check. - glog.Errorf("DBPutSnapshotValidatorEntryWithTxn: called with nil ValidatorEntry, this should never happen") + glog.Errorf("DBPutSnapshotValidatorSetEntryWithTxn: called with nil ValidatorEntry, this should never happen") return nil } - // Put the ValidatorEntry in the SnapshotValidatorByPKID index. - key := DBKeyForSnapshotValidatorByPKID(validatorEntry, snapshotAtEpochNumber) + // Put the ValidatorEntry in the SnapshotSetByPKID index. + key := DBKeyForSnapshotValidatorSetByPKID(validatorEntry, snapshotAtEpochNumber) if err := DBSetWithTxn(txn, snap, key, EncodeToBytes(blockHeight, validatorEntry)); err != nil { return errors.Wrapf( err, - "DBPutSnapshotValidatorEntryWithTxn: problem putting ValidatorEntry in the SnapshotValidatorByPKID index: ", + "DBPutSnapshotValidatorSetEntryWithTxn: problem putting ValidatorEntry in the SnapshotValidatorByPKID index: ", ) } // Put the ValidatorPKID in the SnapshotValidatorByStatusAndStake index. - key = DBKeyForSnapshotValidatorByStake(validatorEntry, snapshotAtEpochNumber) + key = DBKeyForSnapshotValidatorSetByStake(validatorEntry, snapshotAtEpochNumber) if err := DBSetWithTxn(txn, snap, key, EncodeToBytes(blockHeight, validatorEntry.ValidatorPKID)); err != nil { return errors.Wrapf( err, - "DBPutSnapshotValidatorEntryWithTxn: problem putting ValidatorPKID in the SnapshotValidatorByStake index: ", + "DBPutSnapshotValidatorSetEntryWithTxn: problem putting ValidatorPKID in the SnapshotValidatorByStake index: ", ) } return nil } -func DBDeleteSnapshotValidatorEntryWithTxn( +func DBDeleteSnapshotValidatorSetEntryWithTxn( txn *badger.Txn, snap *Snapshot, validatorPKID *PKID, snapshotAtEpochNumber uint64, ) error { if validatorPKID == nil { // This should never happen but is a sanity check. - glog.Errorf("DBDeleteSnapshotValidatorEntryWithTxn: called with nil ValidatorPKID") + glog.Errorf("DBDeleteSnapshotValidatorSetEntryWithTxn: called with nil ValidatorPKID") return nil } - // Look up the existing SnapshotValidatorEntry in the db using the PKID. + // Look up the existing SnapshotValidatorSetEntry in the db using the PKID. // We need to use this validator's values to delete the corresponding indexes. - validatorEntry, err := DBGetSnapshotValidatorByPKIDWithTxn(txn, snap, validatorPKID, snapshotAtEpochNumber) + snapshotValidatorSetEntry, err := DBGetSnapshotValidatorSetEntryByPKIDWithTxn(txn, snap, validatorPKID, snapshotAtEpochNumber) if err != nil { return errors.Wrapf( - err, "DBDeleteSnapshotValidatorEntryWithTxn: problem retrieving ValidatorEntry for PKID %v: ", validatorPKID, + err, "DBDeleteSnapshotValidatorSetEntryWithTxn: problem retrieving ValidatorEntry for PKID %v: ", validatorPKID, ) } // If there is no ValidatorEntry in the DB for this PKID, then there is nothing to delete. - if validatorEntry == nil { + if snapshotValidatorSetEntry == nil { return nil } - // Delete ValidatorEntry from PrefixSnapshotValidatorByPKID. - key := DBKeyForSnapshotValidatorByPKID(validatorEntry, snapshotAtEpochNumber) + // Delete ValidatorEntry from PrefixSnapshotSetByPKID. + key := DBKeyForSnapshotValidatorSetByPKID(snapshotValidatorSetEntry, snapshotAtEpochNumber) if err = DBDeleteWithTxn(txn, snap, key); err != nil { return errors.Wrapf( - err, "DBDeleteSnapshotValidatorEntryWithTxn: problem deleting ValidatorEntry from index PrefixSnapshotValidatorByPKID", + err, "DBDeleteSnapshotValidatorSetEntryWithTxn: problem deleting ValidatorEntry from index PrefixSnapshotSetByPKID", ) } // Delete ValidatorEntry.PKID from PrefixSnapshotValidatorByStatusAndStake. - key = DBKeyForSnapshotValidatorByStake(validatorEntry, snapshotAtEpochNumber) + key = DBKeyForSnapshotValidatorSetByStake(snapshotValidatorSetEntry, snapshotAtEpochNumber) if err = DBDeleteWithTxn(txn, snap, key); err != nil { return errors.Wrapf( - err, "DBDeleteSnapshotValidatorEntryWithTxn: problem deleting ValidatorEntry from index PrefixSnapshotValidatorByStatusAndStake", + err, "DBDeleteSnapshotValidatorSetEntryWithTxn: problem deleting ValidatorEntry from index PrefixSnapshotValidatorByStatusAndStake", ) } @@ -693,7 +693,7 @@ func (bav *UtxoView) GetSnapshotLeaderScheduleValidator(leaderIndex uint16) (*Va // First, check the UtxoView. mapKey := SnapshotLeaderScheduleMapKey{SnapshotAtEpochNumber: snapshotAtEpochNumber, LeaderIndex: leaderIndex} if validatorPKID, exists := bav.SnapshotLeaderSchedule[mapKey]; exists { - return bav.GetSnapshotValidatorByPKID(validatorPKID) + return bav.GetSnapshotValidatorSetEntryByPKID(validatorPKID) } // Next, check the db. validatorEntry, err := DBGetSnapshotLeaderScheduleValidator(bav.Handle, bav.Snapshot, leaderIndex, snapshotAtEpochNumber) @@ -784,7 +784,7 @@ func DBGetSnapshotLeaderScheduleValidatorWithTxn( } // Retrieve ValidatorEntry by PKID from db. - return DBGetSnapshotValidatorByPKIDWithTxn(txn, snap, validatorPKID, snapshotAtEpochNumber) + return DBGetSnapshotValidatorSetEntryByPKIDWithTxn(txn, snap, validatorPKID, snapshotAtEpochNumber) } func DBPutSnapshotLeaderScheduleValidatorWithTxn(