Trying to understand how bitcore-node stores the data in the DB

I’m looking into the data and source of bitcore-node ( http://bitcore.io/ ), I’m a bit confused how all the information is stored in the database, probably it’s in a similar way to others block explorers, so maybe someone could help me.

In this case for example https://blockchain.info/tx/acb2e895a9b3664cfcfb13dd0321ef0c1205cf2aa082681dc6ba424af6456a42

In the database I found two txo- txo-acb2e895a9b3664cfcfb13dd0321ef0c1205cf2aa082681dc6ba424af6456a42-0

{"prevTxId":"8697331c3124c8a4cf2f43afb5732374ea13769e42f10aa3a98148a08989af5e","outputIndex":1,"sequenceNumber":4294967295,"script":"4730440220106c316c22d4bf8c970f8e4b1af1edd95121397cfe29ab987cb000b478ec8e4302202b77986b89d77ab716da3cf53424efacf35e9097b1c89194df220e99503b8cae01","scriptString":"71 0x30440220106c316c22d4bf8c970f8e4b1af1edd95121397cfe29ab987cb000b478ec8e4302202b77986b89d77ab716da3cf53424efacf35e9097b1c89194df220e99503b8cae01","heightConfirmed":37514}

txo-acb2e895a9b3664cfcfb13dd0321ef0c1205cf2aa082681dc6ba424af6456a42-1

{"satoshis":10100000000,"script":"76a91427d25a1ff9a6da31eeb991c48bb6cd95191a6b2c88ac"}

Clearly different data, not sure why in one case we store the previous txid and in the other just the satoshis sent. It has to be obvious, but not sure what I’m missing here.

Another example https://blockchain.info/tx/00eff263871e6d44abed66d90abb10d5a211441668511dba3146e380360efe4b

txo-00eff263871e6d44abed66d90abb10d5a211441668511dba3146e380360efe4b-0 (from 0 to 5 all have similar data)

{"prevTxId":"6aebb4a764a027436f703ca9d42d0735423798f56973f9f236832a91fc2e9026","outputIndex":0,"sequenceNumber":4294967295,"script":"473044022057801e812f964bcb9e8ff17b37acef55afbb7119a66f24a148ba65e86a7aa1e3022064af657d280068b979ea17a29969976aedee8a664b3f1b575ebb88ef9275302601","scriptString":"71 0x3044022057801e812f964bcb9e8ff17b37acef55afbb7119a66f24a148ba65e86a7aa1e3022064af657d280068b979ea17a29969976aedee8a664b3f1b575ebb88ef9275302601","heightConfirmed":10712}

Also,… they are inputs, not outputs of the transaction.

I’m trying to read the code, but I can’t get to understand all the pieces.

As someone points me in reddit, it looks like it’s using txo– to store both, outputs and inputs. So I’m not sure how bitcore manages to get the inputs/outputs separately later (the API allows you to request only the inputs for example).

blockchan.info payment processing, how long?

I try to implement the payment processing API from blockchain.info
I created a request by the example of their website, and receive a generated bitcoin adress:

See an example here: https://blockchain.info/address/1Jv2S3iMo71c1WZEuE25Gemx7qjmqcVqNX
I send 0.001 BTC to that adress. It has already 8 confirmations atm. But when are they send to my adress? How much confirmations does blockchain.info needs?

Thank you

Bitcoin Testnet Trouble with bitcoin-ruby

I am having trouble with the bitcoin-ruby gem. I get the following error when trying to send money to another address I have on the test network:

/var/lib/gems/2.2.0/gems/bitcoin-ruby-0.0.7/lib/bitcoin/protocol/txout.rb:76:in
pk_script=': undefined methodbytesize’ for nil:NilClass
(NoMethodError)

I am generating a private key with:

def new_address
 Bitcoin::generate_address
end

And getting details for the private key with:

def key_details(prikey, pubkey)
  #returns prikey, prikey_hash58, pubkey_hash58, pubkey_uncompressed, address as a hash
  my_key = Bitcoin::Key.new(prikey, pubkey)
  { prikey:prikey, 
    prikey_base58:my_key.to_base58, 
    pubkey_58:my_key.hash160, 
    pubkey: my_key.pub_uncompressed, 
    address:my_key.addr
  }
end

The code I have to send money to myself is as follows:

require 'bitcoin'
require_relative 'utilities.rb'
require 'open-uri'

Bitcoin.network = :testnet3

def build_transaction(prev_tx, prev_out_index, key, value, addr, message)
  include Bitcoin::Builder

  new_tx = build_tx do |t|
    t.input do |i|
      i.prev_out prev_tx
      i.prev_out_index prev_out_index
      i.signature_key key
    end
    t.output do |o|
      o.value value 
      o.script {|s| s.type :address; s.recipient addr }
    end
  end
end

def prev_tx(prev_hash, network)
  if network == "testnet3"
    prev_tx = Bitcoin::P::Tx.from_json(open("http://test.webbtc.com/tx/#{prev_hash}.json"))
  else
    prev_tx = Bitcoin::P::Tx.from_json(open("http://webbtc.com/tx/#{prev_hash}.json"))
  end
end

def key(publ_key, priv_key)
  key = Bitcoin::Key.new(priv_key, publ_key)
end

def bin_to_hex(s)
  s.unpack('H*').first
end

#transaction inputs
priv_key = "private_key"
publ_key = "public_key_long_format"
address = "address"
previous_tx = "previous_tx_hash"

# generate tx info off inputs
key = Bitcoin::Key.new(priv_key, publ_key)
prev_tx = prev_tx(previous_tx, "testnet3")
prev_out_index = 1
tx_value = prev_tx.outputs[prev_out_index].value

# build new tx
tx = build_transaction(prev_tx, prev_out_index, key, tx_value, address, "hello")

#
# puts tx.to_json
puts bin_to_hex(tx.to_payload)

What if block N were based not on N-1 but on both N-2 and N-3?

If the bitcoin protocol were changed so that the merkle root from block N-1 were replaced in block N by the two roots of blocks N-2 and N-3, then it would become possible to mine 2 blocks at a time in parallel and increase the block frequency compared to the time each block took to propagate. Each block would still get confirmed by 6 of the following 7 blocks. The block size variance could be controlled by requiring that transactions with odd hashes go into odd-numbered blocks and vice-versa. What would be the limitations of this variant protocol compared to the existing Bitcoin protocol, assuming nobody considered one confirmation to be much better than zero?

How does Bitcoin Core sort memory pool transactions by fee?

I’m trying to understand the algorithm Bitcoin Core uses to sort memory pool transactions by fee.

Gavin’s fee rework proposal states:

Next, sort all remaining memory pool transactions by fee-paid-per-kilobyte, and include as many as will fit until the block is maximum block size bytes big, not including “free” transactions (transactions with fee-per-kb of less than the default spam threshold of 0.0001 BTC/kilobyte).

This makes sense, because a miner should want to load a block with transactions carrying the most “fee density”, or fee per byte.

However, reviewing the CreateNewBlock function in miner.cpp, it’s not clear how Bitcoin Core accounts for transaction length when prioritizing memory pool items:

// Prioritise by fee once past the priority size or we run out of high-priority
// transactions:
if (!fSortedByFee &&
    ((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
{
    fSortedByFee = true;
    comparer = TxPriorityCompare(fSortedByFee);
    std::make_heap(vecPriority.begin(), vecPriority.end(), comparer);
}

TXPriorityCompare seems to just look at absolute fee:

public:
    TxPriorityCompare(bool _byFee) : byFee(_byFee) { }

    bool operator()(const TxPriority& a, const TxPriority& b)
    {
        if (byFee)
        {
            if (a.get<1>() == b.get<1>())
                return a.get<0>() < b.get<0>();
            return a.get<1>() < b.get<1>();
        }
        else
        {
            if (a.get<0>() == b.get<0>())
                return a.get<1>() < b.get<1>();
            return a.get<0>() < b.get<0>();
        }
    }
};

In other words, this looks like a sort on absolute transaction fee, not fee density. What am I missing?

php private key to wallet import format (wip)

I have been writing a php file intended to create a bitcoin private key and change it to wallet import format. The issue is when I try a command in a gnome-terminal such as:
bitcoin-cli importprivkey ygm8fAjomMyKVcb8vK8MPBByYpMDySY1vAxcpw6DUfPBHb8kv “” false
I get an error:
error: {“code”:-5,”message”:”Invalid private key encoding”}

From this I’ve infered that my code is flawed in creating a private key and changing it into wallet import format. But from what I see in the source code it should do what it says to at https://en.bitcoin.it/wiki/Wallet_import_format under “Private key to WIF”

Here are the php files:

test1.php:

<?php
require('devRand.php');
#require('base58.php');

$a = new btcMakePrivKey();
#echo $GLOBALS['base58oper'];
function decodeHex($hex) {
    $hexchars = "0123456789ABCDEF";

    $hex = strtoupper($hex);
    $return = "0";
    for ($i = 0; $i < strlen($hex); $i++) {
        $current = (string) strpos($hexchars, $hex[$i]);
        $return = (string) bcmul($return, "16", 0);
        $return = (string) bcadd($return, $current, 0);
    }
    return $return;
}
function encodeBase58($hex) 
{
    $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    if (strlen($hex) % 2 != 0) {
        die("encodeBase58: uneven number of hex characters");
    }
    $orighex = $hex;

    $hex = decodeHex($hex);
    $return = "";
    while (bccomp($hex, 0) == 1) {
        $dv = (string) bcdiv($hex, "58", 0);
        $rem = (integer) bcmod($hex, "58");
        $hex = $dv;
        $return = $return . $base58chars[$rem];
    }
    $return = strrev($return);

    //leading zeros
    for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
        $return = "1" . $return;
    }
    return $return;
}
class btcMakePrivKey
{
    //$base58oper = new base58();
    public function __construct()
    {
        #$this->base58oper = new base58();
        $count = 0;  $privKey = "";
        while($count < 62)
        {
            $appendable = devurandom_rand(0,15);
            switch($appendable)
            {
                case 10:
                $appendable = 'A';
                break;
                case 11:
                $appendable = 'B';
                break;
                case 12:
                $appendable = 'C';
                break;
                case 13:
                $appendable = 'D';
                break;
                case 14:
                $appendable = 'E';
                break;
                case 15:
                $appendable = 'F';
                break;
                default:
                break;
            }
            $count = $count + 1;
            $privKey = $privKey . $appendable;
        }
        $privKey = $this->addLeadingByte($privKey);
        #echo $privKey . "\n\n";
        $pkHashed = $this->sha256Hash($this->sha256Hash($privKey) );
        $checksum = substr($pkHashed, 0, 8);
        $privKey = $privKey . $checksum;
        #$privKey = $this->base58oper->encode($privKey);
        $privKey = encodeBase58($privKey);
        echo $privKey;// . "\n\n" . $pkHashed . "\n\n" . $checksum;
    }
    private function addLeadingByte($privKey)
    {
        return '80' . $privKey;
    }
    private function sha256Hash($privKey)
    {
        return hash('sha256', $privKey, false);
    }
}

and devRand.php:

<?php
//equiv to rand, mt_rand
//returns int in *closed* interval [$min,$max]
function devurandom_rand($min = 0, $max = 0x7FFFFFFF) {
    $diff = $max - $min;
    if ($diff < 0 || $diff > 0x7FFFFFFF) {
    throw new RuntimeException("Bad range");
    }
    $bytes = mcrypt_create_iv(4, MCRYPT_DEV_URANDOM);
    if ($bytes === false || strlen($bytes) != 4) {
        throw new RuntimeException("Unable to get 4 bytes");
    }
    $ary = unpack("Nint", $bytes);
    $val = $ary['int'] & 0x7FFFFFFF;   // 32-bit safe
    $fp = (float) $val / 2147483647.0; // convert to [0,1]
    return round($fp * $diff) + $min;
}

I intend my code to create a bitcoin private key and convert it to a useable key in wallet import format, which can be used in bitcoin-cli importprivkey insertKeyHere “” false