Skip to content

Conversation

arepala-uml
Copy link
Contributor

  1. Fetched Conway block CBOR data using gouroboros starter kit and converted it to hex using xxd for testing. (https://cexplorer.io/block/27807a70215e3e018eec9be8c619c692e06a78ebcb63daf90d7abe823f3bbf47)
  2. Added two test cases where one using inbuilt cbor.Marshal() and another using cbor.Encode() to ensure round-trip serialization of ConwayBlock.
  3. Compared original and re-encoded CBOR bytes in both tests, logged mismatch details, and saved outputs to respective hex files for inspection.

Closes #1043

@arepala-uml arepala-uml marked this pull request as ready for review July 7, 2025 03:35
@arepala-uml arepala-uml requested a review from a team as a code owner July 7, 2025 03:35
@arepala-uml arepala-uml requested review from wolf31o2 and agaffney July 7, 2025 03:36
Copy link
Contributor

@agaffney agaffney left a comment

Choose a reason for hiding this comment

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

Our cbor.Encode() function is nothing but a wrapper for the upstream cbor.Marshal() function with the proper encoder mode set. We only need/want to test with our custom cbor.Encode(), since the plain upstream function will produce different results in some cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a good reason to store this block as hex? It requires an extra step to decode, and it takes up 2x the disk space.

Copy link
Member

Choose a reason for hiding this comment

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

I told him to store it as hex.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should name this file in a way that tells us where it came from. We at least want the network name, block hash, and block slot, so that we can look it up again later

Copy link
Member

Choose a reason for hiding this comment

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

I prefer we put it into a variable within the Go code rather than as a separate file we have to load, and we simply comment this information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed the hex from the file and stored it inside the code itself. Added the information about block hash, block slot in the comments. Please review it once

}
}

func isValidHexString(s string) error {
Copy link
Member

Choose a reason for hiding this comment

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

You won't need this. You're providing the string. You know it's good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this extra validation check and please review it once.

Copy link
Member

Choose a reason for hiding this comment

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

These are block tests. Create a block_test.go file rather than putting tests not related to protocol parameters in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created a new file block_test.go and moved the tests to this file. Please review it once


func TestConwayBlock_CborRoundTrip_UsingCustomEncode(t *testing.T) {
// Read the hex-encoded CBOR string for a Conway block
filePath := filepath.Join("testdata", "conway_block.cbor.hex")
Copy link
Member

Choose a reason for hiding this comment

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

Put this into a string var instead of reading from disk. We can reuse it across the package without doing disk seeks in every test.

Something like:

// taken from https://cexplorer.io/block/abcd...
var conwayBlockHex string = "..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have made the changes for this like stored in a variable

t.Errorf("Custom CBOR round-trip mismatch for Conway block\nOriginal CBOR (hex): %x\nCustom Encoded CBOR (hex): %x", dataBytes, encoded)
}

// Save the re-encoded CBOR for comparison
Copy link
Member

Choose a reason for hiding this comment

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

Skip this. The text/hex output is good enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this part like storing back again the rendered CBOR. Please review it once.

@arepala-uml
Copy link
Contributor Author

arepala-uml commented Jul 7, 2025

Our cbor.Encode() function is nothing but a wrapper for the upstream cbor.Marshal() function with the proper encoder mode set. We only need/want to test with our custom cbor.Encode(), since the plain upstream function will produce different results in some cases.

I have removed the test function with inbuilt cbor.Marshal() function

@arepala-uml arepala-uml requested review from agaffney and wolf31o2 July 7, 2025 16:23
@agaffney agaffney force-pushed the cbor_block_test branch 3 times, most recently from b4364fd to 5a84909 Compare July 12, 2025 16:07
@wolf31o2 wolf31o2 dismissed agaffney’s stale review July 12, 2025 22:38

Requested changes have been made.

@wolf31o2 wolf31o2 merged commit 968b34e into main Jul 12, 2025
9 checks passed
@wolf31o2 wolf31o2 deleted the cbor_block_test branch July 12, 2025 22:38
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.

Create tests for creating block CBOR
3 participants