操作系統centos7.6
概述
Geth(go-ethereum)是由以太坊基金會提供的官方以太坊協議實現,用Go編程語言編寫的。Geth提供了一個交互式命令控制檯,通過命令控制檯中包含了以太坊的各種功能(API)。
1. 本地編譯geth
cd ~/go/src/github.com/
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum/
git branch -a
git checkout
git checkout release/1.9
make all
cp ./build/bin/geth /usr/bin
geth
最常用的CLI客戶端abigen
源代碼生成器,用於將以太坊智能合約定義轉換爲易於使用的,編譯時類型安全的Go軟件包bootnode
以太坊客戶端實現的精簡版本,僅參與網絡節點發現協議,但不運行任何更高級別的應用程序協議evm
能夠在可配置的環境和執行模式下運行字節碼- rlpdump 用於將二進制RLP轉儲(以太坊協議使用的數據編碼,無論是網絡還是共識方式)轉換爲用戶友好的層次表示形式(例如rlpdump --hex CE0183FFFFFFC4C304050583616263)。
2. 利用docker啓動節點
編寫節點啓動腳本
mkdir -p /root/ethereum_node
cd /root/ethereum_node
vi start-node.sh
start-node.sh
內容如下:
#!/bin/bash
docker rm -f eth_node || true
DATADIR=/root/ethereum_node/chain
mkdir -p $DATADIR
docker run -it -d --name eth_node -v $DATADIR:/root/.ethereum -p 8545:8545 -p 30303:30303 ethereum/client-go --ws --rpc --rpcaddr 0.0.0.0 --rpccorsdomain '*' --rpcapi "db,eth,net,web3,personal" --allow-insecure-unlock --wsapi "personal,web3" --rinkeby console
執行腳本
chmod 777 start-node.sh
./start-node.sh
漫長的等待,待區塊數據全部同步。
3. 登錄到控制檯
登錄 javascript控制檯,在控制檯裏可執行web3.js的api。
web3.js是一個javascript庫,你可以使用HTTP或IPC連接本地或遠程以太它節點進行交互。
web3的JavaScript庫能夠與以太坊區塊鏈交互。 它可以檢索用戶帳戶,發送交易,與智能合約交互等
api參考:https://web3js.readthedocs.io/en/v1.2.4/
geth attach rpc:http://xx.xx.xx.xx:8545
or
geth attach ipc://root/ethereum_node/chain/rinkeby/geth.ipc
查看eth所有的方法
eth
4. 創建賬戶
personal.listAccounts
personal.newAccount('pld123')
0xe7c5b662c719fe2a99fe20327fb2bf1aa8c0fdb2
personal.newAccount('pld123')
0x4fced1c852abcab76a9d2761c1db8d59e53a310c
去水龍頭獲取測試幣:https://faucet.rinkeby.io/
需要 tweet或facebook賬號,發送指定內容的消息,最多可獲最多18.75個eth。
5. 構造一筆交易
方式一:使用sendTransaction
方法
personal.unlockAccount(web3.eth.accounts[0], 'pld123', 300)
src = web3.eth.accounts[0];
dst = web3.eth.accounts[1];
#獲取餘額
web3.fromWei(eth.getBalance(eth.accounts[0]), 'ether')
web3.fromWei(eth.getBalance(eth.accounts[1]), 'ether')
# 轉賬
web3.eth.sendTransaction({from: src, to: dst, value: web3.toWei(0.01, "ether"), data: ""});
方式一:使用signTransaction
和sendRawTransaction
方法
personal.unlockAccount(web3.eth.accounts[0], 'pld123', 300)
web3.eth.signTransaction({
from: eth.accounts[0],
gasPrice: "20000000000",
gas: "21000",
nonce: web3.eth.getTransactionCount(eth.accounts[0])+1,
to: eth.accounts[1],
value: web3.toWei(0.01, "ether"),
data: ""
})
eth.sendRawTransaction()