Sei sulla pagina 1di 25

Blockchain

What Is Blockchain?
• Shared ledger of transactions in a community
• Transactions are:
– Authenticated – only the owner of some given asset can
transfer part or all of it to someone else
– Unforgeable – no one can create fake transactions without
others noticing
– Decentralized – no single root of trust, majority decides
– Consensus-based, single copy – branches may exist for a short
time but then they converge to a single copy
• Good for:
– Keeping track of any kind of assets, transactions and contracts
– Auditing, transparency
Blockchain vs Traditional
Traditional Blockchain
Transactions centrally recorded in a trusted Transactions recorded in a distributed
place (e.g., bank, escrow) manner in the ledger (owner records,
anyone can verify)
Trusted place can become compromised Cannot lie about transactions
and lie about transactions
No transparency, trusted places have all the Fully transparent, all transactions are
records visible in the community
No anonymity, all transactions require Anonymous, transactions pertain to virtual
proof of real identity identity without link to actual identity
Expensive, must pay the trusted place Cheap, verifiers are paid a little
Lots of paperwork Fully digital and automated
High price of entry (real ID, physical access) Anyone with computer can enter
Trusted place is single point of failure Since transactions are replicated, nodes can
come and go and the integrity remains
protected
Transaction Records
• Broadcast to peers in the community, and propagate
to everyone
• Each peer keeps a copy of the entire ledger
– Resilient to failures/departures of nodes
Blockchain Insides
The nonce A mined The nonce B mined
for this block for this block
Previous block hash Previous block hash
0xab33203125 0x431ab12bc0
Transaction 1 hash Transaction 1 hash
0xfe02323211 0xfe03dfc110
Transaction 2 hash Transaction 2 hash
0x192200ba12 0x1921163422
Transaction 3 hash Transaction 3 hash
0xf43fee3291 0x5367ef2138

• This linking of blocks together and usage of one-way hashes prevents


tampering with transactions and with the timeline
• The nonce for each block is a string that, together with other strings in the
block makes the output hash have given number of zeros in a given
position – finding this nonce is called “mining” in Bitcoin
• Blockchain is append-only
Building of Blockchain

1. Send transaction to peers


2. Peers validate and propagate the transaction
3. Any node can gather several transactions into a block, include the hash of the
previous block, and mine the nonce. Then includes the block in its chain and
broadcasts to others
4. Others validate and append the block to their chains
Bitcoin
• Probably the most famous use of blockchain
– Money transfer between parties
– Money is digital, but nowadays bitcoins can be exchanged
for real money
• Money printed out of thin air?
– We usually call something “money” if other agree that it is
valuable and agree to accept it in exchange for goods
– Paper money is similar in a sense that it only has value
because we have all agreed so
– Same goes for gold/metal money
– Money needs to be scarce; Bitcoin ensures that new coins
cannot be minted easily
What Is a Transaction
• Transfer of coins
– Sender’s public key
– Hash of previous transaction for that coin
– Sender’s signature over that hash
• Each transaction is usually in a separate block
B pays C C pays D D pays E

B pub key C pub key D pub key

Data Data Data

Hash 1 Hash 2 Hash 3

A’s sig B’s sig C’s sig


of hash 1 of hash 2 of hash 3
What Is the Data Part?
B sending 50 BTC to C, that B previously got from A

Input:
Previous tx:
f5d8ee39a430901c91a5917b9f2dc19d6d1a0e9cea205b009ca73dd04470b9a6
Index: 0
scriptSig: Hash of the previous transaction (A->B) for 50 BTC in output 0
304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d
10
90db022100e2ac980643b0b82c0e88ffdfec6b64e3e6ba35e7ba5fdd7d5d6cc8d25
c6b241501
Signature of the previous transaction using sender’s (A’s)
Output: private key
Value: 5000000000
Transfer all 50 BTC
scriptPubKey: OP_DUP OP_HASH160
404371705fa9bd789a2fcd52d2c580b65d35549d
OP_EQUALVERIFY OP_CHECKSIG
C’s public key
Instructions for verification in a scripting
language
Bitcoin Transaction – More Details
• A transaction can have multiple inputs and outputs
• If there is any leftover between inputs and outputs it
goes to the node that validates this transaction:
– The validator links this transaction into the chain
– The leftover is the transaction fee
• Nodes can offer higher transaction fees to motivate
others to validate their transactions sooner
• A transaction can also be:
– Payment to many nodes, payment to the first one that claims it,
delayed payment
• Validators also get to “mine” coins
– Whoever links the block claims the reward
– The reward is fixed an is declining over time
Mining
• There are many right nonces, not just one
– The hash just has to start with the given number of zeros
– Many hashes are possible, thus many nonces are possible
• Mining (finding the right nonce) is necessary proof of
work that
– Makes linking new blocks hard
– Controls the speed of linking new blocks. At any time only a
handful of nodes are likely to find the right nonce.
• Only the first one that finds the right nonce wins
– Gets to link the block
– Gets a reward from the network – the reward goes down
over time and will reach 0 soon. When it does transaction
fees will have to go up to incentivize validation
Blockchain Operations
• Mining
– Finding a nonce that completes the current block that is
trying to be linked with the rest of the chain
• Validating a transaction
– Following the chain of transactions from the first mining of
the coins to the current transaction
– For each, check that signatures match the hash and the
public key in scriptSig
• Validating a block
– Validate all the transactions
– Validate that the block has the hash of the block that is the
latest in your chain
What If?
• Double spending
– A gives the same money to B and to C
– One of these transactions will get linked first. At that point
the other one fails to validate.
• A node makes up coins
– The only way to claim coins is to mine them by linking a new
block into the chain
– What if the node makes up a block to link? Others wouldn’t
validate it.
• Someone steals coins
– Cannot spend them without the private key of the owner
– Has to steal private key too
What If?
• Two nodes try to link a block at the same time
– A links a block and propagates to its peers
– B links a block and propagates to its peers
– At some point say the block from B reaches a peer C, which
has already heard from A and has linked A’s new block
– C refuses to link the block from B because now it does not fit
with its new head of the chain
– Majority wins, and one of the nodes loses
• A node modifies a block in his chain
– The rest of the nodes still have correct chains
– Cannot propagate this change as anyone can detect it in the
process of validating blocks
What If?
• A node refuses to include a transaction in a block
– Transactions are broadcast to everyone
– Some other node will include it
– Similarly a node may refuse to rebroadcast some
transactions; this is where having many peers helps
• A group of nodes colludes to delay some block
announcements to give themselves an unfair
advantage
– This is possible and may have already happened
– More peers helps but in reality if someone controls many
nodes they can tip the scales in their own favor
– Similarly they could delay linking of some transactions
Play With Fake Bitcoins
• Follow the instructions here:
https://bitcoin.org/en/developer-examples#testing-
applications
• Make sure to play with a test network, not with real
network:
– Look at regtest mode examples
for

