Skip to content

Commit

Permalink
add README.md content
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsao committed Oct 25, 2024
1 parent 94f7871 commit 67b80f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# lattice-gym
# lattice-gym

Welcome to lattice-gym!
In this repository, we're providing serveral conflict-free replication data type (CRDT) examples in simple socket application.

## Prerequisties
```
git clone [email protected]:ytsao/lattice-gym.git
cd lattice-gym
conda env -f environment.yml
```

## Supporting types of CRDT

**state-based CRDT:**
1. G-counter
2. D-counter
3. PN-counter
4. 2P-set
5. OR-set

**operation-based CRDT:**
1. PN-counter
2. OR-set

### Example
In this example environment, we have 1 server to send the all clients information to each client and we have 2 clients to receive and send their current state.

**Terminal 1 - turn-on server:**
```
conda activate lattice-gym
python server.py
Enter the number of clients in the counter: 2
```

**Terminal 2 - turn-on client 1:**
```
cd lattice-gym/state_based_CRDT
conda activate lattice-gym
python PN-counter.py
```

**Terminal 3 - turn-on client 2:**
```
cd lattice-gym/state_based_CRDT
conda activate lattice-gym
python PN-counter.py
```

After that, you can use the application to synchronize the increment and decrement counter with another client.
8 changes: 3 additions & 5 deletions state_based_CRDT/D-counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def _update(self):
# CRDT implementation -> user interface : increment
# increments at vector index corresponding to local node id
if self.node_id != -1:
self.state_vector[self.node_id] += 1
self.state_vector[self.node_id] -= 1
else:
print("there is only 1 client")

def _merge(self, other_state_vector: list):
# called asynchronously
# coordinatewise max
for i in range(len(self.state_vector)):
self.state_vector[i] = max(self.state_vector[i], other_state_vector[i])
self.state_vector[i] = min(self.state_vector[i], other_state_vector[i])

def _value(self):
# CRDT implementation -> user interface : value
Expand Down Expand Up @@ -114,9 +114,7 @@ def start(self):
time.sleep(5)
# self.client.sendto(f"state_vector: {self.state_vector[0]},{self.state_vector[1]}".encode(self.DATA_FORMAT), self.dest_address)
sv: str = ",".join([str(i) for i in self.state_vector])
# randomly pick the neighbor
# todo:


# send to all others
for each_dest_address in self.dest_address:
self.client.sendto(f"state_vector: {sv}".encode(self.DATA_FORMAT), each_dest_address)
Expand Down
4 changes: 1 addition & 3 deletions state_based_CRDT/G-counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ def start(self):
time.sleep(5)
# self.client.sendto(f"state_vector: {self.state_vector[0]},{self.state_vector[1]}".encode(self.DATA_FORMAT), self.dest_address)
sv: str = ",".join([str(i) for i in self.state_vector])
# randomly pick the neighbor
# todo:


# send to all others
for each_dest_address in self.dest_address:
self.client.sendto(f"state_vector: {sv}".encode(self.DATA_FORMAT), each_dest_address)
Expand Down

0 comments on commit 67b80f3

Please sign in to comment.