Skip to content

Commit e840cba

Browse files
authored
Create DistributedDataStore.sol
```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; // This contract demonstrates CAP/PACELC concepts in a simplified form contract DistributedDataStore { enum ConsistencyMode { // CAP: Choose C over A when partitioned STRONG_CONSISTENCY, // CAP: Choose A over C when partitioned EVENTUAL_CONSISTENCY } enum LatencyMode { // PACELC: Choose L over C when normal LOW_LATENCY, // PACELC: Choose C over L when normal HIGH_CONSISTENCY } struct DataItem { bytes value; uint256 version; } // Mode configurations ConsistencyMode public partitionMode; LatencyMode public normalMode; // Simulated partition flag (would be set by an oracle in a real system) bool public isPartitioned; // Data storage mapping(bytes32 => DataItem) private mainData; mapping(bytes32 => DataItem) private replicaData; // Events event DataWritten(bytes32 key, uint256 version); event PartitionStatusChanged(bool isPartitioned); constructor(ConsistencyMode _partitionMode, LatencyMode _normalMode) { partitionMode = _partitionMode; normalMode = _normalMode; } // Simulate a network partition function setPartitionStatus(bool _isPartitioned) public { isPartitioned = _isPartitioned; emit PartitionStatusChanged(_isPartitioned); } // Write data according to the configured modes function write(bytes32 key, bytes calldata value) public returns (bool) { uint256 newVersion = block.timestamp; if (isPartitioned) { if (partitionMode == ConsistencyMode.STRONG_CONSISTENCY) { // Cannot guarantee consistency, so refuse the write revert("Write rejected during partition with strong consistency mode"); } else { // Accept write to available replica, will reconcile later mainData[key] = DataItem(value, newVersion); emit DataWritten(key, newVersion); } } else { // No partition, write to both copies mainData[key] = DataItem(value, newVersion); replicaData[key] = DataItem(value, newVersion); emit DataWritten(key, newVersion); } return true; } // Read data according to the configured modes function read(bytes32 key) public view returns (bytes memory, uint256) { if (isPartitioned) { if (partitionMode == ConsistencyMode.STRONG_CONSISTENCY) { // Cannot guarantee consistency, so refuse the read revert("Read rejected during partition with strong consistency mode"); } else { // Return available data, might be stale return (mainData[key].value, mainData[key].version); } } else { if (normalMode == LatencyMode.LOW_LATENCY) { // Just read from one replica for lower latency return (mainData[key].value, mainData[key].version); } else { // In a real system, we would check both replicas and return the newest // Here we simulate this by comparing versions DataItem memory main = mainData[key]; DataItem memory replica = replicaData[key]; if (main.version >= replica.version) { return (main.value, main.version); } else { return (replica.value, replica.version); } } } } // Simulate reconciliation after partition heals function reconcile() public { // In a real system, this would involve complex sync logic // For this demo, we just pick the newer version for each key // (Not implemented here as we'd need a way to enumerate keys) } } ```
1 parent 1fe6b5f commit e840cba

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
// This contract demonstrates CAP/PACELC concepts in a simplified form
5+
contract DistributedDataStore {
6+
enum ConsistencyMode {
7+
// CAP: Choose C over A when partitioned
8+
STRONG_CONSISTENCY,
9+
10+
// CAP: Choose A over C when partitioned
11+
EVENTUAL_CONSISTENCY
12+
}
13+
14+
enum LatencyMode {
15+
// PACELC: Choose L over C when normal
16+
LOW_LATENCY,
17+
18+
// PACELC: Choose C over L when normal
19+
HIGH_CONSISTENCY
20+
}
21+
22+
struct DataItem {
23+
bytes value;
24+
uint256 version;
25+
}
26+
27+
// Mode configurations
28+
ConsistencyMode public partitionMode;
29+
LatencyMode public normalMode;
30+
31+
// Simulated partition flag (would be set by an oracle in a real system)
32+
bool public isPartitioned;
33+
34+
// Data storage
35+
mapping(bytes32 => DataItem) private mainData;
36+
mapping(bytes32 => DataItem) private replicaData;
37+
38+
// Events
39+
event DataWritten(bytes32 key, uint256 version);
40+
event PartitionStatusChanged(bool isPartitioned);
41+
42+
constructor(ConsistencyMode _partitionMode, LatencyMode _normalMode) {
43+
partitionMode = _partitionMode;
44+
normalMode = _normalMode;
45+
}
46+
47+
// Simulate a network partition
48+
function setPartitionStatus(bool _isPartitioned) public {
49+
isPartitioned = _isPartitioned;
50+
emit PartitionStatusChanged(_isPartitioned);
51+
}
52+
53+
// Write data according to the configured modes
54+
function write(bytes32 key, bytes calldata value) public returns (bool) {
55+
uint256 newVersion = block.timestamp;
56+
57+
if (isPartitioned) {
58+
if (partitionMode == ConsistencyMode.STRONG_CONSISTENCY) {
59+
// Cannot guarantee consistency, so refuse the write
60+
revert("Write rejected during partition with strong consistency mode");
61+
} else {
62+
// Accept write to available replica, will reconcile later
63+
mainData[key] = DataItem(value, newVersion);
64+
emit DataWritten(key, newVersion);
65+
}
66+
} else {
67+
// No partition, write to both copies
68+
mainData[key] = DataItem(value, newVersion);
69+
replicaData[key] = DataItem(value, newVersion);
70+
emit DataWritten(key, newVersion);
71+
}
72+
73+
return true;
74+
}
75+
76+
// Read data according to the configured modes
77+
function read(bytes32 key) public view returns (bytes memory, uint256) {
78+
if (isPartitioned) {
79+
if (partitionMode == ConsistencyMode.STRONG_CONSISTENCY) {
80+
// Cannot guarantee consistency, so refuse the read
81+
revert("Read rejected during partition with strong consistency mode");
82+
} else {
83+
// Return available data, might be stale
84+
return (mainData[key].value, mainData[key].version);
85+
}
86+
} else {
87+
if (normalMode == LatencyMode.LOW_LATENCY) {
88+
// Just read from one replica for lower latency
89+
return (mainData[key].value, mainData[key].version);
90+
} else {
91+
// In a real system, we would check both replicas and return the newest
92+
// Here we simulate this by comparing versions
93+
DataItem memory main = mainData[key];
94+
DataItem memory replica = replicaData[key];
95+
96+
if (main.version >= replica.version) {
97+
return (main.value, main.version);
98+
} else {
99+
return (replica.value, replica.version);
100+
}
101+
}
102+
}
103+
}
104+
105+
// Simulate reconciliation after partition heals
106+
function reconcile() public {
107+
// In a real system, this would involve complex sync logic
108+
// For this demo, we just pick the newer version for each key
109+
// (Not implemented here as we'd need a way to enumerate keys)
110+
}
111+
}

0 commit comments

Comments
 (0)