Slide 1

Slide 1 text

Programming Languages × Blockchains Vlad Ki ESM.one

Slide 2

Slide 2 text

Mainstream PLs won't work

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

DApps in 2013

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

DApps in 2018

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

function createGen0Auction(uint256 _genes) public onlyCOO { require(gen0CreatedCount < gen0CreationLimit); uint256 kittyId = _createKitty(0, 0, 0, _genes, address(this)); _approve(kittyId, saleAuction); saleAuction.createAuction(kittyId, _computeNextGen0Price(), 0, gen0AuctionDuration, address(this)); gen0CreatedCount++; } function _computeNextGen0Price() internal view returns (uint256) { uint256 avePrice = saleAuction.averageGen0SalePrice(); require(avePrice < 340282366920938463463374607431768211455); uint256 nextPrice = avePrice + (avePrice / 2); if (nextPrice < gen0StartingPrice) { nextPrice = gen0StartingPrice; } return nextPrice; }

Slide 13

Slide 13 text

#!/bin/bash while { echo -ne "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nlol\r\n"; } \ | nc -l 80 do : done

Slide 14

Slide 14 text

WTF?!

Slide 15

Slide 15 text

Developers are losing abstractions!

Slide 16

Slide 16 text

ACID Atomic Consistent Isolated Durable

Slide 17

Slide 17 text

BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; UPDATE accounts SET balance = balance + 100 WHERE addr = 0x4206f95fc533483; UPDATE accounts SET balance = balance - 100 WHERE addr = 0x083c41ea13af6c2; COMMIT;

Slide 18

Slide 18 text

SELECT address FROM accounts ORDER BY random() LIMIT 1;

Slide 19

Slide 19 text

CREATE SEQUENCE txn; BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; INSERT INTO tx VALUES (nextval('txn'), current_timestamp); INSERT INTO tx VALUES (nextval('txn'), current_timestamp); COMMIT;

Slide 20

Slide 20 text

BASE Basically Available Soft State Eventually Consistent

Slide 21

Slide 21 text

http:/ /docs.basho.com/riak/kv/2.2.3/learn/concepts/clusters/

Slide 22

Slide 22 text

$ curl http://riak/buckets/chain/keys/0x4206f95fc533483 < HTTP/1.1 300 Multiple Choices < X-Riak-Vclock: a85hYGDgyGDKBVIszMk55zKYEhnzWBlKIniO8kGF2TyvHYIKf0cIszUnMTBz HYVKbIhEUl+VK4spDFTPxhHzFyqhEoVQz7wkSAGLMGuz6FSocFIUijE3pt5H lsgCAA== < Content-Type: multipart/mixed; boundary=lol --lol Content-Type: application/json Link: ; rel="up" Etag: 16vic4eU9ny46o4KPiDz1f Last-Modified: Wed, 10 Mar 2010 18:01:06 GMT 100 --lol Content-Type: application/json Link: ; rel="up" Etag: 4v5xOg4bVwUYZdMkqf0d6I Last-Modified: Wed, 10 Mar 2010 18:00:04 GMT 200

Slide 23

Slide 23 text

Logical Clocks https:/ /cacm.acm.org/magazines/2016/4/200168-why-logical-clocks-are-easy/abstract

Slide 24

Slide 24 text

https:/ /hal.inria.fr/file/index/docid/555588/filename/techreport.pdf

Slide 25

Slide 25 text

Definition counter : Type := nat * nat. Definition zero := (O, O). Definition succ (c : counter) := match c with (p, n) => (p + 1, n) end. Definition pred (c : counter) := match c with (p, n) => (p, n + 1) end.

Slide 26

Slide 26 text

$ curl -XPOST http://riak/types/counters/buckets/ chain/datatypes/0x4206f95fc533483 \ -d '{"increment": 100}' $ curl -XPOST http://riak/types/counters/buckets/ chain/datatypes/0x4206f95fc533483 \ -d '{"decrement": 50}' $ curl http://riak/types/counters/buckets/chain/ datatypes/0x4206f95fc533483 {"type":"counter", "value": 100}

Slide 27

Slide 27 text

В риаке конфликты на чтение Пиши в блокчейн! А зачем? Глобальный консенсус! Так языка запросов даже нет! Ты чё, пёс, это же крипта!

Slide 28

Slide 28 text

UTXO Unconvincing Transaction Outcome

Slide 29

Slide 29 text

Smart Contracts Smart contracts help you exchange money, property, shares, or anything of value in a transparent, conflict-free way while avoiding the services of a middleman.

Slide 30

Slide 30 text

european :: Date -> Contract -> Contract european t u = at t (u `or` zero)

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Smart Contracts Smart contracts are executable programs run on top of an immutable distributed database whose inputs and outputs are maintained globally consistent by a distributed consensus protocol.

Slide 33

Slide 33 text

pragma solidity ^0.4.0; contract SimpleStorage { uint storedData; function set(uint x) public { storedData = x; } function get() public constant returns (uint) { return storedData; } }

Slide 34

Slide 34 text

