4. What is Blockchain
• Blockchain = Chain of Data Block
Distributed Ledger
Secure
Transactions are
verified by the
entire network
Decentralized
(no owner)
Immutable
Peer-to-Peer
Network
Nodes should
ledger of facts +
history of updates
7. Ledger : “a collection of financial accounts or transactions”
Ledger
Kenneth Alice
#30
Amy Jen
#50
Immutable : The ledger cannot be changed. Ever!
What is ledger?What is Immutable?
8. What is block?
BLOCK
Block Header
Hash of previous
block header
Merkle root
Timestamp
Nonce
Transactions
Block : A block records some or all of the most recent transactions. Thus a block is like a
page of a ledger or record book.
10. Block header
BLOCK Header
Hash of previous
block header
Merkle root
Timestamp
Nonce
• Hash of previous block header
• Timestamp : It indicates when the
block was created.
• Merkle root : hash-based data
structure that is a generalization of the
hash list.
• Nonce :Counter used for the Proof of
Work algorithm. Each time you mine,
the nonce increases by 1 .
11. Merkle tree
Merkle trees are useful in synchronizing data across a distributed data store because
it allows each node in the distributed system to quickly and efficiently identify
records that have changed without having to send all the data to make the
comparison.
13. Merkle proofs in Ethereum
• Transaction
• Receipts(essentially,
pieces of data showing
the effect of each
transaction)
• State
14. Merkle proofs in Ethereum
• Has this transaction been included in a particular block? –
Transaction
• Tell me all instances of an event of type X (eg. a crowdfunding
contract reaching its goal) emitted by this address in the past 30
days – Receipts
• What is the current balance of my account? – State
• Does this account exist? – State
• Pretend to run this transaction on this contract. What would
the output be? – State
Source : https://blog.ethereum.org/2015/11/15/merkling-in-ethereum/
17. • Creating the candidate block
Mining
HASHING ( SHA256 )
Block’s Block header data
Hash of previous block
header
Merkle root
Timestamp
Nonce
No : (
Increment the nonce by 1.
Hash again!
Is hash (A8SBERA….) lower than the
target? (check the leading zeros)
YES! Problem Solved.
Proof of
Work
18. Difficulty in Bitcoin
• Difficulty is a measure of how difficult it is to find a hash below a given target
• The Bitcoin network has a global block difficulty. Valid blocks must have a
hash below this target. Mining pools also have a pool-specific share diffic
ulty setting a lower limit for shares.
• How often does the network difficulty change?
• Every 2016 blocks.
• What is the formula for difficulty?
• difficulty = difficulty_1_target / current_target
• (target is a 256 bit number)
19. Difficulty in Ethereum
• parentTimestamp - indicates the timestamp of a previous block
• parentDifficulty - indicates the difficulty of a previous block
• currentTimestamp - indicates the timestamp of a current block
• currentBlockNumber - indicates the number of a current block (previous block number = currentBlock
Number - 1)
• currentDifficulty -- indicates the difficulty of a current block
• Frontier:
• block_diff = parent_diff + parent_diff // 2048 * (1 if block_timestamp - parent_timestamp < 13 else -1)
+ int(2**((block.number // 100000) - 2))
• Homestead:
• block_diff = parent_diff + parent_diff // 2048 * max(1 - (block_timestamp - parent_timestamp) // 10,
-99) + int(2**((block.number // 100000) - 2))
Frontier Homestead Metropolis Serenity
20. • Block header hash is less than the target
• Block size is within acceptable limits
• Block timestamp is less than two hours in the future.
• The first transaction is a coinbase transaction ( and only the first )
• The coinbase transaction has a valid reward.
• All transactions within the blocks are valid ( also have a checklist on their
own )
Source : https://dev.to/damcosset/blockchain-what-is-mining-2eod
Validate the new block
21. Node
Time
David Sandra 5 BTC
Brian List 3.02 BTC
Mary Sandra 10 BTC
Lisa Sandra 10 BTC
Sandra David 10 BTC
BLOCK #186
Block ID
0x0235de9a6
Previous Block
0x6e257071cec
Transactions
BLOCK #186
Block ID
0x6e257071cec
Previous Block
0x1dcc4de8dec
Transactions
BLOCK #186
Block ID
0x6e257071cec
Previous Block
0x1dcc4de8dec
Transactions
BLOCK #186
Block ID
0x6e257071cec
Previous Block
0x1dcc4de8dec
Transactions
BLOCK #186
Block ID
0x6e257071cec
Previous Block
0x1dcc4de8dec
Transactions
Blockchain network
Blockchain
Lisa Sandra 10 BTC
Brian List 5 BTC
Mary Sandra 10 BTC
…… ……… .. BTC
22. What does “distributed” mean ?
Distributed: A distributed ledger is a database that is consensually shared and
synchronized across network spread across multiple node
Ledger
Ledger
Ledger
Ledger
Ledger Ledger
node
node
node
nodenode
node
23. What is Consensus Algorithm?
• Consensus algorithm (consensus protocol/ consensus mechanism)
• Algorithm to reach agreement among the blockchain nodes
• All nodes should agree about the changes in the distributed ledger
• Proof-of-work (PoW), Proof-of-stake, other algorithms
28. Proof-of-Work (PoW)
• A “Proof-of-work” (PoW) is a piece of data which is
• Difficult to produce
• Easy for others to verify
• Producing a proof-of-work can be a random guessing process
or can be organized in mining pools (join PoW production)
• Example
• Find a number x, such that SHA256(text + x) has 10
leading zeroes.
• 10 zeroes == network difficulty
29. Proof-of-Work : Problems
• Needs computing power
• Computationally expensive
• Energy intensive
• 51% attack
• Attackers holding more than 50% of the power could potentially
reverse-back transactions (double-spend money) / deny service
• Hashing algorithm types for PoW consensus
• ASIC mineable (e.g. SHA256), CPU mineable (e.g. CryptoNight),
• GPU mineable (e.g. ETHash), CPU + GPU mineable (e.g. Equihash)
30. Proof-of-Work : Problems (2)
• Transactions speed – average wait time
• Bitcoin : new block mined in ~ 9-10 minutes
• Ethereum : new block mined in ~ 10-15 seconds
• Business needs real-time transactions ( milliseconds)
• Transactions throughput – transactions per second (tps)
• Bitcoin: 2000-3000 / transactions per block 3-5 tps ( up to 7 tps)
• Ethereum: 200-300 / transactions per block 10-15 tps
• Business needs thousands tps (e.g. VISA performs 2000 tps)
31. Proof-of-Work Cryptocurrencies
• Different cryptocurrencies use different hashing algorithms
• Bitcoin, Bitcoin Cash – SHA256 – ASIC mineable
• Ethereum, Ethereum Classic – ETHash – GPU mineable
• Zcash, Bitcoin Gold – Equihash – CPU and GPU mineable
• Monero – CryptoNight – CPU and GPU mineable
• Litecoin, Dogecoin – Scrypt – GPU mineable
• Dash – X11 – ASIC, CPU and GPU mineable
• Stratis – X13 – ASIC, CPU and GPU mineable
32. Proof of Stake (PoS)
• PoS is designed to increase network security and reduce resource
wasting
• The creator of the next block is chosen in
• Combination of random selection and wealth
• E.g. holding 1% of the coins gives the chance to verify (mine) 1%
of the “Proof of Stake blocks”
• The “Monopoly Problem”: a monopolist ( holder of the most coins)
could double spend or deny / filter other’s transactions
• Executing a monopoly attack is much more expensive than in
PoW
34. Proof of Authority
• Proof-of-Authority (PoA) assigns a set of trusted nodes
(authorities) to process transactions and build new blocks
• New blocks need to be signed by the majority of authorities
• Works very well in private blockchain (cross validation)
• Great performance, fast transactions, high throughput
• Examples:
• Ethereum Kovan Testnet – https://kovan.etherscan.io/
• Ethereum Rinkeby Testnet -- https://www.rinkeby.io/
36. Public / Private Keys and Transaction
Wallet master
key (seed)
Private key Public key address
transaction Signed
transaction
Transaction data
Signature(v, r, s)
Public key :(x, y)
Sign by private key
signed
transaction
verify by address
Valid / invalid
37. Verify Message Signature in Etherscan
• Verify message signature at https://etherscan.io/verifySig by :
• Signer address (40 hex digits)
• Signature (130 hex digits)
• Original message text
• The result is : valid / invalid
38. Verifying an Ethereum Signature - Example
import eth_keys, binascii
msg = b'Hi guys, I am Kenneth and left message for signing'
msgSigner = '0xcF77d2Bc22d451657b30b7816F805773Bee23f1c'
signature = eth_keys.keys.Signature(binascii.unhexlify(
'64ee37d70272d7959d098f79b132cedf845d43196e12538334767675c37bec3d56f9f3dc9129afda470e
4ce78ee389e6b199f07141970b0c9dccbda636b644f100'))
signerRecoveredPubKey = signature.recover_public_key_from_msg(msg)
signerRecoveredAddress = signerRecoveredPubKey.to_checksum_address()
print('Signer public key (128 hex digits):', signerRecoveredPubKey)
print('Signer address:', signerRecoveredAddress)
print('Signature valid?:', signerRecoveredAddress == msgSigner)
39. What is Ethereum
• This is how Ethereum’s website defines itself:
• “Ethereum is a decentralized platform that runs smart
contracts: applications that run exactly as programmed
without any possibility of downtime, censorship, fraud or
third party interference. These apps run on a custom built
blockchain, an enormously powerful shared global
infrastructure that can move value around and represent
the ownership of property.”
41. Ethereum ID
Network Network ID Note
Main 1
Morden 2 Deprecated
Ropsten 3 POW
Rinkeby 4 POA, supported by geth only
Kovan 42 POA, supported by Parity only
43. Ethereum account
• State
• Accounts
• Contracts Accounts (controlled by code)
• Address
• Balance
• Ether
• Externally Owned Accounts (EOAs: User accounts is controlled by
private keys)
• Address
• Balance
• Ether
Contract account
<code>
<code>
<code>
External owned
account
44. Ethereum account
• Has an address
• Private key protected by password
• Has an address but NO private key
• Hold/Run code
• Associated with accounts(s)
• Lists incoming transactions
Contract
account<code>
<code>
<code>
External owned
account
45. Ethereum account
• Account state
• nonce: If the account is an externally
owned account, this number represents
the number of transactions sent from the
account’s address.
• balance: The number of Wei owned by
this address. There are 1e+18 Wei per
Ether. storageRoot: A 256-bit hash of the
root node of a Merkle Patricia tree that
encodes the storage contents of the .
• codeHash: The hash of the EVM
(Ethereum Virtual Machine) code of this
account .
49. Contracts Accounts- Pros and cons
• Pros
• Contract account can list incoming transactions
• Contract account can be setup as Multisig Accounts
• A Multisig Account can be structured such that it has a daily limit
which you specify, and only if the daily limit is exceeded will
multiple signatures be required
• Cons
• Contract accounts incur a cost whenever you use them.
• Contract accounts can’t initiate new transactions on their own.
50. MetaMask
• A Ether Wallet
• Chrome、Firefox and Opera Plugin
• Similar to MIST-Browser
• No geth node
• Is a bridge
• Like a “light-client”
• A “Key store” for your private keys
57. MetaMask – get Testcoin
• Ropsten faucet
• https://faucet.metamask.io/
• Kovan faucet
• https://gitter.im/kovan-testnet/faucet
• Rinkeby faucet
• https://faucet.rinkeby.io/
• Twitter
• Google
• Facebook
58. ACTIVITY 1:
TRANSFERRING ETHEREUM (10 MINS)
Instructions:
1. Open Ethereum Wallet
2. Get assigned account address
3. Transfer Ether to an assigned address.
60. What is Solidity
.
• Solidity is a contract-oriented, high-level language for
implementing smart contracts. It was influenced by C++,
Python and JavaScript and is designed to target the
Ethereum Virtual Machine (EVM).
Contract EventVariable Function
contract
variable
variable
fuction
fuction
event
61. Basic Types
.
Name Notes Examples
String Sequence of characters “Hi there!” “Mike”
Bool Boolean value True False
Int Integer, positive or negative. Has no decimal 0 -25000 59158
Unit ‘unsigned’ integer, positive number. Has no decimal 0 25000 989910
Fixed/ufixed ‘Fixed’ point number, Number with decimal after it 20.001 -42.4242 3.14
Address Has methods tied to it for sending money 0x18bae199c8dbae199c8d