Prompt
Implement a G-Counter (Grow-only Counter) CRDT for an offline-first Expo/React Native app.
A G-Counter is a Conflict-free Replicated Data Type where each replica (e.g. a device with an Expo SecureStore-assigned replicaId) tracks its own increments in a private slot. Counters are persisted locally (AsyncStorage / expo-sqlite) and merged whenever the device reconnects — taking element-wise max guarantees the replicas converge without a server or consensus. The total value is the sum of all slots.
Write four functions:
createGCounter(replicaCount)— return an array ofreplicaCountzerosincrement(counter, replicaId)— return a new counter with slotreplicaIdincremented by 1merge(counterA, counterB)— return a new counter where each slot is Math.max of the twovalue(counter)— return the sum of all slots
Hints
Solution
Your Code
15 min