Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
bd29a0b
Rename module
superzordon Mar 25, 2023
f259e77
Update go mod
superzordon Mar 25, 2023
f7479f0
Fix consumer
superzordon Mar 25, 2023
51a70d5
Fix is scanning logic
superzordon Mar 26, 2023
2a245ed
Add logs to scanner
superzordon Mar 26, 2023
884c386
Update is scanning logic
superzordon Mar 26, 2023
945840d
Update helpers
superzordon Mar 27, 2023
57525da
Update cleanup routine to print on cleanup
superzordon Mar 27, 2023
64e6cb9
Update file watcher
superzordon Mar 27, 2023
b6ce7ad
Update handler
superzordon Mar 27, 2023
cfb6791
Update state consumer
superzordon Mar 27, 2023
0ba48ac
Update consumer to use state change entry
superzordon Mar 31, 2023
31bb562
Updates to batching procedure, migrations
superzordon Apr 3, 2023
211e941
Update check for file procedure
superzordon Apr 4, 2023
fa616c1
Clean up code, add additional comments, simplify interfaces
superzordon Apr 4, 2023
56274ad
Update method signatures
superzordon Apr 4, 2023
6492175
Support additional entry types
superzordon Apr 11, 2023
09ada46
Add additional entry types
superzordon Apr 21, 2023
e3fa8dc
Updates to consumer
superzordon May 3, 2023
f848811
Update EOF error handling
superzordon May 3, 2023
7b3fcf9
Updates to restart logic
superzordon May 4, 2023
228de34
Updates to resume logic
superzordon May 9, 2023
9321b85
Update error handling for unexpected EOFs
superzordon May 9, 2023
bee93b5
Updates to error handling
superzordon May 11, 2023
680fd2c
Add helpers for tx index metadata
superzordon Jun 1, 2023
b21ed35
Updates to state consumer
superzordon Jun 7, 2023
716596d
Updates to consumer
superzordon Jun 23, 2023
e910678
Remove debugging
superzordon Jun 26, 2023
bba12bd
Update mempool recovery procedure
superzordon Jul 6, 2023
bdd9703
Update comments
superzordon Jul 7, 2023
37a1ae0
Updates
superzordon Jul 15, 2023
950efb6
Updates to helper functions
superzordon Jul 27, 2023
0f97f29
Update sync events, add additional sync events
superzordon Jul 30, 2023
0762ba6
Clean up code
superzordon Aug 3, 2023
ec3e3d9
Address PR feedback
superzordon Aug 8, 2023
02cf01e
Add logging
superzordon Aug 16, 2023
fe2ff9c
Updates to consumer
superzordon Aug 25, 2023
81be6a7
Updates to batching
superzordon Sep 15, 2023
c9c924b
Update to error handling
superzordon Jan 7, 2024
0f75e86
Change error handling
superzordon Jan 17, 2024
3197554
Update core and backend imports to use local repos
tholonious Feb 9, 2024
8b0390f
Change glog.Error to glog.V(2).Info for expected EOF errors. Use erro…
lazynina Feb 13, 2024
2ffe9db
Merge pull request #5 from deso-protocol/consumer-cleanup
lazynina Feb 13, 2024
5514f72
Add function to filter keys by prefix
lazynina Feb 15, 2024
3398567
Merge pull request #6 from deso-protocol/add-filter-keys-by-prefix
lazynina Feb 16, 2024
328023c
Failing txn support in ComputeTransactionMetadata
lazynina Feb 27, 2024
d31b1a1
Merge pull request #7 from deso-protocol/failing-txn-support
lazynina Feb 28, 2024
a4da407
remove logic related to failing txns
lazynina Mar 29, 2024
493571e
Merge pull request #8 from deso-protocol/feature/pos-mempool-updates-…
lazynina Mar 29, 2024
e783ec6
Upgrade go version
lazynina Apr 8, 2024
0e2c0e6
Upgrade go version (#9)
lazynina Apr 11, 2024
b183c80
Upgrade deps
lazynina Apr 8, 2024
eb2c50e
Upgrade deps (#10)
lazynina Apr 11, 2024
1760678
state-consumer branch for pg data handler fix-upsert-blocks-for-pos
lazynina Apr 16, 2024
40bfae3
Add ComputeTransactionMetadata for pos txn types
lazynina Apr 16, 2024
fa4dda5
Add ComputeTransactionMetadata for pos txn types (#11)
lazynina Apr 17, 2024
9a6f21b
Add affected public keys for coin lockup txns
lazynina Apr 16, 2024
c942488
Add affected public keys for coin lockup txns (#12)
lazynina Apr 17, 2024
a8332b0
look up staker public key base58 check from state change metadata map
lazynina Apr 16, 2024
fbc3984
look up staker public key base58 check from state change metadata map…
lazynina Apr 17, 2024
76b5ad0
state-consumer branch for pg data handler fix-upsert-blocks-for-pos
lazynina Apr 16, 2024
1d48c4e
Merge branch 'fix-upsert-blocks-for-pos' of github.com:deso-protocol/…
lazynina Apr 17, 2024
c11d7d7
empty commit
lazynina Apr 19, 2024
e1069c0
state-consumer branch for pg data handler fix-upsert-blocks-for-pos (…
lazynina Apr 19, 2024
90f334e
Expose GetUtxOpByOperationType and exclude Zero public key from affec…
lazynina Apr 19, 2024
31cdc92
Expose GetUtxOpByOperationType and exclude Zero public key from affec…
lazynina Apr 19, 2024
cbffdd7
Fix issues w/ consumer
superzordon May 22, 2024
5772f4d
Update consumer logic to more accurately handle file pointers
superzordon May 24, 2024
fbabb7f
Add additional file object to handle tracking the first element in th…
superzordon May 24, 2024
a4865d3
Remove unused variables
superzordon May 24, 2024
4806b8d
Add db transactions
superzordon May 25, 2024
3a6a053
Update consumer/consumer.go
superzordon May 28, 2024
0ed2969
Remove unused lines
superzordon May 28, 2024
519fdca
Merge branch 'z/pos-bugfixes' of https://github.com/deso-protocol/sta…
superzordon May 30, 2024
5317815
Merge pull request #17 from deso-protocol/z/pos-bugfixes
superzordon May 30, 2024
7758f2e
Downgrade uint256
superzordon Jun 5, 2024
486d38c
Revert 'Upgrade deps' (#18)
lazynina Jun 5, 2024
650e64f
Revert "Upgrade go version (#9)" (#19)
lazynina Jun 5, 2024
ad94202
Fix txindex state change metadata field
superzordon Jun 13, 2024
69f4e40
Merge pull request #20 from deso-protocol/z/fix-post-state-change-met…
superzordon Jun 13, 2024
d3612d3
Merge pull request #4 from deso-protocol/feature/proof-of-stake
lazynina Jun 17, 2024
33fc5fc
set global params
lazynina Jul 12, 2024
5c78102
Trigger build
lazynina Jul 12, 2024
f55214e
Merge pull request #21 from deso-protocol/ln/update-state-syncer-unco…
lazynina Jul 25, 2024
37778a3
Add special ExtraData encoders for RepostedPostHash.
mattfoley8 Sep 12, 2024
8b49923
Pluralize name.
mattfoley8 Sep 12, 2024
0fbdfb7
One more rename.
mattfoley8 Sep 12, 2024
7a30f7f
Add additional custom encoders.
mattfoley8 Sep 12, 2024
5504460
Retrigger CI.
mattfoley8 Sep 12, 2024
78e2449
Use the backend utils instead of recreating.
mattfoley8 Sep 12, 2024
5db4822
Merge pull request #24 from deso-protocol/mf/add-extra-data-encoders
superzordon Sep 12, 2024
51a7b98
Add config flag to only sync committed entries
superzordon Sep 14, 2024
657484f
Cleanup go mod
superzordon Sep 14, 2024
4ee02e9
Commit every block
superzordon Sep 16, 2024
ee58044
Merge pull request #25 from deso-protocol/z/sync-only-committed
superzordon Sep 17, 2024
4bf6ab8
Update create post association txindex
superzordon Sep 17, 2024
250b972
Update state consumer to have submit post related public keys.
poolcoke Sep 28, 2024
9d8dbf1
upgrade all deps and go version
lazynina Oct 10, 2024
178ab90
bump deps
lazynina Oct 10, 2024
280bfdd
holiman uint256 -> deso-protocol uint256
lazynina Oct 11, 2024
e28eb05
gofmt
lazynina Oct 11, 2024
c7d0c80
upgrade deps
lazynina Oct 11, 2024
084630e
go mod tidy
lazynina Oct 11, 2024
1b9a608
Merge pull request #26 from deso-protocol/j/update-txindex-for-feed
superzordon Oct 14, 2024
ca0171e
Revert "Update create post association txindex"
superzordon Oct 14, 2024
0a2da05
Merge pull request #29 from deso-protocol/revert-26-j/update-txindex-…
superzordon Oct 14, 2024
4d1f05c
go get -u and go mod tidy
lazynina Oct 16, 2024
77fe09d
upgrade deps
lazynina Oct 17, 2024
99cbacf
Merge pull request #28 from deso-protocol/ln/upgrade-deps-and-go-version
lazynina Oct 18, 2024
dc58d4a
go mod tidy for fix max bids branch (#33)
lazynina Nov 5, 2024
0ac4397
downgrade klauspost (#34)
lazynina Nov 5, 2024
81f9b08
Add caching and txindex updates
superzordon Nov 7, 2024
af43410
Merge pull request #35 from deso-protocol/z/caching-and-txindex
superzordon Nov 7, 2024
538926c
Add follows to txn meta
superzordon Nov 10, 2024
3d55bbc
Fix key
superzordon Nov 10, 2024
d8a92d6
Fix field name
superzordon Nov 10, 2024
f5d0ecb
Merge pull request #36 from deso-protocol/z/update-txn-meta
superzordon Nov 10, 2024
39dda89
State syncer mempool syncing and test suite updates (#37)
superzordon Apr 4, 2025
354fd07
upgrade deps 05-05-2025 (#38)
lazynina May 7, 2025
a7eafc1
Replace fmt.Errorf calls that have no replacements with errors.New (#39)
lazynina May 7, 2025
ed994bf
Create readme for state consumer
superzordon May 13, 2025
fcf954d
Remove files
superzordon May 13, 2025
5dc0f47
Merge pull request #40 from deso-protocol/z/readme
superzordon May 14, 2025
639c0c1
clean up go.mod after backend dep upgrades (#41)
lazynina May 14, 2025
bf54f9c
Add DeepWiki badge to README.md (#42)
chrisjacob May 27, 2025
ec2d04f
Update error handling
superzordon Jun 30, 2025
acd5cab
Merge pull request #43 from deso-protocol/z/update-error-handling
superzordon Jun 30, 2025
a4320d9
Updates
superzordon Jul 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/tests/ss/
.env
.vscode/
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# State Consumer for DeSo Blockchain

The **State Consumer** is a Golang interface and framework designed to extract, decode, and process on-chain state from the DeSo (Decentralized Social) blockchain. The repository's purpose is to provide a flexible, datastore-agnostic mechanism that reads a binary state change file (produced by a DeSo node), decodes the state change entries, and then applies those changes (insert, update, or delete) via a configurable handler.

> **Note:** A working DeSo node must be configured to generate state change files. This file serves as the source of truth for the consumer, while the state handler (for example, a Postgres data handler) applies those changes into the target datastore.

---

## Key Concepts

### 1. DeSo Node
A **DeSo Node** is a running instance on the DeSo blockchain. It:
- **Syncs the blockchain:** Initializes by syncing from other nodes.
- **Processes on-chain transactions:** Continuously updates its state as new transactions are mined.
- **Generates State Change Files:** Writes a binary log of state changes (state change file) that the state consumer uses as input.

*Tip:* Make sure your node is configured to create and expose these state change files (usually via a shared directory or volume).

### 2. State Consumer Interface
The state consumer interface is a Go interface that encapsulates the following:
- **Reading the State Change File:** It can read binary state change files produced by a DeSo node.
- **Decoding State Change Entries:** It decodes the individual state change entries (which include operations such as insert, update, or delete).
- **Calling Handler Functions:** It provides a hook to call a handler function for processing those decoded entries.

This interface is purposely designed to be **datastore agnostic**, which means you can implement a handler to store the on-chain state in your database or any other system.

### 3. Data Handler Service
A **Data Handler Service** is a concrete implementation of the state consumer interface. For example, the [Postgres Data Handler](https://github.com/deso-protocol/postgres-data-handler) is one such implementation that:
- **Processes entries in batches:** For high performance and consistency.
- **Handles transaction management:** Using transaction savepoints, batching, and optional mempool syncing.
- **Applies state changes into a datastore:** In this case, the service performs insert, update, and delete operations on a PostgreSQL database.

Since the core consumer is datastore independent, you can create your own handler if you wish to persist the state in an alternative datastore (for example, a key–value store, another SQL database, or an in-memory cache).

### 4. StateChangeEntry Encoder

The **StateChangeEntry encoder** is a critical component of the syncing process. The type `StateChangeEntry` defines the structure of each state operation recorded by the DeSo node. Below is a detailed explanation of each property:

- **OperationType (StateSyncerOperationType):**
- Specifies the type of operation (insert, update, or delete) that should be performed on the datastore.

- **KeyBytes ([]byte):**
- Represents the key that identifies the record in the core Badger DB. This could relate to various entities like posts or profiles.

- **Encoder (DeSoEncoder):**
- Holds the encoder instance responsible for serializing and deserializing the entity's data.
- This encoder abstracts the logic needed to translate structured data into a binary format.

- **EncoderBytes ([]byte):**
- Contains a raw byte representation of the encoder.
- During operations such as hypersync, rather than re-encoding the data, the raw bytes are stored directly for improved performance.

- **AncestralRecord (DeSoEncoder):**
- Stores the previous state (ancestral record) of the data that can be used to revert changes.
- This is especially crucial for mempool transactions where state changes might need to be reverted upon block confirmation.

- **AncestralRecordBytes ([]byte):**
- A raw byte representation of the ancestral record.
- Used for efficiently restoring an earlier state without re-encoding.

- **EncoderType (EncoderType):**
- Indicates which type of encoder is used. Different encoder types correspond to different on-chain data formats (e.g., posts, profiles, transactions).
- This enables the consumer to properly decode the binary data.

- **FlushId (uuid.UUID):**
- Uniquely identifies the flush batch that this state change belongs to.
- It helps group multiple state changes that occur during the same flush operation.

- **BlockHeight (uint64):**
- Denotes the block height at which the state change occurred.
- Acts as a temporal marker to ensure the correct ordering and consistency of state updates.

- **Block (*MsgDeSoBlock):**
- For UTXO-based operations or when block-related data is relevant, this field contains the block information associated with the state change.
- It is only applicable for specific operation types.

- **IsReverted (bool):**
- A flag indicating whether the state change has been reverted.
- Particularly useful in mempool scenarios where previously applied entries might need to be undone.

By encapsulating all of these fields, the StateChangeEntry encoder provides a robust mechanism for serializing the complete on-chain state, ensuring that each update can be accurately applied or reversed during the syncing process.

---

## How It Works

1. **DeSo Node Setup:**
Your DeSo node must be configured to write a state change file (and optionally an index/state progress file) to a directory (e.g., `/state-changes`).

2. **Running the State Consumer:**
The state consumer reads from the state change file, decodes the on-chain state change entries, and then calls the respective handler functions. These functions then process each entry based on its operation type (insert/update/delete) and the specific encoder type (e.g., posts, profiles, likes).

3. **Implement/Customize the Data Handler:**
- For a complete, working example of a data handler, please refer to the [Postgres Data Handler](https://github.com/deso-protocol/postgres-data-handler) repository.
- To create your own data handler, implement the methods defined by the `StateSyncerDataHandler` interface as described in [`consumer/interfaces.go`](./consumer/interfaces.go).

4. **Extensibility:**
Because the state consumer interface is designed to be datastore agnostic, you have the flexibility to write handlers for any back-end storage or processing system without modifying the core consumer logic.

---

## Getting Started

### Prerequisites
- A working DeSo node configured to emit the state change file.
- Go (Golang) installed.
- Familiarity with building and running Docker containers if you plan to deploy across multiple services.

### Quick Setup Guide

1. **Clone this Repository:**
```bash
git clone https://github.com/deso-protocol/state-consumer.git
cd state-consumer
```

2. **Configure Environment Variables:**
Ensure that your environment has the following variables (or equivalent configuration):
- `STATE_CHANGE_DIR`: Path to the state change file directory.
- `CONSUMER_PROGRESS_DIR`: Directory to store consumer progress files.
- Additional settings for batching (e.g., `BATCH_BYTES`, `THREAD_LIMIT`) if needed.

3. **Implement/Customize the Data Handler:**
- For a complete, working example of a data handler, please refer to the [Postgres Data Handler](https://github.com/deso-protocol/postgres-data-handler) repository.
- To create your own data handler, implement the methods defined by the `StateSyncerDataHandler` interface as described in [`consumer/interfaces.go`](./consumer/interfaces.go).


### Deployment Recommendations

- **Containerization:**
When deploying in production, consider a multi-container setup:
1. **DeSo Node Container:** Generates the state change file.
2. **State Consumer Container:** Reads the file and applies changes via your custom data handler.
3. **Target Data Store Container:** (e.g., Postgres, Elasticsearch, etc.) which stores the on-chain state.

- **Networking and Volumes:**
Make sure the state consumer container has access to the state change file directory (using shared volumes or network file shares).

- **Monitoring and Logging:**
Use proper logging (e.g., via glog) and monitoring tools to track syncing progress and detect errors.

---

## Contributing

Contributions, bug fixes, and feature requests are welcome. Please feel free to open an issue or submit a pull request.

## Have more questions?

DeepWiki (powered by Devin AI) provides up-to-date documentation you can talk to for this repo, click the button below to try it out.

[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/deso-protocol/state-consumer)

Loading