Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Refactor repo/db.MessageStore.Put() to protect against unwanted use cases #1855

Open
@placer14

Description

@placer14

We're exposing Put as a public method for interacting with the database schema but not protecting from bad inputs.

For example, if we're persisting an outbound message, the consumer has to know that it needs 0 in the time column. Instead of putting all of that responsibility on the coder, we need to write functions which validate input for the purpose it was meant for. I recommend making a CacheOutboundOrderMessage(...) which is guaranteed to validate and persist the data properly...which would be separate from the needs of a CacheInboundOrderMessage. This way, someone using the db package can just observe the arguments (along with maybe the function name and some godocs) and be able to make some safe assumptions that they won't accidentally create bad data for the app. The things which make "no sense" to happen should be removed entirely from the function interface or handled properly.

  • Create separate functions for the two use cases of the existing method Put(messageID, orderID string, mType pb.Message_MessageType, peerID string, msg Message, err string, receivedAt int64, pubkey []byte) error:
    • CacheOutboundOrderMessage(orderID, peerID string, mType pb.Message_MessageType, msg Message, pubkey []byte)
      • grouping string types together
      • removing receivedAt (set to 0 in schema) and err (set to "" in schema) args which are not used for this case
      • removing messageID can be internally derived
    • CacheInboundOrderMessage(orderID, peerID string, mType pb.Message_MessageType, msg Message, err error, pubkey []byte)
      • grouping string types together
      • removing receivedAt (set to time.Now().UnixNano() in schema) which is not used for this case
      • removing messageID can be internally derived
      • changing err to error type from string (and use the error string in the schema)
    • Validate inputs on both methods:
      • Calculate the messageID within the functions or error
      • Test parsing peerID and pubkey to ensure they are valid/usable
      • Ensure mType is a valid enum value
      • Validate other inputs are present/non-zero

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions