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

test(consensus-types): add more test for block_denebplus #1742

Merged
merged 4 commits into from
Jul 16, 2024

Conversation

hoank101
Copy link
Contributor

@hoank101 hoank101 commented Jul 15, 2024

Summary by CodeRabbit

  • Tests
    • Added comprehensive tests for the BeaconBlockDenebPlus type, including versioning, state root setting, body retrieval, marshaling/unmarshaling, and tree generation.

Copy link
Contributor

coderabbitai bot commented Jul 15, 2024

Walkthrough

The new tests in block_denebplus_test.go cover various functionalities of the BeaconBlockDenebPlus type, including version retrieval, state root setting, body and header retrieval, SSZ marshaling/unmarshaling, and tree generation. These comprehensive tests ensure reliability and correctness for the BeaconBlockDenebPlus type.

Changes

File Path Change Summary
mod/consensus-types/pkg/types/block_denebplus_test.go Added test functions for BeaconBlockDenebPlus: versioning, state root setting, body/header retrieval, SSZ marshaling/unmarshaling, and tree generation.
mod/consensus-types/pkg/types/block_denebplus_test.go Introduced generateBeaconBlockDenebPlus() function for generating instances of BeaconBlockDenebPlus.

Poem

In the code where blocks do dwell,
A rabbit's tales of tests to tell,
DenebPlus blocks, we now inspect,
With every function to perfect.
Marshaling, roots, and trees so fine,
Ensuring all in code align.
Test we shall, with glee and cheer,
For solid code, we hold dear. 🐇✨


Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@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.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 69f5f37 and ddf7235.

Files selected for processing (1)
  • mod/consensus-types/pkg/types/block_denebplus_test.go (1 hunks)
Additional comments not posted (8)
mod/consensus-types/pkg/types/block_denebplus_test.go (8)

36-91: LGTM!

The function generateBeaconBlockDenebPlus correctly generates a BeaconBlockDenebPlus instance with hardcoded values for testing purposes.


93-96: LGTM!

The function TestBeaconBlockDenebPlus_Version correctly tests the version retrieval method of BeaconBlockDenebPlus.


98-104: LGTM!

The function TestBeaconBlockDenebPlus_IsNil correctly tests the IsNil method for both nil and non-nil instances of BeaconBlockDenebPlus.


106-113: LGTM!

The function TestBeaconBlockDenebPlus_SetStateRoot correctly tests the SetStateRoot method by setting a new state root and verifying the equality.


115-120: LGTM!

The function TestBeaconBlockDenebPlus_GetBody correctly tests the GetBody method by verifying the body is not nil and comparing it with the raw body.


122-130: LGTM!

The function TestBeaconBlockDenebPlus_GetHeader correctly tests the GetHeader method by verifying the equality of various header fields.


132-226: LGTM!

The function TestBeaconBlockDenebPlus_MarshalSSZ_UnmarshalSSZ correctly tests the SSZ marshaling and unmarshaling methods for various cases, including valid, empty, and invalid buffer size cases.


229-241: LGTM!

The function TestBeaconBlockDenebPlus_GetTree correctly tests the GetTree method by generating the tree, comparing the tree root with the expected root, and ensuring no errors occur.

Copy link
Contributor

@ocnc ocnc left a comment

Choose a reason for hiding this comment

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

Would be good if the tests also tested a case where attestation data is not empty, as thats what deneb+ adds

@hoank101
Copy link
Contributor Author

Would be good if the tests also tested a case where attestation data is not empty, as thats what deneb+ adds

yeah, I just updated the test, thank you

Copy link
Contributor

@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.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between ddf7235 and a551c45.

Files selected for processing (1)
  • mod/consensus-types/pkg/types/block_denebplus_test.go (1 hunks)
Additional comments not posted (7)
mod/consensus-types/pkg/types/block_denebplus_test.go (7)

102-105: Test for version retrieval

