日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 物聯網 > 區(qū)塊鏈
[導讀] Tendermint(TM)是the Cosmos network旗下的一個區(qū)塊鏈項目。TM能安全且保持一致性地在多臺機器之間復制應用程序。TM的共識算法基于節(jié)點不可信的設計,也就是允許拜占庭錯

Tendermint(TM)是the Cosmos network旗下的一個區(qū)塊鏈項目。TM能安全且保持一致性地在多臺機器之間復制應用程序。TM的共識算法基于節(jié)點不可信的設計,也就是允許拜占庭錯誤。TM主要分成兩個部分。

一是一個區(qū)塊鏈共識引擎(Tendermint Core)。他主要負責節(jié)點之間的數據同步有序傳輸,實現拜占庭共識機制。二是區(qū)塊鏈應用接口(ABCI)。它是一種接口通訊協議,可以通過各種編程語言實現應用邏輯。應用邏輯和TM Core通過ABCI實現了解耦。


下面簡單介紹一下如何在windows環(huán)境下編譯及使用Tendermint。

下載源碼及依賴

當然前提是已經配置好go環(huán)境和git,并且需要梯子,使用如下命令:

go get github.com/Masterminds/glide

go get github.com/tendermint/tendermint/cmd/tendermint

執(zhí)行時間比較長,耐心等待完成后,可以發(fā)現%GOPATH%目錄和%GOPETH%srcgithub.com目錄下多出許多東西

編譯

有兩種方式:

2.1

cd $GOPATH/src/github.com/tendermint/tendermint

glide install

go install 。/cmd/tendermint

這里作者遇到一個坑,執(zhí)行glide install報錯:[ERROR] Failed to find glide.yaml file in directory tree: Cannot resolve parent of D:

首先執(zhí)行glide create,然后按照提示一步步操作,在執(zhí)行glide install。

這時又遇到一個問題:[ERROR] Unable to export dependencies to vendor directory: Error moving files: exit status 1. output: Access is denied. 0 dir(s) moved.

網上查到資料,是因為glide版本造成的,修改到v0.12.3版本即可解決。

接下來重新glide install,新的問題出現了:Error looking for crypto/ed25519: Cannot detect VCS.這是由于國內訪問golang.org被墻導致的,可以添加鏡像文件解決:

glide mirror set https://golang.org/x/crypto https://github.com/golang/crypto

2.2

以上步驟很麻煩,不用怕,我們可以手動使用go build命令編譯:

cd %GOPATH/src/github.com/tendermint/cmd/tendermint

go build

cd %GOPATH/src/github.com/tendermint/abci/cmd/abci-cli

go build

兩次build之后,分別生成了tendermint.exe和abci-cli.exe,將這兩個文件拷到%GOPATH/bin目錄下

tendermint version

abci-cli version

以上兩個命令如果有版本號輸出,那么可以進入下一個步驟了。

運行官方的例子

官方的代碼為我們展示了一個kvstore的例子,它的作用就是存儲數據,類似與levelDB。

打開三個cmd窗口,cmd1,cmd2,cmd3

在cmd1中輸入 abci-cli kvstore

可以看到程序在Waiting for new connecTIon.。.

接下來在cmd2輸入 tendermint init,然后tendermint node

C:Usersch》tendermint init

I[2019-08-04|18:28:48.105] Found private validator module=main keyFile=.tendermintconfigpriv_validator_key.json stateFile=.tendermintdatapriv_validator_state.json

I[2019-08-04|18:28:48.113] Found node key module=main path=.tendermintconfig ode_key.json

I[2019-08-04|18:28:48.118] Found genesis file module=main path=.tendermintconfiggenesis.json

C:Usersch》tendermint node

I[2019-08-04|18:28:57.497] Version info module=main software=0.32.1 block=10 p2p=7

I[2019-08-04|18:28:57.514] StarTIng Node module=main impl=Node

