JavaScripter

Blockchain

Blockchain is a growing list of records, called blocks, which are linked using cryptography.



Blockchain analogy in < 100 words

You and your neighbors (“nodes”) have a spreadsheet of transactions (a ”ledger”). Public accountants (”miners”) have the same spreadsheet (it’s ”distributed”). When any neighbor makes a new transaction, every accountant is notified by email. The accountants verify transactions 247. Every hour, each accountant submits totals in a new tab (a “block”) and their calculations are provided (”Proof of Work”). If the majority of accountants agree, everyone updates their workbook (”blockchain”) and the first to report it accurately gets their salary ( ”mining reward”). The accountants use Excel to open spreadsheets, which Microsoft (“developers”) updates every year.

↑ Top

When to use blockchain?

Image
↑ Top

Proof of work/stake

Similar to Bitcoin, miners are tasked with solving a complex mathematical problem in order to successfully “mine” a block. Any computational problem that requires orders of magnitude more resources to solve algorithmically than it takes to verify the solution is a good candidate for proof of work (factorization of the product of two large prime numbers). In order to discourage centralisation due to the use of specialised hardware, as has occurred in the Bitcoin network, Ethereum chose a memory-hard computational problem which makes Ethereum’s Proof of Work ASIC-resistant.

Image
↑ Top

Ethereum

Ethereum
is a prograddmmable blockchain. Rather than give users a set of pre-defined operations (e.g. bitcoin transactions), Ethereum allows users to create their own operations of any complexity they wish. In this way, it serves as a platform for many different types of decentralized blockchain applications, including but not limited to cryptocurrencies. Exchanges of any complexity could be carried out automatically and reliably using code running on Ethereum. Whereas the Bitcoin blockchain was purely a list of transactions, Ethereum’s basic unit is the account. The Ethereum blockchain tracks the state of every account, and all state transitions on the Ethereum blockchain are transfers of value and information between accounts. There are two types of accounts:
  • Externally Owned Accounts (EOAs), which are controlled by private keys
  • Contract Accounts, which are controlled by their contract code and can only be “activated” by an EOA
EVM
At the heart of it is the Ethereum Virtual Machine (“EVM”), which can execute code of arbitrary algorithmic complexity. Ethereum is “Turing complete” and includes a peer-to-peer network protocol. Each and every node of the network runs the EVM and executes the same instructions in order to maintain consensus across the blockchain. Decentralized consensus gives Ethereum extreme levels of fault tolerance, ensures zero downtime, and makes data stored on the blockchain forever unchangeable and censorship-resistant. Users must pay transaction fees to the nodes that validate the network. This protects the Ethereum blockchain from frivolous or malicious computational tasks, like DDoS attacks or infinite loops. The sender of a transaction must pay for each step of the “program” they activated, including computation and memory storage.

↑ Top

Solidity

A language similar to JavaScript which is used for developing Ethereum smart contracts.

Smart Contract
A smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible. The aim of smart contracts is to provide security that is superior to traditional contract law and to reduce other transaction costs associated with contracting.

Flaws with Ethereum contracts
  • Ambiguities and easy-but-insecure constructs
  • Compiler bugs
  • Ethereum Virtual Machine bugs
  • Attacks on the blockchain network
  • The immutability of bugs
  • No central source documenting known vulnerabilities

Development Tools
  • Truffle is a tool for compiling Solidity contracts into EVM bytecode and deploying them to Ethereum networks. Also creates ABI interface for interacting with contract.
  • Web3 is a library used by JavaScript apps for programmatic access to contracts deployed on the Ethereum network.
  • MetaMask is a chrome extension users must have in their browser to transact with Ethereum applications on the web.

To get started with Solidity, you can use Remix, which is an browser-based IDE. Here are some example contracts:

  1. Voting
  2. Blind Auction
  3. Safe remote purchase
  4. Micropayment Channel


Solidity vs JavaScript
  • Version Pragma - Source files can (and should) be annotated with a version pragma to reject being compiled with future compiler versions that might introduce incompatible changes.
  • Static typing - Argument and variable datatypes need to be declared
  • New types
    • Address - holds a 20 byte value (size of an Ethereum address) and serve as a base for all contracts.
    • Function modifiers - automatically check a condition prior to executing the function
    • Events - for interacting with EVM
    • Structs - custom defined types that can group several variables
    • Enums - sequential set of integer values
    • Mappings - hash tables which consist of key types and corresponding value type pairs
    • Time units - Suffixes like seconds, minutes, hours, days, weeks and years after literal numbers can be used to convert between units of time where seconds are the base unit
  • Data location - Every complex type, i.e. arrays and structs, has an additional annotation, the “data location”, about whether it is stored in memory (which is not persisting) or in storage (where the state variables are held).
  • Gas - each transaction is charged with a certain amount of Ether, whose purpose is to limit the amount of work that is needed to execute the transaction and to pay for this execution.
↑ Top