function createGen0Auction(uint256 _genes) public onlyCOO { require(gen0CreatedCount < gen0CreationLimit); uint256 kittyId = _createKitty(0, 0, 0, _genes, address(this)); _approve(kittyId, saleAuction); saleAuction.createAuction(kittyId, _computeNextGen0Price(), 0, gen0AuctionDuration, address(this)); gen0CreatedCount++; } function _computeNextGen0Price() internal view returns (uint256) { uint256 avePrice = saleAuction.averageGen0SalePrice(); require(avePrice < 340282366920938463463374607431768211455); uint256 nextPrice = avePrice + (avePrice / 2); if (nextPrice < gen0StartingPrice) { nextPrice = gen0StartingPrice; } return nextPrice; }

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec5 97f00b1f6f3dd2d66f4c9c6c445836d8b§234') .then(console.log); > { "hash": "0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c 445836d8b", "nonce": 2, "blockHash": "0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e 133e34b46", "blockNumber": 3, "transactionIndex": 0, "from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f", "value": '123450000000000000', "gas": 314159, "gasPrice": '2000000000000', "input": "0x57cb2fc4" }

Slide 37

Slide 37 text

Why?

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

ERC20 allowance contract ERC20 is ERC20Basic { function transferFrom(address from, address to, uint256 value) public returns (bool); function approve(address spender, uint256 value) public returns (bool); } https:/ /docs.google.com/document/d/ 1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit https:/ /github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Ensure Properties!

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Coq Dependent Type Theory Tactics Extraction to OCaml and Haskell Formalization of mathematics

Slide 46

Slide 46 text

Theorem pred_and_succ_covariant: forall c: counter, pred (succ c) = succ (pred c). intros c. induction c. compute. reflexivity. Qed.

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

pragma solidity ^0.4.0; contract Fund { mapping(address => uint) shares; function withdraw() public { if (msg.sender.call.value(shares[msg.sender])()) shares[msg.sender] = 0; } }

Slide 50

Slide 50 text

The DAO https:/ /abhiroop.github.io/Exceptions-and-Transactions/

Slide 51

Slide 51 text

Interface State Machine Plutus Plutus Core Solidity EVM (stack) IELE (register) Chain Bitcoin Script Ivy Vyper

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Hardening Strategies Interfaces (*.h, *.mli) Interfaces + property claims (f . g = g . f) Interfaces + property proofs Implementation property claims Implementation property proofs

Slide 55

Slide 55 text

Hardening Strategies End-to-end compilation Verify interfaces in Coq Use plutus core/iele as compilation targets

Slide 56

Slide 56 text

Contracts as State Machines

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

Denotate to simple languages!

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

System F

Slide 61

Slide 61 text

Turing vs Church

Slide 62

Slide 62 text

Interface State Machine Plutus Plutus Core Solidity EVM (stack) IELE (register) Chain Bitcoin Script Ivy Vyper

Slide 63

Slide 63 text

Interface State Machine Plutus Plutus Core Solidity EVM (stack) IELE (register) Chain Bitcoin Script Ivy Vyper run . compile = meaning

Slide 64

Slide 64 text

Gotcha

Slide 65

Slide 65 text

gcd :: (Integral a) => a -> a -> a gcd x y = gcd' (abs x) (abs y) where gcd' a 0 = a gcd' a b = gcd' b (a `rem` b)

Slide 66

Slide 66 text

Lemma euclid_rec : forall v3 : Z, (0 <= v3)%Z -> forall u1 u2 u3 v1 v2 : Z, (u1 * a + u2 * b)%Z = u3 -> (v1 * a + v2 * b)%Z = v3 -> (forall d : Z, gcd u3 v3 d -> gcd a b d) -> Euclid. Proof. intros v3 Hv3; generalize Hv3; pattern v3 in |- *. apply Z_lt_rec. clear v3 Hv3; intros. elim (Z_zerop x); intro. apply Euclid_intro with (u := u1) (v := u2) (d := u3). assumption. apply H2. rewrite a0; auto. set (q := (u3 / x)%Z) in *. assert (Hq : (0 <= u3 - q * x < x)%Z). replace (u3 - q * x)%Z with (u3 mod x)%Z. apply Z_mod_lt; omega. assert (xpos : (x > 0)%Z). omega. generalize (Z_div_mod_eq u3 x xpos). unfold q in |- *. intro eq; pattern u3 at 2 in |- *; rewrite eq; ring. apply (H (u3 - q * x)%Z Hq (proj1 Hq) v1 v2 x (u1 - q * v1)%Z (u2 - q * v2)%Z). tauto. replace ((u1 - q * v1) * a + (u2 - q * v2) * b)%Z with (u1 * a + u2 * b - q * (v1 * a + v2 * b))%Z. rewrite H0; rewrite H1; trivial. ring. intros; apply H2. apply gcd_for_euclid with q; assumption. assumption. Qed.

Slide 67

Slide 67 text

twitter.com/kievfprog kievfprog.net

Slide 68

Slide 68 text

Haskell Rust Scala F# F* Ur Agda Idris Coq SML OCaml QuickCheck SMT

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Yay Formal Methods! [email protected]