# 2022/09/21 -- 2022/09/28 NOTE
## 盧柏璋 Working NOTE
---
Original Ourchain Installation NOTE:
https://hackmd.io/Bo8C0LogQfmq_eSuOExr0w?view
---
Useful shell command learnt:
1. **top**: call out monitor for checking tasks working on the computer, press "q" to leave the monitor mode on terminal
2. **pgrep bitcoin**: check the label(5 or 4 digit integer Eg:29379) of the task bitcoin that you are running
3. **kill #task label integer#**: shut down the task you are running
4. **make -j 8**: means use multi-thread to work the make command, it would be 8 times faster than the normal make command
5. **gnome-screeshot**: take a screen-shot of the screen
6. **pkill -9 bitcoind**: forcely kill all working bitcoind tasks
7. **bitcoin-cli stop**: stop the bitcoin app
---
NOTE: when the following error code occur on the terminal, the possible reason and method to tackel it:
1. Error: Cannot obtain a lock on data directory /home/lab408/.bitcoin/regtest. Bitcoin Core is probably already running.
1.1 ANS: "pkill" all running tasks or kill the bitcoin that is running on the computer and rerun the command :
bitcoind --regtest --daemon
To initiate the new node
2.
---
## 0.測試實驗
Purpose of Experiment:
Understand the changes on ourchain when rpc commands input to the terminal, the EXP only focus on the changes to the folder:
**"/home/lab408/Desktop/po-bitcoin-testnode1/ourchain-release/src/relay/regtest"**
Notation: use **data_folder** for abbreviation of the above folder
**Experiment 1:**
Purpose: Understand where do the blockchain data stored
```
./bitcoind --regtest --datadir=relay --daemon
./bitcoind --regtest --daemon
```
See the Difference in **data_folder**
**Experiment 2:**
Purpose: when pkill command is used, will the blockchain data still remian?
```
```
See the Difference in data_folder
**Experiment 3:**
Purpose: after mining 101 blocks, what would be changed in **data_folder**
```
```
See the Difference in data_folder
**Experiment 4:**
Purpose: when the contract is deployed, will elimination of the original contract#my-contract.c# affect the data in **data_folder**
```
```
See the Difference in data_folder
## 1.安裝 Install
備註 PS:
1.The Lab 408 Computer use Ubuntu OS so use "sudo" in front of commands
2.沒有Linux環境請使用docker實驗 [參考這裡 here](https://hackmd.io/Bo8C0LogQfmq_eSuOExr0w?both#4%E7%94%A8Use-Docker%E5%BB%BA%E7%BD%AE-Build-OurChain)
**安裝相關依賴套件, Installation of Dependent Packages **
```
sudo apt-get update
sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils
sudo apt-get install software-properties-common
```
**若為 Ubuntu Ubuntu 18.04 或更新版本,請執行**
```
sudo add-apt-repository -y ppa:luke-jr/bitcoincore # for Ubuntu 18.04, 20.04
```
**接著繼續安裝必要套件**
```
sudo apt-get update
sudo apt-get install libdb4.8-dev libdb4.8++-dev
sudo apt-get install libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-program-options-dev libboost-test-dev libboost-thread-dev
sudo apt-get install git vim
```
##### note: 亦可參考 https://github.com/bitcoin/bitcoin/blob/master/doc/build-unix.md
**下載 Download OurChain code**
```
git clone -b test_version https://bitbucket.org/lab408/ourchain-release.git
```
**編譯 Compile OurChain**
```
cd ourchain-release/
./autogen.sh
./configure
make -j 8 # Or just use make but slower
sudo make install # 這行是 optional 取決於你是否要把程式加到path 一般來說make完後檔案會在src內
ldconfig # sudo if needed
```
**新增 /home/lab408/Desktop/po-bitcoin-testnode1/ourchain-release/src/.bitcoin**
```shell=
vim /home/lab408/Desktop/po-bitcoin-testnode1/ourchain-release/src/.bitcoin
server=1
rpcuser=test
rpcpassword=test
rpcport=8332
rpcallowip=0.0.0.0/0
datadir=relay # means save block data to the relay folder under src
```
恭喜你裝完了
---
## 2.實驗 Experience
**啟動比特幣回歸測試節點**
--regtest代表回歸測試 在你的電腦上創建一個測試鏈 而非連到當前的主要OurChain(or bitcoin)網路
--因為我把project建立在桌面上而非根目錄下,所以有些指令可能不同
```
Original version: bitcoind --regtest --daemon
My project version: ./bitcoind --regtest --datadir=relay --daemon
```
**使用bitcoin-cli與你剛剛架設起來的測試OurChain溝通實驗**
在實驗的時候記得加上--regtest
代表你用bitcoin-cli要互動的對象是你創的回歸測試鏈
```
bitcoin-cli --regtest getblockchaininfo //取得你剛剛架的回歸測試ourchain的整個區塊鏈資訊
```
**OurChain挖礦實驗**
在實驗的時候記得加上--regtest
代表你用bitcoin-cli要互動的對象是你創的回歸測試鏈
```
bitcoin-cli --regtest generate 101 //不同於bitcoin, ourcoin 挖礦使用的不是 generateblock, 而是 generate #number_of_blocks in integer
```
bitcoin-cli是bitcoin提供的一個 RPC interface
可以把他想成一個與bitcoin整個鏈互動的街口
透過bitcoin-cli提供的function來與整個bitcoin互動
有了bitcoin-cli你就可以開始你想要的實驗了
bitcoin-cli提供了很多跟ourchain互動的方法
像是剛剛的getblockchaininfo
包含其他像是創建地址、轉錢、挖礦等, 所有比特幣功能
備註:OurChain是基於bitcoin修改來的 所以適用以上功能
[請參考這裡了解所有bitcoin-cli提供的方法, all these supports](https://bitcoin.org/en/developer-reference#bitcoin-core-apis)
恭喜你 到這裡 你應該就有能力寫一個blockchain exploere 或是寫個簡單版本的wallet了
主要就是透過python(或你偏好的語言)找他的rpc-call套件
去訪問bitcoin網路取得你所需要的資料 並顯示在你的網頁上 或 APP上
---
## 3.怎麼寫 OurContract
大致上與C一樣,不過有幾個重點需要注意
標頭檔必須加入 Need to include the following header file
#include<ourcontract.h>
必須使用 need to use
int contract_main(int argc,char** argv)
取代原本的to replace int main(int argc,char** argv),而且不可以使用do not use main()
因為沒有no stdout,所以請勿使用please do not use printf()等等功能,請使用
err_printf
他會輸出在bitcoin執行的資料夾下的it will output to contracts/err
範例:Hello World
#include<ourcontract.h>
#include<stdio.h>
int contract_main(int argc,char** argv)
{
err_printf("Hello World %s\n",argv[0]);
return 0;
}
其餘功能[請參考 Refer to other functions](https://bitbucket.org/lab408/ourchain-release/src/test_version/src/contract/libourcontract.c)
如何在OurChain上執行合約 How to execute:
首先開啟ourchain後,確定自己有足夠多錢支付執行合約的手續費後,使用下面指令來發布合約並執行合約一次後
bitcoin-cli --regtest deploycontract [contract_path]
會獲得合約的名稱[contract_address]
{
"txid" : "[txid]"
"contract address" : "[contract_address]"
}
未來要再次執行合約,使用
bitcoin-cli --regtest callcontract [contract_address] ...
就可以再次執行合約,之後主動或等待他人挖礦後,合約就會自動被ourchain執行
其中上方指令後面所輸入的
[contract_address] ...
將會是int contract_main(int argc,char** argv)的
argv[0] ...
---
## 4.用Use Docker建置 Build OurChain
[教學 Instruction](https://hackmd.io/@jKIkT8QLQ4SzITq06RQTvQ/H1108hT9V?type=view)