Open yondonfu opened 5 years ago
I'd be in favor of adding the extra gas cost for the SSTORE initially, and then considering optimizations later. I'm not sure if the potentially heavy cryptographic operations would be cheaper than the SSTORE anyway.
One other optimization idea is to include an optional incentive to remove expired data from storage (which should provide gas refunds). For any tickets you cashed previously that have been expired, remove their entry from the mapping in the same call as cashing your ticket, and reduce the cost.
We need to prevent recipients from cashing in the same winning ticket on-chain multiples times - each winning ticket can only be cashed in once.
One way to defend against replay attacks is to have the contract maintain a mapping of ticket IDs corresponding to cashed tickets. We can define a ticket ID as the hash of all the relevant metadata in the ticket including the sender specified random nonce so each ticket ID should be unique. When a recipient cashes in a winning ticket, the contract will check if the ticket ID has already been used and if so, the transaction will revert.
The downside of this solution is that it adds an additional 20000 gas for an SSTORE whenever a recipient cashes a winning ticket thereby increasing the transaction cost.
Other ways to defend against replay attacks include:
The original solution should work for an initial implementation and research into optimizations can be left for subsequent iterations.