環境node
虛擬主機: ubuntu 18虛擬機linux
宿主主機: win10; ip:192.168.0.160git
安裝go,並編譯gethgithub
將下載好的golang包上傳到root目錄。golang
sudo apt-get install -y build-essential golang
git clone https://github.com/ethereum/go-ethereum cd go-ethereum make geth # 添加快捷訪問 cp build/bin/geth /usr/bin/geth
建立數據存儲目錄,分別爲8001-8002以此類推web
mkdir -p /node/node8001 mkdir -p /node/node8002
geth account new - -datadir "/node/node8001"
執行後會要求設置帳戶的unlock口令,請記住配置的口令. 會生成一個密碼保存文件夾(keystore), 內含一個密碼保存文件,名字以下shell
UTC--2018-12-13T02-58-07.160085261Z--f8c5cad127f7053e143204450c18b6bc1f353c9bnpm
生成文件名爲genesis.json的文件, 內容以下:json
{ "config": { "chainId": 115, "homesteadBlock": 0, "byzantiumBlock": 12, "eip155Block": 0, "eip158Block": 0 }, "nonce":"0x0000000000000042", "mixhash":"0x0000000000000000000000000000000000000000000000000000000000000000", "difficulty":"0x0001", "alloc":{"f8c5cad127f7053e143204450c18b6bc1f353c9b":{"balance":"990000000000000000000000000000"}}, "timestamp":"0x5b3bcd2f", "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000", "extraData":"", "gasLimit":"0xffffffff" }
/usr/bin/gethfw --datadir /node/node8001 init ./genesis.json /usr/bin/gethfw --datadir /node/node8002 init ./genesis.json
單獨啓一個終端來啓動節點ubuntu
/usr/bin/geth --targetgaslimit 4294967295 --identity "node8001" --datadir /node/node8001 --port 8801 --rpc --rpcaddr "0.0.0.0" --rpcport 8145 --rpccorsdomain "http://192.168.0.160:8081" --rpcapi "admin,debug,eth,net,personal,shh,txpool,web3" --networkid 115 --nodiscover --mine
單獨啓一個終端來啓動節點
/usr/bin/geth --targetgaslimit 4294967295 --identity "node8002" --datadir /node/node8002 --port 8802 --rpc --rpcaddr "0.0.0.0" --rpcport 8546 --rpccorsdomain "http://192.168.0.160:8081" --rpcapi "admin,debug,eth,net,personal,shh,txpool,web3" --networkid 115 --nodiscover
此時,節點已經啓動OK, 咱們能夠來查看一下節點狀態了:
先前的兩個終端就讓他們掛在那裏執行程序,另外開啓一個終端,執行以下命令:
/usr/bin/gethfw attach ipc:/node/node8001/geth.ipc
而後會自動進入控制檯了, 輸入admin命令,能夠查看當前鏈的信息,輸出相似以下:
{
datadir: "/node/node8001",
nodeInfo: {
enode: "enode://9e7c5f604f49638524b3d95078bb4644e4e379f6fa532094ccd1bbd9c975e81a1e66fd602530c274240ee0f563f917fc4de0665e3e5abeaf021f3efaa0565eeb@[::]:8801?discport=0",
ip: "::",
listenAddr: "[::]:8801",
name: "Geth/node8001/v1.8.3-stable/linux-amd64/go1.10.4",
ports: {
discovery: 0,
listener: 8801
},
protocols: {
eth: {
config: {...},
difficulty: 78940001,
genesis: "0xa6ebc49109d376f9afc21fb6ab9d4b62da2442c6d81e6ab90633a03e851693d6",
head: "0xafe6c156e1920039334e4a470aa80c7d8ad9202fdf62a38ed2ec27e076685949",
network: 115......
至此, 以太坊私鏈算是啓動完成了。
參考https://github.com/ethereum/remix-ide 文檔, 開始部署。
git clone https://github.com/ethereum/remix-ide.git
cd remix-ide
npm start
此時,就能夠在localhost:8080訪問RemixIDe了。
經過在瀏覽器打開localhost:8080地址, 進入Remix-IDE.
加入以下代碼
pragma solidity ^0.4.16; interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; } contract TokenERC20 { string public name; string public symbol; uint8 public decimals = 18; // 18 是建議的默認值 uint256 public totalSupply; mapping (address => uint256) public balanceOf; // mapping (address => mapping (address => uint256)) public allowance; event Transfer(address indexed from, address indexed to, uint256 value); event Burn(address indexed from, uint256 value); function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public { totalSupply = initialSupply * 10 ** uint256(decimals); balanceOf[msg.sender] = totalSupply; name = tokenName; symbol = tokenSymbol; } function _transfer(address _from, address _to, uint _value) internal { require(_to != 0x0); require(balanceOf[_from] >= _value); require(balanceOf[_to] + _value > balanceOf[_to]); uint previousBalances = balanceOf[_from] + balanceOf[_to]; balanceOf[_from] -= _value; balanceOf[_to] += _value; Transfer(_from, _to, _value); assert(balanceOf[_from] + balanceOf[_to] == previousBalances); } function transfer(address _to, uint256 _value) public { _transfer(msg.sender, _to, _value); } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(_value <= allowance[_from][msg.sender]); // Check allowance allowance[_from][msg.sender] -= _value; _transfer(_from, _to, _value); return true; } function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; return true; } function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) { tokenRecipient spender = tokenRecipient(_spender); if (approve(_spender, _value)) { spender.receiveApproval(msg.sender, _value, this, _extraData); return true; } } function burn(uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); balanceOf[msg.sender] -= _value; totalSupply -= _value; Burn(msg.sender, _value); return true; } function burnFrom(address _from, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value); require(_value <= allowance[_from][msg.sender]); balanceOf[_from] -= _value; allowance[_from][msg.sender] -= _value; totalSupply -= _value; Burn(_from, _value); return true; } }
在compile頁中, 選中Auto Compile, 進行自動編譯智能合約, (應用不會出現紅色錯誤提示)
在Run頁中, Environment中,選中inject web3(前提是, 已經安裝好了metamash插件,而且已經導入了帳號,配置後帳號環境)。
在account項, 選擇一個有幣的帳號
最後經過點擊deploy按鈕來部署合約, 在部署前, 可填寫構造函數的參數如 20000000, "TESTINT COIN", "TC"
最終完成部署