Improve way of working with OP_MSG checksum and write checksum when needed #2690
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?
- https://github.com/FerretDB/FerretDB/blob/8f774649399a2d02ee1a8c287bc03e9174472bb6/internal/wire/msg_body.go
- https://github.com/FerretDB/FerretDB/blob/89c2880ebd563db14acd259e9dd52acac7b70329/internal/wire/op_msg.go
- https://github.com/FerretDB/FerretDB/blob/43be3d2d3b0ce5978b3ebaa79eee41b3c1c86db1/internal/clientconn/conn.go
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;
- refactor checksum validation and relevant
- add and update unit tests.
Metadata
Assignees
Labels
Projects
Status
No status