Class: Bitcoin::Storage::Models::Tx

Inherits:
Protocol::Tx show all
Defined in:
lib/bitcoin/storage/models.rb

Overview

Transaction retrieved from storage. (see Bitcoin::Protocol::Tx)

Constant Summary

Constants inherited from Protocol::Tx

Protocol::Tx::DEFAULT_BLOCK_PRIORITY_SIZE, Protocol::Tx::SIGHASH_TYPE

Instance Attribute Summary collapse

Attributes inherited from Protocol::Tx

#in, #out, #payload

Instance Method Summary collapse

Methods inherited from Protocol::Tx

#==, #add_in, #add_out, binary_from_hash, binary_from_json, #binary_hash, #calculate_minimum_fee, from_file, from_hash, from_json, from_json_file, #hash_from_payload, #is_coinbase?, #minimum_block_fee, #minimum_relay_fee, #parse_data_from_io, #signature_hash_for_input, #to_hash, #to_json, #to_json_file, #to_payload, #validator, #verify_input_signature

Constructor Details

#initialize(store, data) ⇒ Tx

Returns a new instance of Tx.



58
59
60
61
62
63
64
65
# File 'lib/bitcoin/storage/models.rb', line 58

def initialize store, data
  @store = store
  @id = data[:id]
  @blk_id = data[:blk_id]
  @size = data[:size]
  @idx  = data[:idx]
  super(nil)
end

Instance Attribute Details

#blk_idObject (readonly)

Returns the value of attribute blk_id.



56
57
58
# File 'lib/bitcoin/storage/models.rb', line 56

def blk_id
  @blk_id
end

#hashObject

Returns the value of attribute hash.



55
56
57
# File 'lib/bitcoin/storage/models.rb', line 55

def hash
  @hash
end

#idObject (readonly)

Returns the value of attribute id.



56
57
58
# File 'lib/bitcoin/storage/models.rb', line 56

def id
  @id
end

#idxObject (readonly)

Returns the value of attribute idx.



56
57
58
# File 'lib/bitcoin/storage/models.rb', line 56

def idx
  @idx
end

#lock_timeObject

Returns the value of attribute lock_time.



55
56
57
# File 'lib/bitcoin/storage/models.rb', line 55

def lock_time
  @lock_time
end

#sizeObject (readonly)

Returns the value of attribute size.



56
57
58
# File 'lib/bitcoin/storage/models.rb', line 56

def size
  @size
end

#storeObject (readonly)

Returns the value of attribute store.



56
57
58
# File 'lib/bitcoin/storage/models.rb', line 56

def store
  @store
end

#verObject

Returns the value of attribute ver.



55
56
57
# File 'lib/bitcoin/storage/models.rb', line 55

def ver
  @ver
end

Instance Method Details

#confirmationsObject

get the number of blocks that confirm this tx in the main chain



74
75
76
77
# File 'lib/bitcoin/storage/models.rb', line 74

def confirmations
  return 0  unless get_block
  @store.get_head.depth - get_block.depth + 1
end

#feeObject



90
91
92
# File 'lib/bitcoin/storage/models.rb', line 90

def fee
  @fee ||= total_in - total_out
end

#get_blockObject

get the block this transaction is in



68
69
70
71
# File 'lib/bitcoin/storage/models.rb', line 68

def get_block
  return nil  unless @blk_id
  @block ||= @store.get_block_by_id(@blk_id)
end

#total_inObject

if tx_in is coinbase, set in value as total_out, fee could be 0



84
85
86
87
88
# File 'lib/bitcoin/storage/models.rb', line 84

def total_in
  @total_in ||= self.in.inject(0){ |m, input|
    m + (input.coinbase? ? total_out : (input.get_prev_out.try(:value) || 0))
  }
end

#total_outObject



79
80
81
# File 'lib/bitcoin/storage/models.rb', line 79

def total_out
  @total_out ||= self.out.inject(0){ |e, o| e + o.value }
end