Skip to content

Commit

Permalink
Merge pull request #5080 from onflow/yurii/4649-todos-and-refactoring…
Browse files Browse the repository at this point in the history
…-part-2
  • Loading branch information
durkmurder authored Dec 8, 2023
2 parents 7587d3a + 66b1bab commit 7fdc7fb
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 84 deletions.
3 changes: 2 additions & 1 deletion cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,8 @@ func (fnb *FlowNodeBuilder) initStorage() error {
setups := bstorage.NewEpochSetups(fnb.Metrics.Cache, fnb.DB)
epochCommits := bstorage.NewEpochCommits(fnb.Metrics.Cache, fnb.DB)
commits := bstorage.NewCommits(fnb.Metrics.Cache, fnb.DB)
protocolState := bstorage.NewProtocolState(fnb.Metrics.Cache, setups, epochCommits, fnb.DB, bstorage.DefaultCacheSize)
protocolState := bstorage.NewProtocolState(fnb.Metrics.Cache, setups, epochCommits, fnb.DB,
bstorage.DefaultProtocolStateCacheSize, bstorage.DefaultProtocolStateByBlockIDCacheSize)
versionBeacons := bstorage.NewVersionBeacons(fnb.DB)

fnb.Storage = Storage{
Expand Down
3 changes: 2 additions & 1 deletion consensus/integration/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ func createNode(
qcsDB := storage.NewQuorumCertificates(metricsCollector, db, storage.DefaultCacheSize)
setupsDB := storage.NewEpochSetups(metricsCollector, db)
commitsDB := storage.NewEpochCommits(metricsCollector, db)
protocolStateDB := storage.NewProtocolState(metricsCollector, setupsDB, commitsDB, db, storage.DefaultCacheSize)
protocolStateDB := storage.NewProtocolState(metricsCollector, setupsDB, commitsDB, db,
storage.DefaultProtocolStateCacheSize, storage.DefaultProtocolStateByBlockIDCacheSize)
versionBeaconDB := storage.NewVersionBeacons(db)
protocolStateEvents := events.NewDistributor()

Expand Down
3 changes: 2 additions & 1 deletion integration/testnet/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ func (c *Container) OpenState() (*state.State, error) {
qcs := storage.NewQuorumCertificates(metrics, db, storage.DefaultCacheSize)
setups := storage.NewEpochSetups(metrics, db)
commits := storage.NewEpochCommits(metrics, db)
protocolState := storage.NewProtocolState(metrics, setups, commits, db, storage.DefaultCacheSize)
protocolState := storage.NewProtocolState(metrics, setups, commits, db,
storage.DefaultProtocolStateCacheSize, storage.DefaultProtocolStateByBlockIDCacheSize)
versionBeacons := storage.NewVersionBeacons(db)

return state.OpenState(
Expand Down
1 change: 0 additions & 1 deletion model/flow/protocol_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type DynamicIdentityEntryList []*DynamicIdentityEntry
// Note that the current implementation does not store the identity table directly. Instead, we store
// the original events that constituted the _initial_ identity table at the beginning of the epoch
// plus some modifiers. We intend to restructure this code soon.
// TODO: https://github.com/onflow/flow-go/issues/4649
type ProtocolStateEntry struct {
PreviousEpoch *EpochStateContainer // minimal dynamic properties for previous epoch [optional, nil for first epoch after spork, genesis]
CurrentEpoch EpochStateContainer // minimal dynamic properties for current epoch
Expand Down
88 changes: 86 additions & 2 deletions model/flow/protocol_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,52 @@ func TestNewRichProtocolStateEntry(t *testing.T) {
assert.Equal(t, expectedIdentities, richEntry.NextEpochIdentityTable, "should be equal to next epoch setup participants + current epoch setup participants")
})

// TODO: include test for epoch setup phase where no prior epoch exist (i.e. first epoch setup phase after spork)
t.Run("setup-after-spork", func(t *testing.T) {
stateEntry := unittest.ProtocolStateFixture(unittest.WithNextEpochProtocolState(), func(entry *flow.RichProtocolStateEntry) {
// no previous epoch since we are in the first epoch
entry.PreviousEpochSetup = nil
entry.PreviousEpochCommit = nil
entry.PreviousEpoch = nil

// next epoch is setup but not committed
entry.NextEpochCommit = nil
entry.NextEpoch.CommitID = flow.ZeroID
})
// sanity check that previous epoch is not populated in `stateEntry`
assert.Nil(t, stateEntry.PreviousEpoch)
assert.Nil(t, stateEntry.PreviousEpochSetup)
assert.Nil(t, stateEntry.PreviousEpochCommit)

richEntry, err := flow.NewRichProtocolStateEntry(
stateEntry.ProtocolStateEntry,
stateEntry.PreviousEpochSetup,
stateEntry.PreviousEpochCommit,
stateEntry.CurrentEpochSetup,
stateEntry.CurrentEpochCommit,
stateEntry.NextEpochSetup,
nil,
)
assert.NoError(t, err)
expectedIdentities, err := flow.BuildIdentityTable(
stateEntry.CurrentEpochSetup.Participants,
stateEntry.CurrentEpoch.ActiveIdentities,
stateEntry.NextEpochSetup.Participants,
stateEntry.NextEpoch.ActiveIdentities,
flow.EpochParticipationStatusJoining,
)
assert.NoError(t, err)
assert.Equal(t, expectedIdentities, richEntry.CurrentEpochIdentityTable, "should be equal to current epoch setup participants + next epoch setup participants")
assert.Nil(t, richEntry.NextEpochCommit)
expectedIdentities, err = flow.BuildIdentityTable(
stateEntry.NextEpochSetup.Participants,
stateEntry.NextEpoch.ActiveIdentities,
stateEntry.CurrentEpochSetup.Participants,
stateEntry.CurrentEpoch.ActiveIdentities,
flow.EpochParticipationStatusLeaving,
)
assert.NoError(t, err)
assert.Equal(t, expectedIdentities, richEntry.NextEpochIdentityTable, "should be equal to next epoch setup participants + current epoch setup participants")
})

// Common situation during the epoch commit phase for epoch N+1
// * we are currently in Epoch N
Expand Down Expand Up @@ -165,8 +210,47 @@ func TestNewRichProtocolStateEntry(t *testing.T) {
assert.Equal(t, expectedIdentities, richEntry.NextEpochIdentityTable, "should be equal to next epoch setup participants + current epoch setup participants")
})

// TODO: include test for epoch commit phase where no prior epoch exist (i.e. first epoch commit phase after spork)
t.Run("commit-after-spork", func(t *testing.T) {
stateEntry := unittest.ProtocolStateFixture(unittest.WithNextEpochProtocolState(), func(entry *flow.RichProtocolStateEntry) {
// no previous epoch since we are in the first epoch
entry.PreviousEpochSetup = nil
entry.PreviousEpochCommit = nil
entry.PreviousEpoch = nil
})
// sanity check that previous epoch is not populated in `stateEntry`
assert.Nil(t, stateEntry.PreviousEpoch)
assert.Nil(t, stateEntry.PreviousEpochSetup)
assert.Nil(t, stateEntry.PreviousEpochCommit)

richEntry, err := flow.NewRichProtocolStateEntry(
stateEntry.ProtocolStateEntry,
stateEntry.PreviousEpochSetup,
stateEntry.PreviousEpochCommit,
stateEntry.CurrentEpochSetup,
stateEntry.CurrentEpochCommit,
stateEntry.NextEpochSetup,
stateEntry.NextEpochCommit,
)
assert.NoError(t, err)
expectedIdentities, err := flow.BuildIdentityTable(
stateEntry.CurrentEpochSetup.Participants,
stateEntry.CurrentEpoch.ActiveIdentities,
stateEntry.NextEpochSetup.Participants,
stateEntry.NextEpoch.ActiveIdentities,
flow.EpochParticipationStatusJoining,
)
assert.NoError(t, err)
assert.Equal(t, expectedIdentities, richEntry.CurrentEpochIdentityTable, "should be equal to current epoch setup participants + next epoch setup participants")
expectedIdentities, err = flow.BuildIdentityTable(
stateEntry.NextEpochSetup.Participants,
stateEntry.NextEpoch.ActiveIdentities,
stateEntry.CurrentEpochSetup.Participants,
stateEntry.CurrentEpoch.ActiveIdentities,
flow.EpochParticipationStatusLeaving,
)
assert.NoError(t, err)
assert.Equal(t, expectedIdentities, richEntry.NextEpochIdentityTable, "should be equal to next epoch setup participants + current epoch setup participants")
})
}

// TestProtocolStateEntry_Copy tests if the copy method returns a deep copy of the entry.
Expand Down
1 change: 1 addition & 0 deletions module/metrics/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
ResourceMyReceipt = "my_receipt"
ResourceCollection = "collection"
ResourceProtocolState = "protocol_state"
ResourceProtocolStateByBlockID = "protocol_state_by_block_id"
ResourceApproval = "approval"
ResourceSeal = "seal"
ResourcePendingIncorporatedSeal = "pending_incorporated_seal"
Expand Down
3 changes: 2 additions & 1 deletion storage/badger/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func InitAll(metrics module.CacheMetrics, db *badger.DB) *storage.All {
qcs := NewQuorumCertificates(metrics, db, DefaultCacheSize)
setups := NewEpochSetups(metrics, db)
epochCommits := NewEpochCommits(metrics, db)
protocolState := NewProtocolState(metrics, setups, epochCommits, db, DefaultCacheSize)
protocolState := NewProtocolState(metrics, setups, epochCommits, db,
DefaultProtocolStateCacheSize, DefaultProtocolStateByBlockIDCacheSize)
versionBeacons := NewVersionBeacons(db)

commits := NewCommits(metrics, db)
Expand Down
Loading

0 comments on commit 7fdc7fb

Please sign in to comment.