owned this note
owned this note
Published
Linked with GitHub
# set up a private node on Ethereum教學文件
---
[TOC]
###### tags: `Dapp` `區塊鏈`
----
## 指令
* ==mkdir -p ChainSkills/private==
* ==cd ChainSkills/private==
* ==puppeth==
* [Ethereum - 使用Puppeth创建以太坊私有链](https://luckylh.github.io/post/ethereum-puppeth-to-create-private-chain/)
* puppeth能幫助我們快速寫好私鏈的創世區塊配置(運行配置)的工具
* specify a network name--->==chainskills==
* what would you like to do?--->==2.configure new genesis==
* what would you like to do?--->==1.create new genesis from scratch==
* which conseneus engine to use?--->==1.Ethash -prof-of-work==
* which accounts should be pre-funded?(advisable at least one)--->==enter跳過==
* should the precompile-addresses be pre-funded with 1-wei?--->==yes==
* specify your chain/network id if you want an explicit one--->==4224==
* what would you like to do?--->==2.manage existing genesis 2.export genesis configuration==
* which folder to save the genesis specs into?==按enter==
* what would you like to do --->==按CTRL+C==
* ==ls==
* ==sudo snap install atom --classic==
* [sudo]password for shawn90830--->==輸入sudo admin密碼==
* ==atom chainskills.json==
* 建立創世區塊 (Genesis Block) 需要一些設定參數,我們將參數寫成 JSON 格式的檔案
* chainId:區塊鏈的識別 ID。
* difiiculty:難度參數。
* coinbase:預設將會是你第一個建立帳號的礦工。
* gasLimit:交易所使用到的 gas 限制,建議先預設(歸零),避免後續於測試交易發生資金不足。
* alloc:初始帳號與金額。
* [參數說明](https://geth.ethereum.org/docs/rpc/server)
* ==geth --datadir . init chainskills.json==
* geth是Go語言的產物,能讓電腦在terminal下運行以太坊節點
* 初始化區塊鏈,將以上的創世區塊鏈資訊寫入區塊中
* --datadir:區塊會儲存在 data 資料夾中。
* init:初始參數所使用的配置檔案 chinskills.json
* 初始化成功後,便會在 data 資料夾中產生 geth 和 Keystore 兩個資料夾,前者用來儲存區塊資料,後者則是儲存帳戶資料
* ==ls==
* ==ls keystore/==
* ==geth --datadir . account new==(產生secret key 的 public address 和secret key 的 path---->path不要隨意給別人)
* 設定鑰匙的密碼(要記得!!等等會用到)
* ==ls keystore/==(從keystore查看放置secret key的public address)
* ==geth --datadir . account list==
* ==atom startnode.sh==
* 啟動私有鏈
* 到startnode.sh檔案內打
startnode.cmd--->
==geth --networkid 4224 --mine --miner.threads=1 --datadir "." --nodiscover --rpc --rpcport "8547" --port "30302" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal --allow-insecure-unlock --unlock 0 --password ./password.sec --ipcdisable console
* --rpc-->啟動 rpc 通訊協定功能,如果要佈署「智慧合約」,請務必加入。
* --rpccorsdomain-->允許跨網域的存取調用,「"*"」 代表來自任何網段。
* --nodiscover-->不搜尋其他網段上的節點。關閉自動同步其他節點。
* --rpcapi-->在 rpc 通訊中,提供合約 API 的服務項目。可連接的客戶端應用
* console-->將會啟動命令模式,以便後續下達命令。
* 必須加上 --allow-insecure-unlock 才能夠解鎖帳戶,而解鎖帳戶後才能進行後面的操作
* minerthreads : 挖礦時,CPU占用的執行序數量,default為4。
* port : 以太坊網路的監聽接口,default為30303。
* rpcport : HTTP-RPC的監聽接口,default為8545。
* rpcaddr : HTTP-RPC的監聽地址,default為localhost。
* datadir : 指定存放區塊鏈數據的資料夾。
* keystore(存放帳戶資訊)和geth(存放區塊資訊)
* 開一個password.sec檔案---->在檔案內打此前設定的key密碼
* ==chmod +x startnode.sh==
* ==/startnode.sh==
----
* 開一個新console,讓另一個 console 的 private node 繼續跑
* 新console打==geth attach http://127.0.0.1:8547==
成功了!!!
* eth.accounts--->在private chain上所使用的ethereum帳號,也同時是public key(private key儲存在key store裡)
* 要設立passphrase
* Private key 會用你所輸入的 passphrase 加密存在這個 keystore 檔案裡,如果要備份帳號到其他機器使用,那就需要備份這些 keystore,及其所對應的密碼。
* eth.coinbase()
* 挖礦
* 進行挖礦時會將挖到的 Ether 給 eth.coinbase 這個帳號,eth.coinbase預設就是eth.accounts[0] ,我們來挖礦吧XD
* web3fromWei(eth.getBalance(eth.coinbase),"ether")
* eth.blockNumber--->我們同步的 private chain 有無任何區塊(block)
* eth.getBlock()--->得到block資料

* web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")--->查看accounts[1]帳戶內有多少ether
* web3.fromWei(eth.getBalance(eth.accounts[2]),"ether")--->查看accounts[2]帳戶內有多少ether
* eth.mining()--->檢查有無在挖礦
* miner.stop()--->停止挖礦
* miner.start()--->代表開始挖礦
* miner.start(1)--->代表開始挖礦
* 參數 1 就是代表要開幾個 process 來挖礦,我們設成 1 就好,因為我 們是自己在挖,會跟自己競爭
* 參數越高反而不一定能越快挖到區塊
* 第一次執行挖礦時,geth 會下載 ethash 檔案,檔案很大,因此需要花一些時間,我們要耐心等待(看到 mined potential block 的訊息時才代表真的開始挖礦了ㄋ),請喝杯咖啡再回來吧!
* net.version()--->檢查是否在正確鍊上 以我們例子 ex:4224
* net.peerCount()--->檢查我們node是否有和其他peers node連結
* personal.unlockAccount(eth.accounts[1])
* 請記得帳號的 passphrase,我們在做任何交易時,都會需要 passphrase 來解鎖帳號
* personal.unlockAccount(eth.accounts[2])
* eth.sendTransaction({from:eth.coinbase,to:eth.accounts[1],value:web3.toWei(10,"ether")})
* 從coinbase那挖到10 ether 給account[1]
* eth.sendTransaction({from:eth.coinbase,to:eth.accounts[2],value:web3.toWei(10,"ether")})
* 從coinbase那挖到10 ether 給account[2]
* web3.fromWei(eth.getBalance(eth.accounts[1]),"ether")
* 檢查account[1]有10 ether
* web3.fromWei(eth.getBalance(eth.accounts[2]),"ether")
* 檢查account[2]有10 ether
---
# connect multi-node private Ethereum blockchain指令
* 再開另一台 vmware 虛擬機 --->(切割空間)
* 步驟都同上set up an ethereum node
* 以下是去查看另一個node狀態
* ==admin.peers==--->因為未連上 回傳empty array
* 跑 node 1 ==admin.nodeInfo==
* 複製node 1 的 enode id
* 在node 2 的 geth console 跑 ==admin.addPeer(“//enode id”)==
* ==admin.peers==就連上peer node了
* Set up Boot node作法
* [Boot node作法文件](https://geth.ethereum.org/docs/getting-started/private-net)
### [Go-ethererum 文件](https://geth.ethereum.org/docs/getting-started/private-net)
### [How to Set Up a Private Ethereum Blockchain using Geth](https://arvanaghi.com/blog/how-to-set-up-a-private-ethereum-blockchain-using-geth/)
### [Geth error: fatal: could not list accounts: index 0 higher than number of accounts 0](https://ethereum.stackexchange.com/questions/38090/geth-error-fatal-could-not-list-accounts-index-0-higher-than-number-of-accoun)
### [Fatal: Failed to start mining: etherbase missing: etherbase must be explicitly specified](https://blog.csdn.net/wahaha13168/article/details/82355450)
---
* 私有鍊監控
---
# set up a private node on Ethereum(==windows==)
## Geth(go-ethereum)
* 安裝Geth

* 測試環境:Ganache
* 開發環境:node.js npm truffle solidity [編輯器atom](https://atom.io/)
* 
* 
* 通過puppeth可以在本地端中快速搭建以太坊私鏈
* [Ethereum - 使用Puppeth创建以太坊私有链](https://luckylh.github.io/post/ethereum-puppeth-to-create-private-chain/)
* [安裝Ethereum Go client端(geth)](https://geth.ethereum.org/downloads/)
----
### Chain ID network
* 種類:
* (1)Main net
* (2)Modern Test Net(obsolete)
* (3)Ropsten Test Net
* (4)Rinkebt Test Net
* (42)Kovan Test Net


* 3組 public address 和 private key



* issue: puppeth to generate a genesis block, and then use geth to read it in
* [Invalid genesis file with puppeth and geth](https://github.com/ethereum/go-ethereum/issues/16317)
----
* the data of node被存在這個資料夾


* geth--->chain資料
* keystore--->account資料

* --->account 0 會得到 mining award
* 常見問題--->
* [Incorrect Usage. flag provided but not defined: -minerthreads](https://stackoverflow.com/questions/68803582/incorrect-usage-flag-provided-but-not-defined-minerthreads)
* Fatal: Error starting protocol stack: Access is denied.

* [以太坊geth新版本error:account unlock with HTTP access is forbidden](https://blog.csdn.net/wo541075754/article/details/98847308)
* startnode.cmd--->geth --networkid 4224 --mine --miner.threads=1 --datadir "." --nodiscover --rpc --rpcport "8547" --port "30302" --rpccorsdomain "*" --nat "any" --rpcapi eth,web3,personal --allow-insecure-unlock --unlock 0 --password ./password.sec --ipcdisable console
---
### Successfully Mining

----
#### 參考資料
[NODES AND CLIENTS](https://ethereum.org/en/developers/docs/nodes-and-clients/run-a-node/)
---
# set up a private node on Ethereum(==ubuntu vmware==)
* [Fatal: Unable to attach to remote geth: dial unix /user/.ethereum/geth.ipc: connect: no such file or directory](https://ethereum.stackexchange.com/questions/11248/how-to-call-geth-attach-on-testnet-better-than-writing-full-path)
* 解決方法---> ==geth attach http://127.0.0.1:8547==
* [geth attach does not work (github solutions#1908)](https://github.com/ethereum/go-ethereum/issues/1908)
----
* [Geth 基礎用法及架設 Muti-Nodes 私有鏈](https://medium.com/fukuball-murmur/ethereum-%E9%96%8B%E7%99%BC%E7%AD%86%E8%A8%98-2-2-geth-%E5%9F%BA%E7%A4%8E%E7%94%A8%E6%B3%95%E5%8F%8A%E6%9E%B6%E8%A8%AD-muti-nodes-%E7%A7%81%E6%9C%89%E9%8F%88-b6c853258e4f)
* [How to set up a multi-node private Ethereum blockchain on your Mac](https://medium.com/@prashantramnyc/how-to-set-up-a-multi-node-private-ethereum-blockchain-from-scratch-in-20-mins-or-less-e0d7e091e062)
* 連上ethereum node方法--->(1)rpc (2)ipc
* geth 提供了 RPC(Remote Procedure Calls) 與 IPC(Inter-process Communications) 兩種方式來與 geth 互動,如果你要在 local 機器連上 geth,那就可以使用 IPC;如果要讓遠端連上 geth,那就使用 RPC,可以開 HTTP 或 Web Socket 兩種方式來讓遠端使用
----
## linux environment setup
打開linux cmd
* sudo apt install software-properties-common
* sudo add-apt-repository -y ppa:ethereum/ethereum
* sudo apt update
* sudo apt install ethereum
* geth version
* 上 https://truffleframework.com/ganache下載linux到本地端
* 在download處點開AppImage package
* 點AppImage package按右鍵,點settings,上列點permissions,勾選execute 將==allow executing file as program==框框勾起,才可開啟執行檔
* 由 node -v / npm -v 確定已下載 node.js / npm package
* sudo npm install -g truffle
* truffle version
* sudo add-apt-repository ppa:webupd8team/atom
* sudo apt update
* sudo apt install atom
* which atom
* which apm
* atom
* apm install language-ethereum
----
##
### 要確定的chainskills.json數值
* nonce(32bits)

```python=
for nonce in range(0, 2**32):
block_header = version + previous_block_hash + merkle_root + time + target_bits + nonce
if sha256(sha256(block_header)) < target_bits:
break
```

* 時間戳 timestamp
* extraData
* gasLimit
* difficulty
* mixHash
* coinbase
----
### Block header(包含 Metadata)
```c=
struct header_structure {
uint32_t nVersion; // 4 bytes - Version
uint8_t hashPrevBlock[32]; // 32 bytes - Previous block header hash
uint8_t hashMerkleRoot[32]; // 32 bytes - Merkle root hash
uint32_t nTime; // 4 bytes - Timestamp
uint32_t nBits; // 4 bytes - Difficulty Target
uint32_t nNonce; // 4 bytes Nonce
};
```


* [Merkle tree 演算法(ethereum 用 Merkle Patricia tree)](https://www.itread01.com/articles/1487247623.html)
----
### Block Body
* 紀錄交易紀錄(Transactions)--->[Merkle Tree](https://www.samsonhoi.com/274/blockchain_genesis_block_merkle_tree)

----
* Ethereum Nodes種類
* full node
* light node
* archieve node

----
### Soft fork/hard fork
[硬分叉、軟分叉](https://www.samsonhoi.com/846/blockchain_hardfork_softfork)
----
### [密碼學](https://www.coursera.org/learn/crypto/home/welcome)
---
# 私有鏈搭建
[私有鏈建立](https://dotblogs.com.tw/explooosion/2018/07/30/200754)
[用Geth架設私有鏈](https://medium.com/samumu-clan/%E7%94%A8-geth-%E6%9E%B6%E8%A8%AD%E7%A7%81%E6%9C%89%E9%8F%88-41a2baa0efd8)
* [Hyperledger fabric](https://ithelp.ithome.com.tw/users/20103635/ironman/1609?page=2)
* [Hyperledger Fabric: A Distributed Operating System for
Permissioned Blockchain論文](https://arxiv.org/pdf/1801.10228.pdf)

* [必讀乾貨|Hyperledger Fabric 獨特架構背後的設計哲學](https://www.blocktempo.com/hyberledger-fabric-design-philosophy/)
---
# Set up a private node on Ethereum(with docker)
* [Private Blockchain using docker](https://www.polarsparc.com/xhtml/PrivateEthDocker.html)
----
* [補充資料1 (run an ethereum node on private blockchain using truffle and deploy to private ethereum network)](https://medium.com/blockchainbistro/set-up-a-private-ethereum-blockchain-and-deploy-your-first-solidity-smart-contract-on-the-caa8334c343d)
* [如何使用Homestead文檔及以太坊路線圖--->hard fork 硬分叉](https://kknews.cc/zh-tw/tech/9ayjgj.html)