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

feat: x-ar-io-root-transaction-id header #225

Merged
merged 1 commit into from
Oct 22, 2024
Merged

Conversation

karlprieb
Copy link
Collaborator

No description provided.

FROM new_transactions
WHERE id = @id
UNION
SELECT null, data_size, content_type, content_encoding, true AS stable
SELECT null AS data_root, data_size, content_type, content_encoding, root_transaction_id, true AS stable
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

changed to null AS data_root for readability

Comment on lines +985 to +986
coreRow?.root_transaction_id !== null &&
coreRow?.root_transaction_id !== undefined
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

checking for null AND undefined because it can return null for transactions where root_transaction_id doesn't exist and return undefined for bundles/data items.

Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 11.11111% with 8 lines in your changes missing coverage. Please review.

Project coverage is 68.67%. Comparing base (5184d8d) to head (8bc67a6).
Report is 15 commits behind head on develop.

Files with missing lines Patch % Lines
src/database/standalone-sqlite.ts 0.00% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #225      +/-   ##
===========================================
- Coverage    68.72%   68.67%   -0.06%     
===========================================
  Files           32       32              
  Lines         7978     7986       +8     
  Branches       434      434              
===========================================
+ Hits          5483     5484       +1     
- Misses        2494     2501       +7     
  Partials         1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

📝 Walkthrough

Walkthrough

The pull request introduces several changes across multiple files, primarily focusing on the addition of a new property rootTransactionId in various contexts, including constants, SQL queries, and data handling logic. It modifies SQL queries to include the root_transaction_id column, updates data handling functions to return this new property, and enhances test cases to validate its integration. Additionally, interface definitions are adjusted to accommodate the new property, and existing functions are refined for improved error handling and header management.

Changes

File Change Summary
src/constants.ts Added rootTransactionId: 'X-AR-IO-Root-Transaction-Id' to headerNames constant.
src/database/sql/core/data-attributes.sql Modified SQL queries to include root_transaction_id as null in the first two SELECT statements.
src/database/standalone-sqlite.ts Updated getDataAttributes method to include rootTransactionId in the return object and enhanced error handling.
src/routes/data/handlers.test.ts Added tests for X-AR-IO-Root-Transaction-Id header in createDataHandler function.
src/routes/data/handlers.ts Enhanced setDataHeaders to include rootTransactionId, refined error handling in createRawDataHandler, and updated logic in createDataHandler.
src/types.d.ts Updated ContiguousDataAttributes interface: made contentType optional and added rootTransactionId as optional.
test/end-to-end/data.test.ts Introduced new test suite for X-AR-IO-Root-Transaction-Id header, including setup for Docker Compose and new tests.

Possibly related PRs

Suggested reviewers

  • djwhitt

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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: 1

🧹 Outside diff range and nitpick comments (4)
src/routes/data/handlers.test.ts (1)

700-732: LGTM: Test case for data items with root transaction ID.

The test case correctly verifies that the 'x-ar-io-root-transaction-id' header is set for data items when a rootTransactionId is present. This aligns with the expected behavior.

Consider adding a test case for when rootTransactionId is not present in the data attributes to ensure the header is not set in that scenario. This would provide more comprehensive coverage.

test/end-to-end/data.test.ts (2)

282-334: LGTM: Comprehensive test suite for X-AR-IO-Root-Transaction-Id header.

The new test suite thoroughly checks the behavior of the X-AR-IO-Root-Transaction-Id header for both bundle transactions and data items. The setup and teardown procedures are well-defined, and the test cases cover the expected scenarios.

Consider adding a comment explaining the purpose of the 10-second wait after queueing the bundle (line 316). This will help other developers understand why this delay is necessary.


323-333: LGTM: Well-structured test cases for header verification.

Both test cases effectively verify the presence or absence of the x-ar-io-root-transaction-id header for bundle transactions and data items.

For consistency, consider using a constant for the header name 'x-ar-io-root-transaction-id' instead of repeating the string. This would make future updates easier if the header name changes.

