Skip to content

Commit

Permalink
Optimize Block Fetching for Faster Blockchain Sync (#13)
Browse files Browse the repository at this point in the history
* Update README.md

* Optimize Block Fetching for Faster Blockchain Sync

Modified code to fetch historical blocks in batches of 50 and, once reaching the head block, switch to fetching one block every 3 seconds with retry logic on RPC failure. This approach significantly speeds up the blockchain synchronization process, achieving up to 60% faster sync times in test scenarios.

Changes include:
- Implemented batch fetching of historical blocks, processing up to 50 blocks per batch.
- Added logic to switch to single block fetching mode with a 3-second interval once the head block is reached.
- Introduced retry mechanism on RPC failures to ensure robustness and continuous operation.

These enhancements provide a more efficient and reliable syncing process, allowing for quicker catching up with the latest blocks.

---------

Co-authored-by: BottoSteem <>
  • Loading branch information
BottoStm authored Jul 18, 2024
1 parent fb4d9de commit 419f7ef
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 350 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## steemdb

## steemdb
[https://steemdb.io](https://steemdb.io)

open source blockchain explorer for the steem blockchain - build on phalcon + mongodb
Expand Down
15 changes: 11 additions & 4 deletions docker/sync/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
FROM python:3.6.2-slim
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory
WORKDIR /src

# Install dependencies
RUN apt-get update && apt-get install -y make gcc libssl-dev

RUN pip3 install steem pymongo
# Install Python dependencies
RUN pip install steem pymongo requests

COPY ./docker/sync /src
ADD ./rds-combined-ca-bundle.pem /src/rds-combined-ca-bundle.pem
# Copy the current directory contents into the container at /src
COPY . /src

# Run sync.py when the container launches
CMD ["python", "/src/sync.py"]
68 changes: 68 additions & 0 deletions docker/sync/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

# Steem Blockchain Sync Script

## 📝 Overview

This Python script is designed to synchronize a MongoDB database with the Steem blockchain by fetching and processing blocks. The script has been optimized to fetch historical blocks in batches of 50, and once reaching the head block, it switches to fetching one block every 3 seconds with retry logic on RPC failure. This approach significantly speeds up the synchronization process, achieving up to 60% faster sync times in test scenarios.

## ✨ Features

- ⚡ Fetches historical blocks in batches for faster synchronization.
- 🔄 Switches to single block fetching mode at the head block for real-time updates.
- 🛠️ Robust retry mechanism on RPC failures to ensure continuous operation.
- 📦 Processes various operations within blocks and updates MongoDB collections accordingly.

## 🔍 Differences Between Old and New Code

### Old Code

- 🚶 Processed blocks one at a time.
- 🔧 Relied on environment variables for configuration.
- 🐢 Less efficient in handling historical block synchronization.

### New Code

- 🗂️ **Batch Processing**: Fetches historical blocks in batches of 50, significantly reducing the time required to synchronize the blockchain.
- 🔄 **Retry Logic**: Implements retry logic for RPC failures to ensure continuous and reliable operation.
- 📁 **Configuration Management**: Moved from environment variable-based configuration to a `config.json` file for easier management and flexibility.
- 🚀 **Performance Improvement**: Achieved up to 60% faster sync times in test scenarios due to batch processing and improved error handling.

## 🚀 Running the Script

### Using Docker

1. **Build the Docker Image**:
```sh
docker build -t steemdb_sync .
```

2. **Run the Docker Container**:
```sh
docker run -d --name steem-sync-container steemdb_sync
```

### 🛠️ Configuration

Modify the `config.json` file with the appropriate settings before running the Docker container. Example `config.json`:

```json
{
"mongodb_url": "mongodb://10.10.100.30:27017/",
"steemd_url": "http://10.10.100.12:8080",
"last_block_env": 78090042,
"batch_size": 50
}
```

- `mongodb_url`: The connection string to your MongoDB instance.
- `steemd_url`: The URL of the Steem node you are connecting to.
- `last_block_env`: The block number to start synchronization from.
- `batch_size`: Number of blocks to fetch in one batch (default is 50).

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

## 📜 License

This project is licensed under the MIT License. See the LICENSE file for details.
6 changes: 6 additions & 0 deletions docker/sync/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"mongodb_url": "mongodb://localhost:27017/",
"steemd_url": "http://localhost:8080",
"last_block_env": 1,
"batch_size": 50
}
11 changes: 0 additions & 11 deletions docker/sync/steemdb_sync.service

This file was deleted.

Loading

0 comments on commit 419f7ef

Please sign in to comment.