Class: Glueby::BlockSyncer

Inherits:
Object
  • Object
show all
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.

Examples:

Register a synchronous logic

class Syncer
  def block_sync (block)
    # sync a block
  end

  def tx_sync (tx)
    # sync a tx
  end
end
BlockSyncer.register_syncer(Syncer)

Unregister the synchronous logic

BlockSyncer.unregister_syncer(Syncer)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height) ⇒ BlockSyncer

Returns a new instance of BlockSyncer.

Parameters:

  • height (Integer)

    The block height to be synced in the instance



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.

Returns:

  • (Array<Class>)

    The syncer classes that is registered



49
# File 'lib/glueby/block_syncer.rb', line 49

attr_reader :syncers

.syncersObject (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

#heightObject (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

Parameters:

  • syncer (Class)

    The syncer to be registered.



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

Parameters:

  • syncer (Class)

    The syncer to be unregistered.



60
61
62
63
# File 'lib/glueby/block_syncer.rb', line 60

def unregister_syncer(syncer)
  @syncers ||= []
  @syncers.delete(syncer)
end

Instance Method Details

#runObject

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