|
| 1 | +# Blockchain Data Sync |
| 2 | + |
| 3 | +Many services require to synchronize blockchain data, e.g. blocks, transactions, receipts and traces. This package provides a data synchronization framework to resolve some problems in common: |
| 4 | + |
| 5 | +- Pipeline: poll blockchain data, transform data and store data in separate goroutines. |
| 6 | +- Concurrency: poll blockchain data in parallel during catch up phase. |
| 7 | +- Batch: allow to store data into databases in batch, stead of block by block during catch up pahse. |
| 8 | +- Chain reorg: framework detects chain reorg and defines a common interface for clients to handle chain reorg (e.g. pop data from database). |
| 9 | +- Memory bounded: uses memory bounded channel to cache polled blockchain data to void OOM. |
| 10 | + |
| 11 | +Using this framework, users only need to focus on how to handle the data. |
| 12 | + |
| 13 | +## Adapter |
| 14 | + |
| 15 | +This package provides a [Adapter](./poll/adapter.go) interface to adapt any data source to poll blockchain data. |
| 16 | + |
| 17 | +There are 2 pre-defined adapters: |
| 18 | + |
| 19 | +1. [EVM adapter](./evm/adapter.go): poll data from eSpace RPC. |
| 20 | +2. [Core adapter](./core/adapter.go): poll data from core space RPC. |
| 21 | + |
| 22 | +## Poller |
| 23 | + |
| 24 | +There are 3 kinds of pollers available: |
| 25 | + |
| 26 | +1. [CatchUpPoller](./poll/catchup_poller.go): optimized to poll data in catch up phase with high performance. |
| 27 | +2. [FinalizedPoller](./poll/finalized_poller.go): poll finalized data block by block. |
| 28 | +3. [LatestPoller](./poll/latest_poller.go): poll latest data block by block, and handle the chain reorg. |
| 29 | + |
| 30 | +## Database Processor |
| 31 | + |
| 32 | +This package defines a common interface to transform blockchain data into a database operation, so that the framework will operate database in a transaction. Besides, some common used operations are already defined. |
| 33 | + |
| 34 | +```go |
| 35 | +type Operation interface { |
| 36 | + Exec(tx *gorm.DB) error |
| 37 | +} |
| 38 | + |
| 39 | +// ComposeOperation composes multiple database operations in batch. |
| 40 | +func ComposeOperation(ops ...Operation) Operation |
| 41 | + |
| 42 | +// CreateOperation returns a Create database operation. |
| 43 | +func CreateOperation(models ...any) Operation |
| 44 | + |
| 45 | +// DeleteOperation returns a Delete database operation. |
| 46 | +func DeleteOperation(modelPtr any, conds ...any) Operation |
| 47 | +``` |
| 48 | + |
| 49 | +User could implement below interface to transform the blockchain data into a database operation: |
| 50 | + |
| 51 | +```go |
| 52 | +// Processor is implemented by types that process data to update database. |
| 53 | +type Processor[T any] interface { |
| 54 | + Process(data T) Operation |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +If user want to synchronize the latest data, and handle the chain reorg correctly. Then, please implements the revertable interface as below: |
| 59 | + |
| 60 | +```go |
| 61 | +// RevertableProcessor is implemented by types that process revertable data. |
| 62 | +type RevertableProcessor[T any] interface { |
| 63 | + Processor[T] |
| 64 | + |
| 65 | + // Revert deletes data from database of given data block number. |
| 66 | + Revert(data T) Operation |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +During catch up phase, to achieve batch database operations, user could implement the batchable interface: |
| 71 | + |
| 72 | +```go |
| 73 | +// BatchProcessor is implemented by types that process data in batch. |
| 74 | +// |
| 75 | +// Note, thread-safe is not required in the implementations, since batch |
| 76 | +// related methods are executed in a single thread. |
| 77 | +type BatchProcessor[T any] interface { |
| 78 | + Processor[T] |
| 79 | + |
| 80 | + // BatchProcess processes the given data and returns the number of SQLs |
| 81 | + // to be executed in batch. |
| 82 | + BatchProcess(data T) int |
| 83 | + |
| 84 | + // BatchExec executes SQLs in batch. |
| 85 | + BatchExec(tx *gorm.DB, createBatchSize int) error |
| 86 | + |
| 87 | + // BatchReset reset data for the next batch. |
| 88 | + BatchReset() |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Sync Utilities |
| 93 | + |
| 94 | +There are 3 helper methods available in the framework to poll blockchain data and store in database. Users need to provide custom database processors to handle polled blockchain data. |
| 95 | + |
| 96 | +1. [CatchUpDB](./sync_db.go): catch up blockchain data to the finalized block using `BatchProcessor`. |
| 97 | +2. [StartFinalizedDB](./sync_db.go): start to synchronize data block by block against the finalized block using normal `Processor`. |
| 98 | +3. [StartLatestDB](./sync_db.go): start to synchronize data block by block against the latest block and handle chain reorg using `RevertableProcessor`. |
0 commit comments