Class: Bitcoin::Block
- Inherits:
-
Object
- Object
- Bitcoin::Block
- Defined in:
- lib/bc.rb
Overview
This class represents a block in the Bitcoin block chain. (c.f. en.bitcoin.it/wiki/Block and en.bitcoin.it/wiki/Block_hashing_algorithm)
Instance Attribute Summary collapse
-
#bc ⇒ Object
readonly
This is the Bitcoin::Client instance we are connected to.
-
#block_id ⇒ Object
readonly
This is the unique ID of the block.
-
#difficulty ⇒ Object
readonly
This is a Float representing how difficult it was to generate the block.
-
#height ⇒ Object
readonly
This is the order of this block in the block chain, i.e.
-
#merkle_root ⇒ Object
readonly
This is a base 16 String representation of a SHA-256 hash generated from all the transactions in the block.
-
#nonce ⇒ Object
readonly
This is a 32-bit Fixnum used in order to get a @block_id matching @target.
-
#target ⇒ Object
readonly
This is a compact representation of the prefix this block was attempting to match.
-
#transactions ⇒ Object
readonly
This is an Array of every Transaction that is part of the block.
-
#version ⇒ Object
readonly
This is the block version.
Instance Method Summary collapse
-
#created_at ⇒ Object
This is the Time the block was created at.
-
#initialize(bc, block_id) ⇒ Block
constructor
bc
is a Bitcoin::Client instance. - #inspect ⇒ Object
-
#next_block ⇒ Object
This is the Block created immediately after this one, or
nil
if this is the most recent block. -
#previous_block ⇒ Object
This is the Block created prior to this one, or
nil
if this is the origin block.
Constructor Details
#initialize(bc, block_id) ⇒ Block
bc
is a Bitcoin::Client instance. block_id is our unique (String) block ID. We raise UnknownBitcoinBlock if the bitcoind doesn’t know about a block with that ID.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/bc.rb', line 145 def initialize(bc, block_id) @bc = bc unless @bc.is_a?(Bitcoin::Client) raise TypeError, "bc must be a Bitcoin::Client (#{@bc.class} given)" end unless block_id.is_a?(String) raise TypeError, "block_id must be a String (#{block_id.class} given)" end begin block_data = @bc.jr.getblock(block_id) rescue Jr::ServerError => ex if ex.code == -5 raise UnknownBlock, block_id else raise end end { block_id: :hash, height: :height, version: :version, merkle_root: :merkleroot, created_at_unix_time: :time, nonce: :nonce, difficulty: :difficulty, transaction_ids: :tx, previous_block_id: :nextblockhash, next_block_id: :previoushblockhash }.each do |our_attr, block_data_key| instance_variable_set( "@#{our_attr}", block_data[block_data_key.to_s].freeze ) end @transactions ||= [].freeze end |
Instance Attribute Details
#bc ⇒ Object (readonly)
This is the Bitcoin::Client instance we are connected to.
111 112 113 |
# File 'lib/bc.rb', line 111 def bc @bc end |
#block_id ⇒ Object (readonly)
This is the unique ID of the block. It is a String beginning with a number of ‘0’s.
115 116 117 |
# File 'lib/bc.rb', line 115 def block_id @block_id end |
#difficulty ⇒ Object (readonly)
This is a Float representing how difficult it was to generate the block. It increases logarithmically in proportion to difficulty.
133 134 135 |
# File 'lib/bc.rb', line 133 def difficulty @difficulty end |
#height ⇒ Object (readonly)
This is the order of this block in the block chain, i.e. the number of blocks that came before it.
119 120 121 |
# File 'lib/bc.rb', line 119 def height @height end |
#merkle_root ⇒ Object (readonly)
This is a base 16 String representation of a SHA-256 hash generated from all the transactions in the block.
126 127 128 |
# File 'lib/bc.rb', line 126 def merkle_root @merkle_root end |
#nonce ⇒ Object (readonly)
This is a 32-bit Fixnum used in order to get a @block_id matching @target.
129 130 131 |
# File 'lib/bc.rb', line 129 def nonce @nonce end |
#target ⇒ Object (readonly)
This is a compact representation of the prefix this block was attempting to match.
140 141 142 |
# File 'lib/bc.rb', line 140 def target @target end |
#transactions ⇒ Object (readonly)
This is an Array of every Transaction that is part of the block.
136 137 138 |
# File 'lib/bc.rb', line 136 def transactions @transactions end |
#version ⇒ Object (readonly)
This is the block version. Currently, it should be 1
.
122 123 124 |
# File 'lib/bc.rb', line 122 def version @version end |
Instance Method Details
#created_at ⇒ Object
This is the Time the block was created at.
200 201 202 |
# File 'lib/bc.rb', line 200 def created_at @created_at ||= Time.at(@created_at_unix_time).utc.freeze end |
#inspect ⇒ Object
204 205 206 |
# File 'lib/bc.rb', line 204 def inspect "#<Bitcoin::Block #{@block_id}>" end |
#next_block ⇒ Object
This is the Block created immediately after this one, or nil
if this is the most recent block.
189 190 191 |
# File 'lib/bc.rb', line 189 def next_block @bc.get_block(@next_block_id) end |
#previous_block ⇒ Object
This is the Block created prior to this one, or nil
if this is the origin block.
195 196 197 |
# File 'lib/bc.rb', line 195 def previous_block @bc.get_block(@previous_block_id) end |