介紹區塊鏈,搭建私鏈,智能合約以及開發DAPP。
概念
什么是區塊鏈
廣義:
區塊鏈是分布式數據存儲,點對點傳輸,共識機制,加密算法等計算機技術的新型應用模式。
狹義:
區塊鏈是數據庫的一種,它擁有大量的記錄,並將這些記錄全部存放在區塊內,每個區塊通過加密簽名,鏈接到下一個區塊。人們可以像使用賬本一樣使用區塊鏈,可以共享,也可以被擁有適當權限的人查閱,通俗的說,區塊鏈其實就是公開的分布式賬簿系統。
挖礦
區塊鏈需要存儲記錄,那么為什么要幫別人免費存儲?——獎勵
即參與記錄的機器,當對信息進行加密時,滿足一定的條件時,就會得到一定的獎勵。這個加密計算的過程,叫做挖礦。而運行計算的程序,稱為礦機。
智能合約
一個智能合約是一套以數字形式定義的承諾(promises),包括合約參與方可以在上面執行這些承諾的協議。一個合約由一組代碼(合約的函數)和數據(合約的狀態)組成,通常運行在以太坊虛擬機上。當滿足一定的條件時,自動履行並通知用戶。
以太坊
目前最大的區塊鏈開發者平台
視頻學習:尚硅谷_區塊鏈以太坊核心技術
geth搭建以太坊私鏈
以太坊私有鏈搭建
一台電腦上部署多個節點。
安裝go-ethereum
# 下載代碼
git clone https://github.com/ethereum/go-ethereum.git
# 進入目錄
cd go-ethereum
# 切換分支
git checkout v1.9.9
# 編譯打包
make all
# 移動到可執行目錄
cp build/bin/geth /usr/local/bin
cp build/bin/puppeth /usr/local/bin
創建節點目錄
由於在一台電腦上部署多個節點,所以需要創建多個目錄來保存各自節點的賬號、區塊鏈數據。
mkdir eth
cd eth
mkdir node1
mkdir node2
創建賬號
為每個節點各自創建一個賬號。執行 account new 命令后會提示你輸入密碼,也可以不輸入直接回車跳過即可,生成賬號后會給出對應的地址,記錄下新生成的賬號地址,方便后續配置創世塊使用。如果沒記住也可以通過 eth.accounts 命令查詢。
geth --datadir ./node1/data account new
# 0x2A7d60135d51c799321B191427FB72108e28b75C
geth --datadir ./node2/data account new
# 0xFE677A1EB388E4d04cFb6767547599460442aDf7
配置創世塊
創世塊是區塊鏈中的第一個區塊,可以通過 puppeth 來生成創世區塊,puppeth 是 geth 中自帶的工具,不用單獨安裝,運行 puppeth 命令后會提示你輸入私鏈的名稱。
初始化節點
geth --datadir node1/data init private.json
geth --datadir node2/data init private.json
啟動節點
# 啟動第一個
geth --datadir node1/data \
--networkid 15 \
--rpc --rpcaddr 0.0.0.0 --rpcport 8545 \
--port 30303 \
--nodiscover \
--allow-insecure-unlock \
--rpccorsdomain https://remix.ethereum.org \
console
# 啟動第二個
geth --datadir node2/data \
--networkid 15 \
--rpc --rpcaddr 0.0.0.0 --rpcport 18545 \
--port 60606 \
--nodiscover \
--allow-insecure-unlock \
console
參數含義:
--identity
:指定節點 ID--rpc
:表示開啟 HTTP-RPC 服務--rpcaddr
:HTTP-RPC 服務ip地址--rpcport
:指定 HTTP-RPC 服務監聽端口號(默認為 8545)--datadir
:指定區塊鏈數據的存儲位置--port
:指定和其他節點連接所用的端口號(默認為 30303)--nodiscover
:關閉節點發現機制,防止加入有同樣初始配置的陌生節點--allow-insecure-unlock
:允許 HTTP 解鎖賬號
建立節點之間的連接
在各個節點的 console 輸入 admin.nodeInfo.enode ,可以分別得到節點信息。
node1:
> admin.nodeInfo.enode
"enode://9103e0b2de12611b2a51b41aa5ed526fa65b50b9d88815171017d547c2835f023c87439d7352fa2bcfa52957925441b5ac638f3ffcd5573bdbb5b811e52d50f2@127.0.0.1:30303?discport=0"
>
node2:
> admin.nodeInfo.enode
"enode://2f9304cd83d3a4e91b2e266efceecdf2a680e9fc89eac3cd6fd90e9ca72cd6e7bf807e03e45487859602ffb522f19c3163226ea5a5a1866b822fdbef00aef85b@127.0.0.1:60606?discport=0"
>
在 node2 的 console 中輸入對 node1 的連接:
> admin.addPeer("enode://9103e0b2de12611b2a51b41aa5ed526fa65b50b9d88815171017d547c2835f023c87439d7352fa2bcfa52957925441b5ac638f3ffcd5573bdbb5b811e52d50f2@127.0.0.1:30303?discport=0")
true
>
在 node1 的 console 中驗證 node2 是否成功建立連接:
> admin.peers
[{
caps: ["eth/63", "eth/64"],
enode: "enode://2f9304cd83d3a4e91b2e266efceecdf2a680e9fc89eac3cd6fd90e9ca72cd6e7bf807e03e45487859602ffb522f19c3163226ea5a5a1866b822fdbef00aef85b@127.0.0.1:54276",
id: "9f471227e38b9b194c26cad6ce7874d2d504de0c8938099c6315a4fd293840c2",
name: "Geth/v1.9.9-stable/linux-amd64/go1.13.4",
network: {
inbound: true,
localAddress: "127.0.0.1:30303",
remoteAddress: "127.0.0.1:54276",
static: false,
trusted: false
},
protocols: {
eth: {
difficulty: 3,
head: "0x26896f9739ae0fff8d63c7793fe524052c37471830004e1f42f63d6ae4c937f3",
version: 64
}
}
}]
>
常用命令
- personal.newAccount("password"):創建賬戶;
- personal.listAccounts:查看賬戶,同eth.accounts
- personal.unlockAccount("0xf3b6bbd4c05eeeb78cf574c5c826e8727cd95da1"):解鎖賬戶;
- eth.accounts:枚舉系統中的賬戶;
- eth.getBalance():查看賬戶余額,返回值的單位是 Wei(Wei 是以太坊中最小貨幣面額單位,類似比特幣中的聰,1 ether = 10^18 Wei);
- eth.blockNumber:列出區塊總數;
- eth.getTransaction():獲取交易;
- eth.getBlock():獲取區塊;
- miner.start():開始挖礦;
- miner.stop():停止挖礦;
- eth.coinbase:挖礦獎勵的賬戶
- web3.fromWei():Wei 換算成以太幣;
- web3.toWei():以太幣換算成 Wei;
- txpool.status:交易池中的狀態;
- admin.addPeer():連接到其他節點;
- admin.nodeInfo:查看節點摘要信息
開啟挖礦
在 node2 開啟挖礦:
> miner.start()
INFO [01-15|17:35:09.807] Transaction pool price threshold updated price=1000000000
null
> INFO [01-15|17:35:09.807] Commit new mining work number=1 sealhash=5a0d4f…a8b50e uncles=0 txs=0 gas=0 fees=0 elapsed=153.486µs
WARN [01-15|17:35:09.807] Block sealing failed err="authentication needed: password or unlock"
解鎖挖礦用戶,再挖礦:
> personal.unlockAccount("0xFE677A1EB388E4d04cFb6767547599460442aDf7")
Unlock account 0xFE677A1EB388E4d04cFb6767547599460442aDf7
Password:
true
> miner.start()
INFO [01-15|17:46:06.428] Transaction pool price threshold updated price=1000000000
null
> INFO [01-15|17:46:06.428] Commit new mining work number=1 sealhash=586bf3…585097 uncles=0 txs=0 gas=0 fees=0 elapsed=177.262µs
INFO [01-15|17:46:06.429] Successfully sealed new block number=1 sealhash=586bf3…585097 hash=cafedf…b7e662 elapsed=860.675µs
INFO [01-15|17:46:06.429] :hammer: mined potential block number=1 hash=cafedf…b7e662
INFO [01-15|17:46:06.430] Commit new mining work number=2 sealhash=eb8562…c8587c uncles=0 txs=0 gas=0 fees=0 elapsed=394.973µs
INFO [01-15|17:46:06.430] Signed recently, must wait for others
同理,在 node1 上也開啟挖礦(因為只有2個節點,必須同時挖才行):
> personal.unlockAccount("0x2A7d60135d51c799321B191427FB72108e28b75C")
Unlock account 0x2A7d60135d51c799321B191427FB72108e28b75C
Password:
true
> miner.start()
INFO [01-15|17:46:06.428] Transaction pool price threshold updated price=1000000000
null
> INFO [01-15|17:46:06.428] Commit new mining work number=1 sealhash=586bf3…585097 uncles=0 txs=0 gas=0 fees=0 elapsed=177.262µs
INFO [01-15|17:46:06.429] Successfully sealed new block number=1 sealhash=586bf3…585097 hash=cafedf…b7e662 elapsed=860.675µs
INFO [01-15|17:46:06.429] :hammer: mined potential block number=1 hash=cafedf…b7e662
INFO [01-15|17:46:06.430] Commit new mining work number=2 sealhash=eb8562…c8587c uncles=0 txs=0 gas=0 fees=0 elapsed=394.973µs
INFO [01-15|17:46:06.430] Signed recently, must wait for others
DAPP
基於區塊鏈的APP
remix
啟用插件
- Solidity compiler
- Deploy & run transactions
投票合約
投票合約
pragma solidity >=0.5.0 <6.0.0;
contract Vote {
uint[] _candidates;
mapping(uint => uint) _voters;
constructor(uint[] memory candidates) public {
_candidates = candidates;
}
function doVote(uint candidate) public {
require(validCandidate(candidate));
_voters[candidate] += 1;
}
function display(uint candidate) public view returns (uint) {
assert(validCandidate(candidate));
return _voters[candidate];
}
function validCandidate(uint candidate) private view returns (bool) {
for(uint i; i < _candidates.length; i++) {
if (_candidates[i] == candidate) {
return true;
}
}
return false;
}
}
部署:
投票:
證件存儲合約
pragma solidity >=0.5.0 <6.0.0;
contract IDCard {
string _info;
constructor(string memory info) public {
_info = info;
}
function display() public view returns (string memory) {
return _info;
}
}
合約部署:
truffle
Truffle使開發者從智能合約和 DApp 模板開始,構建越來越復雜的應用程序。
安裝命令行工具:
npm install -g truffle
創建項目
# 創建目錄
mkdir egova-dapp
cd egova-dapp
# 初始化環境
truffle unbox metacoin
如下是對項目結構的說明:
- contracts/: Directory for Solidity contracts
- migrations/: Directory for scriptable deployment files
- test/: Directory for test files for testing your application and contracts
- truffle.js: Truffle configuration file
接下來進行合約的編譯,部署等操作見文檔即可。
web3.js
nodejs 項目使用 web3.js 更方便。
下面采用 koa 框架搭建服務端,在 truffle 的基礎上搭建。
編譯合約
采用 truffle 進行編譯
truffle compile
編譯文件會寫入 build/constracts/ 目錄下。
搭建koa
這里不詳細展開,只介紹 nodejs 與區塊鏈交互部分。
與區塊鏈交互
項目中,server/blockchain.js 是封裝的 web 接口。
// web3js實例對象
const web3 = require('../util/web3');
// 這里是獲取到編譯后的合約,即上面 build/constracts/ 目錄下的 IDCard.json 文件
const { compiledContract } = require('../util/contract');
const config = require('../config');
// 合約
const contract = new web3.eth.Contract(compiledContract.abi);
// 服務接口
const svr = {
deploy: async (data) => {
// 部署合約
const instance = await contract.deploy({
data: compiledContract.bytecode,
// 數組的參數,會傳入部署的合約的構造函數,我這里就是 IDCard 合約
arguments: [data],
}).send({
// 部署合約的賬號
from: config.eth.from,
value: 0,
});
return instance._address
},
display: async (address) => {
// 引用存在的合約
const instance = new web3.eth.Contract(compiledContract.abi, address);
// 調用合約方法
return await instance.methods.display().call();
}
}
module.exports = {
deploy: async (ctx) => {
const body = ctx.request.body;
if (!body.data) {
ctx.throw(400, '.data required');
}
const address = await svr.deploy(body.data);
ctx.body = {
address: address,
}
},
display: async (ctx) => {
const params = ctx.params;
const data = await svr.display(params.address);
ctx.body = {
data: data,
}
}
};
接口測試
部署合約:
查看信息:
web3j
web3j is a lightweight, highly modular, reactive, type safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network。
依賴
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.5.12</version>
</dependency>
使用
// defaults to http://localhost:8545/
Web3j web3 = Web3j.build(new HttpService());
Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
spring-boot項目使用
配置Web3j:
/**
* Web3j配置
*
* @author 奔波兒灞
* @since 1.0
*/
@Data
@Validated
@ConfigurationProperties(prefix = Web3jProperties.PREFIX)
public class Web3jProperties {
static final String PREFIX = "web3j";
private String url = "http://localhost:8545/";
@NotNull
private Deploy deploy;
@NotNull
private Gas gas;
@Data
@Validated
public static class Deploy {
@NotBlank
private String wallet;
@NotBlank
private String password;
}
@Data
@Validated
public static class Gas {
@NotNull
private Long price;
@NotNull
private Long limit;
}
}
/**
* 配置Web3j
*
* @author 奔波兒灞
* @since 1.0
*/
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(Web3jProperties.class)
public class Web3jConfiguration {
private final Web3jProperties properties;
@Bean(destroyMethod = "shutdown")
public Web3j web3j() {
return Web3j.build(new HttpService(properties.getUrl()));
}
@Bean
public Credentials deployCredentials() {
Web3jProperties.Deploy deploy = properties.getDeploy();
Credentials credentials;
try {
return WalletUtils.loadCredentials(deploy.getPassword(), deploy.getWallet());
} catch (IOException e) {
throw new RuntimeException("read wallet failed", e);
} catch (CipherException e) {
throw new RuntimeException("cipher failed", e);
}
}
@Bean
public ContractGasProvider gasProvider() {
Web3jProperties.Gas gas = properties.getGas();
return new StaticGasProvider(BigInteger.valueOf(gas.getPrice()), BigInteger.valueOf(gas.getLimit()));
}
}
配置:
web3j:
url: http://47.101.136.24:8545
deploy:
wallet: wallet.json
password: 123456
gas:
price: 1_000_000_000
limit: 170294
采用 truffle compile
先編譯 .sol 文件成 json
打包代碼:
bin/web3j/bin/web3j truffle generate /Users/xuanbo/Projects/egova/egova-dapp/build/contracts/IDCard.json \
-o src/main/java \
-p com.egova.dapp.contract
測試合約部署:
/**
* 測試Web3j
*
* @author 奔波兒灞
* @since 1.0
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Web3jTest {
@Autowired
private Web3j web3j;
@Autowired
private Credentials deployCredentials;
@Autowired
private ContractGasProvider gasProvider;
@Test
public void version() throws IOException {
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
log.info("clientVersion: {}", clientVersion);
}
@Test
public void deployContract() {
String info = "李四";
RemoteCall<IdCard> deploy = IdCard.deploy(web3j, deployCredentials, gasProvider, info);
deploy.sendAsync().whenComplete(((idCard, e) -> {
if (e == null) {
String address = idCard.getContractAddress();
log.info("deploy success, contract address: {}", address);
} else {
log.error("deploy failed", e);
}
}));
}
}