-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposableStorage.sol
More file actions
95 lines (84 loc) · 3.05 KB
/
Copy pathComposableStorage.sol
File metadata and controls
95 lines (84 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { EfficientHashLib } from "solady/utils/EfficientHashLib.sol";
/**
* @title ComposableStorage
* @dev Contract to handle generic storage operations with cross-chain support
*/
contract ComposableStorage {
using EfficientHashLib for *;
error SlotNotInitialized();
// Mapping to track initialized slots
mapping(bytes32 => bool) private initializedSlots;
// Mapping to track length of dynamic data
mapping(bytes32 => uint256) private dynamicDataLength;
/**
* @dev Internal function to write a value to a specific storage slot
*/
function _writeStorage(bytes32 slot, bytes32 value, bytes32 namespace) private {
bytes32 namespacedSlot = getNamespacedSlot(namespace, slot);
initializedSlots[namespacedSlot] = true;
assembly {
sstore(namespacedSlot, value)
}
}
/**
* @dev Write a value to a specific storage slot
* @param slot The storage slot to write to
* @param value The value to write
*/
function writeStorage(bytes32 slot, bytes32 value, address account) external {
bytes32 namespace = getNamespace(account, msg.sender);
_writeStorage(slot, value, namespace);
}
/**
* @dev Read a value from a specific namespace and slot
* @param namespace The namespace (typically a contract address)
* @param slot The storage slot to read from
* @return The value stored at the specified namespaced slot
*/
function readStorage(bytes32 namespace, bytes32 slot) external view returns (bytes32) {
bytes32 namespacedSlot = getNamespacedSlot(namespace, slot);
if (!initializedSlots[namespacedSlot]) {
revert SlotNotInitialized();
}
bytes32 value;
assembly {
value := sload(namespacedSlot)
}
return value;
}
/**
* @dev Generates a namespaced slot
* @param namespace The namespace (typically a contract address)
* @param slot The storage slot to read from
* return The namespaced slot
*/
function getNamespacedSlot(bytes32 namespace, bytes32 slot) public pure returns (bytes32 result) {
assembly {
mstore(0x00, namespace)
mstore(0x20, slot)
result := keccak256(0x00, 0x40)
}
}
/**
* @dev Generates a namespace for a given account and caller
* @param account The account address
* @param _caller The caller address
* return The generated namespace
*/
function getNamespace(address account, address _caller) public pure returns (bytes32 result) {
assembly {
mstore(0x00, account)
mstore(0x20, shl(96, _caller))
result := keccak256(0x0c, 0x28)
}
}
/**
* @dev Check if a slot has been initialized
*/
function isSlotInitialized(bytes32 namespace, bytes32 slot) external view returns (bool) {
bytes32 namespacedSlot = getNamespacedSlot(namespace, slot);
return initializedSlots[namespacedSlot];
}
}