src/database/standalone-sqlite.ts (1)

984-996: LGTM with a minor suggestion.

The addition of rootTransactionId is well-implemented and aligns with the PR objective. The code organization has been improved by moving the contentType variable.

Consider using the nullish coalescing operator for a more concise null check:

-    const rootTransactionId =
-      coreRow?.root_transaction_id !== null &&
-      coreRow?.root_transaction_id !== undefined
-        ? toB64Url(coreRow.root_transaction_id)
-        : undefined;
+    const rootTransactionId = coreRow?.root_transaction_id != null
+      ? toB64Url(coreRow.root_transaction_id)
+      : undefined;

This change maintains the same logic while making the code more readable.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 8ed4ec8 and 8bc67a6.

📒 Files selected for processing (7)
  • src/constants.ts (1 hunks)
  • src/database/sql/core/data-attributes.sql (1 hunks)
  • src/database/standalone-sqlite.ts (1 hunks)
  • src/routes/data/handlers.test.ts (1 hunks)
  • src/routes/data/handlers.ts (1 hunks)
  • src/types.d.ts (1 hunks)
  • test/end-to-end/data.test.ts (2 hunks)
🧰 Additional context used
🔇 Additional comments (13)
src/database/sql/core/data-attributes.sql (5)

4-6: LGTM: Consistent addition of root_transaction_id

The addition of root_transaction_id as NULL in the stable_transactions SELECT statement is appropriate and maintains consistency with the rest of the query structure.


8-10: LGTM: Consistent treatment of root_transaction_id

The addition of root_transaction_id as NULL in the new_transactions SELECT statement mirrors the changes in the stable_transactions statement, maintaining consistency across the query.


16-18: LGTM: Consistent changes, verify root_transaction_id

The changes in this SELECT statement are consistent with those in the bundles.stable_data_items statement, which is good for maintaining uniformity. However, please verify that the root_transaction_id column exists in the bundles.new_data_items table.

You can use the following script to verify the table structure:

#!/bin/bash
# Description: Verify the structure of bundles.new_data_items table

# Test: Check if root_transaction_id exists in bundles.new_data_items
echo "Checking for root_transaction_id in bundles.new_data_items:"
rg -i "create\s+table.*bundles\.new_data_items" -A 20

12-14: Verify implications of NULL data_root and confirm root_transaction_id

  1. Setting data_root to NULL is a change that might affect downstream logic. Please verify that this change doesn't break any existing functionality.
  2. The inclusion of root_transaction_id without modification suggests this column exists in the bundles.stable_data_items table. Please confirm this is the case.

To verify these changes, you can run the following script:


Line range hint 1-21: Overall structure looks good, consider downstream impacts

The changes to the UNION query are consistent across all four SELECT statements, which is excellent. The addition of root_transaction_id aligns well with the broader changes mentioned in the PR summary.

However, there are two points to consider:

  1. The setting of data_root to NULL for bundle-related tables (lines 12 and 16) might have implications on how data is processed downstream. Please ensure that any code relying on data_root from these tables can handle NULL values appropriately.

  2. The LIMIT 1 at the end of the query (line 21) means only one row will be returned, even if multiple matches are found across the different tables. This behavior hasn't changed, but it's worth confirming that this is the intended behavior, especially with the addition of the new root_transaction_id column.

To verify the impact of these changes, you can run the following script:

src/constants.ts (1)

27-27: LGTM: New header constant added correctly.

The new rootTransactionId constant is well-placed and follows the existing naming conventions. It aligns with the PR objective of adding an 'x-ar-io-root-transaction-id' header.

src/types.d.ts (2)

510-510: LGTM: Addition of rootTransactionId aligns with PR objective.

The addition of the optional rootTransactionId property to the ContiguousDataAttributes interface aligns well with the PR objective of adding an 'X-AR-IO-Root-Transaction-Id' header. This change enhances the data model to support tracking of root transaction IDs, which can be valuable for tracing or auditing purposes.


