+ - 0:00:00
Notes for current slide
Notes for next slide

DApps Dev Club S01E02 deck QR code

1 / 52

Decentralised Applications Development Club

DApps Dev Club

Technical Overview

12/03/2019

2 / 52

Today

  • Partners
  • Intro
  • Ethereum Virtual Machine
  • Smart Contracts
  • Web3.js
  • Next session
  • Announcements
4 / 52

Partners

5 / 52

Partners

Chainstack

Chainstack

6 / 52

Partners

Chainstack

Spartan

Spartan Group

7 / 52

Partners

Chainstack

Spartan

NBC'19

National Blockchain Challenge '19

8 / 52

Partners

Chainstack

Spartan

NBC'19

StartupToken

StartupToken meetup

9 / 52

Partners

Chainstack

Spartan

NBC'19

StartupToken

EngineersSG

Engineers.SG

10 / 52

Intro

11 / 52
  • Before we go into the the topics, for the benefit of those who are attending the their first session today

Intro

DADC

DApps Dev Club

  • Decentralised Applications Development Club
  • Format inspired by the technical book club from SingaporeJS
  • dappsdev.org
  • our discord
12 / 52
  • The DApps Dev Club meets twice a month, and this is our 2nd session
    • In each session, we plan to discuss about 3 or four different topics, selected from the Mastering Ethereum book
  • We are a technical book club, so in each session, when we meet up together, we'll be discussing select parts from this book, plus working through things together
    • I should mention that while this is not a common format, it isn't completely new - in fact it is inspired by the book club that the SingaporeJS meetup runs
  • The nexus for all the club happennings are on this website, dappsdev.org
    • The main thing to look out for on the website are the news feed, which you can subscribe to via the RSS feed, or check back on manually
    • The other thing to loom out for on the website is the sessions page, which is a calendar for all of the events
  • Apart from meeting in person, we can also have discussions online
    • We have a discord server for that, and you can join using the link found on our website

Intro

DADC

Kenneth

  • Also runs the Blockchain&DApps meetup
  • CTO of Bytepay

Kenneth Hu

13 / 52
  • Kenneth is the CTO of Bytepay
  • He knows the Ethereum stack inside out
  • He also runs a blockchain events across Taiwan, Malaysia, and Singapore

Intro

DADC

Kenneth

Brendan

  • Started DApps Dev Club
  • NodeJS developer
14 / 52
  • I'm Brendan, and I work as a software engineer
  • Spend most of my time doing NodeJs
  • Met Kenneth through the EthSG hackathon last year
  • We to collaborate and create a series of educational blockchain meetups, and here we are

Intro

DADC

Kenneth

Brendan

You!

  • No blockchain knowledge required
  • No solidity or smart contract knowledge required
  • Basic Javascript needed in session #05
  • wen ico? wen moon?
15 / 52
  • So, how about you? These sessions are designed with a particular audience in mind.
  • If you know nothing about blockchain, or about smart contracts, that's perfect
  • However, if you don't know some basic JS, you'll need to pick that up to follow along in the later sessions.
    • I'll post some resources that you can use to learn JS after this session, or come talk to me, or ask around in our discord
  • Bring your laptops to the sessions, as you'll be able to paerticipate in the hands on parts of the sessions

Ethereum Virtual Machine

16 / 52
  • So today's topics are the EVM, Smart Contracts, and Web3
  • Mastering Ethereum Chapter 13
  • These are all pretty intertwined, and IMO, you do need to understand all of them, at a basic level, before you can properly understand any one of them
  • The goal is that at the end of this session, you will understand what the purpose of each of these three things is, and how they work with each other.

EVM

Broadly

17 / 52
  • The EVM is a global computer, where every single ethereum node runs a compliant virtual machine
  • The virtual machine executes functions stored in programs (AKA smart contracts), which we'll come to later
  • It is said to be Turing complete
    • Contrast with bitcoin's stack-based scripts, which are non-Turing complete
    • Implication: Can, in theory, write any type of general purpose application
    • Implication: Need a way to prevent infinite resource consumption
    • Quasi --> solution is gas. halt function execution when run out of gas

EVM

Broadly

Distributed Computation

  • EVM + blockhain
  • "Distributed programming is the art of solving the same problem that you can solve on a single computer using multiple computers." - mixu
  • CAP theorem: Choose 2 out of 3 of these
    • Consistency, Availability, Partition tolerance
