Class: Bitcoin::Storage::Backends::DummyStore
- Inherits:
-
StoreBase
- Object
- StoreBase
- Bitcoin::Storage::Backends::DummyStore
show all
- Defined in:
- lib/bitcoin/storage/dummy/dummy_store.rb
Constant Summary
Constants inherited
from StoreBase
StoreBase::DEFAULT_CONFIG, StoreBase::MAIN, StoreBase::ORPHAN, StoreBase::SCRIPT_TYPES, StoreBase::SEQUEL_ADAPTERS, StoreBase::SIDE
Instance Attribute Summary collapse
Attributes inherited from StoreBase
#config, #log
Instance Method Summary
collapse
Methods inherited from StoreBase
#add_watched_address, #backend_name, #check_metadata, #connect, #get_balance, #get_idx_from_tx_hash, #get_locator, #get_txouts_for_address, #import, #in_sync?, #init_sequel_store, #migrate, #new_block, #new_tx, #parse_script, #rescan, #sqlite_pragmas, #store_addr, #store_block, #update_block
Constructor Details
#initialize(*args) ⇒ DummyStore
Returns a new instance of DummyStore.
8
9
10
11
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 8
def initialize *args
reset
super(*args)
end
|
Instance Attribute Details
#blk ⇒ Object
Returns the value of attribute blk.
6
7
8
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 6
def blk
@blk
end
|
#tx ⇒ Object
Returns the value of attribute tx.
6
7
8
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 6
def tx
@tx
end
|
Instance Method Details
#get_block(blk_hash) ⇒ Object
63
64
65
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 63
def get_block(blk_hash)
wrap_block(@blk.find {|blk| blk.hash == blk_hash})
end
|
#get_block_by_depth(depth) ⇒ Object
55
56
57
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 55
def get_block_by_depth(depth)
wrap_block(@blk[depth])
end
|
#get_block_by_id(blk_id) ⇒ Object
67
68
69
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 67
def get_block_by_id(blk_id)
wrap_block(@blk[blk_id])
end
|
#get_block_by_prev_hash(hash) ⇒ Object
59
60
61
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 59
def get_block_by_prev_hash(hash)
wrap_block(@blk.find {|blk| blk.prev_block == [hash].pack("H*").reverse})
end
|
#get_block_by_tx(tx_hash) ⇒ Object
71
72
73
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 71
def get_block_by_tx(tx_hash)
wrap_block(@blk.find {|blk| blk.tx.map(&:hash).include?(tx_hash) })
end
|
#get_depth ⇒ Object
47
48
49
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 47
def get_depth
@blk.size - 1
end
|
#get_head ⇒ Object
51
52
53
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 51
def get_head
wrap_block(@blk[-1])
end
|
#get_tx(tx_hash) ⇒ Object
75
76
77
78
79
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 75
def get_tx(tx_hash)
transaction = @tx[tx_hash]
return nil unless transaction
wrap_tx(transaction)
end
|
#get_tx_by_id(tx_id) ⇒ Object
81
82
83
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 81
def get_tx_by_id(tx_id)
wrap_tx(@tx[tx_id])
end
|
#get_txin_for_txout(tx_hash, txout_idx) ⇒ Object
85
86
87
88
89
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 85
def get_txin_for_txout(tx_hash, txout_idx)
txin = @tx.values.map(&:in).flatten.find {|i| i.prev_out_index == txout_idx &&
i.prev_out == [tx_hash].pack("H*").reverse }
wrap_txin(txin)
end
|
#get_txouts_for_hash160(hash160, unconfirmed = false) ⇒ Object
96
97
98
99
100
101
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 96
def get_txouts_for_hash160(hash160, unconfirmed = false)
@tx.values.map(&:out).flatten.map {|o|
o = wrap_txout(o)
o.hash160 == hash160 ? o : nil
}.compact
end
|
#get_txouts_for_pk_script(script) ⇒ Object
91
92
93
94
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 91
def get_txouts_for_pk_script(script)
txouts = @tx.values.map(&:out).flatten.select {|o| o.pk_script == script}
txouts.map {|o| wrap_txout(o) }
end
|
#has_block(blk_hash) ⇒ Object
39
40
41
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 39
def has_block(blk_hash)
!!get_block(blk_hash)
end
|
#has_tx(tx_hash) ⇒ Object
43
44
45
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 43
def has_tx(tx_hash)
!!get_tx(tx_hash)
end
|
#persist_block(blk, chain, depth, prev_work = 0) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 17
def persist_block(blk, chain, depth, prev_work = 0)
return [depth, chain] unless blk && chain == 0
if block = get_block(blk.hash)
log.info { "Block already stored; skipping" }
return false
end
blk.tx.each {|tx| store_tx(tx) }
@blk << blk
log.info { "NEW HEAD: #{blk.hash} DEPTH: #{get_depth}" }
[depth, chain]
end
|
#reset ⇒ Object
13
14
15
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 13
def reset
@blk, @tx = [], {}
end
|
#store_tx(tx, validate = true) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 31
def store_tx(tx, validate = true)
if @tx.keys.include?(tx.hash)
log.info { "Tx already stored; skipping" }
return tx
end
@tx[tx.hash] = tx
end
|
#to_s ⇒ Object
155
156
157
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 155
def to_s
"DummyStore"
end
|
#wrap_block(block) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 103
def wrap_block(block)
return nil unless block
data = { id: @blk.index(block), depth: @blk.index(block),
work: @blk.index(block), chain: MAIN, size: block.size }
blk = Bitcoin::Storage::Models::Block.new(self, data)
[:ver, :prev_block, :mrkl_root, :time, :bits, :nonce].each do |attr|
blk.send("#{attr}=", block.send(attr))
end
block.tx.each do |tx|
blk.tx << get_tx(tx.hash)
end
blk.recalc_block_hash
blk
end
|
#wrap_tx(transaction) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 118
def wrap_tx(transaction)
return nil unless transaction
blk = @blk.find{|b| b.tx.include?(transaction)}
data = { id: transaction.hash, blk_id: @blk.index(blk),
size: transaction.size }
tx = Bitcoin::Storage::Models::Tx.new(self, data)
tx.ver = transaction.ver
tx.lock_time = transaction.lock_time
transaction.in.each {|i| tx.add_in(wrap_txin(i))}
transaction.out.each {|o| tx.add_out(wrap_txout(o))}
tx.hash = tx.hash_from_payload(tx.to_payload)
tx
end
|
#wrap_txin(input) ⇒ Object
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 132
def wrap_txin(input)
return nil unless input
tx = @tx.values.find{|t| t.in.include?(input)}
data = { tx_id: tx.hash, tx_idx: tx.in.index(input)}
txin = Bitcoin::Storage::Models::TxIn.new(self, data)
[:prev_out, :prev_out_index, :script_sig_length, :script_sig, :sequence].each do |attr|
txin.send("#{attr}=", input.send(attr))
end
txin
end
|
#wrap_txout(output) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/bitcoin/storage/dummy/dummy_store.rb', line 143
def wrap_txout(output)
return nil unless output
tx = @tx.values.find{|t| t.out.include?(output)}
data = {tx_id: tx.hash, tx_idx: tx.out.index(output),
hash160: Bitcoin::Script.new(output.pk_script).get_hash160 }
txout = Bitcoin::Storage::Models::TxOut.new(self, data)
[:value, :pk_script_length, :pk_script].each do |attr|
txout.send("#{attr}=", output.send(attr))
end
txout
end
|