15、ethereum(2/3)

git clone https://github.com/ethereum/ethereumj.git

結構介紹

本項目主要使用spring來管理對象,使用netty完成通信,使用leveldb負責存儲,整體的包的結構如下所示:
這裏寫圖片描述
每個包的作用大致爲:

cli : 負責啓動參數的處理
config: 負責配置文件注入以及一些bean的注入
公共部分:包含系統配置變量(SystemProperties)、配置初始化(Initializer 完成配置的初始化)、倉庫、數據源、驗證器等的注入
blockchain:包含的是一些以太坊發佈時各個版本的不同的特性,這些特性包括不限於難度值計算以及其他一些EIP所描述的bug或者features
net:包含的是以太坊支持的不同的網絡配置,包含主網或者測試網絡不同的配置信息,所謂的配置信息就是blockchain配置所描述的一些不同的EIP或者其他的features
core: 核心部分,它包含賬戶、區塊、創世塊、區塊鏈、transaction、bloom的定義以及區塊如何驗證、如何加入鏈以及transaction如何使用vm執行也就是智能合約的執行都在這裏完成
crypto: 加密工具包含不限於hash算法、ECC算法等
datasource: 提供了兩種數據源實現內存以及leveldb,並使用者兩種數據源擴展了不同實現,這包含緩存數據源、鏈數據源,依據於此又封裝出讀寫緩存、異步讀寫緩存以及鏈存儲相關的數據源實現。
db: 定義瞭如何使用datasource存儲block、transaction,換句話說就是block、transaction的存儲數據結構
facade: 包含了ethereum的實現,就是將塊存儲、驗證、同步、合約執行等做的封裝
mine是挖礦相關的
net 涉及的都是網絡相關的,以太坊節點發現塊同步都是建立在rlpx協議之上,這包含p2p、shh、eth等,另外server包就是節點發現服務啓動入口
samples是一些測試例子
solidity是合約編譯部分的實現
sync 是負責負責塊的同步下載等
trie 是以太坊鏈存儲的數據結構,該包主要是實現該數據結構也就是MPT
utils 工具類,包含rlp編碼等
validator 這是一些驗證器,在驗證block的時候會用到
vm 以太坊vm實現
start是程序入口

參考:https://www.jianshu.com/p/3fc606a556e0

go - 以太坊開發環境搭建介紹

安裝go環境:https://blog.csdn.net/llianlianpay/article/details/79831589
安裝nodejs
git clone https://github.com/ethereum/go-ethereum.git
cd go-ethereum
make all

export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/go/bin  
export PATH=$PATH:/home/n8/IdeaProjects/go/go-ethereum/build/bin

創建genesis.json

{  
  "nonce": "0x0000000000000042",  
  "timestamp": "0x00",  
  "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",  
  "extraData": "0x00",  
  "gasLimit": "0x80000000",  
  "difficulty": "0x400",  
  "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",  
  "coinbase": "0x2D356ee3F5b8718d8690AFCD31Fe2CB5E602677e",  
  "alloc": {},  
  "config": {  
    "chainId": 15,  
    "homesteadBlock": 0,  
    "eip155Block": 0,  
    "eip158Block": 0  
  }  
}  

啓動EVM


geth --datadir "/home/n8/IdeaProjects/go/chain" init /home/n8/IdeaProjects/go/genesis.json  

日誌:
[n8@8888 workspace]$ geth –datadir “/home/n8/IdeaProjects/go/chain” init /home/n8/IdeaProjects/go/genesis.json
INFO [04-08|00:00:53] Maximum peer count ETH=25 LES=0 total=25
INFO [04-08|00:00:53] Allocated cache and file handles database=/home/n8/IdeaProjects/go/chain/geth/chaindata cache=16 handles=16
INFO [04-08|00:00:53] Writing custom genesis block
INFO [04-08|00:00:53] Persisted trie from memory database nodes=0 size=0.00B time=4.07µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-08|00:00:53] Successfully wrote genesis state database=chaindata hash=fd4230…1c2289
INFO [04-08|00:00:53] Allocated cache and file handles database=/home/n8/IdeaProjects/go/chain/geth/lightchaindata cache=16 handles=16
INFO [04-08|00:00:53] Writing custom genesis block
INFO [04-08|00:00:53] Persisted trie from memory database nodes=0 size=0.00B time=1.268µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [04-08|00:00:53] Successfully wrote genesis state database=lightchaindata hash=fd4230…1c2289

