Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add indexer client unit tests #2000

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

tsachiherman
Copy link
Contributor

@tsachiherman tsachiherman commented Mar 24, 2025

What ?

This PR extends the indexer client api unit tests.

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman
@tsachiherman tsachiherman self-assigned this Mar 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman
…-labs/hypersdk into tsachi/add-indexer-client-unit-tests

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman
@tsachiherman tsachiherman marked this pull request as ready for review March 24, 2025 17:31
executedBlock, err := client.GetBlockByHeight(ctx, expectedBlock.Block.Hght, parser)
if tt.err == nil {
executedBlock, err := client.GetBlockByHeight(ctx, tt.blkHeight, parser)
if tt.blkHeightErr == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify this if-condition? I think it's sufficient to check that the errors are "equal" before invoking the if-condition rather than doing an if-else:

r.ErrorIs(tt.blkHeightErr, err)
if tt.blkHeightErr == nil {
    expectedBlock := executedBlocks[tt.blkHeight-1]
    r.Equal(expectedBlock.Block, executedBlock.Block)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that won't work ( as is.. ). The returned error is an rpc error that contains a string representing the internal server error. Hence, we need to compare it by string (ErrorContains).

We do have a PR to update this and create RPC specific error codes, which would resolve this issue (#1942)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah understood, thanks for the clarification!

@@ -90,26 +111,61 @@ func TestIndexerClientTransactions(t *testing.T) {
numTxs = 3
)

parser := chaintest.NewTestParser()
badparser := &chain.TxTypeParser{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: badParser

malformedBlock: false,
},
{
name: "badparser",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "bad parser"

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Copy link
Contributor

@RodrigoVillar RodrigoVillar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left two comments but afterwards, LGTM.

Could we create an issue afterwards to address #2000 (comment)? With the current if-else usage, the table tests are hard to read (ref) but am willing to defer this to a future PR.

if tt.malformedBlock {
// "damage" the last executed block by removing the last result.
executedBlock.ExecutionResults.Results = executedBlock.ExecutionResults.Results[1:]
r.NoError(indexer.Notify(context.Background(), executedBlock))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use the ctx already declared at the beginning of the subtest?

r.ErrorContains(err, tt.blkHeightErr.Error())
}

expectedBlkID := ids.GenerateTestID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use ids.Empty{} here? This way, it's clear that if we're not targeting a particular block (i.e. numExecutedBlocks >= height > 0), then it's clear that we're querying with a invalid block ID (since a valid block cannot have id = ids.Empty{}).

@tsachiherman
Copy link
Contributor Author

Left two comments but afterwards, LGTM.

Could we create an issue afterwards to address #2000 (comment)? With the current if-else usage, the table tests are hard to read (ref) but am willing to defer this to a future PR.

I think that updating the tests would be inevitable once the error codes are refactored. I'll add that to the issue.

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman
…-labs/hypersdk into tsachi/add-indexer-client-unit-tests
RodrigoVillar
RodrigoVillar previously approved these changes Mar 25, 2025
Copy link
Contributor

@RodrigoVillar RodrigoVillar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

blkHeight: 0,
err: errBlockNotFound,
name: "no blocks",
blkHeight: 0,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In production, we should be able to serve the genesis block, should we generate a sequence of blocks that includes a genesis block?

Comment on lines +97 to +98
if tt.numExecutedBlocks == 0 {
r.ErrorContains(err, database.ErrNotFound.Error())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handling the special case this way makes it seem like we default to not including the genesis. Could we include at least the genesis block (we should receive a notification of at least the genesis block on startup)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not intended to cover the genesis block; it's really about covering the no-blocks-of-any-kind usecaswe.
Adding genesis is ok of course, but I don't feel that it's needed since it wouldn't be treated any differently from the indexer perspective.

executedBlock := executedBlocks[tt.blockIndex]
executedTx := executedBlock.Block.Txs[tt.txIndex]

if tt.malformedBlock {
// "damage" the last executed block by removing the last result.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we fix this comment? This removes the first result not the last result

Comment on lines 213 to 224
if tt.getTxErr != nil {
r.ErrorContains(err, tt.getTxErr.Error())
} else {
r.Equal(tt.found, found)
if tt.found {
r.Equal(GetTxResponse{
TxBytes: executedTx.Bytes(),
Timestamp: executedBlock.Block.Tmstmp,
Result: executedBlock.ExecutionResults.Results[tt.txIndex],
}, txResponse)
r.Equal(executedTx, tx)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of conditional nesting in these table tests. Could we try and simplify them so that they have narrow scope and reduce unnecessary nesting as much as possible?

For example, this section could be re-written as:

			txResponse, tx, found, err := client.GetTx(ctx, executedTx.GetID(), tt.parser)
			if tt.getTxErr != nil {
				r.ErrorContains(err, tt.getTxErr.Error())
			}
			r.Equal(tt.found, found)
			if !tt.found {
				return
			}
			r.Equal(GetTxResponse{
				TxBytes:   executedTx.Bytes(),
				Timestamp: executedBlock.Block.Tmstmp,
				Result:    executedBlock.ExecutionResults.Results[tt.txIndex],
			}, txResponse)
			r.Equal(executedTx, tx)

style guide refs:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm putting this in place fails on the tests: badParser and malformed_block

it seems this is because found is re-used across both GetTxResults and GetTx, so we require the error in GetTx to short circuit the test at the error.

Verified

This commit was signed with the committer’s verified signature.
tsachiherman Tsachi Herman

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants