Skip to content

Commit

Permalink
Rm old getactivetopics 1 (#558)
Browse files Browse the repository at this point in the history
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺
v           ✰  Thanks for creating a PR! You're awesome! ✰
v Please note that maintainers will only review those PRs with a
completed PR template.
☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >  -->

## Purpose of Changes and their Description

## Link(s) to Ticket(s) or Issue(s) resolved by this PR

## Are these changes tested and documented?

- [ ] If tested, please describe how. If not, why tests are not needed.
- [ ] If documented, please describe where. If not, describe why docs
are not needed.
- [ ] Added to `Unreleased` section of `CHANGELOG.md`?

## Still Left Todo

*Fill this out if this is a Draft PR so others can help.*
  • Loading branch information
kpeluso authored Aug 30, 2024
1 parent f0e930a commit ba7236c
Show file tree
Hide file tree
Showing 16 changed files with 1,415 additions and 2,026 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ Implements fixes for our [June 2024](https://github.com/sherlock-audit/2024-06-a

### Added

* To be filled in before release of v0.4.0
* [#542](https://github.com/allora-network/allora-chain/pull/542) Add scalable management of active topics with associated queries such as `GetActiveTopicsAtBlock` and `GetNextChurningBlockByTopicId`

### Removed

* To be filled in before release of v0.4.0

* [#542](https://github.com/allora-network/allora-chain/pull/542) As part of active topic management, we removed `GetActiveTopics` and other (especially paginated) remnants of an unpartitioned store of active topics.

### Fixed

Expand Down
12 changes: 4 additions & 8 deletions test/integration/fund_topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ func FundTopic1(m testCommon.TestConfig) {
func CheckTopic1Activated(m testCommon.TestConfig) {
ctx := context.Background()
// Fetch only active topics
pagi := &emissionstypes.QueryActiveTopicsRequest{
Pagination: &emissionstypes.SimpleCursorPaginationRequest{
Limit: 10,
},
}
activeTopics, err := m.Client.QueryEmissions().GetActiveTopics(
topicIsActive, err := m.Client.QueryEmissions().IsTopicActive(
ctx,
pagi)
&emissionstypes.QueryIsTopicActiveRequest{TopicId: 1},
)
require.NoError(m.T, err, "Fetching active topics should not produce an error")

// Verify the correct number of active topics is retrieved
require.Len(m.T, activeTopics.Topics, 1, "Should retrieve exactly one active topics")
require.True(m.T, topicIsActive.IsActive, "Should retrieve exactly one active topics")
}

// Must come after a reputer is registered and staked in topic 1
Expand Down
9 changes: 4 additions & 5 deletions test/invariant/inferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,18 @@ func doInferenceAndReputation(
}

// determine if this state transition is worth trying based on our knowledge of the state
func findActiveTopics(
func findActiveTopicsAtThisBlock(
m *testcommon.TestConfig,
data *SimulationData,
blockHeight int64,
) []*emissionstypes.Topic {
// first off someone has to be registered for both working and reputing in general
if !anyReputersRegistered(data) || !anyWorkersRegistered(data) {
return nil
}
ctx := context.Background()
response, err := m.Client.QueryEmissions().GetActiveTopics(ctx, &emissionstypes.QueryActiveTopicsRequest{
Pagination: &emissionstypes.SimpleCursorPaginationRequest{
Limit: 10,
},
response, err := m.Client.QueryEmissions().GetActiveTopicsAtBlock(ctx, &emissionstypes.QueryActiveTopicsAtBlockRequest{
BlockHeight: blockHeight,
})
requireNoError(m.T, data.failOnErr, err)
return response.Topics
Expand Down
8 changes: 6 additions & 2 deletions test/invariant/transitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func canTransitionOccur(m *testcommon.TestConfig, data *SimulationData, transiti
// figure this out in picking step
return true
case "doInferenceAndReputation":
activeTopics := findActiveTopics(m, data)
blockHeight, err := m.Client.BlockHeight(m.Client.Context().CmdContext)
requireNoError(m.T, data.failOnErr, err)
activeTopics := findActiveTopicsAtThisBlock(m, data, blockHeight)
for i := 0; i < len(activeTopics); i++ {
workerExists := data.isAnyWorkerRegisteredInTopic(activeTopics[i].Id)
reputerExists := data.isAnyReputerRegisteredInTopic(activeTopics[i].Id)
Expand Down Expand Up @@ -279,7 +281,9 @@ func pickActorAndTopicIdForStateTransition(
}
return true, delegator, reputer, &stakeRemoval.Amount, stakeRemoval.TopicId
case "doInferenceAndReputation":
topics := findActiveTopics(m, data)
blockHeight, err := m.Client.BlockHeight(m.Client.Context().CmdContext)
requireNoError(m.T, data.failOnErr, err)
topics := findActiveTopicsAtThisBlock(m, data, blockHeight)
if len(topics) > 0 {
for i := 0; i < 10; i++ {
randIndex := m.Client.Rand.Intn(len(topics))
Expand Down
2,160 changes: 1,028 additions & 1,132 deletions x/emissions/api/v3/query.pulsar.go

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions x/emissions/api/v3/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 0 additions & 85 deletions x/emissions/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,91 +1921,6 @@ func (k *Keeper) GetNextTopicId(ctx context.Context) (TopicId, error) {
return k.nextTopicId.Peek(ctx)
}

// Returns all active topic ids using pagination
func (k Keeper) GetIdsOfActiveTopics(ctx context.Context, pagination *types.SimpleCursorPaginationRequest) ([]TopicId, *types.SimpleCursorPaginationResponse, error) {
limit, start, err := k.CalcAppropriatePaginationForUint64Cursor(ctx, pagination)
if err != nil {
return nil, nil, err
}
rng := new(collections.Range[uint64]).StartExclusive(start)

iter, err := k.activeTopics.Iterate(ctx, rng)
if err != nil {
return nil, nil, err
}
defer iter.Close()

activeTopicIds := make([]TopicId, 0)
nextKey := make([]byte, binary.MaxVarintLen64)
nextTopicId := uint64(0)
for ; iter.Valid(); iter.Next() {
topicId, err := iter.Key()
if err != nil {
return nil, nil, err
}

if uint64(len(activeTopicIds)) >= limit {
break
}

activeTopicIds = append(activeTopicIds, topicId)
nextTopicId = topicId
}
binary.BigEndian.PutUint64(nextKey, nextTopicId)

// If there are no topics, we return the nil for next key
if len(activeTopicIds) == 0 {
nextKey = make([]byte, 0)
}

return activeTopicIds, &types.SimpleCursorPaginationResponse{
NextKey: nextKey,
}, nil
}

// Returns active topic ids at block using pagination
func (k Keeper) GetIdsActiveTopicAtBlock(ctx context.Context, blockHeight int64, pagination *types.SimpleCursorPaginationRequest) ([]TopicId, *types.SimpleCursorPaginationResponse, error) {
limit, start, err := k.CalcAppropriatePaginationForUint64Cursor(ctx, pagination)
if err != nil {
return nil, nil, err
}

end := start + limit
topicIds, err := k.GetActiveTopicIdsAtBlock(ctx, blockHeight)
if err != nil {
return nil, nil, err
}
topicIdsCnt := uint64(len(topicIds.TopicIds))
if topicIdsCnt <= start {
return nil, nil, err
}

if topicIdsCnt < end {
end = uint64(len(topicIds.TopicIds))
}

activeTopicIds := make([]TopicId, 0)
nextKey := make([]byte, binary.MaxVarintLen64)
nextTopicId := uint64(0)
for index := start; index < end; index++ {
id := topicIds.TopicIds[index]

activeTopicIds = append(activeTopicIds, id)
nextTopicId = index + 1
}

binary.BigEndian.PutUint64(nextKey, nextTopicId)

// If there are no topics, we return the nil for next key
if len(activeTopicIds) == 0 {
nextKey = make([]byte, 0)
}

return activeTopicIds, &types.SimpleCursorPaginationResponse{
NextKey: nextKey,
}, nil
}

// Check if the topic is activated or not
func (k *Keeper) IsTopicActive(ctx context.Context, topicId TopicId) (bool, error) {
_, active, err := k.GetNextPossibleChurningBlockByTopicId(ctx, topicId)
Expand Down
Loading

0 comments on commit ba7236c

Please sign in to comment.