LoginSignup
2
1

More than 5 years have passed since last update.

コインベーストランザクションまでトランザクションを遡ってみる

Last updated at Posted at 2017-10-07

前回やったこと

前回の記事ではあるひとつのビットコイントランザクションのインプットに着目してトランザクションを詳しく見てみました。

インプットをコインベーストランザクションまで遡って見てみる

せっかくトランザクションのインプットとなるトランザクションを取得する方法が分かったので、今回はトランザクションを終わりまでどんどん遡ってみましょう。

ちなみに終わりとなるトランザクションはコインベーストランザクションと呼ばれるもので、これはまさにビットコインのマインニングによってビットコインが生まれたときのトランザクションです。

以下のように再帰的にトランザクションを遡るコードを書きました。ちなみに前回もお話した通りインプットはひとつのトランザクションに対してひとつ以上ある場合があります。今回は必ず一番先頭のインプットを遡る対象としています。ちなみに実行に数分かかります

using NBitcoin;
using QBitNinja.Client;
using QBitNinja.Client.Models;
using System;
using System.Collections.Generic;

namespace NBitcoinTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<uint256, Transaction> GetTx = (txId) =>
            {
                QBitNinjaClient client = new QBitNinjaClient(Network.Main);
                var transactionResponse = client.GetTransaction(txId).Result;
                return transactionResponse.Transaction;
            };

            UInt64 txTreeDepth = 0;
            Action<Transaction> Foo = null; // For recursion
            Foo = (tx) =>
            {
                txTreeDepth++;
                if (tx.IsCoinBase)
                {
                    Console.WriteLine(tx.GetHash());
                    Console.WriteLine(txTreeDepth);
                    return;
                }

                var txId = tx.Inputs[0].PrevOut.Hash;
                var prevTransaction = GetTx(txId);
                Foo(prevTransaction);
            };

            var transaction = GetTx(uint256.Parse("7008807adb713205d01d378f3d25f1bc5e06bda7d30b526c2b9d2c018cfa08b4"));
            Foo(transaction);
        }
    }
}

出力は以下のようになります。

58fdd93663c8756855b0bca9401da7e84c9dc748b24d2b66f10571dd5c357778
881

なんと881回もトランザクションの連なりを遡りました。このトランザクションをblockchain.info上にて見てみると、例えば採掘量が25BTCだった時代の採掘だったことが分かります。

このようにマイナーさんによって採掘されたビットコインが、800回以上のトランザクションを経て堀江さんに辿り着いたことを想像すると、どこかロマンチックな気分になるのは私だけでしょうか。

参照

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1