I[2019-08-04|18:28:57.528] Started node module=main nodeInfo=“{ProtocolVersion:{P2P:7 Block:10 App:1} ID_:fe807a7617494c46d38bc040801ab567cc330852 ListenAddr:tcp://0.0.0.0:26656 Network:test-chain-17UaI5 Version:0.32.1 Channels:4020212223303800 Moniker:caohuan Other:{TxIndex:on RPCAddress:tcp://127.0.0.1:26657}}”

I[2019-08-04|18:28:58.539] Executed block module=state height=1 validTxs=0 invalidTxs=0

I[2019-08-04|18:28:58.546] Committed state module=state height=1 txs=0 appHash=0000000000000000

I[2019-08-04|18:28:59.552] Executed block module=state height=2 validTxs=0 invalidTxs=0

I[2019-08-04|18:28:59.558] Committed state module=state height=2 txs=0 appHash=0000000000000000

I[2019-08-04|18:29:00.573] Executed block module=state height=3 validTxs=0 invalidTxs=0

I[2019-08-04|18:29:00.579] Committed state module=state height=3 txs=0 appHash=0000000000000000

I[2019-08-04|18:29:01.592] Executed block module=state height=4 validTxs=0 invalidTxs=0

I[2019-08-04|18:29:01.599] Committed state module=state height=4 txs=0 appHash=0000000000000000

tendermint init是初始化tendermint服務,tendermint node則是啟動服務

服務啟動后,即開始出塊,那是因為和cmd1啟動的abci-cli建立了鏈接,如果想不要自動出空塊,可以在啟動的時候加上參數 tendermint node --consensus.create_empty_blocks=false

tendermint服務啟動后,再看cmd1窗口:

C:Usersch》abci-cli kvstore

I[2019-08-04|18:28:50.646] StarTIng ABCIServer module=abci-server impl=ABCIServer

I[2019-08-04|18:28:50.693] WaiTIng for new connection.。. module=abci-server

I[2019-08-04|18:28:57.473] Accepted a new connection module=abci-server

I[2019-08-04|18:28:57.478] Waiting for new connection.。. module=abci-server

I[2019-08-04|18:28:57.484] Accepted a new connection module=abci-server

I[2019-08-04|18:28:57.488] Waiting for new connection.。. module=abci-server

I[2019-08-04|18:28:57.492] Accepted a new connection module=abci-server

I[2019-08-04|18:28:57.496] Waiting for new connection.。. module=abci-server

能夠發(fā)現這里建立了三個連接,這三個連接分別是:

Info/Query Connection:用于查詢服務通信

Mempool Connection:用于發(fā)送交易通信

Consensus Connection:用于共識通信

后面還會提到這三個連接的作用。

接下來可以開始測試了,在cmd3窗口輸入 curl -s localhost:26657/broadcast_tx_commit?tx=“author=caohuan”

C:Usersch》curl -s localhost:26657/broadcast_tx_commit?tx=“author=caohuan”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“check_tx”: {

“gasWanted”: “1”

},

“deliver_tx”: {

“events”: [

{

“type”: “app”,

“attributes”: [

{

“key”: “Y3JlYXRvcg==”,

“value”: “Q29zbW9zaGkgTmV0b3dva28=”

},

{

“key”: “a2V5”,

“value”: “YXV0aG9y”

}

}

},

“hash”: “B339C04B9163F0585B8DB0703E2A107A9B21034B2F5D18BF9B66BDEF5DD627E3”,

“height”: “168”

}

}

友情提示:這條命令在linux系統(tǒng)中是這么寫的:curl -s ‘localhost:26657/broadcast_tx_commit?tx=“author=caohuan”’。windows環(huán)境中,少了單引號,多了轉義的反斜杠。

現在查詢一下這個操作有沒有成功:curl -s localhost:26657/abci_query?data=“author”

C:Usersch》curl -s localhost:26657/abci_query?data=“author”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“response”: {

“l(fā)og”: “exists”,

“key”: “YXV0aG9y”,

“value”: “Y2FvaHVhbg==”

}

}

}

查詢出的這個value是base64編碼的,可以使用在線工具轉碼,也可以在golang中自己實現一個轉碼,轉碼之后可以發(fā)現“Y2FvaHVhbg==”就是“caohuan”

除了使用curl命令以外,還可以使用瀏覽器,直接在地址欄輸入 http://localhost:26657/abci_query?data=“author” 即可。

以上kvstore的例子就運行成功了,官方還有一個例子counter,這里就不詳細展開介紹了,有興趣的小伙伴可以去官方文檔看一下。

簡單分析一下

