replayprotection.sol

Overview

This Solidity source file implements an abstract contract ReplayProtection that provides a mechanism to prevent replay attacks on messages sent to the contract. Replay protection is essential in blockchain and distributed systems to ensure that a previously processed message cannot be maliciously or accidentally re-submitted and accepted again.

The contract maintains a record of processed messages indexed by their expiration timestamps and message hashes to detect duplicates. It also includes logic to periodically clean up expired entries to optimize storage.

Contract: ReplayProtection

Inheritance

Versioning

State Variables

Variable

Type

Description

messages

mapping(uint32 => mapping(uint256 => bool))

Nested mapping to track processed messages by their expiration timestamp (expireAt) and message hash (messageHash). A true value means the message has been processed.

MAX_CLEANUP_ITERATIONS

uint8 constant

Maximum iterations allowed when cleaning up expired messages to avoid gas exhaustion.

lastMessage

MessageInfo

Struct holding the information about the last processed message, specifically its hash and expiration timestamp.

__value

uint

Dummy variable demonstrating contract functionality (unused in core logic).

Modifiers

Internal Functions

_saveMsg()

afterSignatureCheck(TvmSlice body, TvmCell message)

gc()


Data Structures

MessageInfo (Imported)

This struct holds the essential metadata for replay protection checks.


Error Handling


Interaction with Other Parts of the System


Usage Example

contract MyContract is ReplayProtection {
    
    function processMessage() public saveMsg {
        // Message processing logic here
        // The `saveMsg` modifier ensures replay protection is applied
    }
}

Important Implementation Details and Algorithms


Mermaid Class Diagram

classDiagram
class ReplayProtection {
+string versionRP
+mapping messages
+uint8 MAX_CLEANUP_ITERATIONS
+MessageInfo lastMessage
+uint __value
+modifier saveMsg()
-function _saveMsg()
-function afterSignatureCheck()
-function gc()
}
ReplayProtection --|> Errors
ReplayProtection o-- MessageInfo

References