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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/bitcoin/namecoin.rb', line 151
def self.included(base)
base.constants.each {|c| const_set(c, base.const_get(c)) unless constants.include?(c) }
base.class_eval do
def store_name(script, txout_id)
if script.type == :name_new
log.debug { "name_new #{script.get_namecoin_hash}" }
@db[:names].insert({
:txout_id => txout_id,
:hash => script.get_namecoin_hash })
elsif script.type == :name_firstupdate
name_hash = script.get_namecoin_hash
name_new = @db[:names].where(:hash => name_hash).order(:txout_id).first
if self.class.name =~ /UtxoStore/
txout = @db[:utxo][id: name_new[:txout_id]] if name_new
blk = @db[:blk][id: txout[:blk_id]] if txout
else
txout = @db[:txout][id: name_new[:txout_id]] if name_new
tx = @db[:tx][id: txout[:tx_id]] if txout
blk_tx = @db[:blk_tx][tx_id: tx[:id]] if tx
blk = @db[:blk][id: blk_tx[:blk_id]] if blk_tx
end
unless name_new && blk && blk[:chain] == 0
log.debug { "name_new not found: #{name_hash}" }
return nil
end
unless blk[:depth] <= get_depth - Bitcoin::Namecoin::FIRSTUPDATE_LIMIT
log.debug { "name_new not yet valid: #{name_hash}" }
return nil
end
log.debug { "#{script.type}: #{script.get_namecoin_name}" }
@db[:names].where(:txout_id => name_new[:txout_id], :name => nil).update({
:name => script.get_namecoin_name.to_s.to_sequel_blob })
@db[:names].insert({
:txout_id => txout_id,
:hash => name_hash,
:name => script.get_namecoin_name.to_s.to_sequel_blob,
:value => script.get_namecoin_value.to_s.to_sequel_blob })
elsif script.type == :name_update
log.debug { "#{script.type}: #{script.get_namecoin_name}" }
@db[:names].insert({
:txout_id => txout_id,
:name => script.get_namecoin_name.to_s.to_sequel_blob,
:value => script.get_namecoin_value.to_s.to_sequel_blob })
end
end
def name_show name
names = @db[:names].where(:name => name.to_sequel_blob).order(:txout_id).reverse
return nil unless names.any?
wrap_name(names.first)
end
alias :get_name :name_show
def name_history name
history = @db[:names].where(:name => name.to_sequel_blob)
.where("value IS NOT NULL").order(:txout_id).map {|n| wrap_name(n) }
history.select! {|n| n.get_tx.blk_id } unless self.class.name =~ /Utxo/
history
end
def get_name_by_txout_id txout_id
wrap_name(@db[:names][:txout_id => txout_id])
end
def wrap_name(data)
return nil unless data
Bitcoin::Storage::Models::Name.new(self, data)
end
end
end
|