SlideShare a Scribd company logo
1 of 56
Download to read offline
Tendermint in a nutshell
Brought to you by Tyr Chen
1
What is Tendermint
• software to securely and consistently replicating an application on many
machines
• securely: works even if 1/3 or machines failed
• consistently: every non-faulty machine sees the same tx log and compute the same state
(deterministic)
• two parts:
• blockchain consensus engine: Tendermint Core, ensures that same tx are reordered on every
machine in same order
• generic application interface: ABCI, enables the tx to be processed in any programming language
2
Tendermint vs zookeep / etcd / consul
• untrust env vs trust env
• trust env cannot tolerant a single Byzantine fault
• BFT vs paxos / raft
• can survive < 1/3 failure vs < 1/2 failure
• state machine replication vs kv store on top of classical consensus algo
3
Tendermint vs bitcoin / ethereum
• PoS vs PoW
• focus on hosting arbitrary application states in a p2p BFT env
• can be used as PnP replacement for consensus engine
• other blockchain codebase could be run as an ABCI application using tendermint
consensus
• e.g. ethermint, cosmos
4
Tendermint vs fabric / burrow
• fabric: similar idea on consensus
• burrow: EVM + tendermint + name registry / permission / …
5
Application BlockChain Interface (ABCI)
6
Components in a blockchain app
• networking: for p2p connectivity and data replication
• mempool: for broadcasting tx
• consensus: for agreeing on most recent block
• storage: account states
• VM: for executing turning-complete contracts
• app logic: e.g. permissions
7
ABCI to decouple Tendermint core from app logic 8
What is ABCI?
• An interface that connects Tendermint consensus engine with application
• Socket based protocol
• types are defined in protobuf: ABCI
9
ABCI interface: API
service ABCIApplication {
rpc Echo(RequestEcho) returns (ResponseEcho) ;
rpc Flush(RequestFlush) returns (ResponseFlush);
rpc Info(RequestInfo) returns (ResponseInfo);
rpc SetOption(RequestSetOption) returns (ResponseSetOption);
rpc DeliverTx(RequestDeliverTx) returns (ResponseDeliverTx);
rpc CheckTx(RequestCheckTx) returns (ResponseCheckTx);
rpc Query(RequestQuery) returns (ResponseQuery);
rpc Commit(RequestCommit) returns (ResponseCommit);
rpc InitChain(RequestInitChain) returns (ResponseInitChain);
rpc BeginBlock(RequestBeginBlock) returns (ResponseBeginBlock);
rpc EndBlock(RequestEndBlock) returns (ResponseEndBlock);
}
10
ABCI interface: Block header
message Header {
// basic block info
string chain_id = 1 [(gogoproto.customname)="ChainID"];
int64 height = 2;
google.protobuf.Timestamp time = 3 [(gogoproto.nullable)=false, (gogoproto.stdtime)=true];
int64 num_txs = 4;
int64 total_txs = 5;
// prev block info
BlockID last_block_id = 6 [(gogoproto.nullable)=false];
// hashes of block data
bytes last_commit_hash = 7; // commit from validators from the last block
bytes data_hash = 8; // transactions
// hashes from the app output from the prev block
bytes validators_hash = 9; // validators for the current block
bytes next_validators_hash = 10; // validators for the next block
bytes consensus_hash = 11; // consensus params for current block
bytes app_hash = 12; // state after txs from the previous block
b t l t lt h h 13 // t h h f ll lt f th t f th i bl k
11
ABCI: Core APIs
• DeliverTx
• Each tx is delivered with this message
• application need to validate the tx received from DeliverTx against current state
• application then update the app state based on the tx
• CheckTx
• same as DeliverTx, but only do tx validation
• used by Tendermint Core
• mempool check the validity of tx with CheckTx
• relay valid tx to its peers
• Commit
• compute a cryptographic commitment to the current application state
12
Being deterministic
• without deterministic on tx processing on the p2p network, no consensus could be
formed.
• Things to consider:
• random number generators (without deterministic seeding)
• race conditions on threads (or avoiding threads altogether)
• the system clock (you can’t rely on it, e.g. the seed)
• uninitialized memory (in unsafe programming languages like C or C++)
• floating point arithmetic: floating point determinism
• same compiler
• same platform
• same CPU instruction set
• language features that are random (e.g. iteration on map)
13
Tendermint Consensus
14
Consensus State Transition 15
Concepts
• validators: the node who participants in the protocol, take turns proposing blocks
of transactions and voting on them
• height: the number of the block
• round: a turn that a validator gets to propose a block for the height
• propose: a validator generate a block for the height and broadcast it out for
validating
• prevote: if the proposed block is valid, the validator prevote the block and
broadcast prevote message out and also listen and accumulate the prevotes from
others
• precommit: if 2/3 of validators prevoted the block, the validator precommit the
block and broadcast precommit message out and also listen and accumulate the
precommits from others
• commit: if 2/3 of validators precommitted the block, the block is in committed state
16
Stake
• voting power: not all validators will have the same “weight” in the consensus protocol
• Proof-of-Stake: voting power is denominated in a native currency
17
Finally, tendermint in a nutshell 18
Tendermint: a close look
19
Installation
go get github.com/tendermint/tendermint
cd $GOPATH/src/github.com/tendermint/tendermint
make get_tools && make get_vendor_deps
make install
20
Initialization
$ tendermint version
0.25.0-0c9c329
$ tendermint init
I[10-16|15:56:12.579] Generated private validator module=main path=/Users/tchen/.tendermint/config/priv_va
I[10-16|15:56:12.580] Generated node key module=main path=/Users/tchen/.tendermint/config/node_ke
I[10-16|15:56:12.580] Generated genesis file module=main path=/Users/tchen/.tendermint/config/genesi
21
priv validator
{
"address": "CE925800DC1F3AF72721343A0FE67F18291632F7",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g="
},
"last_height": "0",
"last_round": "0",
"last_step": 0,
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "r2iHwBHUyBVnpQCq+4SHYm42MJbF9ZCdy3XlHvlCnEqVapcqzgzC9GT1dSj3ijMy3+2iAPitWtU04EofMIw72A=="
}
}
22
After running a while…
cat .tendermint/config/priv_validator.json
{
"address": "CE925800DC1F3AF72721343A0FE67F18291632F7",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g="
},
"last_height": "19",
"last_round": "0",
"last_step": 3,
"last_signature": "uPhO3W577w04CxJQ2G7TAO2mJk5k4uGbDGrHnGkxYOqmMtqsDRYA4NQCwWsOxORGOHWoUlS8F24MdASyBJalAA==",
"last_signbytes": "7B2240636861696E5F6964223A227479726368656E222C224074797065223A22766F7465222C22626C6F636B5F6964223A7B22
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "r2iHwBHUyBVnpQCq+4SHYm42MJbF9ZCdy3XlHvlCnEqVapcqzgzC9GT1dSj3ijMy3+2iAPitWtU04EofMIw72A=="
}
}
23
node key
{
"priv_key": {
"type": "tendermint/PrivKeyEd25519",
"value": "EgYVYbHnrqwucutP99yQ1ZN6i+xGfRn2AI576dZ0/uU/8a2XVgvwfc87vKSvUX49qS1uy6xrk35k3hPQKoHlQg=="
}
}
24
Genesis
• genesis_time: official time of blockchain start
• chain_id: unique id for the blockchain. Shall be less than 50 symbols
• validators: list of initial validators. Could be override by app
• pub_key: validator’s pubkey
• power: voting power
• app_hash: expected application hash (returned by ResponseInfo ABCI message), upon
genesis if app’s hash does not match, Tendermint will panic.
• app_state: application state, e.g. initial distribution of tokens
25
genesis le example
{
"genesis_time": "2018-10-16T22:56:12.580361Z",
"chain_id": "test-chain-iUo2E1", // must be unique
"consensus_params": {
"block_size_params": {
"max_bytes": "22020096",
"max_gas": "-1"
},
"evidence_params": {
"max_age": "100000"
}
},
"validators": [
{
"address": "CE925800DC1F3AF72721343A0FE67F18291632F7",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g="
},
"power": "10",
" " ""
26
Start tendermint
• cmd: tendermint node
• by default it connect to ABCI app on 127.0.0.1:26658
• for unix domain socket: do: `tendermint node –proxy_app/var/run/your-app.sock
• in-process app: counter , kvstore , nil
• apps provided by ex_abci: counter, simple_chain
27
Start tendermint example
$ tendermint node --proxy_app=kvstore // the in-process app
I[10-16|16:01:06.946] Starting multiAppConn module=proxy impl=multiAppConn
I[10-16|16:01:06.946] Starting localClient module=abci-client connection=query impl=localClient
I[10-16|16:01:06.946] Starting localClient module=abci-client connection=mempool impl=localClient
I[10-16|16:01:06.946] Starting localClient module=abci-client connection=consensus impl=localClient
I[10-16|16:01:06.946] ABCI Handshake module=consensus appHeight=0 appHash=
I[10-16|16:01:06.946] ABCI Replay Blocks module=consensus appHeight=0 storeHeight=0 stateHeight=0
I[10-16|16:01:06.947] Completed ABCI Handshake - Tendermint and App are synced module=consensus appHeight=0 appHash=
I[10-16|16:01:06.947] This node is a validator module=consensus addr=CE925800DC1F3AF72721343A0FE67F1829
I[10-16|16:01:06.952] P2P Node ID module=p2p ID=e7f354ef522497ba97722bf6658b4e06b1f390ab f
I[10-16|16:01:06.953] Starting Node module=main impl=Node
I[10-16|16:01:06.953] Starting EventBus module=events impl=EventBus
I[10-16|16:01:06.953] Starting RPC HTTP server on tcp://0.0.0.0:26657 module=rpc-server
I[10-16|16:01:06.953] Starting P2P Switch module=p2p impl="P2P Switch"
I[10-16|16:01:06.953] Starting MempoolReactor module=mempool impl=MempoolReactor
I[10-16|16:01:06.953] Starting BlockchainReactor module=blockchain impl=BlockchainReactor
I[10-16|16:01:06.953] Starting ConsensusReactor module=consensus impl=ConsensusReactor
I[10-16|16:01:06.953] ConsensusReactor module=consensus fastSync=false
I[10-16|16:01:06.953] Starting ConsensusState module=consensus impl=ConsensusState
I[10-16|16:01:06.953] Starting baseWAL module=consensus wal=/Users/tchen/.tendermint/data/cs.wa
I[10 16|16 01 06 954] St ti Ti tTi k d l i l Ti tTi k
28
What is WAL?
• Write-Ahead Logging
• Usage:
• to ensure data integration, usually used in database
• changes to data files must be written only after those changes has been logged (and flushed to
permanent storage)
• Pros:
• reduce disk writes significantly - we don’t need to flush data pages to disks every time something
changes
• easy and efficient to sync
• any changes to the data can be redone from the WAL
29
Tendermint consensus
I[10-16|16:01:07.954] Timed out module=consensus dur=997.249ms height=1 round=0 step=Round
I[10-16|16:01:07.955] enterNewRound(1/0). Current: 1/0/RoundStepNewHeight module=consensus height=1 round=0
I[10-16|16:01:07.955] enterPropose(1/0). Current: 1/0/RoundStepNewRound module=consensus height=1 round=0
I[10-16|16:01:07.955] enterPropose: Our turn to propose module=consensus height=1 round=0 proposer=CE925800DC1F3
I[10-16|16:01:07.962] Signed proposal module=consensus height=1 round=0 proposal="Proposal{1/0
I[10-16|16:01:07.963] Received proposal module=consensus proposal="Proposal{1/0 1:1A00E597FAD3
I[10-16|16:01:07.964] Received complete proposal block module=consensus height=1 hash=C896B7C7ACBB819C762F29BF
I[10-16|16:01:07.964] enterPrevote(1/0). Current: 1/0/RoundStepPropose module=consensus
I[10-16|16:01:07.964] enterPrevote: ProposalBlock is valid module=consensus height=1 round=0
I[10-16|16:01:07.965] Signed and pushed vote module=consensus height=1 round=0 vote="Vote{0:CE925800D
I[10-16|16:01:07.965] Added to prevote module=consensus vote="Vote{0:CE925800DC1F 1/00/1(Prevot
I[10-16|16:01:07.965] enterPrecommit(1/0). Current: 1/0/RoundStepPrevote module=consensus height=1 round=0
I[10-16|16:01:07.966] enterPrecommit: +2/3 prevoted proposal block. Locking module=consensus height=1 round=0 hash=C896B7C7
I[10-16|16:01:07.966] Signed and pushed vote module=consensus height=1 round=0 vote="Vote{0:CE925800D
I[10-16|16:01:07.967] Added to precommit module=consensus vote="Vote{0:CE925800DC1F 1/00/2(Precom
I[10-16|16:01:07.967] enterCommit(1/0). Current: 1/0/RoundStepPrecommit module=consensus height=1 commitRound=0
I[10-16|16:01:07.968] Commit is for locked block. Set ProposalBlock=LockedBlock module=consensus height=1 commitRound=0 blo
I[10-16|16:01:07.968] Finalizing commit of block with 0 txs module=consensus height=1 hash=C896B7C7ACBB819C762F29BF
30
Tendermint generate block
I[10-16|16:01:07.968] Block{
Header{
ChainID: test-chain-iUo2E1
Height: 1
Time: 2018-10-16 23:01:07.955323 +0000 UTC
NumTxs: 0
TotalTxs: 0
LastBlockID: :0:000000000000
LastCommit:
Data:
Validators: 40B38DD6331AA92F194295AE613E13109D0B019B
NextValidators: 40B38DD6331AA92F194295AE613E13109D0B019B
App:
Consensus: 0E520AF30D47BE28F293E040E418D0361BFB5370
Results:
Evidence:
Proposer: CE925800DC1F3AF72721343A0FE67F18291632F7
}#C896B7C7ACBB819C762F29BF541B8FE00CDCC8D0
Data{
}#
31
Data folder
$ ls -l ~/.tendermint/data/
total 0
drwxr-xr-x 9 tchen staff 288 Oct 16 16:01 blockstore.db
drwx------ 3 tchen staff 96 Oct 16 16:01 cs.wal
drwxr-xr-x 8 tchen staff 256 Oct 16 16:01 evidence.db
drwx------ 3 tchen staff 96 Oct 16 16:01 mempool.wal
drwxr-xr-x 10 tchen staff 320 Oct 16 16:01 state.db
drwxr-xr-x 8 tchen staff 256 Oct 16 16:01 tx_index.db
32
RPC status
$ curl -s localhost:26657/status |jq
{
"jsonrpc": "2.0",
"id": "",
"result": {
"node_info": {
"id": "e7f354ef522497ba97722bf6658b4e06b1f390ab",
"listen_addr": "tcp://0.0.0.0:26656",
"network": "test-chain-iUo2E1",
"version": "0.25.0-0c9c3292",
"channels": "4020212223303800",
"moniker": "ip-10-0-1-9.us-west-2.compute.internal",
"other": {
"amino_version": "0.12.0",
"p2p_version": "0.5.0",
"consensus_version": "v1/0.2.2",
"rpc_version": "0.7.0/3",
"tx_index": "on",
"rpc_address": "tcp://0.0.0.0:26657"
}
}
33
Send transaction
curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"'
{
"jsonrpc": "2.0",
"id": "",
"result": {
"check_tx": {
"gasWanted": "1"
},
"deliver_tx": {
"tags": [
{
"key": "YXBwLmNyZWF0b3I=", // app.creator
"value": "amFl" // jae
},
{
"key": "YXBwLmtleQ==", // app.key
"value": "YWJjZA==" // abcd
}
]
},
"h h" "88D4266FD4E6338D13B845FCF289579D209C8978"
34
Transaction in block
I[10-16|16:20:37.273] Timed out module=consensus dur=997.228ms height=114 round=0 step=Ro
I[10-16|16:20:37.273] enterNewRound(114/0). Current: 114/0/RoundStepNewHeight module=consensus height=114 round=0
I[10-16|16:20:37.273] enterPropose(114/0). Current: 114/0/RoundStepNewRound module=consensus height=114 round=0
I[10-16|16:20:37.273] enterPropose: Our turn to propose module=consensus height=114 round=0 proposer=CE925800DC
I[10-16|16:20:37.276] Signed proposal module=consensus height=114 round=0 proposal="Proposal{
I[10-16|16:20:37.277] Received proposal module=consensus proposal="Proposal{114/0 1:8A667F8EB44D
I[10-16|16:20:37.278] Received complete proposal block module=consensus height=114 hash=CFDA9B2C639A5F974A62287
I[10-16|16:20:37.278] enterPrevote(114/0). Current: 114/0/RoundStepPropose module=consensus
I[10-16|16:20:37.278] enterPrevote: ProposalBlock is valid module=consensus height=114 round=0
I[10-16|16:20:37.279] Signed and pushed vote module=consensus height=114 round=0 vote="Vote{0:CE92580
I[10-16|16:20:37.280] Added to prevote module=consensus vote="Vote{0:CE925800DC1F 114/00/1(Prev
I[10-16|16:20:37.280] enterPrecommit(114/0). Current: 114/0/RoundStepPrevote module=consensus height=114 round=0
I[10-16|16:20:37.280] enterPrecommit: +2/3 prevoted proposal block. Locking module=consensus height=114 round=0 hash=CFDA9B
I[10-16|16:20:37.281] Signed and pushed vote module=consensus height=114 round=0 vote="Vote{0:CE92580
I[10-16|16:20:37.282] Added to precommit module=consensus vote="Vote{0:CE925800DC1F 114/00/2(Prec
I[10-16|16:20:37.282] enterCommit(114/0). Current: 114/0/RoundStepPrecommit module=consensus height=114 commitRound=0
I[10-16|16:20:37.282] Commit is for locked block. Set ProposalBlock=LockedBlock module=consensus height=114 commitRound=0
I[10-16|16:20:37.283] Finalizing commit of block with 1 txs module=consensus height=114 hash=CFDA9B2C639A5F974A62287
I[10-16|16:20:37.283] Block{
Header{
Ch i ID t t h i iU 2E1
35
Query the tx
$ curl -s 'localhost:26657/abci_query?data="abcd"' | jq
{
"jsonrpc": "2.0",
"id": "",
"result": {
"response": {
"log": "exists",
"index": "-1",
"key": "YWJjZA==",
"value": "YWJjZA=="
}
}
}
36
Send tx with key/value
$ curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"'
{
"jsonrpc": "2.0",
"id": "",
"result": {
"check_tx": {
"gasWanted": "1"
},
"deliver_tx": {
"tags": [
{
"key": "YXBwLmNyZWF0b3I=",
"value": "amFl"
},
{
"key": "YXBwLmtleQ==",
"value": "bmFtZQ=="
}
]
},
"h h" "57D835FBBA0DBF922D8A2EDA56922C9B24E77609"
37
Query the key
$ curl -s 'localhost:26657/abci_query?data="name"'
{
"jsonrpc": "2.0",
"id": "",
"result": {
"response": {
"log": "exists",
"index": "-1",
"key": "bmFtZQ==", // name
"value": "c2F0b3NoaQ==" // satoshi
}
}
}
38
Stopping tendermint
captured interrupt, exiting...
I[10-16|16:31:39.538] Stopping Node module=main impl=Node
I[10-16|16:31:39.538] Stopping Node module=main
I[10-16|16:31:39.538] Stopping EventBus module=events impl=EventBus
E[10-16|16:31:39.538] Not stopping PubSub -- have not been started yet module=pubsub impl=PubSub
I[10-16|16:31:39.538] Stopping IndexerService module=txindex impl=IndexerService
I[10-16|16:31:39.538] Stopping P2P Switch module=p2p impl="P2P Switch"
I[10-16|16:31:39.538] Stopping ConsensusReactor module=consensus impl=ConsensusReactor
I[10-16|16:31:39.538] Stopping ConsensusState module=consensus impl=ConsensusState
I[10-16|16:31:39.538] Stopping TimeoutTicker module=consensus impl=TimeoutTicker
I[10-16|16:31:39.538] Stopping baseWAL module=consensus wal=/Users/tchen/.tendermint/data/cs.wa
I[10-16|16:31:39.544] Stopping EvidenceReactor module=evidence impl=EvidenceReactor
I[10-16|16:31:39.544] Stopping PEXReactor module=p2p impl=PEXReactor
I[10-16|16:31:39.544] Stopping AddrBook module=p2p book=/Users/tchen/.tendermint/config/addrboo
I[10-16|16:31:39.544] Stopping MempoolReactor module=mempool impl=MempoolReactor
I[10-16|16:31:39.544] Stopping BlockchainReactor module=blockchain impl=BlockchainReactor
E[10-16|16:31:39.544] Not stopping BlockPool -- have not been started yet module=blockchain impl=BlockPool
I[10-16|16:31:39.544] Saving AddrBook to file module=p2p book=/Users/tchen/.tendermint/config/addrboo
E[10-16|16:31:39.544] Stopped accept routine, as transport is closed module=p2p numPeers=0
I[10-16|16:31:39.544] Closing rpc listener module=main listener="&{Listener:0xc0017362d8 sem:0xc000
I[10 16|16 31 39 544] RPC HTTP t d d l " t t [ ] 26657 f l
39
Real world application: Build a simpli ed ethereum
40
Requirement
• just replicate ethereum’s MPT based account system
• A wallet consists of a key pair that user can send tokens
• Tendermint as the consensus layer
• no EVM to execute smart contracts
• the whole app state is changed tx after tx, and the app state of the last tx of a block is
committed back to tendermint as app root
41
Implementation
• ex_abci
• gen_server for handling: info, check_tx, begin_block, deliver_tx, end_block, commit,
flush
• merkle_patricia_tree to handle MPT (using LevelDB)
• keccakf1600_orig for sha3
• libsecp256k1 for signing and verifying
42
Data structure
message Transaction {
bytes from = 1;
bytes to = 2;
uint64 nonce = 3;
uint64 total = 4;
bytes pub_key = 5;
bytes signature = 6;
}
message AccountState {
uint64 balance = 1;
uint64 nonce = 2;
uint64 num_txs = 3;
}
43
MPT
• Purpose: Maintain the Merkle Patricia Tree
• Interfaces:
• open: open a level db and restore last committed root_hash, if any
• put: update a state for a key (account address) to the trie
• get: get a state from a key from the trie
• update_block: update last_block height, and put the root_hash for the height
• get_app_hash: get the root_hash for the last block
• get_info: retrieve last_block, last_block_hash
44
Account
• Purpose: Maintain account system
• Interfaces:
• get: get the state of the account in the world state
• put: update the state to the account in the world state
45
Tx
• Purpose: sign and verify transactions
• Interfaces:
• sign: calculate the sha3 of the tx and then do ecdsa sign
• verify: calculate the sha3 of the tx and then verify against the signature
46
Wallet
• Purpose: create wallet, create tx and retrieve the state from the chain, acting as a
client library
• Interfaces:
• new: create a new wallet with key pair and wallet address, using sha3 and ecdsa
• declare: a special transaction that could get 10000 genesis tokens. For test purpose.
• transfer: move tokens between accounts, tx will be signed by sender. Validator will verify the
signature and make sure these conditions are satisfied:
• from / to are not the same address
• nonce is expected
• balance is greater or equal to the total tokens in the tx
• balance: get balance of an address
• nonce: get nonce of an address
• info: get info of an address
• chain_info: get the status of the chain
47
Tendermint callbacks: Server
48
Handle info
def handle_call({:handle_info, request}, _from, %{trie: trie} = state) do
last_block = Mpt.get_last_block(trie)
app_hash = Mpt.get_app_hash(trie)
Logger.info(
"Tendermint version: #{request.version}, last_block: #{last_block}, app_hash: #{
Base.encode16(app_hash)
}"
)
response = %Abci.ResponseInfo{
data: "Elixir SimpleChain",
version: "1.0.0",
last_block_height: last_block,
last_block_app_hash: app_hash
}
{:reply, response, state}
end
49
Handle check_tx
def handle_call({:handle_check_tx, request}, _from, %{trie: trie} = state) do
%Abci.RequestCheckTx{tx: data} = request
tx = data |> Base.decode64!() |> Transaction.decode()
Logger.debug(fn -> "Check tx: #{inspect(tx)}" end)
response = struct(Abci.ResponseCheckTx, verify(tx, trie, data))
{:reply, response, state}
end
50
Handle deliver_tx
def handle_call({:handle_deliver_tx, request}, _from, %{trie: trie} = state) do
%Abci.RequestDeliverTx{tx: data} = request
tx = data |> Base.decode64!() |> Transaction.decode()
Logger.debug(fn -> "Deliver tx: #{inspect(tx)}" end)
response = struct(Abci.ResponseDeliverTx, verify(tx, trie, data))
trie =
case response.code == 0 do
true -> update_state(tx, trie)
_ -> trie
end
{:reply, response, %{state | trie: trie}}
end
51
Handle end_block
def handle_call({:handle_end_block, request}, _from, %{trie: trie} = state) do
height = request.height
Logger.debug(fn -> "End block: #{height}" end)
Mpt.update_block(trie, request.height)
response = %Abci.ResponseEndBlock{
validator_updates: [],
tags: []
}
{:reply, response, state}
end
52
Handle commit
def handle_call({:handle_commit, request}, _from, %{trie: trie} = state) do
Logger.debug(fn -> "Commit block: #{inspect(request)}" end)
response = %Abci.ResponseCommit{
data: Mpt.get_app_hash(trie)
}
{:reply, response, state}
end
53
How do we update world state?
defp update_state(%Transaction{from: from, to: to, nonce: n1, total: total}, trie) do
acc1 = Account.get(trie, from)
trie =
Account.put(trie, from, %AccountState{
acc1
| nonce: n1 + 1,
balance: acc1.balance - total,
num_txs: acc1.num_txs + 1
})
acc2 =
case Account.get(trie, to) do
nil -> %AccountState{nonce: 0, balance: total, num_txs: 1}
v -> %AccountState{v | balance: v.balance + total, num_txs: v.num_txs + 1}
end
Account.put(trie, to, acc2)
end
54
Demo: Final Chapter
55
56

More Related Content

What's hot

Kubernetes Networking with Cilium - Deep Dive
Kubernetes Networking with Cilium - Deep DiveKubernetes Networking with Cilium - Deep Dive
Kubernetes Networking with Cilium - Deep DiveMichal Rostecki
 
Zookeeper 활용 nifi clustering
Zookeeper 활용 nifi clusteringZookeeper 활용 nifi clustering
Zookeeper 활용 nifi clusteringNoahKIM36
 
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)Kai Wähner
 
Blockchain Intro to Hyperledger Fabric
Blockchain Intro to Hyperledger Fabric Blockchain Intro to Hyperledger Fabric
Blockchain Intro to Hyperledger Fabric Araf Karsh Hamid
 
Bitcoin, Ethereum, Smart Contract & Blockchain
Bitcoin, Ethereum, Smart Contract & BlockchainBitcoin, Ethereum, Smart Contract & Blockchain
Bitcoin, Ethereum, Smart Contract & BlockchainJitendra Chittoda
 
Understanding private blockchains
Understanding private blockchainsUnderstanding private blockchains
Understanding private blockchainsCoin Sciences Ltd
 
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...Jitendra Bafna
 
Blockchain Explorer
Blockchain ExplorerBlockchain Explorer
Blockchain ExplorerRihusoft
 
An Introduction to Confluent Cloud: Apache Kafka as a Service
An Introduction to Confluent Cloud: Apache Kafka as a ServiceAn Introduction to Confluent Cloud: Apache Kafka as a Service
An Introduction to Confluent Cloud: Apache Kafka as a Serviceconfluent
 
How Blockchain Works - 블록체인의 원리
How Blockchain Works - 블록체인의 원리How Blockchain Works - 블록체인의 원리
How Blockchain Works - 블록체인의 원리SeokWon Kim
 
Delight: An Improved Apache Spark UI, Free, and Cross-Platform
Delight: An Improved Apache Spark UI, Free, and Cross-PlatformDelight: An Improved Apache Spark UI, Free, and Cross-Platform
Delight: An Improved Apache Spark UI, Free, and Cross-PlatformDatabricks
 
Learning Solidity
Learning SolidityLearning Solidity
Learning SolidityArnold Pham
 
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...StreamNative
 
Developing applications with Hyperledger Fabric SDK
Developing applications with Hyperledger Fabric SDKDeveloping applications with Hyperledger Fabric SDK
Developing applications with Hyperledger Fabric SDKHorea Porutiu
 
Hyperledger Fabric in a Nutshell
Hyperledger Fabric in a NutshellHyperledger Fabric in a Nutshell
Hyperledger Fabric in a NutshellDaniel Chan
 
Blockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricBlockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricAraf Karsh Hamid
 
Blockchain Interoperability using Cosmos Interblockchain Communication
Blockchain Interoperability using Cosmos Interblockchain CommunicationBlockchain Interoperability using Cosmos Interblockchain Communication
Blockchain Interoperability using Cosmos Interblockchain CommunicationChjango Unchained
 

What's hot (20)

Kubernetes Networking with Cilium - Deep Dive
Kubernetes Networking with Cilium - Deep DiveKubernetes Networking with Cilium - Deep Dive
Kubernetes Networking with Cilium - Deep Dive
 
Zookeeper 활용 nifi clustering
Zookeeper 활용 nifi clusteringZookeeper 활용 nifi clustering
Zookeeper 활용 nifi clustering
 
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)
Apache Kafka as Data Hub for Crypto, NFT, Metaverse (Beyond the Buzz!)
 
Blockchain Intro to Hyperledger Fabric
Blockchain Intro to Hyperledger Fabric Blockchain Intro to Hyperledger Fabric
Blockchain Intro to Hyperledger Fabric
 
Bitcoin, Ethereum, Smart Contract & Blockchain
Bitcoin, Ethereum, Smart Contract & BlockchainBitcoin, Ethereum, Smart Contract & Blockchain
Bitcoin, Ethereum, Smart Contract & Blockchain
 
Understanding private blockchains
Understanding private blockchainsUnderstanding private blockchains
Understanding private blockchains
 
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...
MuleSoft Surat Meetup#45 - Anypoint Flex Gateway as a Kubernetes Ingress Cont...
 
Blockchain Explorer
Blockchain ExplorerBlockchain Explorer
Blockchain Explorer
 
An Introduction to Confluent Cloud: Apache Kafka as a Service
An Introduction to Confluent Cloud: Apache Kafka as a ServiceAn Introduction to Confluent Cloud: Apache Kafka as a Service
An Introduction to Confluent Cloud: Apache Kafka as a Service
 
Hyperledger
HyperledgerHyperledger
Hyperledger
 
How Blockchain Works - 블록체인의 원리
How Blockchain Works - 블록체인의 원리How Blockchain Works - 블록체인의 원리
How Blockchain Works - 블록체인의 원리
 
Delight: An Improved Apache Spark UI, Free, and Cross-Platform
Delight: An Improved Apache Spark UI, Free, and Cross-PlatformDelight: An Improved Apache Spark UI, Free, and Cross-Platform
Delight: An Improved Apache Spark UI, Free, and Cross-Platform
 
Ethereum
EthereumEthereum
Ethereum
 
Learning Solidity
Learning SolidityLearning Solidity
Learning Solidity
 
Kafka 101
Kafka 101Kafka 101
Kafka 101
 
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...
Streaming Millions of Contact Center Interactions in (Near) Real-Time with Pu...
 
Developing applications with Hyperledger Fabric SDK
Developing applications with Hyperledger Fabric SDKDeveloping applications with Hyperledger Fabric SDK
Developing applications with Hyperledger Fabric SDK
 
Hyperledger Fabric in a Nutshell
Hyperledger Fabric in a NutshellHyperledger Fabric in a Nutshell
Hyperledger Fabric in a Nutshell
 
Blockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricBlockchain - HyperLedger Fabric
Blockchain - HyperLedger Fabric
 
Blockchain Interoperability using Cosmos Interblockchain Communication
Blockchain Interoperability using Cosmos Interblockchain CommunicationBlockchain Interoperability using Cosmos Interblockchain Communication
Blockchain Interoperability using Cosmos Interblockchain Communication
 

Similar to Tendermint in a nutshell: A close look at consensus

Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to EthereumArnold Pham
 
Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Sneha Inguva
 
Combining Phase Identification and Statistic Modeling for Automated Parallel ...
Combining Phase Identification and Statistic Modeling for Automated Parallel ...Combining Phase Identification and Statistic Modeling for Automated Parallel ...
Combining Phase Identification and Statistic Modeling for Automated Parallel ...Mingliang Liu
 
September Ethereum Berlin Workshop
September Ethereum Berlin WorkshopSeptember Ethereum Berlin Workshop
September Ethereum Berlin Workshopaeronbuchanan
 
Programming smart contracts in solidity
Programming smart contracts in solidityProgramming smart contracts in solidity
Programming smart contracts in solidityEmanuel Mota
 
Communication in Python and the C10k problem
Communication in Python and the C10k problemCommunication in Python and the C10k problem
Communication in Python and the C10k problemJose Galarza
 
GEO mapbox geo_api_develop2 Intro
 GEO mapbox geo_api_develop2 Intro GEO mapbox geo_api_develop2 Intro
GEO mapbox geo_api_develop2 IntroMax Kleiner
 
Facebook C++网络库Wangle调研
Facebook C++网络库Wangle调研Facebook C++网络库Wangle调研
Facebook C++网络库Wangle调研vorfeed chen
 
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereum
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereumDappsmedia smartcontract _write_smartcontracts_on_console_ethereum
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereumTomoaki Sato
 
ql.io: Consuming HTTP at Scale
ql.io: Consuming HTTP at Scale ql.io: Consuming HTTP at Scale
ql.io: Consuming HTTP at Scale Subbu Allamaraju
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...Alexandre Moneger
 
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CanSecWest
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Ovidiu Farauanu
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin敬倫 林
 
EthereumBlockchainMarch3 (1).pptx
EthereumBlockchainMarch3 (1).pptxEthereumBlockchainMarch3 (1).pptx
EthereumBlockchainMarch3 (1).pptxWijdenBenothmen1
 
Track c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eveTrack c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -evechiportal
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomyDongmin Yu
 
CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsTim Burks
 
Blockchain meets database
Blockchain meets databaseBlockchain meets database
Blockchain meets databaseYongraeJo
 

Similar to Tendermint in a nutshell: A close look at consensus (20)

Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to Ethereum
 
Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)Networking and Go: An Engineer's Journey (Strangeloop 2019)
Networking and Go: An Engineer's Journey (Strangeloop 2019)
 
WCM Transfer Services
WCM Transfer Services WCM Transfer Services
WCM Transfer Services
 
Combining Phase Identification and Statistic Modeling for Automated Parallel ...
Combining Phase Identification and Statistic Modeling for Automated Parallel ...Combining Phase Identification and Statistic Modeling for Automated Parallel ...
Combining Phase Identification and Statistic Modeling for Automated Parallel ...
 
September Ethereum Berlin Workshop
September Ethereum Berlin WorkshopSeptember Ethereum Berlin Workshop
September Ethereum Berlin Workshop
 
Programming smart contracts in solidity
Programming smart contracts in solidityProgramming smart contracts in solidity
Programming smart contracts in solidity
 
Communication in Python and the C10k problem
Communication in Python and the C10k problemCommunication in Python and the C10k problem
Communication in Python and the C10k problem
 
GEO mapbox geo_api_develop2 Intro
 GEO mapbox geo_api_develop2 Intro GEO mapbox geo_api_develop2 Intro
GEO mapbox geo_api_develop2 Intro
 
Facebook C++网络库Wangle调研
Facebook C++网络库Wangle调研Facebook C++网络库Wangle调研
Facebook C++网络库Wangle调研
 
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereum
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereumDappsmedia smartcontract _write_smartcontracts_on_console_ethereum
Dappsmedia smartcontract _write_smartcontracts_on_console_ethereum
 
ql.io: Consuming HTTP at Scale
ql.io: Consuming HTTP at Scale ql.io: Consuming HTTP at Scale
ql.io: Consuming HTTP at Scale
 
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
BSides LV 2016 - Beyond the tip of the iceberg - fuzzing binary protocols for...
 
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
CSW2017 Henry li how to find the vulnerability to bypass the control flow gua...
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin
 
EthereumBlockchainMarch3 (1).pptx
EthereumBlockchainMarch3 (1).pptxEthereumBlockchainMarch3 (1).pptx
EthereumBlockchainMarch3 (1).pptx
 
Track c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eveTrack c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eve
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
 
Blockchain meets database
Blockchain meets databaseBlockchain meets database
Blockchain meets database
 

More from ArcBlock

ArcBlock Introduction to Blockchain
ArcBlock Introduction to BlockchainArcBlock Introduction to Blockchain
ArcBlock Introduction to BlockchainArcBlock
 
Forge blockchain deployment made easy
Forge  blockchain deployment made easyForge  blockchain deployment made easy
Forge blockchain deployment made easyArcBlock
 
Designing Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable TokensDesigning Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable TokensArcBlock
 
Build a Decentralized, public verifiable Database with ex_abci and Tendermint
Build a Decentralized, public verifiable Database with ex_abci and TendermintBuild a Decentralized, public verifiable Database with ex_abci and Tendermint
Build a Decentralized, public verifiable Database with ex_abci and TendermintArcBlock
 
ArcBlock Presents 5 Winning Factors to Building a Successful DApp
ArcBlock Presents 5 Winning Factors to Building a Successful DAppArcBlock Presents 5 Winning Factors to Building a Successful DApp
ArcBlock Presents 5 Winning Factors to Building a Successful DAppArcBlock
 
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity Verification
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity VerificationQRCodes are Fun, Easy, and Useful for Links, Payments and Identity Verification
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity VerificationArcBlock
 
Designing Decentralized Applications (DApps)
Designing Decentralized Applications (DApps) Designing Decentralized Applications (DApps)
Designing Decentralized Applications (DApps) ArcBlock
 
Cryptography for everyone
Cryptography for everyoneCryptography for everyone
Cryptography for everyoneArcBlock
 
Introduction to HTTP/2 and How To Use It
Introduction to HTTP/2 and How To Use ItIntroduction to HTTP/2 and How To Use It
Introduction to HTTP/2 and How To Use ItArcBlock
 
IPFS: A Whole New World
IPFS: A Whole New WorldIPFS: A Whole New World
IPFS: A Whole New WorldArcBlock
 
Ethereum virtual machine for Developers Part 1
Ethereum virtual machine for Developers Part 1Ethereum virtual machine for Developers Part 1
Ethereum virtual machine for Developers Part 1ArcBlock
 
Understanding hd wallets design and implementation
Understanding hd wallets  design and implementationUnderstanding hd wallets  design and implementation
Understanding hd wallets design and implementationArcBlock
 
Technical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitTechnical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitArcBlock
 
Introduction to CQRS & Commended
Introduction to CQRS & CommendedIntroduction to CQRS & Commended
Introduction to CQRS & CommendedArcBlock
 
Decipher Multi-Factor Authentication - A Developers Introduction
Decipher Multi-Factor Authentication - A Developers IntroductionDecipher Multi-Factor Authentication - A Developers Introduction
Decipher Multi-Factor Authentication - A Developers IntroductionArcBlock
 
Introduction to aws data pipeline services
Introduction to aws data pipeline servicesIntroduction to aws data pipeline services
Introduction to aws data pipeline servicesArcBlock
 
Introduction to Ethereum Smart Contracts
Introduction to Ethereum Smart Contracts Introduction to Ethereum Smart Contracts
Introduction to Ethereum Smart Contracts ArcBlock
 
ArcBlock Presents An Introduction to Blockchain
ArcBlock Presents An Introduction to BlockchainArcBlock Presents An Introduction to Blockchain
ArcBlock Presents An Introduction to BlockchainArcBlock
 

More from ArcBlock (18)

ArcBlock Introduction to Blockchain
ArcBlock Introduction to BlockchainArcBlock Introduction to Blockchain
ArcBlock Introduction to Blockchain
 
Forge blockchain deployment made easy
Forge  blockchain deployment made easyForge  blockchain deployment made easy
Forge blockchain deployment made easy
 
Designing Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable TokensDesigning Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable Tokens
 
Build a Decentralized, public verifiable Database with ex_abci and Tendermint
Build a Decentralized, public verifiable Database with ex_abci and TendermintBuild a Decentralized, public verifiable Database with ex_abci and Tendermint
Build a Decentralized, public verifiable Database with ex_abci and Tendermint
 
ArcBlock Presents 5 Winning Factors to Building a Successful DApp
ArcBlock Presents 5 Winning Factors to Building a Successful DAppArcBlock Presents 5 Winning Factors to Building a Successful DApp
ArcBlock Presents 5 Winning Factors to Building a Successful DApp
 
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity Verification
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity VerificationQRCodes are Fun, Easy, and Useful for Links, Payments and Identity Verification
QRCodes are Fun, Easy, and Useful for Links, Payments and Identity Verification
 
Designing Decentralized Applications (DApps)
Designing Decentralized Applications (DApps) Designing Decentralized Applications (DApps)
Designing Decentralized Applications (DApps)
 
Cryptography for everyone
Cryptography for everyoneCryptography for everyone
Cryptography for everyone
 
Introduction to HTTP/2 and How To Use It
Introduction to HTTP/2 and How To Use ItIntroduction to HTTP/2 and How To Use It
Introduction to HTTP/2 and How To Use It
 
IPFS: A Whole New World
IPFS: A Whole New WorldIPFS: A Whole New World
IPFS: A Whole New World
 
Ethereum virtual machine for Developers Part 1
Ethereum virtual machine for Developers Part 1Ethereum virtual machine for Developers Part 1
Ethereum virtual machine for Developers Part 1
 
Understanding hd wallets design and implementation
Understanding hd wallets  design and implementationUnderstanding hd wallets  design and implementation
Understanding hd wallets design and implementation
 
Technical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitTechnical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnit
 
Introduction to CQRS & Commended
Introduction to CQRS & CommendedIntroduction to CQRS & Commended
Introduction to CQRS & Commended
 
Decipher Multi-Factor Authentication - A Developers Introduction
Decipher Multi-Factor Authentication - A Developers IntroductionDecipher Multi-Factor Authentication - A Developers Introduction
Decipher Multi-Factor Authentication - A Developers Introduction
 
Introduction to aws data pipeline services
Introduction to aws data pipeline servicesIntroduction to aws data pipeline services
Introduction to aws data pipeline services
 
Introduction to Ethereum Smart Contracts
Introduction to Ethereum Smart Contracts Introduction to Ethereum Smart Contracts
Introduction to Ethereum Smart Contracts
 
ArcBlock Presents An Introduction to Blockchain
ArcBlock Presents An Introduction to BlockchainArcBlock Presents An Introduction to Blockchain
ArcBlock Presents An Introduction to Blockchain
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

Tendermint in a nutshell: A close look at consensus

  • 1. Tendermint in a nutshell Brought to you by Tyr Chen 1
  • 2. What is Tendermint • software to securely and consistently replicating an application on many machines • securely: works even if 1/3 or machines failed • consistently: every non-faulty machine sees the same tx log and compute the same state (deterministic) • two parts: • blockchain consensus engine: Tendermint Core, ensures that same tx are reordered on every machine in same order • generic application interface: ABCI, enables the tx to be processed in any programming language 2
  • 3. Tendermint vs zookeep / etcd / consul • untrust env vs trust env • trust env cannot tolerant a single Byzantine fault • BFT vs paxos / raft • can survive < 1/3 failure vs < 1/2 failure • state machine replication vs kv store on top of classical consensus algo 3
  • 4. Tendermint vs bitcoin / ethereum • PoS vs PoW • focus on hosting arbitrary application states in a p2p BFT env • can be used as PnP replacement for consensus engine • other blockchain codebase could be run as an ABCI application using tendermint consensus • e.g. ethermint, cosmos 4
  • 5. Tendermint vs fabric / burrow • fabric: similar idea on consensus • burrow: EVM + tendermint + name registry / permission / … 5
  • 7. Components in a blockchain app • networking: for p2p connectivity and data replication • mempool: for broadcasting tx • consensus: for agreeing on most recent block • storage: account states • VM: for executing turning-complete contracts • app logic: e.g. permissions 7
  • 8. ABCI to decouple Tendermint core from app logic 8
  • 9. What is ABCI? • An interface that connects Tendermint consensus engine with application • Socket based protocol • types are defined in protobuf: ABCI 9
  • 10. ABCI interface: API service ABCIApplication { rpc Echo(RequestEcho) returns (ResponseEcho) ; rpc Flush(RequestFlush) returns (ResponseFlush); rpc Info(RequestInfo) returns (ResponseInfo); rpc SetOption(RequestSetOption) returns (ResponseSetOption); rpc DeliverTx(RequestDeliverTx) returns (ResponseDeliverTx); rpc CheckTx(RequestCheckTx) returns (ResponseCheckTx); rpc Query(RequestQuery) returns (ResponseQuery); rpc Commit(RequestCommit) returns (ResponseCommit); rpc InitChain(RequestInitChain) returns (ResponseInitChain); rpc BeginBlock(RequestBeginBlock) returns (ResponseBeginBlock); rpc EndBlock(RequestEndBlock) returns (ResponseEndBlock); } 10
  • 11. ABCI interface: Block header message Header { // basic block info string chain_id = 1 [(gogoproto.customname)="ChainID"]; int64 height = 2; google.protobuf.Timestamp time = 3 [(gogoproto.nullable)=false, (gogoproto.stdtime)=true]; int64 num_txs = 4; int64 total_txs = 5; // prev block info BlockID last_block_id = 6 [(gogoproto.nullable)=false]; // hashes of block data bytes last_commit_hash = 7; // commit from validators from the last block bytes data_hash = 8; // transactions // hashes from the app output from the prev block bytes validators_hash = 9; // validators for the current block bytes next_validators_hash = 10; // validators for the next block bytes consensus_hash = 11; // consensus params for current block bytes app_hash = 12; // state after txs from the previous block b t l t lt h h 13 // t h h f ll lt f th t f th i bl k 11
  • 12. ABCI: Core APIs • DeliverTx • Each tx is delivered with this message • application need to validate the tx received from DeliverTx against current state • application then update the app state based on the tx • CheckTx • same as DeliverTx, but only do tx validation • used by Tendermint Core • mempool check the validity of tx with CheckTx • relay valid tx to its peers • Commit • compute a cryptographic commitment to the current application state 12
  • 13. Being deterministic • without deterministic on tx processing on the p2p network, no consensus could be formed. • Things to consider: • random number generators (without deterministic seeding) • race conditions on threads (or avoiding threads altogether) • the system clock (you can’t rely on it, e.g. the seed) • uninitialized memory (in unsafe programming languages like C or C++) • floating point arithmetic: floating point determinism • same compiler • same platform • same CPU instruction set • language features that are random (e.g. iteration on map) 13
  • 16. Concepts • validators: the node who participants in the protocol, take turns proposing blocks of transactions and voting on them • height: the number of the block • round: a turn that a validator gets to propose a block for the height • propose: a validator generate a block for the height and broadcast it out for validating • prevote: if the proposed block is valid, the validator prevote the block and broadcast prevote message out and also listen and accumulate the prevotes from others • precommit: if 2/3 of validators prevoted the block, the validator precommit the block and broadcast precommit message out and also listen and accumulate the precommits from others • commit: if 2/3 of validators precommitted the block, the block is in committed state 16
  • 17. Stake • voting power: not all validators will have the same “weight” in the consensus protocol • Proof-of-Stake: voting power is denominated in a native currency 17
  • 18. Finally, tendermint in a nutshell 18
  • 20. Installation go get github.com/tendermint/tendermint cd $GOPATH/src/github.com/tendermint/tendermint make get_tools && make get_vendor_deps make install 20
  • 21. Initialization $ tendermint version 0.25.0-0c9c329 $ tendermint init I[10-16|15:56:12.579] Generated private validator module=main path=/Users/tchen/.tendermint/config/priv_va I[10-16|15:56:12.580] Generated node key module=main path=/Users/tchen/.tendermint/config/node_ke I[10-16|15:56:12.580] Generated genesis file module=main path=/Users/tchen/.tendermint/config/genesi 21
  • 22. priv validator { "address": "CE925800DC1F3AF72721343A0FE67F18291632F7", "pub_key": { "type": "tendermint/PubKeyEd25519", "value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g=" }, "last_height": "0", "last_round": "0", "last_step": 0, "priv_key": { "type": "tendermint/PrivKeyEd25519", "value": "r2iHwBHUyBVnpQCq+4SHYm42MJbF9ZCdy3XlHvlCnEqVapcqzgzC9GT1dSj3ijMy3+2iAPitWtU04EofMIw72A==" } } 22
  • 23. After running a while… cat .tendermint/config/priv_validator.json { "address": "CE925800DC1F3AF72721343A0FE67F18291632F7", "pub_key": { "type": "tendermint/PubKeyEd25519", "value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g=" }, "last_height": "19", "last_round": "0", "last_step": 3, "last_signature": "uPhO3W577w04CxJQ2G7TAO2mJk5k4uGbDGrHnGkxYOqmMtqsDRYA4NQCwWsOxORGOHWoUlS8F24MdASyBJalAA==", "last_signbytes": "7B2240636861696E5F6964223A227479726368656E222C224074797065223A22766F7465222C22626C6F636B5F6964223A7B22 "priv_key": { "type": "tendermint/PrivKeyEd25519", "value": "r2iHwBHUyBVnpQCq+4SHYm42MJbF9ZCdy3XlHvlCnEqVapcqzgzC9GT1dSj3ijMy3+2iAPitWtU04EofMIw72A==" } } 23
  • 24. node key { "priv_key": { "type": "tendermint/PrivKeyEd25519", "value": "EgYVYbHnrqwucutP99yQ1ZN6i+xGfRn2AI576dZ0/uU/8a2XVgvwfc87vKSvUX49qS1uy6xrk35k3hPQKoHlQg==" } } 24
  • 25. Genesis • genesis_time: official time of blockchain start • chain_id: unique id for the blockchain. Shall be less than 50 symbols • validators: list of initial validators. Could be override by app • pub_key: validator’s pubkey • power: voting power • app_hash: expected application hash (returned by ResponseInfo ABCI message), upon genesis if app’s hash does not match, Tendermint will panic. • app_state: application state, e.g. initial distribution of tokens 25
  • 26. genesis le example { "genesis_time": "2018-10-16T22:56:12.580361Z", "chain_id": "test-chain-iUo2E1", // must be unique "consensus_params": { "block_size_params": { "max_bytes": "22020096", "max_gas": "-1" }, "evidence_params": { "max_age": "100000" } }, "validators": [ { "address": "CE925800DC1F3AF72721343A0FE67F18291632F7", "pub_key": { "type": "tendermint/PubKeyEd25519", "value": "lWqXKs4MwvRk9XUo94ozMt/togD4rVrVNOBKHzCMO9g=" }, "power": "10", " " "" 26
  • 27. Start tendermint • cmd: tendermint node • by default it connect to ABCI app on 127.0.0.1:26658 • for unix domain socket: do: `tendermint node –proxy_app/var/run/your-app.sock • in-process app: counter , kvstore , nil • apps provided by ex_abci: counter, simple_chain 27
  • 28. Start tendermint example $ tendermint node --proxy_app=kvstore // the in-process app I[10-16|16:01:06.946] Starting multiAppConn module=proxy impl=multiAppConn I[10-16|16:01:06.946] Starting localClient module=abci-client connection=query impl=localClient I[10-16|16:01:06.946] Starting localClient module=abci-client connection=mempool impl=localClient I[10-16|16:01:06.946] Starting localClient module=abci-client connection=consensus impl=localClient I[10-16|16:01:06.946] ABCI Handshake module=consensus appHeight=0 appHash= I[10-16|16:01:06.946] ABCI Replay Blocks module=consensus appHeight=0 storeHeight=0 stateHeight=0 I[10-16|16:01:06.947] Completed ABCI Handshake - Tendermint and App are synced module=consensus appHeight=0 appHash= I[10-16|16:01:06.947] This node is a validator module=consensus addr=CE925800DC1F3AF72721343A0FE67F1829 I[10-16|16:01:06.952] P2P Node ID module=p2p ID=e7f354ef522497ba97722bf6658b4e06b1f390ab f I[10-16|16:01:06.953] Starting Node module=main impl=Node I[10-16|16:01:06.953] Starting EventBus module=events impl=EventBus I[10-16|16:01:06.953] Starting RPC HTTP server on tcp://0.0.0.0:26657 module=rpc-server I[10-16|16:01:06.953] Starting P2P Switch module=p2p impl="P2P Switch" I[10-16|16:01:06.953] Starting MempoolReactor module=mempool impl=MempoolReactor I[10-16|16:01:06.953] Starting BlockchainReactor module=blockchain impl=BlockchainReactor I[10-16|16:01:06.953] Starting ConsensusReactor module=consensus impl=ConsensusReactor I[10-16|16:01:06.953] ConsensusReactor module=consensus fastSync=false I[10-16|16:01:06.953] Starting ConsensusState module=consensus impl=ConsensusState I[10-16|16:01:06.953] Starting baseWAL module=consensus wal=/Users/tchen/.tendermint/data/cs.wa I[10 16|16 01 06 954] St ti Ti tTi k d l i l Ti tTi k 28
  • 29. What is WAL? • Write-Ahead Logging • Usage: • to ensure data integration, usually used in database • changes to data files must be written only after those changes has been logged (and flushed to permanent storage) • Pros: • reduce disk writes significantly - we don’t need to flush data pages to disks every time something changes • easy and efficient to sync • any changes to the data can be redone from the WAL 29
  • 30. Tendermint consensus I[10-16|16:01:07.954] Timed out module=consensus dur=997.249ms height=1 round=0 step=Round I[10-16|16:01:07.955] enterNewRound(1/0). Current: 1/0/RoundStepNewHeight module=consensus height=1 round=0 I[10-16|16:01:07.955] enterPropose(1/0). Current: 1/0/RoundStepNewRound module=consensus height=1 round=0 I[10-16|16:01:07.955] enterPropose: Our turn to propose module=consensus height=1 round=0 proposer=CE925800DC1F3 I[10-16|16:01:07.962] Signed proposal module=consensus height=1 round=0 proposal="Proposal{1/0 I[10-16|16:01:07.963] Received proposal module=consensus proposal="Proposal{1/0 1:1A00E597FAD3 I[10-16|16:01:07.964] Received complete proposal block module=consensus height=1 hash=C896B7C7ACBB819C762F29BF I[10-16|16:01:07.964] enterPrevote(1/0). Current: 1/0/RoundStepPropose module=consensus I[10-16|16:01:07.964] enterPrevote: ProposalBlock is valid module=consensus height=1 round=0 I[10-16|16:01:07.965] Signed and pushed vote module=consensus height=1 round=0 vote="Vote{0:CE925800D I[10-16|16:01:07.965] Added to prevote module=consensus vote="Vote{0:CE925800DC1F 1/00/1(Prevot I[10-16|16:01:07.965] enterPrecommit(1/0). Current: 1/0/RoundStepPrevote module=consensus height=1 round=0 I[10-16|16:01:07.966] enterPrecommit: +2/3 prevoted proposal block. Locking module=consensus height=1 round=0 hash=C896B7C7 I[10-16|16:01:07.966] Signed and pushed vote module=consensus height=1 round=0 vote="Vote{0:CE925800D I[10-16|16:01:07.967] Added to precommit module=consensus vote="Vote{0:CE925800DC1F 1/00/2(Precom I[10-16|16:01:07.967] enterCommit(1/0). Current: 1/0/RoundStepPrecommit module=consensus height=1 commitRound=0 I[10-16|16:01:07.968] Commit is for locked block. Set ProposalBlock=LockedBlock module=consensus height=1 commitRound=0 blo I[10-16|16:01:07.968] Finalizing commit of block with 0 txs module=consensus height=1 hash=C896B7C7ACBB819C762F29BF 30
  • 31. Tendermint generate block I[10-16|16:01:07.968] Block{ Header{ ChainID: test-chain-iUo2E1 Height: 1 Time: 2018-10-16 23:01:07.955323 +0000 UTC NumTxs: 0 TotalTxs: 0 LastBlockID: :0:000000000000 LastCommit: Data: Validators: 40B38DD6331AA92F194295AE613E13109D0B019B NextValidators: 40B38DD6331AA92F194295AE613E13109D0B019B App: Consensus: 0E520AF30D47BE28F293E040E418D0361BFB5370 Results: Evidence: Proposer: CE925800DC1F3AF72721343A0FE67F18291632F7 }#C896B7C7ACBB819C762F29BF541B8FE00CDCC8D0 Data{ }# 31
  • 32. Data folder $ ls -l ~/.tendermint/data/ total 0 drwxr-xr-x 9 tchen staff 288 Oct 16 16:01 blockstore.db drwx------ 3 tchen staff 96 Oct 16 16:01 cs.wal drwxr-xr-x 8 tchen staff 256 Oct 16 16:01 evidence.db drwx------ 3 tchen staff 96 Oct 16 16:01 mempool.wal drwxr-xr-x 10 tchen staff 320 Oct 16 16:01 state.db drwxr-xr-x 8 tchen staff 256 Oct 16 16:01 tx_index.db 32
  • 33. RPC status $ curl -s localhost:26657/status |jq { "jsonrpc": "2.0", "id": "", "result": { "node_info": { "id": "e7f354ef522497ba97722bf6658b4e06b1f390ab", "listen_addr": "tcp://0.0.0.0:26656", "network": "test-chain-iUo2E1", "version": "0.25.0-0c9c3292", "channels": "4020212223303800", "moniker": "ip-10-0-1-9.us-west-2.compute.internal", "other": { "amino_version": "0.12.0", "p2p_version": "0.5.0", "consensus_version": "v1/0.2.2", "rpc_version": "0.7.0/3", "tx_index": "on", "rpc_address": "tcp://0.0.0.0:26657" } } 33
  • 34. Send transaction curl -s 'localhost:26657/broadcast_tx_commit?tx="abcd"' { "jsonrpc": "2.0", "id": "", "result": { "check_tx": { "gasWanted": "1" }, "deliver_tx": { "tags": [ { "key": "YXBwLmNyZWF0b3I=", // app.creator "value": "amFl" // jae }, { "key": "YXBwLmtleQ==", // app.key "value": "YWJjZA==" // abcd } ] }, "h h" "88D4266FD4E6338D13B845FCF289579D209C8978" 34
  • 35. Transaction in block I[10-16|16:20:37.273] Timed out module=consensus dur=997.228ms height=114 round=0 step=Ro I[10-16|16:20:37.273] enterNewRound(114/0). Current: 114/0/RoundStepNewHeight module=consensus height=114 round=0 I[10-16|16:20:37.273] enterPropose(114/0). Current: 114/0/RoundStepNewRound module=consensus height=114 round=0 I[10-16|16:20:37.273] enterPropose: Our turn to propose module=consensus height=114 round=0 proposer=CE925800DC I[10-16|16:20:37.276] Signed proposal module=consensus height=114 round=0 proposal="Proposal{ I[10-16|16:20:37.277] Received proposal module=consensus proposal="Proposal{114/0 1:8A667F8EB44D I[10-16|16:20:37.278] Received complete proposal block module=consensus height=114 hash=CFDA9B2C639A5F974A62287 I[10-16|16:20:37.278] enterPrevote(114/0). Current: 114/0/RoundStepPropose module=consensus I[10-16|16:20:37.278] enterPrevote: ProposalBlock is valid module=consensus height=114 round=0 I[10-16|16:20:37.279] Signed and pushed vote module=consensus height=114 round=0 vote="Vote{0:CE92580 I[10-16|16:20:37.280] Added to prevote module=consensus vote="Vote{0:CE925800DC1F 114/00/1(Prev I[10-16|16:20:37.280] enterPrecommit(114/0). Current: 114/0/RoundStepPrevote module=consensus height=114 round=0 I[10-16|16:20:37.280] enterPrecommit: +2/3 prevoted proposal block. Locking module=consensus height=114 round=0 hash=CFDA9B I[10-16|16:20:37.281] Signed and pushed vote module=consensus height=114 round=0 vote="Vote{0:CE92580 I[10-16|16:20:37.282] Added to precommit module=consensus vote="Vote{0:CE925800DC1F 114/00/2(Prec I[10-16|16:20:37.282] enterCommit(114/0). Current: 114/0/RoundStepPrecommit module=consensus height=114 commitRound=0 I[10-16|16:20:37.282] Commit is for locked block. Set ProposalBlock=LockedBlock module=consensus height=114 commitRound=0 I[10-16|16:20:37.283] Finalizing commit of block with 1 txs module=consensus height=114 hash=CFDA9B2C639A5F974A62287 I[10-16|16:20:37.283] Block{ Header{ Ch i ID t t h i iU 2E1 35
  • 36. Query the tx $ curl -s 'localhost:26657/abci_query?data="abcd"' | jq { "jsonrpc": "2.0", "id": "", "result": { "response": { "log": "exists", "index": "-1", "key": "YWJjZA==", "value": "YWJjZA==" } } } 36
  • 37. Send tx with key/value $ curl -s 'localhost:26657/broadcast_tx_commit?tx="name=satoshi"' { "jsonrpc": "2.0", "id": "", "result": { "check_tx": { "gasWanted": "1" }, "deliver_tx": { "tags": [ { "key": "YXBwLmNyZWF0b3I=", "value": "amFl" }, { "key": "YXBwLmtleQ==", "value": "bmFtZQ==" } ] }, "h h" "57D835FBBA0DBF922D8A2EDA56922C9B24E77609" 37
  • 38. Query the key $ curl -s 'localhost:26657/abci_query?data="name"' { "jsonrpc": "2.0", "id": "", "result": { "response": { "log": "exists", "index": "-1", "key": "bmFtZQ==", // name "value": "c2F0b3NoaQ==" // satoshi } } } 38
  • 39. Stopping tendermint captured interrupt, exiting... I[10-16|16:31:39.538] Stopping Node module=main impl=Node I[10-16|16:31:39.538] Stopping Node module=main I[10-16|16:31:39.538] Stopping EventBus module=events impl=EventBus E[10-16|16:31:39.538] Not stopping PubSub -- have not been started yet module=pubsub impl=PubSub I[10-16|16:31:39.538] Stopping IndexerService module=txindex impl=IndexerService I[10-16|16:31:39.538] Stopping P2P Switch module=p2p impl="P2P Switch" I[10-16|16:31:39.538] Stopping ConsensusReactor module=consensus impl=ConsensusReactor I[10-16|16:31:39.538] Stopping ConsensusState module=consensus impl=ConsensusState I[10-16|16:31:39.538] Stopping TimeoutTicker module=consensus impl=TimeoutTicker I[10-16|16:31:39.538] Stopping baseWAL module=consensus wal=/Users/tchen/.tendermint/data/cs.wa I[10-16|16:31:39.544] Stopping EvidenceReactor module=evidence impl=EvidenceReactor I[10-16|16:31:39.544] Stopping PEXReactor module=p2p impl=PEXReactor I[10-16|16:31:39.544] Stopping AddrBook module=p2p book=/Users/tchen/.tendermint/config/addrboo I[10-16|16:31:39.544] Stopping MempoolReactor module=mempool impl=MempoolReactor I[10-16|16:31:39.544] Stopping BlockchainReactor module=blockchain impl=BlockchainReactor E[10-16|16:31:39.544] Not stopping BlockPool -- have not been started yet module=blockchain impl=BlockPool I[10-16|16:31:39.544] Saving AddrBook to file module=p2p book=/Users/tchen/.tendermint/config/addrboo E[10-16|16:31:39.544] Stopped accept routine, as transport is closed module=p2p numPeers=0 I[10-16|16:31:39.544] Closing rpc listener module=main listener="&{Listener:0xc0017362d8 sem:0xc000 I[10 16|16 31 39 544] RPC HTTP t d d l " t t [ ] 26657 f l 39
  • 40. Real world application: Build a simpli ed ethereum 40
  • 41. Requirement • just replicate ethereum’s MPT based account system • A wallet consists of a key pair that user can send tokens • Tendermint as the consensus layer • no EVM to execute smart contracts • the whole app state is changed tx after tx, and the app state of the last tx of a block is committed back to tendermint as app root 41
  • 42. Implementation • ex_abci • gen_server for handling: info, check_tx, begin_block, deliver_tx, end_block, commit, flush • merkle_patricia_tree to handle MPT (using LevelDB) • keccakf1600_orig for sha3 • libsecp256k1 for signing and verifying 42
  • 43. Data structure message Transaction { bytes from = 1; bytes to = 2; uint64 nonce = 3; uint64 total = 4; bytes pub_key = 5; bytes signature = 6; } message AccountState { uint64 balance = 1; uint64 nonce = 2; uint64 num_txs = 3; } 43
  • 44. MPT • Purpose: Maintain the Merkle Patricia Tree • Interfaces: • open: open a level db and restore last committed root_hash, if any • put: update a state for a key (account address) to the trie • get: get a state from a key from the trie • update_block: update last_block height, and put the root_hash for the height • get_app_hash: get the root_hash for the last block • get_info: retrieve last_block, last_block_hash 44
  • 45. Account • Purpose: Maintain account system • Interfaces: • get: get the state of the account in the world state • put: update the state to the account in the world state 45
  • 46. Tx • Purpose: sign and verify transactions • Interfaces: • sign: calculate the sha3 of the tx and then do ecdsa sign • verify: calculate the sha3 of the tx and then verify against the signature 46
  • 47. Wallet • Purpose: create wallet, create tx and retrieve the state from the chain, acting as a client library • Interfaces: • new: create a new wallet with key pair and wallet address, using sha3 and ecdsa • declare: a special transaction that could get 10000 genesis tokens. For test purpose. • transfer: move tokens between accounts, tx will be signed by sender. Validator will verify the signature and make sure these conditions are satisfied: • from / to are not the same address • nonce is expected • balance is greater or equal to the total tokens in the tx • balance: get balance of an address • nonce: get nonce of an address • info: get info of an address • chain_info: get the status of the chain 47
  • 49. Handle info def handle_call({:handle_info, request}, _from, %{trie: trie} = state) do last_block = Mpt.get_last_block(trie) app_hash = Mpt.get_app_hash(trie) Logger.info( "Tendermint version: #{request.version}, last_block: #{last_block}, app_hash: #{ Base.encode16(app_hash) }" ) response = %Abci.ResponseInfo{ data: "Elixir SimpleChain", version: "1.0.0", last_block_height: last_block, last_block_app_hash: app_hash } {:reply, response, state} end 49
  • 50. Handle check_tx def handle_call({:handle_check_tx, request}, _from, %{trie: trie} = state) do %Abci.RequestCheckTx{tx: data} = request tx = data |> Base.decode64!() |> Transaction.decode() Logger.debug(fn -> "Check tx: #{inspect(tx)}" end) response = struct(Abci.ResponseCheckTx, verify(tx, trie, data)) {:reply, response, state} end 50
  • 51. Handle deliver_tx def handle_call({:handle_deliver_tx, request}, _from, %{trie: trie} = state) do %Abci.RequestDeliverTx{tx: data} = request tx = data |> Base.decode64!() |> Transaction.decode() Logger.debug(fn -> "Deliver tx: #{inspect(tx)}" end) response = struct(Abci.ResponseDeliverTx, verify(tx, trie, data)) trie = case response.code == 0 do true -> update_state(tx, trie) _ -> trie end {:reply, response, %{state | trie: trie}} end 51
  • 52. Handle end_block def handle_call({:handle_end_block, request}, _from, %{trie: trie} = state) do height = request.height Logger.debug(fn -> "End block: #{height}" end) Mpt.update_block(trie, request.height) response = %Abci.ResponseEndBlock{ validator_updates: [], tags: [] } {:reply, response, state} end 52
  • 53. Handle commit def handle_call({:handle_commit, request}, _from, %{trie: trie} = state) do Logger.debug(fn -> "Commit block: #{inspect(request)}" end) response = %Abci.ResponseCommit{ data: Mpt.get_app_hash(trie) } {:reply, response, state} end 53
  • 54. How do we update world state? defp update_state(%Transaction{from: from, to: to, nonce: n1, total: total}, trie) do acc1 = Account.get(trie, from) trie = Account.put(trie, from, %AccountState{ acc1 | nonce: n1 + 1, balance: acc1.balance - total, num_txs: acc1.num_txs + 1 }) acc2 = case Account.get(trie, to) do nil -> %AccountState{nonce: 0, balance: total, num_txs: 1} v -> %AccountState{v | balance: v.balance + total, num_txs: v.num_txs + 1} end Account.put(trie, to, acc2) end 54
  • 56. 56