When, and why is it better to use functions _msgSender()
& _msgData()
introduced by the Context.sol
by OpenZeppelin, instead of msg.sender
& msg.data
?
2 Answers
If you plan to use meta transactions based on ERC2771, the msg.sender address will be the relayer's address that sends the transaction to the contract. The real sender's address will be appended at the end of the calldata, and Context.sol aids in distinguishing between the forwarder address and the actual sender address (similarly for _msgData).
However, if you do not intend to support this feature, there is no need to use Context.sol.
If you're not using a custom Context, with a custom implementation of _msgSender, then it's equivalent to msg.sender and you don't need to use it.
If you need your contracts to be more generic and support a potential custom Context, you should use _msgSender. This is the reason why we use it in the contracts.