18 / 52
  • Using blockchain to ensure that consensus is achieved between nodes, one can use EVM + blockchain to achieve a general purpose distributed computation model
  • it is worthwhile reading up about the distributed computing, and analysing Ethereum through the lens of it
    • for example, where does Ethereum sit within the CAP theorem?
    • with sharding, and without sharding?
  • We will not be going through this is detail here, but I think it is worthwhile doing a deep dive into this on your own
    • If you're interested, start a discussion on our discord

EVM

Broadly

Distributed Computation

Execution/1

19 / 52
  • So what actually happens when an Ethereum node receives a request to execute something on the EVM?
  • It reads the EVM byte code
  • Each byte code is just a number, but it corresponds to a particular low level instruction...

EVM

Broadly

Distributed Computation

Execution/2

20 / 52
  • And these are the low level instructions that those numbers correspond to.
  • If you're familiar with assembly language, this is the same concept
    • Byte code has a 1-to-1 correspondence with op-codes
    • op-codes are slightly more human readable, but still too low level for most of us, so that's where solidity comes in. more on this later!
    • op-codes, and therefore byte codes, are intended mainly as compiler targets

EVM

Broadly

Distributed Computation

Execution/3

  • Opcode & bytecode
    • Software level abstraction over hardware instructions on different platforms
  • Side note: Virtual machines?
    • Same type as JVM, or .NET CLR
    • Not the same type as Virtual Box
  • Is not intended to be human readable
21 / 52
  • The node reads the EVM byte code, and runs each of those instructions in turn, until it is complete, or runs out of gas
  • Every invocation is sandboxed, and you can think of this as being analogous to a database queries wrapped in a transaction so that it can be rolled back
  • note about the other type of VM

EVM

Broadly

Distributed Computation

Execution

Implementations

  • The abstraction via op-codes enables multiple implementations
  • Currently
    • Go - geth: core/vm
    • Rust - parity: ethcore
    • C++ - cpp-ethereum: libevm
    • Python - py-evm
    • Java - ethereumJ
    • many more
22 / 52
  • Since op-codes are an abstraction, this gives developers the freedom to create their own implementation of the virtual machine itself
  • Choice of programming language, choice of operating system to compile on
  • Can even benefit from reusing the same test harness to test your own implementation against an existing one while bootstrapping

Hands on

remix: bit.do/dadc-remix

gist: bit.do/dadc-sol

remix   gist

23 / 52
  • time to crack out your laptops, if you've brought them!

Smart Contracts

24 / 52

Smart Contracts

What & Why

  • What
    • Ethereum's interpretation: General purpose computation that takes place on a blockchain or distributed ledger
  • Why
    • Negotiate and enforce an agreement or a transaction
    • A means to achieve decentralised automation
25 / 52
  • the term was first coined by nick szabo
  • smart contracts can mean different things in different contexts
  • in ethereum, they really are general purpose computations encoded by programs stored on its blockchain
  • they are useful in many ways, but the most notable one is that it can automate tasks and execution in a decentralised manner
  • advanced used case: build a decentralised autonomous organisation

Smart Contracts

What & Why

How

  • How
    • Code stored on a blockchain, running on participating nodes
    • Set of rules that defines how they interact with each other
    • When rules are met, and consensus is achieved, they are enacted, and added to the next block
26 / 52
  • the key to how it works:
    • all nodes operate by the same set of rules for execution
    • decentralised consensus required for state

Smart Contracts

What & Why

How

Blockchain/1

  • The EVM is embedded in a blockchain state divided into accounts
  • All accounts in ethereum are stored in a Merkle radix tree
  • Programs in the EVM live in accounts known as contracts. In addition to an address, a balance, and a sequence number (equal to the number of transactions sent by the account - also known as a nonce)
  • Contracts keep the hash of their EVM bytecode, and the Merkle root of their internal storage tree
27 / 52
  • let's delve a little into exactly what anchors a smart contract into the blockchain
  • this guide by CoinCulture describes this in detail, right down to the data structures used
  • we will not go into this level of detail here, but if you're interested, read the page that's linked on this slide, it's extremely detailed

Smart Contracts

What & Why

How

Blockchain/2

  • An account can have at most one program associated with it - any time a transaction is made to the contract, or it is the target of another contract executing the CALL opcode, the code of that contract will execute
  • Note that once deployed, the code of a contract may not be changed. The Merkle root of the account/contract storage is updated after any successful transaction where execution of the SSTORE opcode results in a value being stored at a new key or a change to the value stored at an existing key.
