Skip to content

Improve way of working with OP_MSG checksum and write checksum when needed #2690

Open
@rumyantseva

Description

What should be done?

If the checksumPresent flag bit is enabled, the OP_MSG includes a checksum. The checksum is calculated using the CRC-32 algorithm with the Castagnoli polynomial and encompasses all bytes in the message except for the checksum itself. Both the header bytes and the body (excluding the last four bytes) are included in the calculation. The checksum value is stored in the last four bytes of the message.

Writing the Checksum

Currently, in FerretDB, we have separate structures to store the header and body of the message. This separation causes difficulties when working with the checksum because we require data from both the header and body. To write the checksum, we need to modify the header by summing four bytes in the message length field for the checksum. Afterward, we calculate the checksum and attach it to the body.

Current location to implement the checksum writing functionality would be somewhere in this section of code: conn.go. However, this particular code section is already problematic and includes a TODO comment referencing #273, which states the need to restructure some aspects of the wire package.

To address this checksum issue, we need to come up with a better structure for the wire package that allows us to modify the header, calculate the checksum, and store it according to the requirements.

Reading and Validating the Checksum

Currently, we validate the checksum from the ReadMessage function. The reasons behind this approach are the same as for writing the checksum: we need both the header and body to calculate the checksum. However, there is also a checksum field in the OpMsg structure. Currently, this field is not really utilized (see the TODOs that mention the current issue). Once again, we need to find a way to make the checksum calculation and validation more closely related to the OpMsg structure.

Where?

Definition of Done

The aim of this task is to come up with a better design of wire package that will handle checksum calculation and validation close to the other OP_MSG related code.

  • check TODOs:
    • refactor checksum validation and relevant wire parts;
    • implement checksum writing;
  • add and update unit tests.

Metadata

Assignees

Labels

area/typesIssues about data types, BSON, wire protocol, etccode/choreCode maintenance improvementsnot readyIssues that are not ready to be worked on; PRs that should skip CI

Projects

  • Status

    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions