IS3101 Cryptocurrency & Blockchain
Lecture 4 Decentralization & Smart Contract
• How Bitcoin Achieves Decentralization
Copyright By PowCoder代写 加微信 powcoder
• Smart Contract
• Smart Contract – Week 4 Online Quiz (instead of tutorial exercise)
– Available on Canvas – 5 minutes duration – Due in 1 week time
How Bitcoin Achieves Decentralization
• Scroogecoin
• Howdowegotoadecentralized version of this?
• Itisnotpurelytechnicalbutit’sa combination of technical and clever incentive engineering
Centralization vs. decentralization
• Competing paradigms that underlie many digital technologies
Decentralization is not all-or-nothing
decentralized protocol, but dominated by centralized webmail services
Aspects of decentralization in Bitcoin
1. Who maintains the ledger?
2. Who has authority over which transactions are valid? 3. Who creates new bitcoins?
4. Who determines how the rules of the system change? 5. How do bitcoins acquire exchange value?
Beyond the protocol:
exchanges, wallet software, service providers…
Aspects of decentralization in Bitcoin
• • • • • • •
Peer-to-peer network:
open to anyone, low barrier to entry
open to anyone, but inevitable concentration of power often seen as undesirable
Updates to software:
core developers trusted by community, have great power
• Moretechnicallevel
Distributed consensus
Bitcoin’s key challenge
• Key technical challenge of decentralized
• e-cash: distributed consensus
• or: how to decentralize ScroogeCoin
Why consensus protocols?
• Traditional motivation: reliability in distributed systems
• Distributed key-value store enables various applications:
• DNS, public key directory, stock trades …
Good targets for Altcoins!
Defining distributed consensus
• Two requirements:
1. The protocol terminates and all correct nodes
decide on the same value
2. This consensus value must have been proposed by
some correct node
Bitcoin is a peer-to-peer system
• When Alice wants to pay Bob:
she broadcasts the transaction to all Bitcoin nodes
Note: Bob’s computer is not in the picture
signed by to pkBob : H( )
How consensus could work in Bitcoin
At any given time:
● Allnodeshaveasequenceofblocksoftransactions they’ve reached consensus on
● Eachnodehasasetofoutstandingtransactionsit’s heard about
How consensus could work in Bitcoin
Consensus protocol
agreed upon
OK to select any valid block, even if proposed by only one node
Why consensus is hard
• Nodes may crash
• Nodes may be malicious
• Network is imperfect
• Not all pairs of nodes connected
• Faults in network • Latency
No notion of global time
Many impossibility results
• Byzantine generals problem
• Fischer-Lynch-Paterson (deterministic nodes): consensus impossible with a single faulty node
Some well-known protocols
Paxos (protocols for solving consensus)
Never produces inconsistent result, but can (rarely) get stuck
Understanding impossibility results
• These results say more about the model than about the problem
• The models were developed to study systems like distributed databases
Bitcoin consensus: theory & practice
• Bitcoin consensus works better in practice than in theory
• Theory is still catching up
• BUT theory is important, can help predict unforeseen attacks
Some things Bitcoin does differently
Introduces incentives
• Possible only because it’s a currency!
Embraces randomness
• Does away with the notion of a specific end-point
• Consensus happens over long time scales — about 1 hour
Consensus without identity: the block chain
• TechnicaldetailsofBitcoin’s consensus algorithm
Pragmatic: some protocols need node IDs Security: assume less than 50% malicious
Why identity?
Why don’t Bitcoin nodes have identities?
Identity is hard in a P2P system — Sybil attack Pseudonymity is a goal of Bitcoin
Weaker assumption: select random node
Analogy: lottery or raffle
When tracking & verifying identities is hard, we give people tokens, tickets, etc.
Now we can pick a random ID & select that node
Key idea: implicit consensus
In each round, random node is picked
This node proposes the next block in the chain
Other nodes implicitly accept/reject this block
• by either extending it
• or ignoring it and extending chain from earlier block
Every block contains hash of the block it extends
Consensus algorithm (simplified)
1. New transactions are broadcast to all nodes
2. Each node collects new transactions into a block
3. In each round a random node gets to broadcast its block
4. Other nodes accept the block only if all transactions in it are valid (unspent, valid signatures)
5. Nodes express their acceptance of the block by including its hash in the next block they create
What can a malicious node do?
Double- spending attack
signed by A
PaytopkB :H( )
signed by A
PaytopkA’ :H( )
Honest nodes will extend the longest valid branch
From Bob the merchant’s point of view
1 confirmation 3 confirmations
Double-spend probability decreases exponentially with # of confirmations
Most common heuristic: 6 confirmations
double-spend attempt
Hear about CA → B transaction 0 confirmations
Protection against invalid transactions is cryptographic, but enforced by consensus
Protection against double-spending is purely by consensus
You’re never 100% sure a transaction is in consensus branch. Guarantee is probabilistic
Introduction
SMART CONTRACTS I
• Youwillbeabletoexplain:
– Elements of a smart contracts
– Problems that can be solved by smart contracts – Structure of a smart contracts
– Understand smart contracts
– Use remix
Objectives:
Smart Contract
Image source: https://www.kdpllp.com/how-to-build-trust-with-customers-online/ and Wikipedia
1. Design 2. Code
3. Deploy 4. Execute 5. Test
Development steps
• LikeBitcoinscriptfeature
• HyperledgerChaincode
• Dockerenvironment(Virtualmachine) • Smart Contract
Smart Contract Basics: Why Smart Contracts?
Image source: https://codebrahma.com/brief-intro-smart-contracts-endless-possibilities/
1. Transfers assets other than value or cryptocurrency
2. Specifies rules for an operation on blockchain
3. Implements policies for transfer of assets in decentralized networks
4. Represents business logic layer
5. Includes messages that invoke functions
Applications
Ethereum Stack
• CurrencyTransfer – Service
– Product – Utility
Smart Contracts Defined (Part 1) (Remix IDE and Greeter Demos)
• Definethestructureofsmartcontracts
• Understandsmartcontracts
• UseRemixtoinvoke/interactwithsmartcontracts
Application-specific semantics
• Immutable recording & Trust model
• Application-specific semantics & constraints of the
transaction
– Name of the contract, Identifiers, State variables
– Use ASCII character set, and camel case convention – Include collection of functions
• Remix.Ethereum.org
IDE Layout
https://remix-ide.readthedocs.io/en/latest/layout.html
Solidity Editor
Here’s the list of some important features:
• It display opened files as tabs.
• Compilation Warning and Error are displayed in the gutter
• Remix saves the current file continuously (5s after the last changes)
• +/- on the top left corner enable you to
increase/decrease the font size of the editor
Features, available in the terminal:
• It enables the execution of the JavaScript script which interacts with the current context.
• It displays important actions made while interacting with the Remix IDE (i.e. sending a new transaction).
• It displays transactions that are mined in the current context.
• It allows searching for the data and clearing the logs from the terminal.
• You can run scripts by inputting them at the bottom after the >.
Design > Code > Test
Image: https://dribbble.com/shots/5616613-design-code-test
A Greeter (Hello World)Class Diagram
Name of the Smart Contract
String type variable
Collection of function:
• Constructor function
• Set function
• Hello function
Greeter Solidity Code
State (computer science)
• Ininformationtechnologyandcomputerscience,a system is described as stateful if it is designed to remember preceding events or user interactions; the remembered information is called the state of the system.
https://en.wikipedia.org/wiki/State_(computer_science)
Run the smart contract code
Compiler (Solidity)
• ClickingtheSolidityiconin the icon panel brings you to the Solidity Compiler.
• (F)Compilingistriggered.
• (D)Ifyouwantthefiletobe compiled each time the file is saved – check the auto compile checkbox.
https://remix-ide.readthedocs.io/en/latest/compile.html
Deploy & Run
• The Deploy & Run module allows you to send transactions to the current environment.
• To use this module, you need to have a contract compiled.
• JavaScript VM: All the transactions will be executed in a sandbox blockchain in the browser. This means nothing will be persisted when you reload the page. The JsVM is its own blockchain and on each reload it will start a new blockchain, the old one will not be saved.
• Account: the list of accounts associated with the current environment (and their associated balances). On the JsVM, you have a choice of 5 accounts. If using Injected Web3 with MetaMask, you need to change the account in MetaMask.
• Gas Limit: This sets the maximum amount of gas that will be allowed for all the transactions created in Remix.
• Value: This sets the amount of ETH, WEI, GWEI etc that is sent to a contract or a payable function.
https://remix-ide.readthedocs.io/en/latest/run.html
Smart Contracts Defined (Part 2) (Simple Storage Demo)
SimpleStorage
• Decentralization • SmartContract • RemixIDE
• Solidity
References:
History of payment systems and the road to Bitcoin
1. Narayanan et al. Ch2
2. Haber and Stornetta, 1991, “How to Time-Stamp a Digital Document.”
3. Nakamoto, 2008, “Bitcoin: A Peer-to-Peer Electronic Cash System.”
4. Solidity Document
5. Remix IDE Document
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com