Try   HackMD

set up a private node on Ethereum教學文件


tags: Dapp 區塊鏈

指令

  • mkdir -p ChainSkills/private
  • cd ChainSkills/private
  • 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:初始帳號與金額。
      • 參數說明
  • 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作法

Go-ethererum 文件

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

Fatal: Failed to start mining: etherbase missing: etherbase must be explicitly specified


  • 私有鍊監控

set up a private node on Ethereum(windows)

Geth(go-ethereum)


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


  • the data of node被存在這個資料夾

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


Successfully Mining


參考資料

NODES AND CLIENTS


set up a private node on Ethereum(ubuntu vmware)



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)
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)

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 };


Block Body


  • Ethereum Nodes種類
    • full node
    • light node
    • archieve node

Soft fork/hard fork

硬分叉、軟分叉


密碼學


私有鏈搭建

私有鏈建立 用Geth架設私有鏈


Set up a private node on Ethereum(with docker)