System Models
Bitcoin Crawler includes built-in system models that provide essential blockchain monitoring functionality out of the box. Use them without writing any model code - just subscribe to their events and query their state.
What are System Models?β
System models are pre-built models that handle common blockchain monitoring tasks:
- Network Chain Validation Model: Validates blockchain integrity and manages chain state
- Mempool Model: Monitors unconfirmed transactions (when enabled)
These models:
- Work automatically when you start Bitcoin Crawler
- Generate events you can subscribe to
- Maintain state you can query
- Follow same patterns as custom models
System Models Architectureβ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β System Models in Framework β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Blockchain Node
β
βΌ
βββββββββββββββββββββββ
β Network Provider β Fetches blocks and mempool
βββββββββββββββββββββββ
β
ββββββββββββββββββββ¬βββββββββββββββββββ
βΌ βΌ βΌ
ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββ
β Network β β Mempool β β Your Custom β
β Model β β Model β β Models β
β (built-in) β β (built-in) β β (your code) β
ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββ
β β β
ββββββββββββββββββββ΄βββββββββββββββββββββββ
βΌ
ββββββββββββββββββββ
β Event Store β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Transport β
ββββββββββββββββββββ
Network Chain Validation Modelβ
Validates blockchain chain integrity and maintains a validated block chain window in memory.
Purposeβ
The Network Model:
- Verifies each block connects to previous block
- Validates Merkle tree roots (Bitcoin-specific)
- Detects blockchain reorganizations
- Maintains sliding window of recent blocks
- Provides validated block data for queries
What It Tracksβ
Chain state:
- Current number of blocks in memory
- Latest block height
- First block height in window
- Chain validity status
Block data:
- Recent blocks with full data
- Block hashes and headers
- Transaction data
Validation status:
- Whether chain is valid
- Whether Merkle trees verified
- Reorganization history
Events Generatedβ
Block Added:
- New block validated and added to chain
- Contains block height, hash, transactions
- Emitted after passing all validations
Block Removed:
- Block removed during reorganization
- Contains removed block height and hash
- Part of reorg process
Reorganization Detected:
- Blockchain fork detected and handled
- Contains fork point and affected blocks
- Informational (state already corrected)
Chain Initialized:
- Network model started
- Contains initial configuration
Chain Validation Processβ
βββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββ
β Network Validation Flow β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
New Block Arrives
β
βΌ
ββββββββββββββββββββ
β Verify block β Check hash connects to previous
β links to chain β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Validate Merkle β Verify Merkle root (if enabled)
β tree β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Add to chain β Store in validated chain window
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Emit BlockAdded β Notify subscribers
β event β
ββββββββββββββββββββ
Reorganization Handlingβ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Reorganization Detection & Handling β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Detect fork:
Current chain: ... β Block 100 β Block 101 β Block 102
Node's chain: ... β Block 100 β Block 101 β Block 102'
β
βΌ
Identify fork point: Block 101 (last common)
β
βΌ
Roll back:
- Emit BlockRemoved for Block 102
- Remove from chain window
β
βΌ
Replay:
- Fetch Block 102' from node
- Validate new block
- Emit BlockAdded for Block 102'
β
βΌ
Emit ReorganizationDetected event
- Contains fork point
- Lists affected blocks
β
βΌ
Chain now matches node's chain
Important: Your model states automatically update during reorg. The reorganization event is informational - for logging or custom business logic only.
Merkle Tree Validationβ
For Bitcoin and Bitcoin-like chains:
What it validates:
- Merkle root in block header matches calculated root
- Transaction hashes form valid Merkle tree
- Ensures block data integrity
Why it matters:
- Detects corrupted block data
- Ensures data from node is valid
- Additional verification layer
Configuration:
- Enabled by default
- Can be disabled for performance
- Recommended to keep enabled for production
Performance: Minimal impact on modern servers
Querying Network Modelβ
Through Transport APIs:
Get network statistics:
- Current chain size
- Block height range
- Validity status
- Chain window info
Get specific block:
- Query by height
- Returns block if in chain window
- Includes full transaction data
Get multiple blocks:
- Last N blocks
- All blocks in window
- Range queries
Get last block:
- Most recent validated block
- Latest height and data
All queries work through HTTP, WebSocket, IPC, or other configured transports.
Using Network Eventsβ
Subscribe to network events in your applications:
Monitor new blocks:
- React when blocks are added
- Update UI or trigger processing
- Log block information
Handle reorganizations:
- Get notified of reorgs
- Implement custom logging
- Alert on unusual activity
- Note: your state already correct
Track chain status:
- Monitor chain validity
- Alert if validation fails
- Track chain growth
Use in custom models:
- Your models can subscribe to network events
- Build logic on top of validated blocks
- Leverage chain validation
Mempool Modelβ
Monitors unconfirmed transactions in mempool. Disabled by default - must be enabled in configuration.
Purposeβ
The Mempool Model:
- Tracks pending transactions before confirmation
- Monitors transaction additions and removals
- Handles multiple mempool sources
- Provides real-time mempool state
- Generates events for mempool changes
What It Tracksβ
Mempool state:
- Total transaction IDs tracked
- Number of fully loaded transactions
- Synchronization status
- Performance metrics
Transaction data:
- Transaction IDs in mempool
- Full transaction details (when loaded)
- Fee rates and sizes
- Input/output data
Load status:
- Which transactions are fully loaded
- Which are ID-only (below fee threshold)
- Load timestamps
Events Generatedβ
Transaction Added:
- New transaction appeared in mempool
- Contains transaction ID
- Initial detection event
Transaction Loaded:
- Transaction details fully fetched
- Contains complete transaction data
- Follows Added event
Transaction Removed:
- Transaction left mempool
- Reason: confirmed in block or dropped
- Contains confirmation height (if confirmed)
Transaction Updated:
- Transaction status changed
- Example: RBF replacement
- Contains updated data
Mempool Initialized:
- Mempool monitoring started
- Contains initial configuration
Mempool Monitoring Flowβ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Mempool Monitoring Process β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Poll mempool (every ~10 seconds)
β
βΌ
ββββββββββββββββββββ
β Fetch mempool β Get list of transaction IDs
β transaction IDs β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Compare with β Find new transactions
β known txids β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Emit Transaction β For each new transaction
β Added event β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Load details β Fetch transaction data
β (if fee >= min) β (filtered by fee rate)
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Emit Transaction β After details loaded
β Loaded event β
ββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Track until β Monitor for confirmation
β confirmed/droppedβ or removal from mempool
ββββββββββββββββββββ
Configurationβ
Enable mempool monitoring:
- Disabled by default
- Enable in configuration
- Works only through RPC providers
Settings:
- Sync strategy threshold
- Minimum fee rate filter
- Multiple provider URLs (optional)
See Mempool Monitoring documentation for detailed configuration.
Querying Mempool Modelβ
Through Transport APIs:
Get mempool statistics:
- Total transactions tracked
- Loaded transaction count
- Synchronization status
- Performance metrics
Get all transactions:
- Filter by load status
- Stream large datasets
- Batch processing
Get transaction IDs:
- With or without load info
- Stream for large mempools
Check specific transaction:
- Verify if in mempool
- Get load status
- Retrieve details
Using Mempool Eventsβ
Subscribe to mempool events in your applications:
Track pending payments:
- Detect incoming transactions
- Show pending status in UI
- Monitor until confirmed
Analyze fee market:
- Track fee distributions
- Calculate fee estimates
- Monitor market changes
Build transaction tools:
- Identify stuck transactions
- Track replacements (RBF)
- Monitor confirmation times
Use in custom models:
- Subscribe to mempool events
- Filter by your criteria
- Maintain pending state
- Update when confirmed
Performance Considerationsβ
Request volume:
- Significantly higher than block processing
- Multiple providers multiply requests
- Monitor rate limits carefully
Memory usage:
- Tracks many transactions in memory
- Consider for large mempools
- Filter with minimum fee rate
Event frequency:
- High event rate during busy periods
- Can be thousands of events per minute
- Filter aggressively in your code
See Mempool Monitoring documentation for optimization strategies.
Using System Modelsβ
In Your Applicationsβ
Subscribe to events:
- Network events for block monitoring
- Mempool events for pending transactions
- React to state changes
Query state:
- Get current chain status
- Check mempool statistics
- Retrieve specific blocks or transactions
Build on top:
- Use system events as triggers
- Combine with your custom logic
- Leverage validated data
In Custom Modelsβ
Your custom models can use system model events:
React to network events:
- Process blocks after validation
- Handle reorganizations
- Use validated chain data
React to mempool events:
- Track pending transactions
- Filter by your criteria
- Maintain pending balances
Combine with custom logic:
- Add business rules
- Filter and transform
- Store relevant data only
This gives complete control over how system data affects your business logic.
Best Practicesβ
Network Modelβ
Always available: Works automatically, no configuration needed
Subscribe to reorg events: For logging and monitoring
Don't store block data: Query from network model instead
Monitor chain validity: Alert if isValid becomes false
Mempool Modelβ
Enable only if needed: Increases costs and complexity
Filter aggressively: Don't process every transaction
Set appropriate fee threshold: Ignore very low-fee transactions
Monitor request volume: Much higher than block processing
Separate pending state: Keep mempool data separate from confirmed
Architecture Contextβ
System models integrate with all framework components:
Network Providers fetch data β System Models process System Models generate events β Event Store persists Transport exposes β Clients query and subscribe Custom Models can use β System Model events as triggers
System models provide foundation - you build business logic on top.