509-509: Verify the impact of making contentType optional.

The change from contentType: string | undefined to contentType?: string provides more flexibility but could potentially affect parts of the codebase that assume contentType is always present. Please ensure that this change doesn't introduce any unintended side effects in the existing codebase.

To verify the impact, you can run the following script:

✅ Verification successful

Impact of making contentType optional: Verified

No direct accesses to contentType without null checks were found, ensuring that making it optional does not introduce unintended side effects.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for usages of contentType that might assume it's always defined
rg -i 'contentType\s*[!?]' --type ts

Length of output: 853


Script:

#!/bin/bash
# Search for direct accesses to contentType without optional chaining or null checks
rg 'contentType\.' --type ts

Length of output: 30

src/routes/data/handlers.test.ts (1)

677-698: LGTM: Test case for transactions without root transaction ID.

The test case correctly verifies that the 'x-ar-io-root-transaction-id' header is not set for transactions. This aligns with the expected behavior.

test/end-to-end/data.test.ts (1)

49-50: LGTM: New constant for bundle transaction ID.

The addition of bundle1 constant follows the existing naming convention and will be useful for the new test suite.

src/database/standalone-sqlite.ts (2)

995-996: LGTM! Changes improve code consistency and readability.

The addition of rootTransactionId to the returned object and the direct inclusion of contentType in the return statement are well-implemented. These changes align with the PR objective and improve code readability.


Line range hint 1-2500: Verify integration of rootTransactionId across the system.

While the changes to getDataAttributes are well-implemented, it appears that rootTransactionId is not used or propagated in other parts of this file. Consider reviewing if this new property should be integrated into other methods or classes for consistency and completeness.

To ensure proper integration, please run the following script to check for potential usage points of rootTransactionId:

This script will help identify areas where rootTransactionId might need to be added or used for complete integration.

src/routes/data/handlers.ts (1)

123-125: ⚠️ Potential issue

Ensure rootTransactionId is safely handled before adding to response headers

Please verify that dataAttributes.rootTransactionId is properly validated or sanitized before including it in the response headers to prevent potential security risks such as header injection attacks or unintended exposure of sensitive information.

Comment on lines +676 to +733
describe('X-AR-IO-Root-Transaction-Id', () => {
it("shouldn't return root transaction id for transactions", async () => {
app.get(
'/:id',
createDataHandler({
log,
dataIndex,
dataSource,
blockListValidator,
manifestPathResolver,
}),
);

return request(app)
.get('/not-a-real-id')
.expect(200)
.then((res: any) => {
assert.equal(
res.headers['x-ar-io-root-transaction-id'],
undefined,
);
});
});

it('should return root transaction id for data items', async () => {
dataIndex.getDataAttributes = () =>
Promise.resolve({
size: 10,
contentType: 'application/octet-stream',
rootTransactionId: 'root-tx',
isManifest: false,
stable: true,
verified: true,
signature: null,
});

app.get(
'/:id',
createDataHandler({
log,
dataIndex,
dataSource,
blockListValidator,
manifestPathResolver,
}),
);

return request(app)
.get('/not-a-real-id')
.expect(200)
.then((res: any) => {
assert.equal(
res.headers['x-ar-io-root-transaction-id'],
'root-tx',
);
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding more test cases for comprehensive coverage.

The new test cases for the X-AR-IO-Root-Transaction-Id header are well-implemented and cover the main scenarios. To further improve the test suite, consider adding the following test cases:

  1. Test when rootTransactionId is undefined or null in the data attributes.
  2. Test with different HTTP methods (e.g., HEAD request) to ensure consistent behavior.
  3. Test error scenarios, such as when getDataAttributes fails.

These additional tests would provide more comprehensive coverage and help catch potential edge cases.

@djwhitt djwhitt merged commit 0b06c6a into develop Oct 22, 2024
5 checks passed
@djwhitt djwhitt deleted the PE-6932-root-tx-id-header branch October 22, 2024 23:15
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.

3 participants