Class: Glueby::BlockSyncer
- Inherits:
-
Object
- Object
- Glueby::BlockSyncer
- Defined in:
- lib/glueby/block_syncer.rb
Overview
You can use BlockSyncer when you need to synchronize the state of an application with the state of a blockchain. When BlockSyncer detects the generation of a new block, it executes the registered syncer code on a block-by-block or transaction-by-transaction basis. By using this, an application can detect that the issued transaction has been captured in blocks, receive a new remittance, and so on.
# Syncer logic registration
For registration, create a class that implements the method that performs synchronization processing and registers it in BlockSyncer. Implement methods with the following name in that class.
Method name | Arguments | Call conditions —————— | ——————— | —————————— block_sync (block) | block: Tapyrus::Block | When a new block is created block_tx (tx) | tx: Tapyrus::Tx | When a new block is created, it is executed for each tx contained in that block.
# Run BlockSyncer
Run the ‘glueby: block_syncer: start` rake task periodically with a program for periodic execution such as cron. If it detects the generation of a new block when it is executed, the synchronization process will be executed. Determine the execution interval according to the requirements of the application.
Class Attribute Summary collapse
-
.r syncers(syncers) ⇒ Array<Class>
The syncer classes that is registered.
-
.syncers ⇒ Object
readonly
Returns the value of attribute syncers.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
Class Method Summary collapse
-
.register_syncer(syncer) ⇒ Object
Register syncer class.
-
.unregister_syncer(syncer) ⇒ Object
Unregister syncer class.
Instance Method Summary collapse
-
#initialize(height) ⇒ BlockSyncer
constructor
A new instance of BlockSyncer.
-
#run ⇒ Object
Run a block synchronization.
Constructor Details
#initialize(height) ⇒ BlockSyncer
Returns a new instance of BlockSyncer.
67 68 69 |
# File 'lib/glueby/block_syncer.rb', line 67 def initialize(height) @height = height end |
Class Attribute Details
.r syncers(syncers) ⇒ Array<Class>
Returns The syncer classes that is registered.
49 |
# File 'lib/glueby/block_syncer.rb', line 49 attr_reader :syncers |
.syncers ⇒ Object (readonly)
Returns the value of attribute syncers.
49 50 51 |
# File 'lib/glueby/block_syncer.rb', line 49 def syncers @syncers end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
44 45 46 |
# File 'lib/glueby/block_syncer.rb', line 44 def height @height end |
Class Method Details
.register_syncer(syncer) ⇒ Object
Register syncer class
53 54 55 56 |
# File 'lib/glueby/block_syncer.rb', line 53 def register_syncer(syncer) @syncers ||= [] @syncers << syncer end |
.unregister_syncer(syncer) ⇒ Object
Unregister syncer class
60 61 62 63 |
# File 'lib/glueby/block_syncer.rb', line 60 def unregister_syncer(syncer) @syncers ||= [] @syncers.delete(syncer) end |
Instance Method Details
#run ⇒ Object
Run a block synchronization
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/glueby/block_syncer.rb', line 72 def run return if self.class.syncers.nil? self.class.syncers.each do |syncer| instance = syncer.new instance.block_sync(block) if instance.respond_to?(:block_sync) if instance.respond_to?(:tx_sync) block.transactions.each { |tx| instance.tx_sync(tx) } end end end |