Skip to content

Commit

Permalink
feat(perf): removed unneeded state-index query (dymensionxyz#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Apr 17, 2024
1 parent 4c05a1d commit 25afe20
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
16 changes: 5 additions & 11 deletions settlement/dymension/dymension.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,11 @@ func (d *HubClient) PostBatch(batch *types.Batch, daClient da.Client, daResult *

// GetLatestBatch returns the latest batch from the Dymension Hub.
func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieveBatch, error) {
var latestStateInfoIndexResp *rollapptypes.QueryGetLatestStateIndexResponse

var stateInfoResp *rollapptypes.QueryGetStateInfoResponse
err := d.RunWithRetry(func() error {
var err error
latestStateInfoIndexResp, err = d.rollappQueryClient.LatestStateIndex(d.ctx,
&rollapptypes.QueryGetLatestStateIndexRequest{RollappId: d.config.RollappID})
stateInfoResp, err = d.rollappQueryClient.StateInfo(d.ctx,
&rollapptypes.QueryGetStateInfoRequest{RollappId: d.config.RollappID})

if status.Code(err) == codes.NotFound {
return retry.Unrecoverable(settlement.ErrBatchNotFound)
Expand All @@ -302,17 +301,12 @@ func (d *HubClient) GetLatestBatch(rollappID string) (*settlement.ResultRetrieve
if err != nil {
return nil, err
}

// not supposed to happen, but just in case
if latestStateInfoIndexResp == nil {
if stateInfoResp == nil {
return nil, settlement.ErrEmptyResponse
}

latestBatch, err := d.GetBatchAtIndex(rollappID, latestStateInfoIndexResp.StateIndex.Index)
if err != nil {
return nil, err
}
return latestBatch, nil
return d.convertStateInfoToResultRetrieveBatch(&stateInfoResp.StateInfo)
}

// GetBatchAtIndex returns the batch at the given index from the Dymension Hub.
Expand Down
5 changes: 1 addition & 4 deletions settlement/dymension/dymension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ func TestPostBatch(t *testing.T) {
wg.Add(eventsCount)
// Reset the mock functions
testutil.UnsetMockFn(cosmosClientMock.On("BroadcastTx"))
testutil.UnsetMockFn(rollappQueryClientMock.On("LatestStateIndex"))
testutil.UnsetMockFn(rollappQueryClientMock.On("StateInfo"))
// Set the mock logic based on the test case
if !c.isBatchSubmitSuccess {
Expand All @@ -174,8 +173,6 @@ func TestPostBatch(t *testing.T) {
}
if c.shouldMockBatchIncluded {
if c.isBatchIncludedSuccess {
rollappQueryClientMock.On("LatestStateIndex", mock.Anything, mock.Anything).Return(
&rollapptypes.QueryGetLatestStateIndexResponse{StateIndex: rollapptypes.StateInfoIndex{Index: 1}}, nil)
daMetaData := &da.DASubmitMetaData{
Height: 1,
Client: da.Mock,
Expand All @@ -186,7 +183,7 @@ func TestPostBatch(t *testing.T) {
}},
nil)
} else {
rollappQueryClientMock.On("LatestStateIndex", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error"))
rollappQueryClientMock.On("StateInfo", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("error"))
}
}
hubClient, err := newDymensionHubClient(settlement.Config{}, pubsubServer, log.TestingLogger(), options...)
Expand Down

0 comments on commit 25afe20

Please sign in to comment.