time PBL

16
Ethereum
• A decentralized network to run smart contracts
– Think of Ethereum network as a distributed computer running
your program
• Good for:
– Storing wills, sale contracts, service contracts, registries of
debts and promises, creating markets, decentralized orgs
– Crowdfunding – make a contract with your funders that
defines when funds will be available to you
• Ether
- The currency used to pay for a contract to be executed on
blockchain, and pay for services. Miners mine Ether.
• Gas
- The currency used to pay transaction fees to miners to
include your transaction into the blockchain
Smart Contracts
• Computer code that can facilitate exchange of
goods, services or of anything valuable
– Runs on the Ethereum network when conditions are met
– No set operations; programmers can define their own
– Several specific programming languages (e.g., Solidity)
• Ethereum Virtual Machine (EVM) enables anyone to
run any program on the network
- Given enough time and memory
- Programs run as intended because they are executed by
everyone in the network
• Smart contracts could have bugs and problems
- If there is a problem, there is no way to stop the exploit or
issue an update
Smart Contracts
• Example from blockgeeks.org:
– Suppose you rent an apartment from me. You can do this
through the blockchain by paying in cryptocurrency. You get a
receipt which is held in our virtual contract;. I give you the
digital entry key which comes to you by a specified date. If the
key doesn’t come on time, the blockchain releases a refund. If I
send the key before the rental date, the function holds it
releasing both the fee and key to you and me respectively when
the date arrives. The system works on the If-Then premise and
is witnessed by hundreds of people, so you can expect a
faultless delivery. If I give you the key, I’m sure to be paid. If you
send a certain amount in cryptocurrency, you receive the key.
The document is automatically canceled after the time, and the
code cannot be interfered by either of us without the other
knowing since all participants are simultaneously alerted.
DAOs
• Decentralized Autonomous Organizations
- People write code that will govern the behavior of the
organization
- Initial funding period (ICO) – people purchase tokens in the
given organization (like shares) and pay in Ether
- DAO begins to operate
- People can make proposals how to spend the money and
those that have tokens now have voting rights on the
proposals
The DAO Hack
• “The DAO” was the project to build a distributed VC
– Funded through token sale on Ethereum; raised 150 M
– ~3M M of Ether got stolen through a hack in DAO software, but
all 150M could be stolen because no updates
– This was not Ethereum’s fault but the problem was that there
was no way to retrieve the money
• Ethereum community voted to execute a hard fork
– Correct the block where the thiefs got the money
– Transfer the stolen money to a new account
– Effectively return the money to DAO
– Great for DAO, but this goes against core Ethereum values
– Ethereum split into two currencies: Ethereum Classic (no fork)
and Ethereum (with fork)
More about the DAO Hack
• Read this article and discuss with a partner:
https://www.coindesk.com/understanding-dao-hack-
journalists

for

time PBL

22
Uses of Blockchain Today
• Very diverse and fast-growing
• Private blockchains for companies and governments to
record transactions (IBM, Walmart, Ukraine)
• Ripple – private blockchain for global payments in
financial sector
• Decentralized registry for DNS (blockstack.org)
• Decentralized public key infrastructure (instead of
trusted certificates)
• Distributed storage
• And many more
Deficiencies
• Privacy of transactions and identities is hard
– Everything is public or at least visible to members
• Small throughput
– Because chaining is costly there is a limited number of
transactions possible ~ hundreds per second
• Large storage/computation requirement
– Each node stores and verifies the entire chain
– Optimizations exist where only parts are stored and dead
nodes are pruned
• No undo
– What happens on the chain stays on the chain
Play With Ethereum
• A good tutorial is at:
– https://solidity.readthedocs.io/en/v0.4.24/introduction-to-smart-
contracts.html
• You will need Ethereum wallet
– Create one at http://www.myetherwallet.com
• You can test your contracts on Remix
– http://remix.ethereum.org
– I believe you cannot send anything to for
another person, so this is just between
you and you. You can however create two time PBL
wallets and practice sending to one and to other
from the contract.

25

Potrebbero piacerti anche