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

RPC API: ensure hash field included in batches json #1684

Merged
merged 2 commits into from
Dec 7, 2023

Conversation

BedrockSquirrel
Copy link
Collaborator

Why this change is needed

Tenscan is showing N/A for hash column on batches tables because the field is missing from the json responses. It was lost as part of the json marshalling fixes recently.

What changes were made as part of this PR

Ensure hash field included in the batch header json output.

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

Copy link

coderabbitai bot commented Dec 7, 2023

Walkthrough

The codebase has been updated to enhance the BatchHeader struct with a new Hash field, affecting its JSON representation. Additionally, a new function VerifyBatchesDataAction has been added to test batch data retrieval via an RPC endpoint. A new test function TestRPC has also been introduced in the tenscan package to verify and debug RPC endpoints used by Tenscan.

Changes

File Path Change Summary
go/common/headers.go
.../networktest/actions/publicdata/tenscan_data.go
Added Hash field to batchHeaderEncoding struct; updated MarshalJSON method to include Hash. Added new function VerifyBatchesDataAction to test batches data RPC endpoint.
.../networktest/tests/tenscan/tenscan_rpc_test.go Introduced new test function TestRPC to verify Tenscan RPC endpoints; added tenscan package.
integration/obscuroscan/obscuroscan_test.go Additional assertions added to TestObscuroscan function.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ca7f91b and f904c8f.
Files selected for processing (3)
  • go/common/headers.go (2 hunks)
  • integration/networktest/actions/publicdata/tenscan_data.go (1 hunks)
  • integration/networktest/tests/tenscan/tenscan_rpc_test.go (1 hunks)
Additional comments: 3
go/common/headers.go (2)
  • 49-53: The addition of the Hash field to the batchHeaderEncoding struct aligns with the PR objectives to include the hash field in the batch header JSON output.

  • 73-79: The update to the MarshalJSON method to marshal the new Hash field is consistent with the PR objectives and ensures the hash field is present in the JSON output.

integration/networktest/tests/tenscan/tenscan_rpc_test.go (1)
  • 14-24: The TestRPC function has been correctly implemented to verify and debug Tenscan's RPC endpoints using the networktest framework. It is designed to run only in an IDE environment, which is suitable for local development and debugging purposes.

Comment on lines 73 to 79
// MarshalJSON custom marshals the BatchHeader into a json
func (b *BatchHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(batchHeaderEncoding{
b.Hash(),
b.ParentHash,
b.Root,
b.TxHash,
Copy link

@coderabbitai coderabbitai bot Dec 7, 2023

Choose a reason for hiding this comment

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

The UnmarshalJSON method should be updated to handle the new Hash field to maintain symmetry with the MarshalJSON method.

func (b *BatchHeader) UnmarshalJSON(data []byte) error {
	dec := new(batchHeaderEncoding)
	err := json.Unmarshal(data, dec)
	if err != nil {
		return err
	}

+	b.Hash = dec.Hash
	b.ParentHash = dec.ParentHash
	// ... rest of the fields ...
	return nil
}

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// MarshalJSON custom marshals the BatchHeader into a json
func (b *BatchHeader) MarshalJSON() ([]byte, error) {
return json.Marshal(batchHeaderEncoding{
b.Hash(),
b.ParentHash,
b.Root,
b.TxHash,
func (b *BatchHeader) UnmarshalJSON(data []byte) error {
dec := new(batchHeaderEncoding)
err := json.Unmarshal(data, dec)
if err != nil {
return err
}
b.Hash = dec.Hash
b.ParentHash = dec.ParentHash
// ... rest of the fields ...
return nil
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not necessary because hash is a calculated field.

Copy link

Choose a reason for hiding this comment

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

@BedrockSquirrel Understood, the Hash field is computed using the Hash() method and does not need to be unmarshaled from JSON. Thank you for the clarification.

(•ㅅ•) 
/   づ```

<!-- This is an auto-generated reply by CodeRabbit -->

Comment on lines +13 to +41
// VerifyBatchesDataAction tests the batches data RPC endpoint
func VerifyBatchesDataAction() networktest.Action {
return actions.VerifyOnlyAction(func(ctx context.Context, network networktest.NetworkConnector) error {
client, err := obsclient.Dial(network.ValidatorRPCAddress(0))
if err != nil {
return err
}

pagination := common.QueryPagination{
Offset: 0,
Size: 20,
}
batchListing, err := client.GetBatchesListing(&pagination)
if err != nil {
return err
}
if len(batchListing.BatchesData) != 20 {
return fmt.Errorf("expected 20 batches, got %d", len(batchListing.BatchesData))
}
if batchListing.Total <= 10 {
return fmt.Errorf("expected more than 10 batches, got %d", batchListing.Total)
}
if batchListing.BatchesData[0].Number.Cmp(batchListing.BatchesData[1].Number) < 0 {
return fmt.Errorf("expected batches to be sorted by height descending")
}

return nil
})
}
Copy link

Choose a reason for hiding this comment

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

The function VerifyBatchesDataAction correctly sets up a client and performs validation checks on the retrieved batch data. However, it does not check for the presence of the hash field in the batch data, which is the main objective of the PR. Consider adding a check to ensure that the hash field is present and correctly populated for each batch in the listing.

Comment on lines +21 to +34
pagination := common.QueryPagination{
Offset: 0,
Size: 20,
}
batchListing, err := client.GetBatchesListing(&pagination)
if err != nil {
return err
}
if len(batchListing.BatchesData) != 20 {
return fmt.Errorf("expected 20 batches, got %d", len(batchListing.BatchesData))
}
if batchListing.Total <= 10 {
return fmt.Errorf("expected more than 10 batches, got %d", batchListing.Total)
}
Copy link

Choose a reason for hiding this comment

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

The hardcoded values for pagination (Size: 20) and batch count checks (expected 20 batches, expected more than 10 batches) may not be suitable for all test scenarios. Consider parameterizing these values to make the function more flexible and capable of handling different testing requirements.

Comment on lines +35 to +37
if batchListing.BatchesData[0].Number.Cmp(batchListing.BatchesData[1].Number) < 0 {
return fmt.Errorf("expected batches to be sorted by height descending")
}
Copy link

Choose a reason for hiding this comment

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

The sorting check only compares the first two batches for descending order. To ensure robustness, consider iterating over the entire list of batches to verify that they are sorted correctly by height in descending order.

Copy link
Contributor

@otherview otherview left a comment

Choose a reason for hiding this comment

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

lgtm - We should move the test to the integration test ?
Also avoid starting a new network would be good I think

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f904c8f and c9b3f04.
Files selected for processing (1)
  • integration/obscuroscan/obscuroscan_test.go (1 hunks)
Additional comments: 1
integration/obscuroscan/obscuroscan_test.go (1)
  • 136-142: The added assertions correctly check for the descending order of batch numbers and the presence of the "hash" field in the JSON response, aligning with the PR's objective to ensure the hash field is included in the batch header JSON output.

@BedrockSquirrel BedrockSquirrel merged commit 0fd3773 into main Dec 7, 2023
2 checks passed
@BedrockSquirrel BedrockSquirrel deleted the matt/tenscan-batch-hashes branch December 7, 2023 16:45
tudor-malene pushed a commit that referenced this pull request Dec 8, 2023
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.

2 participants