開發環境下的測試,可直接啓動默認的geth開發環境,注意:雙減號、2與>>之間沒有空格

geth --dev console 2>> geth_dev_log

進入到console下其實就進入了Ethereum的JavaScript環境,大概這裏就是EVM(以太坊虛機)了。

[n8@8888 workspace]$ geth --dev console 2>> geth_dev_log
Welcome to the Geth JavaScript console!

instance: Geth/v1.8.4-unstable-1e248f3a/linux-amd64/go1.10.1
coinbase: 0x6ab4dec5ef8b99154291312802ca1c73a205e634
at block: 0 (Thu, 01 Jan 1970 08:00:00 CST)
 datadir: 
 modules: admin:1.0 clique:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0

> 

如果想關注在操作過程中的日誌,可以另起一個SSH通過tail觀察

tail -f geth_dev_log

...
INFO [04-08|00:03:20] IPC endpoint opened                      url=/tmp/geth.ipc
INFO [04-08|00:03:20] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=42.829µs
WARN [04-08|00:03:20] Block sealing failed                     err="waiting for transactions"
INFO [04-08|00:03:23] Mapped network port                      proto=tcp extport=46790 intport=46790 interface="UPNP IGDv1-IP1"

查看礦工賬號列表、創建礦工賬號、開挖

> exit
[n8@8888 workspace]$ geth --dev --datadir=/home/n8/IdeaProjects/go/test_chain console 2>> geth_dev_log
> eth.accounts
["0xf48700f6cbdc24201dc6a419c2194959cbc813ed"]
> personal.newAccount('nick')
"0x34eab8a10b2a17163816fb88543952f04b2c27cd"
> eth.accounts
["0xf48700f6cbdc24201dc6a419c2194959cbc813ed", "0x34eab8a10b2a17163816fb88543952f04b2c27cd"]
> nick = eth.accounts[1]
"0x34eab8a10b2a17163816fb88543952f04b2c27cd"
> defuat_user = eth.accounts[0]
"0xf48700f6cbdc24201dc6a419c2194959cbc813ed"
> miner.start() //開始挖礦 等3分鐘
null
> miner.stop()  //停挖  
eth.getBalance(nick)  //查看第一個礦工的收入   0
eth.getBalance(defuat_user)  //查看第二個礦工的收入   1.15*e^77

eth下默認會把挖礦的收入分配給第一個賬號…

轉個10個以太幣作爲勞務費

eth.sendTransaction({from: defuat_user, to: nick, value: web3.toWei(5, 'ether')}) //給第二個礦工轉5個以太幣 
"0x25e5046822b12cc1b75068580ca33bf0bb804e28955646b7e6220a87d02d506d"
> eth.getBalance(nick)

查看日誌生成了事務日誌,但金額沒有發生轉帳交易。
因爲區塊鏈的交易確認是要通過挖礦來實現的,沒有人挖礦,就意味着帶有交易信息(就是剛纔的那一大串字符串)的區塊就沒有在網絡的各個節點中記錄,而缺乏記錄就意味着沒有人認可這筆交易…所以當前的交易雖然成功了,但交易還需要等待全網確認…我們來繼續啓動挖礦…挖上個10秒,然後再看看結果…

miner.start() //開始挖礦...  
//miner.stop()  //10秒後停止挖礦  
> eth.getBalance(nick)
5000000000000000000

在log中可以看到交易產生的時間以及受益人,同時可以看到在開始挖礦後,區塊中攜帶交易信息的數量…

