Class: NEAR::Block
- Inherits:
-
Object
- Object
- NEAR::Block
- Defined in:
- lib/near/block.rb
Overview
Represents a NEAR block.
Instance Attribute Summary collapse
- #author ⇒ String readonly
- #chunks ⇒ Array<Hash> readonly
-
#data ⇒ Hash
readonly
The block data, if fetched.
-
#hash ⇒ String
readonly
The hash of the block.
-
#header ⇒ Hash
readonly
The block header.
-
#height ⇒ Integer
readonly
The height of the block.
- #shards ⇒ Array<Hash> readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#each_action {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator
Enumerates the actions in this block.
-
#each_chunk {|Hash| ... } ⇒ Enumerator
Enumerates the chunks in this block.
-
#each_transaction {|NEAR::Transaction| ... } ⇒ Enumerator
Enumerates the transactions in this block.
-
#find_actions(type, signer: nil, receiver: nil, method_name: nil) {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator
If no block is given.
-
#initialize(height: nil, hash: nil, data: nil) ⇒ Block
constructor
A new instance of Block.
- #inspect ⇒ String
-
#receivers ⇒ Array<NEAR::Account>
The set of receivers in the transactions in this block.
-
#signers ⇒ Array<NEAR::Account>
The set of signers in the transactions in this block.
- #to_cli_args ⇒ Array<String>
- #to_h ⇒ Hash
- #to_i ⇒ Integer
- #to_s ⇒ String
Constructor Details
#initialize(height: nil, hash: nil, data: nil) ⇒ Block
Returns a new instance of Block.
30 31 32 33 34 |
# File 'lib/near/block.rb', line 30 def initialize(height: nil, hash: nil, data: nil) @height = height.to_i if height @hash = hash.to_s if hash @data = data.to_h if data end |
Instance Attribute Details
#author ⇒ String (readonly)
56 57 58 |
# File 'lib/near/block.rb', line 56 def @author end |
#chunks ⇒ Array<Hash> (readonly)
72 73 74 |
# File 'lib/near/block.rb', line 72 def chunks @chunks end |
#data ⇒ Hash (readonly)
The block data, if fetched.
52 53 54 |
# File 'lib/near/block.rb', line 52 def data @data end |
#hash ⇒ String (readonly)
The hash of the block.
46 47 48 |
# File 'lib/near/block.rb', line 46 def hash @hash end |
#header ⇒ Hash (readonly)
The block header.
65 66 67 |
# File 'lib/near/block.rb', line 65 def header @header end |
#height ⇒ Integer (readonly)
The height of the block.
40 41 42 |
# File 'lib/near/block.rb', line 40 def height @height end |
#shards ⇒ Array<Hash> (readonly)
79 80 81 |
# File 'lib/near/block.rb', line 79 def shards @shards end |
Class Method Details
.at_hash(hash) ⇒ NEAR::Block
22 23 24 |
# File 'lib/near/block.rb', line 22 def self.at_hash(hash) self.new(hash: hash) end |
.at_height(height) ⇒ NEAR::Block
15 16 17 |
# File 'lib/near/block.rb', line 15 def self.at_height(height) self.new(height: height) end |
Instance Method Details
#each_action {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator
Enumerates the actions in this block.
127 128 129 130 131 132 133 134 |
# File 'lib/near/block.rb', line 127 def each_action(&) return enum_for(:each_action) unless block_given? self.each_transaction do |tx| tx.each_action do |action| yield action, tx end end end |
#each_chunk {|Hash| ... } ⇒ Enumerator
Enumerates the chunks in this block.
167 168 169 170 171 172 173 |
# File 'lib/near/block.rb', line 167 def each_chunk(&) return enum_for(:each_chunk) unless block_given? self.shards.each do |shard| chunk = shard['chunk'] yield chunk if chunk end end |
#each_transaction {|NEAR::Transaction| ... } ⇒ Enumerator
Enumerates the transactions in this block.
109 110 111 112 113 114 115 116 117 |
# File 'lib/near/block.rb', line 109 def each_transaction(&) return enum_for(:each_transaction) unless block_given? self.each_chunk do |chunk| next if !chunk['transactions'] chunk['transactions'].each do |tx| yield NEAR::Transaction.parse(tx) end end end |
#find_actions(type, signer: nil, receiver: nil, method_name: nil) {|NEAR::Action, NEAR::Transaction| ... } ⇒ Enumerator
Returns if no block is given.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/near/block.rb', line 146 def find_actions(type, signer: nil, receiver: nil, method_name: nil, &) return enum_for(:each_action) unless block_given? type = type.to_sym self.each_transaction do |tx| next if signer && !(signer === tx.signer_id) next if receiver && !(receiver === tx.receiver_id) tx.each_action do |action| next unless type == action.type next if method_name && !(method_name === action.method_name) yield action, tx end end end |
#inspect ⇒ String
199 200 201 |
# File 'lib/near/block.rb', line 199 def inspect "#<#{self.class.name} height: #{@height}, hash: #{@hash.inspect}>" end |
#receivers ⇒ Array<NEAR::Account>
The set of receivers in the transactions in this block.
97 98 99 100 |
# File 'lib/near/block.rb', line 97 def receivers self.collect_transaction_field('receiver_id') .map { |id| NEAR::Account.new(id) } end |
#signers ⇒ Array<NEAR::Account>
The set of signers in the transactions in this block.
88 89 90 91 |
# File 'lib/near/block.rb', line 88 def signers self.collect_transaction_field('signer_id') .map { |id| NEAR::Account.new(id) } end |
#to_cli_args ⇒ Array<String>
177 178 179 180 181 |
# File 'lib/near/block.rb', line 177 def to_cli_args return ['at-block-height', @height] if @height return ['at-block-hash', @hash] if @hash ['now'] end |
#to_h ⇒ Hash
195 |
# File 'lib/near/block.rb', line 195 def to_h; @data; end |
#to_i ⇒ Integer
185 |
# File 'lib/near/block.rb', line 185 def to_i; @height; end |
#to_s ⇒ String
189 190 191 |
# File 'lib/near/block.rb', line 189 def to_s (@height || @hash || :now).to_s end |