28 / 52
  • (continues)
  • the main things to take away from this is that there are 2 things stored on the blockchain:
    1. the byte code of the smart contract itself
    2. the stored state of the smart contract

Smart Contracts

What & Why

How

Blockchain

Code + State

  • Code + State
  • State can be updated via SSTORE opcode
    • and SLOAD reads state
  • Ethereum's blockchain contains a ledger of every single state change
29 / 52
  • Before we delve into this - what is state? let's define it
    • data stored either in memory, or persisted
    • important to know that for any data, what are the rules/ conditions for changing it?
  • As we've just established, Ethereum stores both code and state on the blockchain
  • The only thing that can write code is deploy and kill - i.e. analogous to create and delete operations, there is no update equivalent
  • OTOH, state can indeed be updated

Smart Contracts

What & Why

How

Blockchain

Code + State

Storage costs/1

  • it costs 20K gas to store a word (8 bytes)
  • ETH-SGD exchange rate
  • Cost per word = 20000 gas * (10 * 10^-9 ETH/gas) * (188 SGD/1 ETH) = 0.0376 SGD
  • Cost per megabyte = 4700 SGD
30 / 52
  • ETH/SGD exchange rate from CoinHako
  • Let us work out what the real cost of storing data on Ethereum is right now
  • Using the current price of around 188 SGD per ETH, we do some multiplication, and work out that 8 bytes costs just under 4 cents to store
  • This means that 1MB takes close to 5K dollars to store
  • In the kickoff session, we mentioned that smart contract data storage is extremely expensive in a hand-wavey sort of way - but now we have a precise figure
  • This is quite possibly the world's most expensive storage system

Smart Contracts

What & Why

How

Blockchain

Code + State

Storage costs/1

  • Poll: If you work on a CApp, what is the size of your database?
  • Off-chain storage
  • Disbelief?
31 / 52
  • If you are a developer, this gives you an extremely heavy-handed constraint, and in some cases it even forces you away from writing certain types of applications
  • A common middle ground amongst developers is that they write hybrid CApps and DApps, where the storage is shifted off of the blockchain, and only a hash of this off-chain stored state is stored on-chain, for consensus purposes
    • Poll: Shall we recap what a hash is?
  • Now that the headline dollar amount has set in, some of you will be reacting, based on gut feel, that something cannot be right - perhaps the math is wrong, etc. But consider the following:
    1. We're not just storing the latest state, but all the state that precedes it
    2. The storage is massively redundant, meaning that many many different computers each store their own copies of this data
    3. You need pay for the nodes to make it worthwhile to do all this storage for you

Smart Contracts

What & Why

How

Blockchain

Code + State

Storage costs/3

  • The blockchain is huge, despite costs
    • At block ~56 million
    • geth "fast sync": 132 GB
    • geth "full archive": 2191 GB
32 / 52
  • screen shots of these charts coming up next
  • Despite the fact that storing data is so expensive, the size of the blockchain is huge
    • Note that this is not just smart contract data, but also everything else in the blockchain
    • I would like to see someone do an analysis breaking down the percentage each type of data takes up on ethereum's blockchain

Hands on

Time to open your laptops!

Code State Blockchain Demo Instructions

35 / 52
  • We're going to do a demo

Get the git repo

git clone git@github.com:dapps-dev-club/blockchain-demo.git
cd blockchain-demo
git fetch origin feat/codestate:feat/codestate
git checkout feat/codestate
36 / 52

Install and start

npm install
npm run start
# open http://localhost:3000/
37 / 52

So, what's new?

38 / 52
  • You can't have possibly seen this before, because I literally made this at midnight last night!
  • Then begin the demo
const x = (state.x || 0) + args[0];
return { ...state, x };

Arguments:

[1]
40 / 52

Recap

  • Blockchain: immutable ledger
  • EVM: Distributed computing
  • Smart contracts: Programs deployed
  • ... then?
41 / 52
  • So let's do a recap of where we are up to at this point:
  • We have the blockchain which can be thought of as a distributed immutable ledger storing state transitions
  • We have the EVM which can execute programs in a distributed computation manner, and stores state changes from of those programs on the blockchain
  • We have smart contracts which are these programs, deployed onto the blockchain
  • That's what we have so far, but now we have all of these programs, but none of them do anything, because you have no way to start them

Web3

42 / 52

Web3

What & Why

How

  • Web.js
    • Javascript API to talk to Ethereum nodes
    • Uses the JSON-RPC specification
    • Connects to nodes via websockets: ws://...
  • Why is it needed?
    • Allow users to run mart contract functions
    • Need a way to do so from a web app
