與Geth互動的API Document

1. 安裝並啟動Geth

若已有Geth節點則可略過此步驟

(1) 下載

請打開Terminal並輸入

git clone https://github.com/HachimanHiki/NCCU-GraduateProject-Geth.git

(2) Compile

requires both a Go (version 1.10 or later) and a C compiler

輸入

cd NCCU-GraduateProject-Geth
make geth

或是透過此網站的教學

(3) 啟動

輸入

geth --dev --dev.period <second> --rpc --rpcapi "eth,web3,personal" --nousb --allow-insecure-unlock

其中的second代表PoA共識秒數

2. 安裝並啟動API Server

若已有API Server則可略過此步驟

(1) 安裝Node.js

若已安裝請跳過此步,若無安裝請參考此網站

(2) 下載API Server

請打開Terminal並輸入

git clone https://github.com/HachimanHiki/NCCU-GraduateProject-API.git

(3) Install module

輸入

cd NCCU-GraduateProject-API
npm install

(4) 啟動

輸入

npm run dev

3. 共識引擎與API互動

(1) 從Geth取得所有transaction的hash

Http method:Get
Url:http://<server ip>:3000/geth
Content Type:Json
Example:

{
    "transaction":[
        "0xac8a5a7899d4d995ef2054220cf1980af4d2afd48c3a7cb2ee845c9a60d3a3a3",
        "0xb5e2737e053512ec8c376482bd024e2c8b6860907664cc9796bf7150fdd08dc3"
    ]
}

(2) 將共識結果傳送至Geth

Http method:Post
Url:http://<server ip>:3000/consensus
Content Type:Json
Example:

{
    "blockHash": "0xac8a5a7899d4d995ef2054220cf1980af4d2afd48c3a7cb2ee845c9a60d3a3a3",
    "vote":[
        {
            sender: "",
            voteHash: "",
            blockHash: "",
            signature: ""
        }
    ],
    "transaction":[
        "0xac8a5a7899d4d995ef2054220cf1980af4d2afd48c3a7cb2ee845c9a60d3a3a3",
        "0xb5e2737e053512ec8c376482bd024e2c8b6860907664cc9796bf7150fdd08dc3"
    ]
}

在Geth端創造transaction

在NCCU-GraduateProject-Geth的資料夾下

./script/create.sh
./script/loop.sh

在Geth端查看transaction

連結Geth console

geth attach "ipc_url"

ex: geth attach /tmp/geth.ipc

查看區塊內容

eth.getBlock(<block number>)

ex: eth.getBlock(10)

Etheruem Private Chain Explore

(1) 下載

請打開Terminal並輸入

git clone https://github.com/Alethio/ethereum-lite-explorer

(2) 初始設定

在此提供簡易版本,若有問題請參照github的文檔

cp config.default.json config.dev.json
vim config.dev.json

Make necessary modifications into config.dev.json if needed. You must also remove the version query strings ?v=#.#.# from the "plugins" URIs.

(3) 設定private chain位置

vim config.dev.json

After setting up and starting Ganache(Geth), update the config.dev.json file and set the nodeUrl attribute for the eth-lite plugin to http://<serverip>:<port>.

請注意此位置的Geth(http://<serverip>:<port>)需要用以下指令啟動

geth --dev --dev.period <second> --rpc --rpccorsdomain "*" --rpcaddr "0.0.0.0" --rpcapi "eth,web3,personal" --nousb --allow-insecure-unlock

(4) 安裝並啟動

npm run build && npm start

(5) 開啟

http://127.0.0.1:3000

Select a repo