下面簡單分析一下剛才經歷了什么過程

首先開啟abci-cli(cmd1)服務,然后開啟tendermint(cmd2)服務,然后使用客戶端client(cmd3)向tendermint發(fā)送交易,具體過程如下:

1,client產生了一條數據“author=caohuan”,要把這條數據發(fā)送到鏈上去

2,client發(fā)起請求:curl -s localhost:26657/broadcast_tx_commit?tx=“author=caohuan”

3,tendermint收到tx=“author=caohuan”的交易,發(fā)送給abci

4,abci接受到tx之后,調用CheckTx方法,驗證這個tx是否符合交易規(guī)范。在/tendermint/abci/example/kvstore/kvstore.go可以看到代碼,這里的CheckTx沒有做任何事

6,tendermint把該tx存在內存池里,并把這條tx通轉發(fā)給其他tendermint節(jié)點,所有節(jié)點對該交易進行兩輪投票,PreVote--》PreCommit--》Commit,PreVote超過三分之二同意之后進入PreCommit,再超過三分之二投票后進入Commit(在該例中只有一個節(jié)點,所以沒有經過共識)

7,tendermint共識完成之后進入commit階段,再對abci發(fā)送請求:BeginBlock -》 DeliverTx * n次 --》 EndBlock --》 Commit,即:新區(qū)快 --》 正在接受區(qū)塊內容 * n條 --》 區(qū)塊內容接受完了 --》 提交到區(qū)塊鏈。這些方法在/tendermint/abci/example/kvstore/kvstore.go中都可以找得到

8,abci在Commit之后會通知tendermint

9,區(qū)塊鏈上多了一個區(qū)塊記錄著“author=caohuan”

現在在回過頭來看一下剛才提到的三個連接包含哪些東西:

Info/Query Connection:Info,SetOption,Query

Mempool Connection:CheckTx

Consensus Connection:InitChain,BeginBlock,DeliverTx,EndBlock,Commit

這些接口是定義在/tendermint/abci/types/application.go中的,這些接口在協助完成了整個交易過程。

// Info/Query Connection

Info(RequestInfo) ResponseInfo // Return application info

SetOption(RequestSetOption) ResponseSetOption // Set application option

Query(RequestQuery) ResponseQuery // Query for state

// Mempool Connection

CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool

// Consensus Connection

InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain with validators and other info from TendermintCore

BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block

DeliverTx(RequestDeliverTx) ResponseDeliverTx // Deliver a tx for full processing

EndBlock(RequestEndBlock) ResponseEndBlock // Signals the end of a block, returns changes to the validator set

Commit() ResponseCommit // Commit the state and return the application Merkle root hash

也就是說,如果我們想使用tendermint實現自己的一條鏈,就必須實現這些接口。

實現自己的一條鏈

接下來,我們嘗試著實現自己的一條鏈,邏輯很簡單:初始化xiaoMing和daZhuang兩個賬戶,余額各為100,由xiaoMing向daZhuang轉賬。

代碼:

func NewCounterApplication(serial bool) *CounterApplication {

fmt.Println(“NewCounterApplication進來了嗎”)

return &CounterApplication{accountMap: map[string]int{“xiaoMing”: 100, “daZhuang”: 100}}

}

func (app *CounterApplication) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx {

fmt.Println(“DeliverTx進來了嗎”)

//xiaoming轉給dazhuang

tx8 := make([]byte, 8)

copy(tx8[len(tx8)-len(req.Tx):], req.Tx)

balance := int(binary.BigEndian.Uint64(tx8))

if app.accountMap[“xiaoMing”] 《 balance {

return types.ResponseDeliverTx{

Code: code.CodeTypeEncodingError,

Log: fmt.Sprintf(“Insufficient balance”)}

}

app.accountMap[“xiaoMing”] -= balance

app.accountMap[“daZhuang”] += balance

return types.ResponseDeliverTx{Code: code.CodeTypeOK}

}

