# Blockchain course HW2
## Steps
Bitcoin Core is an open-source software that could generate bitcoin node and provides a bitcoin wallet which fully verifies payments. For developer, it provide serial command to start genesis block, transactions, mining, etc.
Before we getting start, we first talk about `bitcoin.conf` file which is a configuration file. Next we will give you some hand-on experience to build you own wallet, mining and have BTC transaction.
[freewil/bitcoin-testnet-box](https://github.com/freewil/bitcoin-testnet-box#using-with-docker)
## To Build Environment
We assume that you have already install these.
```shell=
# Build under ubuntu 16.04
sudo apt -y install git build-essential libtool autotools-dev automake pkg-config bsdmainutils python3
sudo apt -y install libevent-dev libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev
sudo apt -y install qt5-default qttools5-dev-tools libdb++-dev
# Install berkeley DB
# https://github.com/bitcoin/bitcoin/issues/2998
wget https://download.oracle.com/berkeley-db/db-4.8.30.tar.gz
tar zxvf db-4.8.30.tar.gz
cd db-4.8.30/build_unix
../dist/configure --prefix=/usr/local --enable-cxx
make -j4
sudo make install
# Install bitcoin core (iunclude bitcoind, bitcoin-cli, bitcoin-qt)
git clone https://github.com/bitcoin/bitcoin.git
cd bitcoin/
./autogen.sh
./configure --with-gui
make -j4
sudo make install
```
use docker
```shell=
docker run -d -it -p 19000:19000/tcp --name btctestnet tony92151/btc_testnet
docker exec -it btctestnet /bin/bash
```
## `bitcoin.conf` file
`bitcoin.conf` file is quite important to give some parameter when we start a node or use other tools.
`bitcoin.conf` example [here](https://github.com/bitcoin/bitcoin/blob/master/share/examples/bitcoin.conf).
```
.
├── btctest
│ └── bitcoin.conf
├── node1
│ └── bitcoin.conf
└── node2
└── bitcoin.conf
```
## Start genesis block
```shell=
mkdir btctest
wget https://github.com/freewil/bitcoin-testnet-box/raw/master/1/bitcoin.conf -P btctest
# start node wothout screen
bitcoind -datadir=btctest -daemon
# stop node
bitcoin-cli -datadir=btctest stop
# get node information
bitcoin-cli -datadir=btctest -getinfo
```
## Start client
```shell=
mkdir btcnode && cd btcnode
bitcoin-qt -regtest=1 -connect=localhost:19000 -datadir=btcnode
```

Go to `windows`>`information` . It will show you number of blocks.

## Mining
First we need an account to mining.
Click `Receive` and enter label then click `Create new ...` then copy the address.

copy the address

```shell=
bitcoin-cli -datadir={path to btctest} generatetoaddress 10 bcrt1qflkldhvhgq900dvfpyzarvq5j259znucakrggq
```

Now, we have some Immature coin.

It need mining to confirm. But `I'am the only miner`
Try to mine more.
In `Transaction`, we can see the processing bar the left, please mine more to confirm that.

## Transaction
Find another pc and start the client in the same way
```shell=
mkdir btcnode
bitcoin-qt -regtest=1 -connect=localhost:19000 -datadir=btcnode
```

Then click `Receive` > `Create new ...` and copy the address.
Next paste the address back to the previous wallet's `Send` tab.

Final, mine some coin to comfirm that transaction.

## Reference
[1][freewil/bitcoin-testnet-box](https://github.com/freewil/bitcoin-testnet-box#using-with-docker)
[2][Day27|現實中的區塊鏈(4):用Command Line操作Bitcoin](https://ithelp.ithome.com.tw/articles/10216368)
## Youtube tutorial
This is for old version, some commend didn't work.
**reating a Local Bitcoin Testnet / Regtest - Programming Bitcoin**
{%youtube LLZNvl90PC0 %}