43 / 52
  • That is where web3 comes - we use it to talk to smart contracts
  • Web.js is ...
  • Technically, you could roll your own client-side cryptography implementation, and JSON-RPC implementation, plus other things
    • but why would you?
  • web3.js provides a standardised API for various web developers to call smart contracts

Web3

What & Why

How

Alt impls

  • Alternative web3 implementations
    • web3.js is not the only one
    • similar functionality in python, java, etc
  • Develop DApps that are not web applications
    • Less common
44 / 52
  • The terms web3 and web3.js are often used interchangeably
  • It is worth noting that the JS implementation, which is by far the most common one, is not the only one, and there are others targeting different programming languages
  • If you are developing a DApp where the client is not a web app, these will come in handy
  • For the most part though, DApps are web apps
  • In our sessions, we will focus on web3.js - as these alternate varieties are for more advanced users

Web3

What & Why

How

Alt impls

Provider/1

  • How to get web3 in your browser?
  • Give in-browser JS access to you Ethereum private keys?
45 / 52
  • web3.js is a library that has all the utility functions that a client needs in order to communicate with Ethereum nodes
  • but how do we get an instance into the browser?
  • one approach might be to load it up like any other JS library
  • while that works in the general sense, it is important to note that web3.js also needs access to your private keys
  • you cannot trust JS loaded in the browser with your private keys - any funds you have are likely to be stolen very quickly!

Web3

What & Why

How

Alt impls

Provider/2

  • inject a web3 provider
  • done via a browser extension, e.g. MetaMask
    • contains your private keys
  • acts as both wallet and web3 provider
    • wallet: manage accounts and their keys
    • web3 provider: sign transactions, communicate with Ethereum nodes
46 / 52
  • instead we use a browser extension to do this
  • MetaMask is the most common one that is used
  • browser extensions can form the bridge between software that run locally on your device, vs software that runs in the browser
  • the browser extension "injects" a global variable named web3
    • in newer versions of MetaMask, it instead injects an ethereum global from which you have to obtain the web3 variable asynchronously

Web3

What & Why

How

Alt impls

Provider/3

47 / 52
  • compare to AJAX requests. Two differences
    • Need to "aprove" via browser extension
    • Takes much longer
  • in-browser JS has access to a web3 variable
  • subsequent JS code can then access all of the utility functions
  • when a TX needs to be signed
    • this is either a transfer or a smart contract call
    • the in-browser JS execution hands over to the browser extension, which has a pop-up dialog that prompts the user with the details of what the DApp has asked it to do
    • if the user approves the transaction, the extension accesses the private key - without revealing it to the in-browser JS - and signs the TX
    • the signed TX data is returned to the in-browser JS, which at this point will send it to the Ethereum network

What can you make?

  • store information/ records
  • multi-signature schemes
  • manage agreements between users
  • expose functions for others to call
48 / 52
  • these are generic things that ethereum smart contracts enable you to do
  • based on these, what sorts of DApps can you build?
  • NIST has made a handy flowchart that helps you to decide whether you really need a blockchain to build your app on
  • The bent of this is that there are many things being built on blockchain that could be feasibly built using other technologies instead
  • However, I see this a "why?" vs "why not?" sort of thing
  • That being said, it is worth while weighing the pros and cons of any technology when you're building something, and using Ethereum, or the blockchain, is no exception

Next session

50 / 52
  • we have our next session scheduled for 2 Tuesdays from today
  • in our next session, we'll be looking properly at some code for the first time, and writing our first solidity
  • Crypto Zombies is a very gentle and fun intro to solidity - have a play with that if you can
  • Also, those of you who have read the couple of chapters ahead of this session: Thanks for doing so, and keeping with the spirit of a book club!
  • This week we have just one chapter, and that is chapter 7, which is about solidity
  • Finally, we would like to get all of you here to build some DApps together. In the kickoff session, we said to think of some ideas, and we'll help you to refine them.
  • If you've done just that, and already have some ideas for DApps you'd like to build with the club, pls come to the front
    • If you're shy, just stick around and talk about it with us, and those around you

Thanks!

Chainstack, Spartan, NBC'19, StartupToken, Blockchain&DApps

51 / 52
  • be sure to check out the sessions page for updates, and join our discord
  • any other announcements?

Decentralised Applications Development Club

DApps Dev Club

Technical Overview

12/03/2019

2 / 52
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow