/
topics_handler.go
80 lines (69 loc) · 2.63 KB
/
topics_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package app
import (
"fmt"
"sync"
emissionstypes "github.com/allora-network/allora-chain/x/emissions"
emissionskeeper "github.com/allora-network/allora-chain/x/emissions/keeper"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type TopicsHandler struct {
emissionsKeeper emissionskeeper.Keeper
}
func NewTopicsHandler(emissionsKeeper emissionskeeper.Keeper) *TopicsHandler {
return &TopicsHandler{
emissionsKeeper: emissionsKeeper,
}
}
func (th *TopicsHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
fmt.Printf("\n ---------------- TopicsHandler ------------------- \n")
currentTime := uint64(ctx.BlockTime().Unix())
churnReadyTopics, err := th.emissionsKeeper.GetChurnReadyTopics(ctx)
if err != nil {
fmt.Println("Error getting active topics and met demand: ", err)
return nil, err
}
var wg sync.WaitGroup
// Loop over and run epochs on topics whose inferences are demanded enough to be served
// Within each loop, execute the inference and weight cadence checks and trigger the inference and weight generation
for _, topic := range churnReadyTopics.Topics {
// Parallelize the inference and weight cadence checks
wg.Add(1)
go func(topic *emissionstypes.Topic) {
defer wg.Done()
// Check the cadence of inferences
if currentTime-topic.InferenceLastRan >= topic.InferenceCadence {
fmt.Printf("Triggering inference generation for topic: %v metadata: %s default arg: %s. \n",
topic.Id,
topic.Metadata,
topic.DefaultArg)
go generateInferences(topic.InferenceLogic, topic.InferenceMethod, topic.DefaultArg, topic.Id)
}
// Check the cadence of weight calculations
if currentTime-topic.WeightLastRan >= topic.WeightCadence {
fmt.Printf("Triggering Weight cadence met for topic: %v metadata: %s default arg: %s \n",
topic.Id,
topic.Metadata,
topic.DefaultArg)
// Get Latest Weights
weights, err := th.emissionsKeeper.GetWeightsFromTopic(ctx, topic.Id)
fmt.Println("Error getting latest weights: ", err)
if err != nil {
return
}
inferences, err := th.emissionsKeeper.GetLatestInferencesFromTopic(ctx, topic.Id)
// Get Lastest Inference
fmt.Println("Error getting latest inferences: ", err)
if err != nil {
return
}
go generateWeights(weights, inferences, topic.WeightLogic, topic.WeightMethod, topic.Id)
}
}(topic)
}
wg.Wait()
// Return the transactions as they came
return &abci.ResponsePrepareProposal{Txs: req.Txs}, nil
}
}