The test TestBeaconBlockDenebPlus_Version correctly asserts that the version method of BeaconBlockDenebPlus returns the expected version. This is a straightforward test that effectively checks the functionality of the version method.


107-113: Test for nil check

The test TestBeaconBlockDenebPlus_IsNil effectively checks both the nil and non-nil scenarios. It uses the require package to assert the conditions, which is appropriate for unit tests where failing any assertion should stop the test immediately.


115-122: Test for setting the state root

The test TestBeaconBlockDenebPlus_SetStateRoot correctly assigns a new state root and asserts the change. This test ensures that the SetStateRoot method functions as expected. The use of a fixed byte array for the new root is appropriate for this test's scope.


124-129: Test for body retrieval

The test TestBeaconBlockDenebPlus_GetBody verifies that the body of the block is retrieved correctly. It also checks the equality of the retrieved body with the expected body, ensuring the getter's functionality is intact.


131-139: Test for header retrieval

The test TestBeaconBlockDenebPlus_GetHeader ensures that the header of the block is retrieved correctly and all expected fields match. This test is comprehensive and checks multiple fields, which is good for ensuring the integrity of the header retrieval method.


141-236: Review of marshaling and unmarshaling tests

The TestBeaconBlockDenebPlus_MarshalSSZ_UnmarshalSSZ function tests the serialization and deserialization of BeaconBlockDenebPlus. The test cases include valid blocks, empty blocks, and a scenario with an invalid buffer size. This comprehensive approach is excellent for testing the robustness of the SSZ implementation. However, ensure that the error handling for UnmarshalSSZ in the "Invalid Buffer Size" case is adequately tested.


238-251: Test for tree generation

The test TestBeaconBlockDenebPlus_GetTree checks the generation of the tree structure and compares the calculated tree root with an expected root. This test is crucial for verifying the correctness of the tree hash function and is well-implemented.

