📄 UserBackup.sol
D-OPEN SOVEREIGN
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 * 
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 * 
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15
16// SPDX-License-Identifier: Apache-2.0
17pragma solidity ^0.8.24;
18
19contract UserBackup {
20
21    // Keep your existing storage
22    mapping(uint32 => bytes[]) private userData;
23
24    function storeBatchActions(uint32 userId, bytes calldata data) internal  {
25        userData[userId].push(data);
26    }
27
28    function loadActions(uint32 userId) public view returns (bytes[] memory compressedActions) {
29        return userData[userId];
30    }
31
32    // OPTIMIZED: Remove redundant counter - use array.length directly
33    function loadActionsSince(uint32 userId, uint16 index) public view returns (bytes[] memory compressedActions) {
34        bytes[] storage userActions = userData[userId];
35        uint256 totalActions = userActions.length;
36
37        if (totalActions <= index) {
38            return new bytes[](0); // Return empty array instead of revert
39        }
40
41        uint256 resultSize = totalActions - index;
42        bytes[] memory result = new bytes[](resultSize);
43
44        // More gas-efficient loop with unchecked arithmetic
45        unchecked {
46            for (uint256 i = 0; i < resultSize; i++) {
47                result[i] = userActions[index + i];
48            }
49        }
50        return result;
51    }
52
53    function nbWrites(uint32 userId) public view returns (uint256) {
54        return userData[userId].length;
55    }
56
57}
58