<commit_before>require 'sequel'
require 'bitcoin/storage/sequel_store/sequel_migrations'

module Bitcoin::Storage::Backends

  class SequelStore < StoreBase

    attr_accessor :db

    include Bitcoin::Storage::Backends::SequelMigrations

    def initialize config
      @config = config
      connect
      super config
    end
    
    def connect
      @db = Sequel.connect(@config[:db])
      migrate
    end

    def reset
      [:blk, :blk_tx, :tx, :txin, :txout].each {|table| @db[table].delete}
    end

    def store_block(blk)
      @db.transaction do
        block = @db[:blk][:hash => htb(blk.hash).to_sequel_blob]
        return false  if block

        prev_block = get_block(hth(blk.prev_block.reverse))
        if !prev_block && blk.hash != Bitcoin::network[:genesis_hash]
          log.warn { "Invalid Block: #{blk.hash} - prev_block not found" }
          return false
        end
        if prev_block
          depth = prev_block.depth + 1
        else
          depth = 0
        end
        block_id = @db[:blk].insert({
            :hash => htb(blk.hash).to_sequel_blob,
            :depth => depth,
            :version => blk.ver,
            :prev_hash => blk.prev_block.reverse.to_sequel_blob,
            :mrkl_root => blk.mrkl_root.reverse.to_sequel_blob,
            :time => blk.time,
            :bits => blk.bits,
            :nonce => blk.nonce,
            :blk_size => blk.payload.bytesize,
          })
        blk.tx.each_with_index do |tx, idx|
          tx_id = store_tx(tx)
          @db[:blk_tx].insert({
              :blk_id => block_id,
              :tx_id => tx_id,
              :idx => idx,
            })
        end

        log.info { "new head #{blk.hash} - #{get_depth}" }
        depth
      end<commit_msg>fix transaction storage<commit_after>require 'sequel'
require 'bitcoin/storage/sequel_store/sequel_migrations'

module Bitcoin::Storage::Backends

  class SequelStore < StoreBase

    attr_accessor :db

    include Bitcoin::Storage::Backends::SequelMigrations

    def initialize config
      @config = config
      connect
      super config
    end

    def connect
      @db = Sequel.connect(@config[:db])
      migrate
    end

    def reset
      [:blk, :blk_tx, :tx, :txin, :txout].each {|table| @db[table].delete}
    end

    def store_block(blk)
      @db.transaction do
        block = @db[:blk][:hash => htb(blk.hash).to_sequel_blob]
        return false  if block

        prev_block = get_block(hth(blk.prev_block.reverse))
        if !prev_block && blk.hash != Bitcoin::network[:genesis_hash]
          log.warn { "Invalid Block: #{blk.hash} - prev_block not found" }
          return false
        end
        if prev_block
          depth = prev_block.depth + 1
        else
          depth = 0
        end
        block_id = @db[:blk].insert({
            :hash => htb(blk.hash).to_sequel_blob,
            :depth => depth,
            :version => blk.ver,
            :prev_hash => blk.prev_block.reverse.to_sequel_blob,
            :mrkl_root => blk.mrkl_root.reverse.to_sequel_blob,
            :time => blk.time,
            :bits => blk.bits,
            :nonce => blk.nonce,
            :blk_size => blk.payload.bytesize,
          })
        blk.tx.each_with_index do |tx, idx|
          tx_id = store_tx(tx)
          unless tx_id
            if tx = get_tx(tx.hash)
              @db[:blk_tx].insert({
                  :blk_id => block_id,
                  :tx_id => tx.id,
                  :idx => idx,
                })
            else
              return false
            end
          end
        end

        log.info { "new head #{blk.hash} - #{get_depth}" }
        depth
      end
{'fraction_non_alphanumeric': 0.11430898723866413, 'fraction_numerical': 0.0010860711376595167, 'mean_word_length': 1.7410714285714286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7633269', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1152, 'n_words': 241}