Refactor repo/db.MessageStore.Put() to protect against unwanted use cases #1855
Description
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 to0
in schema) anderr
(set to""
in schema) args which are not used for this case - removing
messageID
can be internally derived
- grouping
- CacheInboundOrderMessage(orderID, peerID string, mType pb.Message_MessageType, msg Message, err error, pubkey []byte)
- grouping
string
types together - removing
receivedAt
(set totime.Now().UnixNano()
in schema) which is not used for this case - removing
messageID
can be internally derived - changing err to
error
type fromstring
(and use the error string in the schema)
- grouping
- 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
- CacheOutboundOrderMessage(orderID, peerID string, mType pb.Message_MessageType, msg Message, pubkey []byte)