solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract HybridIntegrationContract {
/**
* @title Hybrid RPC + Event Integration Contract
* @notice Provides idempotent RPC submissions with event emissions for async integration.
* Features:
* - Idempotency: Unique requestId prevents duplicate processing.
* - Retries: Safe to retry failed txs with same requestId (reverts undone).
* - Tracing: traceId propagated through all events for observability.
* - Backpressure: Rate limiting per user (10 reqs/hour).
*/
// Events for tracing and integration (indexed for off-chain querying)
event AlreadyProcessed(bytes32 indexed requestId, bytes32 indexed traceId);
event RequestReceived(bytes32 indexed requestId, bytes32 indexed traceId, bytes payload);
event RequestProcessed(bytes32 indexed requestId, bytes32 indexed traceId, bytes result);
event RequestFailed(bytes32 indexed requestId, bytes32 indexed traceId, string reason);
// Idempotency: Tracks processed requests
mapping(bytes32 => bool) public processed;
// Backpressure: Rate limiting per user
struct UserRate {
uint64 lastReset; // Timestamp of last reset
uint64 count; // Request count in current period
}
mapping(address => UserRate) public rateLimits;
// Constants for backpressure
uint256 public constant RATE_LIMIT_PERIOD = 3600; // 1 hour in seconds
uint256 public constant MAX_REQUESTS_PER_PERIOD = 10; // Max requests per period
/**
* @notice RPC entrypoint: Submit a request idempotently.
* - Checks idempotency first (cheap, emits AlreadyProcessed if done).
* - Applies backpressure on new requests only.
* - Processes synchronously, emits events for tracing/integration.
* - Retries safe: Reverts on failure undo state changes.
* @param requestId Unique idempotency key (e.g., UUID/hash)
* @param traceId Tracing ID (e.g., from client/parent span)
* @param payload Arbitrary payload for processing
*/
function submitRequest(
bytes32 requestId,
bytes32 traceId,
bytes calldata payload
) external {
// Step 1: Idempotency check (fast path for retries)
if (processed[requestId]) {
emit AlreadyProcessed(requestId, traceId);
return;
}
// Step 2: Backpressure (only for new requests)
UserRate storage ur = rateLimits[msg.sender];
uint256 currentTime = block.timestamp;
if (currentTime >= ur.lastReset + RATE_LIMIT_PERIOD) {
ur.lastReset = uint64(currentTime);
ur.count = 0;
}
require(ur.count < MAX_REQUESTS_PER_PERIOD, "Backpressure: Rate limit exceeded");
ur.count++;
// Step 3: Mark as processed (revert undoes if processing fails)
processed[requestId] = true;
// Step 4: Emit received for async workflows/tracing
emit RequestReceived(requestId, traceId, payload);
// Step 5: Simulate processing (replace with real logic: e.g., oracle call, transfer, etc.)
// In production: Could revert on errors, emitting Failed via try/catch or direct.
bytes memory result;
try this._processPayload(payload) returns (bytes memory r) {
result = r;
emit RequestProcessed(requestId, traceId, result);
} catch Error(string memory reason) {
// Unmark on failure? No, revert undoes storage. But for demo, emit and revert.
emit RequestFailed(requestId, traceId, reason);
revert(reason);
} catch {
emit RequestFailed(requestId, traceId, "Unknown error");
revert("Processing failed");
}
}
/**
* @notice Internal processing simulation (external for try/catch).
* Replace with domain logic (e.g., validate payload, call external contract).
*/
function _processPayload(bytes calldata payload) external pure returns (bytes memory) {
// Demo: Echo with prefix. Real: Decode payload, execute action.
return abi.encodePacked("processed:", payload);
}
/**
* @notice Query idempotency status (RPC-style getter for off-chain).
*/
function isProcessed(bytes32 requestId) external view returns (bool) {
return processed[requestId];
}
/**
* @notice Reset rate limit manually (admin, for testing).
*/
function resetRateLimit(address user) external {
// In production: Restrict to owner.
rateLimits[user].lastReset = uint64(block.timestamp);
rateLimits[user].count = 0;
}
}