www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 物聯(lián)網(wǎng) > 區(qū)塊鏈
[導(dǎo)讀] Tendermint(TM)是the Cosmos network旗下的一個(gè)區(qū)塊鏈項(xiàng)目。TM能安全且保持一致性地在多臺(tái)機(jī)器之間復(fù)制應(yīng)用程序。TM的共識(shí)算法基于節(jié)點(diǎn)不可信的設(shè)計(jì),也就是允許拜占庭錯(cuò)

Tendermint(TM)是the Cosmos network旗下的一個(gè)區(qū)塊鏈項(xiàng)目。TM能安全且保持一致性地在多臺(tái)機(jī)器之間復(fù)制應(yīng)用程序。TM的共識(shí)算法基于節(jié)點(diǎn)不可信的設(shè)計(jì),也就是允許拜占庭錯(cuò)誤。TM主要分成兩個(gè)部分。

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


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

下載源碼及依賴

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

go get github.com/Masterminds/glide

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

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

編譯

有兩種方式:

2.1

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

glide install

go install 。/cmd/tendermint

這里作者遇到一個(gè)坑,執(zhí)行g(shù)lide install報(bào)錯(cuò):[ERROR] Failed to find glide.yaml file in directory tree: Cannot resolve parent of D:

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

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

網(wǎng)上查到資料,是因?yàn)間lide版本造成的,修改到v0.12.3版本即可解決。

接下來(lái)重新glide install,新的問題出現(xiàn)了:Error looking for crypto/ed25519: Cannot detect VCS.這是由于國(guó)內(nèi)訪問golang.org被墻導(dǎo)致的,可以添加鏡像文件解決:

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

2.2

以上步驟很麻煩,不用怕,我們可以手動(dòng)使用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,將這兩個(gè)文件拷到%GOPATH/bin目錄下

tendermint version

abci-cli version

以上兩個(gè)命令如果有版本號(hào)輸出,那么可以進(jìn)入下一個(gè)步驟了。

運(yùn)行官方的例子

官方的代碼為我們展示了一個(gè)kvstore的例子,它的作用就是存儲(chǔ)數(shù)據(jù),類似與levelDB。

打開三個(gè)cmd窗口,cmd1,cmd2,cmd3

在cmd1中輸入 abci-cli kvstore

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

接下來(lái)在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服務(wù),tendermint node則是啟動(dòng)服務(wù)

服務(wù)啟動(dòng)后,即開始出塊,那是因?yàn)楹蚦md1啟動(dòng)的abci-cli建立了鏈接,如果想不要自動(dòng)出空塊,可以在啟動(dòng)的時(shí)候加上參數(shù) tendermint node --consensus.create_empty_blocks=false

tendermint服務(wù)啟動(dòng)后,再看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ā)現(xiàn)這里建立了三個(gè)連接,這三個(gè)連接分別是:

Info/Query Connection:用于查詢服務(wù)通信

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

Consensus Connection:用于共識(shí)通信

后面還會(huì)提到這三個(gè)連接的作用。

接下來(lái)可以開始測(cè)試了,在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)境中,少了單引號(hào),多了轉(zhuǎn)義的反斜杠。

現(xiàn)在查詢一下這個(gè)操作有沒有成功: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==”

}

}

}

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

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

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

簡(jiǎn)單分析一下

下面簡(jiǎn)單分析一下剛才經(jīng)歷了什么過程

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

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

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

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

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

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

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

8,abci在Commit之后會(huì)通知tendermint

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

現(xiàn)在在回過頭來(lái)看一下剛才提到的三個(gè)連接包含哪些東西:

Info/Query Connection:Info,SetOption,Query

Mempool Connection:CheckTx

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

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

// 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實(shí)現(xiàn)自己的一條鏈,就必須實(shí)現(xiàn)這些接口。

實(shí)現(xiàn)自己的一條鏈

接下來(lái),我們嘗試著實(shí)現(xiàn)自己的一條鏈,邏輯很簡(jiǎn)單:初始化xiaoMing和daZhuang兩個(gè)賬戶,余額各為100,由xiaoMing向daZhuang轉(zhuǎn)賬。

代碼:

func NewCounterApplication(serial bool) *CounterApplication {

fmt.Println(“NewCounterApplication進(jìn)來(lái)了嗎”)

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

}

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

fmt.Println(“DeliverTx進(jìn)來(lái)了嗎”)

//xiaoming轉(zhuǎn)給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進(jìn)來(lái)了嗎”)

//xiaoming轉(zhuǎn)給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進(jìn)來(lái)了嗎”)

appHash := make([]byte, 8)

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

return types.ResponseCommit{Data: appHash}

}

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

fmt.Println(“Query進(jìn)來(lái)了嗎”)

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參數(shù)啟動(dòng)一次tendermint,以清除之前的數(shù)據(jù),然后啟動(dòng)tendermint和abci-cli開始測(cè)試

首先查詢一下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”

}

}

}

這里查詢出來(lái)的結(jié)果還是base64編碼的。

接下來(lái)轉(zhuǎn)賬:

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進(jìn)制,換算成十進(jìn)制就是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”

}

}

}

經(jīng)過base64解碼,ODM=就是83,MTE3就是117,測(cè)試成功!

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

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