Commit e840cba
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
1 file changed
+111
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
0 commit comments