Slide 1

Slide 1 text

BLOCKCHAIN FOR SHARED STATE ON THE WEB SWIZEC TELLER

Slide 2

Slide 2 text

@SWIZEC HELPING YOU BECOME A BETTER ENGINEER Hi I’m Swizec, I help you become a better engineer teaching React with books, workshops, training software engineer in San Francisco that’s me doing my learn while you poop series

Slide 3

Slide 3 text

CRAZY IDEA TIME @SWIZEC today I want to share a crazy idea it’s a research project of sorts, something I’ve been playing around with when I have time

Slide 4

Slide 4 text

@SWIZEC BLOCKCHAIN IS LIKE THE WEB IN 1995. NERDS ARE EXCITED ABOUT IT, NORMIES SMELL SOMETHING BREWING, NOBODY KNOWS WHAT TO DO WITH IT story time! it was a cozy december night when I was hanging out on Telegram as one does it was peak blockchain craze, normie friends asking me about it for the first time we were talking about what stage of the cycle we’re in then someone mentioned Redux, as one does

Slide 5

Slide 5 text

BLOCKCHAIN REDUX?? @SWIZEC and that gave me an idea … could you build blockchains with normal webapp tools? use it to store all your UI state?

Slide 6

Slide 6 text

WHAT’S A BLOCKCHAIN ANYWAY? @SWIZEC let’s backtrack for a bit what is a blockchain? how does it work? can it do more than crypto?

Slide 7

Slide 7 text

VERIFIED REVERSE-LINKED LIST @SWIZEC a blockchain is just a hash list each block holds some data and a reference to the previous block that reference happens to be a hash of all preceding state once you add forks, it’s a merkle tree

Slide 8

Slide 8 text

AND IT’S IMMUTABLE @SWIZEC and the data is immutable once on the blockchain, always on the blockchain

Slide 9

Slide 9 text

REDUX ALSO IS IMMUTABLE @SWIZEC ok so what’s redux redux is a state management library it uses actions to make copies of state with some changes you could say that it’s a chain of state linked by actions also, immutable

Slide 10

Slide 10 text

EH? @SWIZEC you keep saying immutable and talking about immutability who cares?

Slide 11

Slide 11 text

TEXT WHY IMMUTABILITY ‣ easier to debug ‣ time travel ‣ easier to think about ‣ distributable @SWIZEC you care because immutability makes your life easier plus you can distribute immutable computation

Slide 12

Slide 12 text

WAIT … DISTRIBUTABLE? @SWIZEC wait, we’re on to something blockchain and redux sketches looked similar they’re both distributable and immutable we can work with this!

Slide 13

Slide 13 text

BLOCKCHAIN-REDUX @SWIZEC and so I started working on blockchain-redux a library that lets anyone create a new blockchain, keep their state in blocks transparently distributed, handles communication and so on all you need are the tools you already know

Slide 14

Slide 14 text

@SWIZEC @SWIZEC it’s still a proof of concept at best but last night I built a chat app in about an hour that’s because I don’t know material-ui and wanted to make it pretty

Slide 15

Slide 15 text

TEXT ANYONE CAN BLOCKCHAIN – INIT this.blockchain = await createStore( chatReducer, firebaseMiddleware(FirebaseApp) ); this.unsubscribe = this.blockchain.subscribe(() => { console.log("Update received"); this.forceUpdate(); }); console.log("Blockchain inited!"); this.forceUpdate(); @SWIZEC

Slide 16

Slide 16 text

TEXT ANYONE CAN BLOCKCHAIN – ACTION sendMessage = ({ username, message }) => { this.blockchain.dispatch( addMessage({ username, message }) ); }; @SWIZEC

Slide 17

Slide 17 text

TEXT ANYONE CAN BLOCKCHAIN – REDUCER sendMessage = ({ username, message }) => { this.blockchain.dispatch( addMessage({ username, message }) ); }; @SWIZEC

Slide 18

Slide 18 text

TEXT ANYONE CAN BLOCKCHAIN – REDUCER const messageReducer = function(state = [], action) { switch (action.type) { case "ADD_MESSAGE": let { username, message } = action; return [ ...state, { user: username, message } ]; default: return state; } }; @SWIZEC

Slide 19

Slide 19 text

THAT’S IT @SWIZEC

Slide 20

Slide 20 text

HERE’S HOW IT WORKS @SWIZEC

Slide 21

Slide 21 text

@SWIZEC

Slide 22

Slide 22 text

@SWIZEC

Slide 23

Slide 23 text

@SWIZEC

Slide 24

Slide 24 text

ELEPHANT!? @SWIZEC

Slide 25

Slide 25 text

YES IT’S CENTRALIZED MY CONSENSUS ALGO SUCKS @SWIZEC

Slide 26

Slide 26 text

WEBRTC IS HARD @SWIZEC

Slide 27

Slide 27 text

TEXT WEBRTC WAT? ‣ peer to peer communication between browsers ‣ can send data ‣ (also video and audio but eh) ‣ needs signaling server to perform handshake

Slide 28

Slide 28 text

@SWIZEC

Slide 29

Slide 29 text

@SWIZEC

Slide 30

Slide 30 text

TEXT CAVEATS? MANY ‣ what if everyone leaves site? ‣ consensus is hard ‣ might get slow ‣ how do you handle PWA @SWIZEC

Slide 31

Slide 31 text

IS IT USEFUL? @SWIZEC

Slide 32

Slide 32 text

IT’S DEFINITELY COOL @SWIZEC

Slide 33

Slide 33 text

SWIZEC.COM HIRE ME TO HELP YOU OR YOUR TEAM BECOME BETTER ENGINEERS