func (app *CounterApplication) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx {

fmt.Println(“CheckTx進來了嗎”)

//xiaoming轉給dazhuang

tx8 := make([]byte, 8)

copy(tx8[len(tx8)-len(req.Tx):], req.Tx)

balance := int(binary.BigEndian.Uint64(tx8))

if app.accountMap[“xiaoMing”] 《 balance {

return types.ResponseCheckTx{

Code: code.CodeTypeEncodingError,

Log: fmt.Sprintf(“Insufficient balance”)}

}

fmt.Println(“CheckTx ok”)

return types.ResponseCheckTx{Code: code.CodeTypeOK}

}

func (app *CounterApplication) Commit() (resp types.ResponseCommit) {

fmt.Println(“Commit進來了嗎”)

appHash := make([]byte, 8)

binary.PutVarint(appHash, int64(len(app.accountMap))) //這里隨便commit了一個hash

return types.ResponseCommit{Data: appHash}

}

func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {

fmt.Println(“Query進來了嗎”)

name := string(reqQuery.Data)

amount, ok := app.accountMap[name]

if !ok {

return types.ResponseQuery{Log: fmt.Sprintf(“Invalid name %v”, reqQuery.Data)}

}

return types.ResponseQuery{Value: []byte(fmt.Sprintf(“%v”, amount))}

}

先使用unsafe_reset_all參數啟動一次tendermint,以清除之前的數據,然后啟動tendermint和abci-cli開始測試

首先查詢一下xiaoMing和daZhuang的初始余額:

C:Usersch》curl -s localhost:26657/abci_query?data=“xiaoMing”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“response”: {

“value”: “MTAw”

}

}

}

C:Usersch》curl -s localhost:26657/abci_query?data=“daZhuang”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“response”: {

“value”: “MTAw”

}

}

}

這里查詢出來的結果還是base64編碼的。

接下來轉賬:

C:Usersch》curl localhost:26657/broadcast_tx_commit?tx=0x11

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“check_tx”: {},

“deliver_tx”: {},

“hash”: “4A64A107F0CB32536E5BCE6C98C393DB21CCA7F4EA187BA8C4DCA8B51D4EA80A”,

“height”: “3”

}

}

這里的0x11是16進制,換算成十進制就是17

再次查詢余額:

C:Usersch》curl -s localhost:26657/abci_query?data=“xiaoMing”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“response”: {

“value”: “ODM=”

}

}

}

C:Usersch》curl -s localhost:26657/abci_query?data=“daZhuang”

{

“jsonrpc”: “2.0”,

“id”: “”,

“result”: {

“response”: {

“value”: “MTE3”

}

}

}

經過base64解碼,ODM=就是83,MTE3就是117,測試成功!

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯系該專欄作者,如若文章內容侵犯您的權益,請及時聯系本站刪除。
換一批
延伸閱讀

LED驅動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關鍵字: 驅動電源

在工業(yè)自動化蓬勃發(fā)展的當下,工業(yè)電機作為核心動力設備,其驅動電源的性能直接關系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅動電源設計中至關重要的兩個環(huán)節(jié),集成化方案的設計成為提升電機驅動性能的關鍵。

關鍵字: 工業(yè)電機 驅動電源

LED 驅動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設備的使用壽命。然而,在實際應用中,LED 驅動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設計、生...

關鍵字: 驅動電源 照明系統(tǒng) 散熱

根據LED驅動電源的公式,電感內電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關鍵字: LED 設計 驅動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產業(yè)的重要發(fā)展方向。電動汽車的核心技術之一是電機驅動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅動系統(tǒng)中的關鍵元件,其性能直接影響到電動汽車的動力性能和...

關鍵字: 電動汽車 新能源 驅動電源

在現代城市建設中,街道及停車場照明作為基礎設施的重要組成部分,其質量和效率直接關系到城市的公共安全、居民生活質量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關鍵字: 發(fā)光二極管 驅動電源 LED

LED通用照明設計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數校正(PFC)、空間受限和可靠性等。

關鍵字: LED 驅動電源 功率因數校正

在LED照明技術日益普及的今天,LED驅動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關鍵字: LED照明技術 電磁干擾 驅動電源

開關電源具有效率高的特性,而且開關電源的變壓器體積比串聯穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現在的LED驅動電源

關鍵字: LED 驅動電源 開關電源

LED驅動電源是把電源供應轉換為特定的電壓電流以驅動LED發(fā)光的電壓轉換器,通常情況下:LED驅動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關鍵字: LED 隧道燈 驅動電源
關閉