Skip to content

Commit

Permalink
GROW-705 - Decouple EpochUpdater from ProviderStateQuery (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirtayeb authored Apr 27, 2023
1 parent 43c7af7 commit 7aac888
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions protocol/statetracker/epoch_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type EpochUpdatable interface {
type EpochUpdater struct {
epochUpdatables []*EpochUpdatable
currentEpoch uint64
stateQuery *ProviderStateQuery
stateQuery *EpochStateQuery
}

func NewEpochUpdater(stateQuery *ProviderStateQuery) *EpochUpdater {
func NewEpochUpdater(stateQuery *EpochStateQuery) *EpochUpdater {
return &EpochUpdater{epochUpdatables: []*EpochUpdatable{}, stateQuery: stateQuery}
}

Expand Down
2 changes: 1 addition & 1 deletion protocol/statetracker/provider_state_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewProviderStateTracker(ctx context.Context, txFactory tx.Factory, clientCt
}

func (pst *ProviderStateTracker) RegisterForEpochUpdates(ctx context.Context, epochUpdatable EpochUpdatable) {
epochUpdater := NewEpochUpdater(pst.stateQuery)
epochUpdater := NewEpochUpdater(&pst.stateQuery.EpochStateQuery)
epochUpdaterRaw := pst.StateTracker.RegisterForUpdates(ctx, epochUpdater)
epochUpdater, ok := epochUpdaterRaw.(*EpochUpdater)
if !ok {
Expand Down
31 changes: 21 additions & 10 deletions protocol/statetracker/state_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,33 @@ func (csq *ConsumerStateQuery) GetMaxCUForUser(ctx context.Context, chainID stri
return UserEntryRes.GetMaxCU(), nil
}

type EpochStateQuery struct {
StateQuery
}

func (esq *EpochStateQuery) CurrentEpochStart(ctx context.Context) (uint64, error) {
epochDetails, err := esq.EpochStorageQueryClient.EpochDetails(ctx, &epochstoragetypes.QueryGetEpochDetailsRequest{})
if err != nil {
return 0, utils.LavaFormatError("Failed Querying EpochDetails", err)
}
details := epochDetails.GetEpochDetails()
return details.StartBlock, nil
}

func NewEpochStateQuery(stateQuery *StateQuery) *EpochStateQuery {
return &EpochStateQuery{StateQuery: *stateQuery}
}

type ProviderStateQuery struct {
StateQuery
EpochStateQuery
clientCtx client.Context
}

func NewProviderStateQuery(ctx context.Context, clientCtx client.Context) *ProviderStateQuery {
csq := &ProviderStateQuery{StateQuery: *NewStateQuery(ctx, clientCtx), clientCtx: clientCtx}
sq := NewStateQuery(ctx, clientCtx)
esq := NewEpochStateQuery(sq)
csq := &ProviderStateQuery{StateQuery: *sq, EpochStateQuery: *esq, clientCtx: clientCtx}
return csq
}

Expand Down Expand Up @@ -150,15 +170,6 @@ func (psq *ProviderStateQuery) entryKey(consumerAddress string, chainID string,
return consumerAddress + chainID + strconv.FormatUint(epoch, 10) + providerAddress
}

func (psq *ProviderStateQuery) CurrentEpochStart(ctx context.Context) (uint64, error) {
epochDetails, err := psq.EpochStorageQueryClient.EpochDetails(ctx, &epochstoragetypes.QueryGetEpochDetailsRequest{})
if err != nil {
return 0, utils.LavaFormatError("Failed Querying EpochDetails", err)
}
details := epochDetails.GetEpochDetails()
return details.StartBlock, nil
}

func (psq *ProviderStateQuery) PaymentEvents(ctx context.Context, latestBlock int64) (payments []*rewardserver.PaymentRequest, err error) {
blockResults, err := psq.clientCtx.Client.BlockResults(ctx, &latestBlock)
if err != nil {
Expand Down

0 comments on commit 7aac888

Please sign in to comment.