SlideShare a Scribd company logo
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

Smart contract
Smart contractSmart contract
Smart contract
Akhmad Daniel Sembiring
 
Write smart contract with solidity on Ethereum
Write smart contract with solidity on EthereumWrite smart contract with solidity on Ethereum
Write smart contract with solidity on Ethereum
Murughan Palaniachari
 
Consensus Algorithms.pptx
Consensus Algorithms.pptxConsensus Algorithms.pptx
Consensus Algorithms.pptx
Rajapriya82
 
Blockchain meets database
Blockchain meets databaseBlockchain meets database
Blockchain meets database
YongraeJo
 
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity Fundamentals
Eno Bassey
 
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Edureka!
 
KubeConEU - NATS Deep Dive
KubeConEU - NATS Deep DiveKubeConEU - NATS Deep Dive
KubeConEU - NATS Deep Dive
wallyqs
 
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
Svetlin Nakov
 
Blockchain Presentation
Blockchain PresentationBlockchain Presentation
Blockchain Presentation
Zied GUESMI
 
Hyperledger Fabric
Hyperledger FabricHyperledger Fabric
Hyperledger Fabric
Murughan Palaniachari
 
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
Svetlin Nakov
 
Blockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricBlockchain - HyperLedger Fabric
Blockchain - HyperLedger Fabric
Araf Karsh Hamid
 
Introduction to Solidity and Smart Contract Development (9).pptx
Introduction to Solidity and Smart Contract Development (9).pptxIntroduction to Solidity and Smart Contract Development (9).pptx
Introduction to Solidity and Smart Contract Development (9).pptx
Gene Leybzon
 
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
Simplilearn
 
Blockchain and Decentralization
Blockchain and DecentralizationBlockchain and Decentralization
Blockchain and Decentralization
Priyab Satoshi
 
Ethereum (Blockchain Network)
Ethereum (Blockchain Network)Ethereum (Blockchain Network)
Ethereum (Blockchain Network)
Qais Ammari
 
Distributed systems and blockchain technology
Distributed systems and blockchain technologyDistributed systems and blockchain technology
Distributed systems and blockchain technology
Alket Cecaj
 
Introduction to Blockchain and Smart Contracts
Introduction to Blockchain and Smart ContractsIntroduction to Blockchain and Smart Contracts
Introduction to Blockchain and Smart Contracts
Techracers
 
Scaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge ProofsScaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge Proofs
Hyojun Kim
 
Ethereum Blockchain with Smart contract and ERC20
Ethereum Blockchain with Smart contract and ERC20Ethereum Blockchain with Smart contract and ERC20
Ethereum Blockchain with Smart contract and ERC20
Truong Nguyen
 

What's hot (20)

Smart contract
Smart contractSmart contract
Smart contract
 
Write smart contract with solidity on Ethereum
Write smart contract with solidity on EthereumWrite smart contract with solidity on Ethereum
Write smart contract with solidity on Ethereum
 
Consensus Algorithms.pptx
Consensus Algorithms.pptxConsensus Algorithms.pptx
Consensus Algorithms.pptx
 
Blockchain meets database
Blockchain meets databaseBlockchain meets database
Blockchain meets database
 
Ethereum Solidity Fundamentals
Ethereum Solidity FundamentalsEthereum Solidity Fundamentals
Ethereum Solidity Fundamentals
 
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
Blockchain Training | Blockchain Tutorial for Beginners | Blockchain Technolo...
 
KubeConEU - NATS Deep Dive
KubeConEU - NATS Deep DiveKubeConEU - NATS Deep Dive
KubeConEU - NATS Deep Dive
 
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
Consensus Algorithms - Nakov at CryptoBlockCon - Las Vegas (2018)
 
Blockchain Presentation
Blockchain PresentationBlockchain Presentation
Blockchain Presentation
 
Hyperledger Fabric
Hyperledger FabricHyperledger Fabric
Hyperledger Fabric
 
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
Consensus Algorithms - Nakov @ jProfessionals - Jan 2018
 
Blockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricBlockchain - HyperLedger Fabric
Blockchain - HyperLedger Fabric
 
