Commit c2afb7a9f1b5793ca0d4a6cfc93eba7454fa8195

Authored by Marius Hanne
1 parent ed06fb34a6
Exists in namecoin

namecoin storage: don't index pk_scripts, more efficient storage logic

Showing 3 changed files with 21 additions and 16 deletions Side-by-side Diff

lib/bitcoin/storage/models.rb
... ... @@ -167,9 +167,7 @@
167 167 end
168 168  
169 169 def expires_in
170   - 36000 - (@store.get_depth - get_txout.get_tx.get_block.depth)
171   - rescue
172   - nil
  170 + 36000 - (@store.get_depth - get_block.depth) rescue nil
173 171 end
174 172  
175 173 def to_json(opts = {})
lib/bitcoin/storage/sequel.rb
... ... @@ -166,7 +166,7 @@
166 166 store_addr(txout_id, script.get_hash160)
167 167 store_name(script, txout_id) if Bitcoin.namecoin?
168 168 else
169   - log.warn { "Unknown script type: #{script.to_string}" }
  169 + log.warn { "Unknown script type" }
170 170 end
171 171 txout_id
172 172 end
173 173  
174 174  
... ... @@ -190,18 +190,22 @@
190 190  
191 191 elsif script.type == :name_firstupdate
192 192 name_hash = script.get_name_hash
193   - name_new = wrap_name(@db[:names].where(:hash => name_hash).order(:txout_id).first)
194   - unless name_new && name_new.get_block
  193 + name_new = @db[:names].where(:hash => name_hash).order(:txout_id).first
  194 + txout = @db[:txout][id: name_new[:txout_id]] if name_new
  195 + tx = @db[:tx][id: txout[:tx_id]] if txout
  196 + blk_tx = @db[:blk_tx][tx_id: tx[:id]] if tx
  197 + blk = @db[:blk][id: blk_tx[:blk_id]] if blk_tx
  198 + unless name_new && blk && blk[:chain] == 0
195 199 log.warn { "name_new not found: #{name_hash}" }
196 200 return nil
197 201 end
198   - unless name_new.get_block.depth <= get_depth - 12
  202 + unless blk[:depth] <= get_depth - 12
199 203 log.warn { "name_new not yet valid: #{name_hash}" }
200 204 return nil
201 205 end
202 206  
203 207 log.info { "#{script.type}: #{script.get_name}" }
204   - @db[:names].where(:txout_id => name_new.txout_id, :name => nil).update({
  208 + @db[:names].where(:txout_id => name_new[:txout_id], :name => nil).update({
205 209 :name => script.get_name.to_s.to_sequel_blob })
206 210 @db[:names].insert({
207 211 :txout_id => txout_id,
... ... @@ -344,7 +348,8 @@
344 348 end
345 349  
346 350 def name_history name
347   - @db[:names].where(:name => name).order(:txout_id).map {|n| wrap_name(n) }
  351 + @db[:names].where(:name => name).order(:txout_id)
  352 + .map {|n| wrap_name(n) }.select {|n| n.get_tx.blk_id }
348 353 end
349 354  
350 355 # wrap given +block+ into Models::Block
lib/bitcoin/storage/sequel_store/sequel_migrations.rb
... ... @@ -53,7 +53,7 @@
53 53 primary_key :id
54 54 column :tx_id, :int, :null => false, :index => true
55 55 column :tx_idx, :int, :null => false
56   - column :pk_script, :bytea, :null => false, :index => true
  56 + column :pk_script, :bytea, :null => false
57 57 column :value, :bigint
58 58 column :type, :int, :null => false, :index => true
59 59 end
... ... @@ -80,12 +80,14 @@
80 80 "ORDER BY tx.id DESC")
81 81 end
82 82  
83   - unless @db.tables.include?(:names)
84   - @db.create_table :names do
85   - column :txout_id, :int, :null => false, :index => true
86   - column :hash, :bytea, :index => true
87   - column :name, :bytea, :index => true
88   - column :value, :bytea
  83 + if Bitcoin.namecoin?
  84 + unless @db.tables.include?(:names)
  85 + @db.create_table :names do
  86 + column :txout_id, :int, :null => false, :index => true
  87 + column :hash, :bytea, :index => true
  88 + column :name, :bytea, :index => true
  89 + column :value, :bytea
  90 + end
89 91 end
90 92 end
91 93 end