lib/bitcoin/storage/sequel/sequel_store.rb in bitcoin-ruby-0.0.4 vs lib/bitcoin/storage/sequel/sequel_store.rb in bitcoin-ruby-0.0.5

- old
+ new

@@ -75,11 +75,11 @@ # store txouts txout_i = 0 txout_ids = @db[:txout].insert_multiple(new_tx.map.with_index {|tx, tx_idx| tx, _ = *tx tx.out.map.with_index {|txout, txout_idx| - script_type, a, n = *parse_script(txout, txout_i) + script_type, a, n = *parse_script(txout, txout_i, tx.hash) addrs += a; names += n; txout_i += 1 txout_data(new_tx_ids[tx_idx], txout, txout_idx, script_type) } }.flatten) # store addrs persist_addrs addrs.map {|i, h| [txout_ids[i], h]} @@ -105,11 +105,11 @@ @db[:blk].where(hash: new_main.map {|h| h.htb.blob }).update(chain: MAIN) end end # parse script and collect address/txout mappings to index - def parse_script txout, i + def parse_script txout, i, tx_hash = "" addrs, names = [], [] script = Bitcoin::Script.new(txout.pk_script) rescue nil if script if script.is_hash160? || script.is_pubkey? @@ -120,15 +120,16 @@ end elsif Bitcoin.namecoin? && script.is_namecoin? addrs << [i, script.get_hash160] names << [i, script] else - log.warn { "Unknown script type"}# #{tx.hash}:#{txout_idx}" } + log.info { "Unknown script type in #{tx_hash}:#{i}" } + log.debug { script.to_string } end script_type = SCRIPT_TYPES.index(script.type) else - log.error { "Error parsing script"}# #{tx.hash}:#{txout_idx}" } + log.error { "Error parsing script #{tx_hash}:#{i}" } script_type = SCRIPT_TYPES.index(:unknown) end [script_type, addrs, names] end @@ -168,11 +169,11 @@ @db.transaction do transaction = @db[:tx][:hash => tx.hash.htb.blob] return transaction[:id] if transaction tx_id = @db[:tx].insert(tx_data(tx)) tx.in.each_with_index {|i, idx| store_txin(tx_id, i, idx)} - tx.out.each_with_index {|o, idx| store_txout(tx_id, o, idx)} + tx.out.each_with_index {|o, idx| store_txout(tx_id, o, idx, tx.hash)} tx_id end end # prepare txin data for storage @@ -195,12 +196,12 @@ pk_script: txout.pk_script.blob, value: txout.value, type: script_type } end # store output +txout+ - def store_txout(tx_id, txout, idx) - script_type, addrs, names = *parse_script(txout, idx) + def store_txout(tx_id, txout, idx, tx_hash = "") + script_type, addrs, names = *parse_script(txout, idx, tx_hash) txout_id = @db[:txout].insert(txout_data(tx_id, txout, idx, script_type)) persist_addrs addrs.map {|i, h| [txout_id, h] } names.each {|i, script| store_name(script, txout_id) } txout_id end @@ -343,11 +344,11 @@ # wrap given +block+ into Models::Block def wrap_block(block) return nil unless block - data = {:id => block[:id], :depth => block[:depth], :chain => block[:chain], :work => block[:work].to_i, :hash => block[:hash].hth} + data = {:id => block[:id], :depth => block[:depth], :chain => block[:chain], :work => block[:work].to_i, :hash => block[:hash].hth, :size => block[:blk_size]} blk = Bitcoin::Storage::Models::Block.new(self, data) blk.ver = block[:version] blk.prev_block = block[:prev_hash].reverse blk.mrkl_root = block[:mrkl_root].reverse @@ -369,11 +370,11 @@ return nil unless transaction block_id ||= @db[:blk_tx].join(:blk, id: :blk_id) .where(tx_id: transaction[:id], chain: 0).first[:blk_id] rescue nil - data = {id: transaction[:id], blk_id: block_id} + data = {id: transaction[:id], blk_id: block_id, size: transaction[:tx_size], idx: transaction[:idx]} tx = Bitcoin::Storage::Models::Tx.new(self, data) inputs = db[:txin].filter(:tx_id => transaction[:id]).order(:tx_idx) inputs.each { |i| tx.add_in(wrap_txin(i)) } @@ -427,9 +428,25 @@ raise "Merkle root #{blk.depth} invalid!" unless blk.verify_mrkl_root print "#{blk.hash} #{blk.depth} OK\r" prev_blk = blk end log.info { "Last #{count} blocks are consistent." } + end + + # get total received of +address+ address + def get_received(address) + return 0 unless Bitcoin.valid_address?(address) + + txouts = get_txouts_for_address(address) + return 0 unless txouts.any? + + txouts.inject(0){ |m, out| m + out.value } + + # total = 0 + # txouts.each do |txout| + # tx = txout.get_tx + # total += txout.value + # end end end end