Blockchain at Heart

How it works outside of the cryptocurrency trend

@m4d_z
alwaysdata logo
Let's talk about blockchain

It’s a revolution!

Blockchain Is the Most Disruptive Invention Since the Internet (?)

DuckDuckGo Research

It’s not only the cryptocurrency
it’s far simple
and even bigger

  • Decentralization
  • Smart Contracts
  • Mining
  • Resources Consumption
  • etc.

Nope

Let me tell you a little story

The first Blockchain in History
started in… 1995

The checksum is published
daily in the NYT

Crypto folks are clever

So Blockchain is more
a matter of Cryptography

Once upon a time
there was a Blockchain…

What is it?

A Blockchain,
is a series (a chain)
of structured data (blocks)

Still clever

Database

Immutable

But why?
  • Track the data
  • Certify

Decentralized & Distributed

  • Transparency
    all nodes handle all data
  • Redundancy
    No more Main node vs. Replicas

Authority

Public vs Private

The Byzantine Fault

Trusted Mutual Agreement

  • Proof of Work
  • Proof of Stake/Delegated Proof of Stake
  • Byzantine Fault Tolerance
    (Delegated BFT/Practical BFT)

PoW (Nakamoto agreement)

  • Resolve a computational puzzle
  • So called Mining
  • Eats a lot of power
  • The General is the 1st one to solve its Sudoku

PoS

  • Wealthy / Oldest Members
  • Time in the network is the key
  • Game theories
  • The General is always the oldest/more decorated

pBFT : Practical Byzantine Fault Tolerance

  • Control by trust
  • Closed Network, so private
  • The General is always the boss’ son
Not too lost?

Why using it on a private network?

  • Transparency/Confidence
  • BFT
  • Distributed

With a classical DB cluster

  • Primary / Secondaries
  • Primary write-mode / Secondaries read-only
  • Secondaries are replicas
  • ⇒ Cycle

With a Blockchain

  • All nodes in write-mode
  • Valid by mutual agreement
  • All nodes get complete work

Think it like
Git vs SVN

Automation

“Smart Contracts”

  1. Certified code, auto-executed
  2. On each transaction,
    ⇒ Smart Contracts
So, let me tell you... how it works!

Tech & Crypto

Restart from scratch

A Block

{
  index: 1,
  hash: "7A7D12...E62EC2",
  parent: "3A1A1D...684E1E",
  data: [...],
  timestamp: 1548237157237
}

Hash

SHA256({
  index: 1,
  parent: "3A1A1D...684E1E",
  data: [...],
  timestamp: 1548237157237
}) === "7A7D12...E62EC2"

Data

{
  data: [
    {hash:"67c...640", data: ..., timestamp: 1548237157237},
    {hash:"82a...503", data: ..., timestamp: 1548237157252},
  ]
}

A Blockchain

class Blockchain {
  constructor() {
    this.blocks = [{
      hash: "000000",
      index: 0,
      data: null,
      timestamp: Date.now()
    }]
  }
}

Adding a block

class Blockchain {...
  addBlock(data) {
    const block = {
      index: this.blocks.length,
      parent: this.blocks[this.blocks.length -1].hash
      data,
      timestamp: Date.now()
    }
    block.hash = CryptoJS.SHA256(`
      ${block.index};${block.parent};
      ${JSON.stringify(data)};${block.timestamp}
    `)
    return this.blocks.push(block)
  }
}

Adding a transaction

class Blockchain {...
  transaction(data) {
    const transaction = {
      data,
      timestamp: Date.now()
    }
    transaction.hash = CryptoJS.SHA256(`
      ${JSON.stringify(data)};${transaction.timestamp}
    `)
    return transaction
  }
}

In action

const blockchain = new Blockchain
blockchain.addBlock([
  blockchain.transaction({
    id: 2820628...37, name: 'Jane Doe', note: 'Registration'
  })
  blockchain.transaction({
    id: 2820628...37,
    prescription: [{
      name: 'Paracetamol',
      dosage: '3-6/day'
    }]
  })
])

Integrity

Moaaaar

blockchain.addBlock{[
  blockchain.transaction({
    id: 1940269...51,
    name: 'John Doe',
    note: 'Registration'
  })
]}
[{
  index: 0, hash: "000000", data: null, timestamp: 1548237157237
},{
  index: 1,
  hash: "7A7D12...E62EC2", parent: "000000",
  data: [{
    {hash:"67C...640", data: ..., timestamp: 1548237157237},
    {hash:"82A...503", data: ..., timestamp: 1548237157252}
  }], timestamp: 1548237157237
},{
  index: 2,
  hash: "6DE54C...87FEB7D", parent: "7A7D12...E62EC2",
  data: [{
    {hash:"AE4...953", data: ..., timestamp: 1548237157237}
  }], timestamp: 1548237157237
}]