在現實中,挖礦的實際意義在於申請記賬權,一旦幫網絡上交易登記的賬務,也就相當於認可了對方的交易。


testrpc & truffle

實驗一:將testrpc部署到eth-host主機上,並對外啓動eth測試網絡服務:http://eth-host:8545
實驗二:以webpack爲例在tru-host主機(truffle)上部署智能合約,並對外提供轉賬服務:http://192.168.3.103:8088

  1. 部署eth-host虛機

主機功能:搭建以太坊虛機,爲智能合約應用服務器提供賬本、記賬等基礎的以太坊測試網絡服務。

(1) 虛機與網絡:在vmware上使用最簡版的centos7安裝虛機即可,安裝完成後,爲方便局域網訪問,將虛機網絡設置爲橋接方式。
(2) 配置主機host:
192.168.3.102 eth-host
192.168.3.103 tru-host
(3) 安裝nodejs
(4) 關閉防火牆
(5) 部署ethereumjs-testrpc
npm install -g ethereumjs-testrpc
testrpc
即可啓動帶有初始化10個賬本的eth測試網絡服務。

  1. 部署tru-host虛機
    主機功能:安裝部署DAPP智能合約應用服務器,爲web服務提供智能合約的執行服務。
    (1) 虛機與網絡:在vmware上使用帶GNOME視窗的centos7版本安裝虛機,安裝完成後,將虛機網絡設置爲橋接模式。
    (2) 配置主機host:
    192.168.3.102 eth-host
    192.168.3.103 tru-host
    (3) 安裝nodejs
yum install gcc gcc-c++
./configure
make && make install 

vim /etc/profile  
export NODE_HOME=/usr/local/node  
export PATH=$NODE_HOME/bin:$PATH  
source /etc/profile
node --version

(4) 關閉防火牆

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啓動
firewall-cmd --state #查看默認防火牆狀態(關閉後顯示notrunning,開啓後顯示running)

(5) 安裝git
yum install git

(6) 部署truffle項目:
npm install -g truffle
cd && mkdir t01 && cd t01
truffle unbox webpack

本地開發環境
truffle develop
編譯和移植:

這裏有兩種方法,第一種是在步驟4中的開發終端直接輸入

compile
migrate
或者在新的終端中在上面加上truffle

truffle compile
truffle migrate

truffle(develop)> migrate
Using network 'develop'.

另起一個console
npm run dev


修改package.json
回到項目目錄,編輯package.json,
webpack-dev-server –host tru-host –public 192.168.1.149 –port 8088 –disable-host-check true –allowed-hosts 192.168.1.101,192.168.1.150

npm run dev
There was an error fetching your accounts.
修改/root/t01/app/javascripts/app.js
window.web3 = new Web3(new Web3.providers.HttpProvider(“http://192.168.1.149:9545“));
將127.0.0.1:9545 -> 192.168.1.149:9545

使用testrpc
配置truffle.js
默認爲:

module.exports = {
  networks: {
    development: {
      host: '127.0.0.1',
      port: 7545,
      network_id: '*' // Match any network id
    }
  }
}

更改後:

module.exports = {
  networks: {
    testrpc_150: {
        network_id: 201,        
        host: "192.168.1.150", 
        port: 8545
    },
    development: {
      host: '127.0.0.1',
      port: 7545,
      network_id: '*' // Match any network id
    }
  }
}

修改/root/t01/app/javascripts/app.js
window.web3 = new Web3(new Web3.providers.HttpProvider(“http://192.168.1.150:8545“));
將192.168.1.149:9545 -> 192.168.1.150:8545

重啓testrpc
testrpc –networkId 201

testrpc --help
  options:
  --port/-p <port to bind to, default 8545>
  --host/-h <host to bind to, default 0.0.0.0>
  --fork/-f <url>   (Fork from another currently running Ethereum client at a given block)

  --db <db path>   (directory to save chain db)
  --seed <seed value for PRNG, default random>
  --deterministic/-d     (uses fixed seed)
  --mnemonic/-m <mnemonic>
  --accounts/-a <number of accounts to generate at startup>
  --acctKeys <path to file>  (saves generated accounts and private keys as JSON object in specified file)
  --secure/-s   (Lock accounts by default)
  --unlock <accounts>   (Comma-separated list of accounts or indices to unlock)

  --blocktime/-b <block time in seconds>
  --networkId/-i <network id> (default current time)
  --gasPrice/-g <gas price>   (default 20000000000)
  --gasLimit/-l <gas limit>   (default 90000)

  --debug       (Output VM opcodes for debugging)
  --verbose/-v
  --mem         (Only show memory output, not tx history)
  --help / -?    (this output)

關閉truffle,啓動:

$ truffle migrate –network testrpc_150
這裏寫圖片描述

整個流程串通!

安裝chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

err:There was an error fetching your accounts.

安裝MetaMask插件
7.1在centos下安裝lantern[不能生效 。。。]
下載lantern for ubuntu安裝包 https://github.com/getlantern/lantern
ar -x xx.deb
解壓deb包得到data.tar.gz
$xz -d *.tar.xz
$tar -xvf *.tar
解壓data.tar.gz得到usr

cp -r usr/* /usr/. 
yum install libapp* 
yum install epel-release 
yum install libappindicator-gtk3.x86_64 

$lantern 

下載離線版 MetaMask CRX
rar命令語法
將/etc 目錄壓縮爲etc.rar 命令爲:
rar a etc.rar /etc
將etc.rar 解壓 命令爲:
rar x etc.rar
unrar -e etc.tar
chrome://extensions/
打開“擴展程序”之後,直接把之前下載的crx文件拖入瀏覽器的擴展程序就行了,程序會自動安裝。


[n8@8888 node_modules]$ pwd
/home/n8/IdeaProjects/antd-demor/node_modules
[n8@8888 node_modules]$ ls -a |grep bin
babel-helper-builder-binary-assignment-operator-visitor
.bin
[n8@8888 node_modules]$ cd .bin
[n8@8888 .bin]$ ls -ll
...
lrwxrwxrwx 1 n8 n8 25 329 09:42 webpack -> ../webpack/bin/webpack.js
lrwxrwxrwx 1 n8 n8 47 329 09:42 webpack-dev-server -> ../webpack-dev-server/bin/webpack-dev-server.js

[n8@8888 .bin]$ ./webpack-dev-server --help 
Connection options:
  --port                The port
  --disable-host-check  Will not check the host                           [布爾]
  --socket              Socket to listen
  --public              The public hostname/ip address of the server    [字符串]
  --host                The hostname/ip address the server will bind to
                                                  [字符串] [默認值: "localhost"]
  --allowed-hosts       A comma-delimited string of hosts that are allowed to
                        access the dev server                           [字符串]

連接選項:
–port 端口
–disable-host-check 禁止檢查主機端口 [布爾值:true/false]
–socket 用於監聽的Socket
–public 用於公共訪問的域名或IP地址 [字符串]
–host 綁定到本機的域名或IP地址 [字符串] [缺省: “localhost”]
–allowed-hosts 使用西文逗號分割的允許訪問的主機域名或IP列表 [字符串]

查看truffle版本:

[root@tru-host javascripts]# truffle version
Truffle v4.1.5 (core: 4.1.5)
Solidity v0.4.21 (solc-js)

參考: https://blog.csdn.net/xc70203/article/details/77988473
https://blog.csdn.net/u010670897/article/details/79789665
https://blog.csdn.net/u010670897/article/details/79789665
https://blog.csdn.net/chenyufeng1991/article/details/53456270
http://truffle.tryblockchain.org/14838695696327.html

Could not connect to your Ethereum client. Please check that your Ethereum client:
- is running
- is accepting RPC connections (i.e., “–rpc” option is used in geth)
- is accessible over the network
- is properly configured in your Truffle configuration file (truffle.js)

發佈了74 篇原創文章 · 獲贊 32 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章