Comment on lines +36 to +100
func generateBeaconBlockDenebPlus() *types.BeaconBlockDenebPlus {
var byteArray [256]byte
byteSlice := byteArray[:]
return &types.BeaconBlockDenebPlus{
BeaconBlockHeaderBase: types.BeaconBlockHeaderBase{
Slot: 12345,
ProposerIndex: 67890,
ParentBlockRoot: [32]byte{
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},
StateRoot: [32]byte{
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18,
17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
},
Body: &types.BeaconBlockBodyDenebPlus{
BeaconBlockBodyBase: types.BeaconBlockBodyBase{
RandaoReveal: [96]byte{
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, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96},
Eth1Data: &types.Eth1Data{
DepositRoot: common.Root{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
DepositCount: 12345,
BlockHash: gethprimitives.ExecutionHash{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
},
Graffiti: [32]byte{
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,
},
Deposits: []*types.Deposit{},
},
ExecutionPayload: &types.ExecutableDataDeneb{
LogsBloom: byteSlice,
ExtraData: make([]byte, 0),
Transactions: make([][]byte, 0),
Withdrawals: make([]*engineprimitives.Withdrawal, 0),
},
Attestations: []*types.AttestationData{
{
Slot: 12345,
Index: 1,
BeaconBlockRoot: [32]byte{
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,
},
},
},
SlashingInfo: []*types.SlashingInfo{},
BlobKzgCommitments: []eip4844.KZGCommitment{},
},
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Review of helper function generateBeaconBlockDenebPlus()

This function is crucial as it generates a predefined instance of BeaconBlockDenebPlus for testing. The function correctly initializes all necessary fields with mock data, which is a good practice for isolated unit tests. However, using hardcoded values (like slot numbers, proposer index, and byte arrays) can limit the flexibility of tests. Consider parameterizing these values to increase test versatility.

- func generateBeaconBlockDenebPlus() *types.BeaconBlockDenebPlus {
+ func generateBeaconBlockDenebPlus(slot uint64, proposerIndex uint64, parentBlockRoot [32]byte, stateRoot [32]byte) *types.BeaconBlockDenebPlus {
-   Slot:          12345,
-   ProposerIndex: 67890,
-   ParentBlockRoot: [32]byte{1, 2, 3, ..., 32},
-   StateRoot: [32]byte{32, 31, 30, ..., 1},
+   Slot:          slot,
+   ProposerIndex: proposerIndex,
+   ParentBlockRoot: parentBlockRoot,
+   StateRoot: stateRoot,
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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func generateBeaconBlockDenebPlus() *types.BeaconBlockDenebPlus {
var byteArray [256]byte
byteSlice := byteArray[:]
return &types.BeaconBlockDenebPlus{
BeaconBlockHeaderBase: types.BeaconBlockHeaderBase{
Slot: 12345,
ProposerIndex: 67890,
ParentBlockRoot: [32]byte{
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},
StateRoot: [32]byte{
32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18,
17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1},
},
Body: &types.BeaconBlockBodyDenebPlus{
BeaconBlockBodyBase: types.BeaconBlockBodyBase{
RandaoReveal: [96]byte{
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, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96},
Eth1Data: &types.Eth1Data{
DepositRoot: common.Root{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
DepositCount: 12345,
BlockHash: gethprimitives.ExecutionHash{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
},
Graffiti: [32]byte{
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,
},
Deposits: []*types.Deposit{},
},
ExecutionPayload: &types.ExecutableDataDeneb{
LogsBloom: byteSlice,
ExtraData: make([]byte, 0),
Transactions: make([][]byte, 0),
Withdrawals: make([]*engineprimitives.Withdrawal, 0),
},
Attestations: []*types.AttestationData{
{
Slot: 12345,
Index: 1,
BeaconBlockRoot: [32]byte{
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,
},
},
},
SlashingInfo: []*types.SlashingInfo{},
BlobKzgCommitments: []eip4844.KZGCommitment{},
},
}
}
func generateBeaconBlockDenebPlus(slot uint64, proposerIndex uint64, parentBlockRoot [32]byte, stateRoot [32]byte) *types.BeaconBlockDenebPlus {
var byteArray [256]byte
byteSlice := byteArray[:]
return &types.BeaconBlockDenebPlus{
BeaconBlockHeaderBase: types.BeaconBlockHeaderBase{
Slot: slot,
ProposerIndex: proposerIndex,
ParentBlockRoot: parentBlockRoot,
StateRoot: stateRoot,
},
Body: &types.BeaconBlockBodyDenebPlus{
BeaconBlockBodyBase: types.BeaconBlockBodyBase{
RandaoReveal: [96]byte{
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, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96},
Eth1Data: &types.Eth1Data{
DepositRoot: common.Root{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
DepositCount: 12345,
BlockHash: gethprimitives.ExecutionHash{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
},
},
Graffiti: [32]byte{
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,
},
Deposits: []*types.Deposit{},
},
ExecutionPayload: &types.ExecutableDataDeneb{
LogsBloom: byteSlice,
ExtraData: make([]byte, 0),
Transactions: make([][]byte, 0),
Withdrawals: make([]*engineprimitives.Withdrawal, 0),
},
Attestations: []*types.AttestationData{
{
Slot: 12345,
Index: 1,
BeaconBlockRoot: [32]byte{
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,
},
},
},
SlashingInfo: []*types.SlashingInfo{},
BlobKzgCommitments: []eip4844.KZGCommitment{},
},
}
}

@hoank101 hoank101 requested a review from ocnc July 16, 2024 01:31
Copy link
Contributor

@ocnc ocnc left a comment

Choose a reason for hiding this comment

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

lgtm

@ocnc ocnc merged commit 92c0dec into berachain:main Jul 16, 2024
15 checks passed
@hoank101 hoank101 deleted the hoa/add-test-block-denebplus branch July 17, 2024 02:27
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