Hashes

  1. Protect the whole chain integrity
  2. Change a single bit
    ⇒ recompute the whole chain
  3. Immutability

Signatures

Adding a client

class Blockchain {
  constructor() {...
    this.users = []
  }

  register(user) {
    const keys = GenKeyPair()
    user.pubKey = keys.pubKey
    this.users.push(user)
    return key.privKey
  }
}

Signing the transaction

class Blockchain {...
  transaction(data, privKey) {...
    transaction.sign = Sign(transaction.hash, privKey)
  }
}
blockchain.transaction({
  id: 1940269...51,
  name: 'John Doe',
  note: 'Registration'
}, Storage.privKey)

Verifying the transaction

class Blockchain {...
  findUserForTransaction(transaction) {
    return this.users.find(user => Verify(transaction.sign, user.pubKey))
  }
  checkTransaction(transaction) {
    return !!findUserForTransaction(transaction)
  }
}

Certify

class Blockchain {...
  register(user, privKey) {
    const keys = GenKeyPair()
    user.pubKey = keys.pubKey
    user.pubKey.sign = Sign(keys.pubKey, privKey)
    this.users.push(user)
    return key.privKey
  }
  checkUserKey(pubKey) {
    return !!this.users.find(user => Verify(pubKey.sign, user.pubKey))
  }
  checkTransaction(transaction) {
    const user = findUserForTransaction(transaction)
    return user ? checkUserKey(user.pubKey) : false
  }
}

Control the blocks

class Blockchain {...
  addBlock(data) {
    if (!data.every(this.checkTransaction)) {
      throw
    }
    ...
  }
}

Encrypt

const pharmacy = blockchain.getUser(pharmacyID)

transaction({
  id: 2820628...37,
  prescription: Encrypt(JSON.stringify([{
    name: 'Paracetamol',
    dosage: '3-6/day'
  }]), pharmacy.pubKey)
})

Synchronize

  • Broadcast UDP (dgram)
    • Transactions
    • Blocks
    • Metadata
  • Disk persistence
  • LRU (Least Recently Used) Local Cache

Agreement

Nakamoto Agreement

class Blockchain {...
  const inc = 5, prefix = Array(inc).fill(0).join('')
  addBlock(data) {...
    while (true) {
      block.nonce = Math.random()
      block.hash = CryptoJS.SHA256(`
        ${block.nonce};${block.index};${block.parent};
        ${JSON.stringify(data)};${block.timestamp}
      `)
      if (block.hash.substr(0, inc) === prefix) {
        this.blocks.push(block); break
      }
    }
  }
}

Code Is Law

Smart Contracts

class SmartContract {
  validate (transaction) {
    return true
  }

  exec (transaction) {
    return true
  }
}

At run

class Blockchain {...
  addContract (contract) {
    this.contracts.push(contract)
  }
  addBlock (data, ...) {...
    this.blocks.push(block)
    block.data.forEach(transaction => {
      this.contracts
      .filter(contract => contract.validate(transaction))
      .each(contract => contract.exec(transaction))
    })
  }
}

Nice!
Ready to ship it in production!

Nope, nope, nope, nope…

Cryptography!

Blockchain

  • is just a database
  • should be asynchronous
  • may be distributed

If your use-case need:

  • Traceability
  • Versionning (Immutability)
  • High availability
  • Resilience
  • Eventually w/o a central authority

The blockchain doesn’t interact very well IRL

  • Resources
    Nakamoto Agreement
  • Trust transmission
    Real → Virtual
  • N00b audience procurement
    e.g. doctors
  • Confidence
    Private Blockchain
Evaluate and make choices

Choose to use a blockchain with caution :
the technology is brilliant in terms of cryptography,
but not all that glitters is gold out there.
It’s just a tool in your toolbox.

m4dz's avatar
m4dz

Paranoïd Web Dino · Tech Evangelist

alwaysdata logo
https://www.alwaysdata.com

Questions?

Thank You!


Available under licence CC BY-SA 4.0

Illustrations

Wikipedia Commons

Interleaf images

Courtesy of Unsplash and Pexels contributors

Icons

All icons are from The Noun Project

Fonts

  • Cover Title: Sinzano
  • Titles: Argentoratum
  • Body: Mohave
  • Code: Fire Code

Tools

Powered by Reveal.js

Source code available at
https://git.madslab.net/talks