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

PoS Rename SnapshotValidatorEntries to SnapshotValidatorSet #583

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
17 changes: 9 additions & 8 deletions lib/block_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SnapshotAtEpochNumber, ValidatorPKID> to a ValidatorEntry.
// It contains the snapshot value of a ValidatorEntry at the given SnapshotAtEpochNumber.
SnapshotValidatorEntries map[SnapshotValidatorMapKey]*ValidatorEntry
// SnapshotValidatorSet is a map of <SnapshotAtEpochNumber, ValidatorPKID> 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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions lib/block_view_flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions lib/db_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,16 @@ type DBPrefixes struct {
// Prefix, <SnapshotAtEpochNumber uint64> -> *GlobalParamsEntry
PrefixSnapshotGlobalParamsEntry []byte `prefix_id:"[85]" is_state:"true"`

// PrefixSnapshotValidatorByPKID: Retrieve a snapshot ValidatorEntry by <SnapshotAtEpochNumber, PKID>.
// PrefixSnapshotValidatorSetByPKID: Retrieve a ValidatorEntry from a snapshot validator set by
// <SnapshotAtEpochNumber, PKID>.
// Prefix, <SnapshotAtEpochNumber uint64>, <ValidatorPKID [33]byte> -> *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, <SnapshotAtEpochNumber uint64>, <TotalStakeAmountNanos *uint256.Int>, <ValidatorPKID [33]byte> -> 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, <SnapshotAtEpochNumber uint64> -> *uint256.Int
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/pos_epoch_complete_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 14 additions & 14 deletions lib/pos_epoch_complete_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
Loading