區塊鏈測試環境搭建
1.從docker hub選擇下載相關容器 https://hub.docker.com/search/
2.容器下載 docker pull ubuntu
3.容器加速 https://www.daocloud.io/mirror#accelerator-doc
4.進入容器安裝環境
docker run -it ubuntu /bin/bash
cat /etc/issue
5.安裝-新建使用者和賦予ROOT許可權
apt-get update
apt-get install vim
apt-get install sudo
adduser deploy
chmod 777 /etc/sudoers 可讀可寫可執行
vim /etc/sudoers 找到root deploy ALL=(ALL=ALL) ALL 儲存退出
chmod 440 /etc/sudoers
su deploy
6.安裝 nodejs truffle和testrpc
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
安裝失敗https://blog.csdn.net/xiaoleizhanghahaha/article/details/83715721
https://mirrors.tuna.tsinghua.edu.cn/help/nodesource/
sudo apt-get install nodejs
node –v
npm –v
npm install -g cnpm --registry=https://registry.npm.taobao.org
sudo cnpm install -g ethereumjs-testrpc
https://www.npmjs.com/package/ethereumjs-testrpc-persist
sudo cnpm install -g truffle
truffle version
https://www.npmjs.com/package/truffle
sudo apt-get install tree
docker commit
7.程式碼整合環境
.idea + solidty
8.docker 掛載本地目錄
docker run -it -v /Users/xuxinlai/blockchain/ethereum:/home/deploy ec0a49148d7b /bin/bash
9.Demo
1.truffle init
2.程式碼:MyCoin.sol
pragma solidity ^0.4.17; contract MyCoin { // getBalance function getBalance() public pure returns(uint) { return 1; } }
3.程式碼:2_deploy_contracts.js
var MyCoin = artifacts.require("./MyCoin.sol"); module.exports = function(deployer) { //add for demo deployer.deploy(MyCoin); };
4.程式碼:TestMyCoin.sol
pragma solidity ^0.4.17; import "truffle/Assert.sol"; import "../contracts/MyCoin.sol"; contract TestMyCoin { function testHelloWorld() public{ MyCoin instance = new MyCoin(); uint expected = 1; Assert.equal(instance.getBalance(), expected, "Test OK"); } }
5.程式碼:truffle.js
module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "123456" // 匹配任何network id } } };
truffle compile
6.testrpc &
7.truffle migrate
8.truffle test
9. docker save -o testrpc-truffle-demo.tar a63219c08e93
10. docker load -i testrpc-truffle-demo.tar
11. docker tag a63219c08e93 test-truffle-demo:v1