BlockChainを作ってみる


# Blockchainを作る
[yutopio/Minchain](https://github.com/yutopio/MinChain)を用いて自分のblockchainサーバーを作ってみる
## git cloneする
```
$ git clone git@github.com:yutopio/MinChain.git

$ cd MinChain 

# requirement をインストールする
$ dotnet restore                                                
master@MinChain
  Restoring packages for /Users/ryof-mbp/dev/blockchain/MinChain/MinChain/MinChain.csproj...
  Generating MSBuild file /Users/ryof-mbp/dev/blockchain/MinChain/MinChain/obj/MinChain.csproj.nuget.g.props.
  Restore completed in 217.84 ms for ~/MinChain/MinChain/MinChain.csproj.

# サーバーをビルドする
$ dotnet build                                                                                                       
master@MinChain
Microsoft (R) Build Engine version 15.5.180.51428 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 25.57 ms for ~/MinChain/MinChain/MinChain.csproj.
  MinChain -> ~/MinChain/MinChain/bin/Debug/netcoreapp2.0/MinChain.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.26

# ビルドが完了すると以下のようなファイル編成に
$ ls
master@MinChain
LICENSE      MinChain     MinChain.sln README.md

# ディレクトリに移動する
$ cd MinChain/bin/Debug/netcoreapp2.0                                                                                master@MinChain

$ ls
master@MinChain
MinChain.deps.json              MinChain.pdb                    MinChain.runtimeconfig.json
MinChain.dll                    MinChain.runtimeconfig.dev.json

# キーを生成して、json形式で保存する
[~/MinChain/MinChain/bin/Debug/netcoreapp2.0]
$ dotnet MinChain.dll genkey > key.json
master@MinChain

$ cat key.json
master@MinChain
{
  "pub": "ksQgFFpmM4AZ3fi823FEPSB/vwrWWPEuBdAqBGUjK2Z4xknEIFX1M/a58rpaXgQ2GtBa3IEsatZAU1pSO9S8DrqU8pfX",
  "prv": "4fvmYGDETdHPMsTZQM3jWbq1DExjqRGbZh5eSwpRync=",
  "addr": "n+4RHW3wgCjUS5+Tf0H46EaaoTShy3kB4EsF7rZLKAg="
}

# genesis blockを生成する
$ dotnet MinChain.dll genesis key.json genesis.bin 
master@MinChain
Creating new genesis block.
{
  "id": "000da341f453355d7edaa43b37df791cefaa2737f0360a0ed256b2124496bf05",
  "prev": "0000000000000000000000000000000000000000000000000000000000000000",
  "difficulty": 2E-06,
  "nonce": 17903325795121511535,
  "timestamp": "2018-03-15T12:22:26.228371Z",
  "root": "sxG4g/Ala9QgPG8myGWjI7WZRapOyPSgfFqhtvTj3xk=",
  "height": 0,
  "txs": [
    {
      "id": "7791418e89b44db7f710a8bf3137699fb6793ca069d6a8ed2cd0d64abde7e111",
      "timestamp": "2018-03-15T12:22:26.138471Z",
      "in": [],
      "out": [
        {
          "to": "846ec098d15863af2963aaf7acc685cdfdbec663584e90a9b10a6dfc88c13761",
          "val": 1000000
        }
      ]
    }
  ]
}
#生成された様子
$ ls
master@MinChain
MinChain.deps.json              MinChain.pdb                    MinChain.runtimeconfig.json     key.json
MinChain.dll                    MinChain.runtimeconfig.dev.json genesis.bin

#config sampleを作る
$ dotnet MinChain.dll config > config.json                                                                           master@MinChain

$ ls
master@MinChain
MinChain.deps.json              MinChain.pdb                    MinChain.runtimeconfig.json     genesis.bin
MinChain.dll                    MinChain.runtimeconfig.dev.json config.json                     key.json

$ vi config.json
master@MinChain

KeyPairPath = "<YOUR OWN KEYPAIR>.json",
                    GenesisPath = "<GENESIS BLOCK>.bin",
-- 中に genesis.bin, key.json　を設定する

# 動かす
$ dotnet MinChain.dll run config.json
master@MinChain
dbug: MinChain.Executor[0]
      Attempt to run Block:000da34
dbug: MinChain.Executor[0]
      Attempt to run TX:7791418
warn: MinChain.Executor[0]
      Applying Block 0:000da34.
info: MinChain.ConnectionManager[0]
      Start listening on 0.0.0.0:9333
dbug: MinChain.Executor[0]
      Attempt to run TX:42e3495
info: MinChain.ConnectionManager[0]
      Peer #1 connected to 127.0.0.1:52383.
info: MinChain.ConnectionManager[0]
      Peer #0 connected to 127.0.0.1:9333.
info: MinChain.Mining[0]
      Block mined: {
        "id": "0006e08e8fb519afe1c7025389e2f4104e6329f226bc98e7fe07fe97413c7efb",
        "prev": "000da341f453355d7edaa43b37df791cefaa2737f0360a0ed256b2124496bf05",
        "difficulty": 1.8E-06,
        "nonce": 15449940397331524244,
        "timestamp": "2018-03-15T12:33:03.032075Z",
        "root": "/pz2sm6wuNRpTSYEuB8QTLt4s1+DTS/64wd/Z0YTK5I=",
        "height": 0,
        "txs": [
          {
            "id": "42e34959579a15aa60e2f77c8012dcad6794e4f1dc7b015eb8cc6004e183e508",
            "timestamp": "2018-03-15T12:33:02.975339Z",
            "in": [],
            "out": [
              {
                "to": "846ec098d15863af2963aaf7acc685cdfdbec663584e90a9b10a6dfc88c13761",
                "val": 1000000
              }
            ]
          }
        ]
      }
```