Introduction to Solidity and Smart Contract Development (9).pptx
Introduction to Solidity and Smart Contract Development (9).pptxIntroduction to Solidity and Smart Contract Development (9).pptx
Introduction to Solidity and Smart Contract Development (9).pptx
 
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
What is A Smart Contract? | Smart Contracts Tutorial | Smart Contracts in Blo...
 
Blockchain and Decentralization
Blockchain and DecentralizationBlockchain and Decentralization
Blockchain and Decentralization
 
Ethereum (Blockchain Network)
Ethereum (Blockchain Network)Ethereum (Blockchain Network)
Ethereum (Blockchain Network)
 
Distributed systems and blockchain technology
Distributed systems and blockchain technologyDistributed systems and blockchain technology
Distributed systems and blockchain technology
 
Introduction to Blockchain and Smart Contracts
Introduction to Blockchain and Smart ContractsIntroduction to Blockchain and Smart Contracts
Introduction to Blockchain and Smart Contracts
 
Scaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge ProofsScaling Ethereum using Zero-Knowledge Proofs
Scaling Ethereum using Zero-Knowledge Proofs
 
Ethereum Blockchain with Smart contract and ERC20
Ethereum Blockchain with Smart contract and ERC20Ethereum Blockchain with Smart contract and ERC20
Ethereum Blockchain with Smart contract and ERC20
 

Similar to Tendermint in a nutshell

Introduction to Ethereum
Introduction to EthereumIntroduction to Ethereum
Introduction to Ethereum
Arnold 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
 
WCM Transfer Services
WCM Transfer Services WCM Transfer Services
WCM Transfer Services
Alfresco Software
 
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 Workshop
aeronbuchanan
 
Programming smart contracts in solidity
Programming smart contracts in solidityProgramming smart contracts in solidity
Programming smart contracts in solidity
Emanuel 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 problem
Jose Galarza
 
GEO mapbox geo_api_develop2 Intro
 GEO mapbox geo_api_develop2 Intro GEO mapbox geo_api_develop2 Intro
GEO mapbox geo_api_develop2 Intro
Max 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_ethereum
Tomoaki 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).pptx
WijdenBenothmen1
 
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 anatomy
Dongmin 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 APIs
Tim Burks
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
lcplcp1
 

Similar to Tendermint in a nutshell (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
 
Introduction to eBPF and XDP
Introduction to eBPF and XDPIntroduction to eBPF and XDP
Introduction to eBPF and XDP
 

More from ArcBlock

ArcBlock Introduction to Blockchain
ArcBlock Introduction to BlockchainArcBlock Introduction to Blockchain
ArcBlock Introduction to Blockchain
ArcBlock
 
Forge blockchain deployment made easy
Forge  blockchain deployment made easyForge  blockchain deployment made easy
Forge blockchain deployment made easy
ArcBlock
 
Designing Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable TokensDesigning Decentralized Apps: Programmable Tokens
Designing Decentralized Apps: Programmable Tokens
ArcBlock
 
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
 
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
ArcBlock
 
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
ArcBlock
 
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 everyone
ArcBlock
 
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
ArcBlock
 
IPFS: A Whole New World
IPFS: A Whole New WorldIPFS: A Whole New World
IPFS: A Whole New World
ArcBlock
 
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
ArcBlock
 
Understanding hd wallets design and implementation
Understanding hd wallets  design and implementationUnderstanding hd wallets  design and implementation
Understanding hd wallets design and implementation
ArcBlock
 
Technical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnitTechnical Learning Series - Elixir ExUnit
Technical Learning Series - Elixir ExUnit
ArcBlock
 
Introduction to CQRS & Commended
Introduction to CQRS & CommendedIntroduction to CQRS & Commended
Introduction to CQRS & Commended
ArcBlock
 
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
ArcBlock
 
Introduction to aws data pipeline services
Introduction to aws data pipeline servicesIntroduction to aws data pipeline services
Introduction to aws data pipeline services
ArcBlock
 
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 Blockchain
ArcBlock
 

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

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 

Tendermint in a nutshell

  • 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