Building Java Applications On The Ethereum Blockchain
Building Java Applications On The Ethereum Blockchain
Ethereum Blockchain
Eoin Woods
Endava
@eoinwoodz
1
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
Agenda
• Blockchain Recap
• Ethereum
• Application Design
• Development
• (Solidity – Ethereum’s Language)
• Summary
2
Blockchain Recap
3
What is Blockchain?
• Enabling technology of Bitcoin, Ethereum, …
• Distributed database without a controlling authority
• Auditable database with provable lineage
• A way to collaborate with parties without direct trust
• Architectural component for highly distributed
Internet-scale systems
4
Architectural Characteristics of a Blockchain
• P2P distributed • (Very) eventual consistency
• Append only “ledger” • Computationally expensive
• Cryptographic security • Limited query model (key only)
(integrity & non-repudiation) • Lack of privacy (often)
• Eventual consistency • low throughput scalability
• Smart contracts (generally – 10s txn/sec)
• Fault tolerant reliability
5
What Makes a Good Blockchain Application?
• Multi-organisational • Simple query requirements
• No trusted intermediary • Multiple untrusted writers
• Need shared source of state • Latency insensitive
(e.g. transactions, identity) • Relatively low throughput
• Need for immutability (e.g. • Need for resiliency
proof of existence)
• Transactional work
• Fairly small data size
“If your requirements are fulfilled by today’s relational databases, you’d be insane to use a blockchain”
– Gideon Greenspan 6
What is Blockchain being Used For?
Keybase
Identity management Georgia government
post-trade processing verified data
records
7
Public and Permissioned Blockchains
Public Permissioned
Throughput Low Medium
Latency High Medium
# Readers High High
# Writers High Low
Centrally Managed No Yes
Transaction Cost High “Free”
9
Ethereum
10
Ethereum
• Open source blockchain
• Founded 2014 – Vitalik Buterin,
Gavin Wood, Joseph Lubin
• Swiss governing foundation
• “Blockchain App Platform”
• Vibrant ecosystem
Ethereum Key Concepts
• Blocks
• Consensus
• Smart Contracts
• Events and Logs
• Transactions & Calls
• Ether, Gas, Gas Cost, Fees
Ethereum
Ethereum
Mining Node
Ethereum Full Node
Ethereum Client
Application Smart Contracts
RPC Interface Ethereum Full
Ethereum RPC Node
Client
Ethereum
Light Node
Ethereum
Mining Node
13
Ethereum Blockchain – Txns, State, Receipts
14
https://blog.ethereum.org/2015/11/15/merkling-in-ethereum
Ethereum Consensus
• Process by which distributed system decides on state
• Currently uses a ”Proof of Work” system
• Each (mining) node gathers transactions into candidate block
• “Ethash” algorithm used to generate a hash at a target difficulty
• If “first”, broadcast the resulting block
• Forks can occur due asynchrony – longer fork used
• Proof of Stake approach planned and in trial (Casper)
15
Smart Contracts
• Stored procedures for Ethereum
• Execute EVM bytecode
• Four languages:
• Solidity, LLL, Serpent, Vyper
• EVM code deploys via a txn
• Invoked from other contracts or
off chain using address
16
Events and Logs
• EVM code is quite isolated from the outside world
• no System.out.println() … no java.io.File !
• Events can link EVM code to the outside world
• Events are types in Solidity
• Fired from the code and written to EVM ”logs”
• Clients can observe events by reading the logs
• Useful for logging and for off-chain communication
17
Events and Logs
Web3J makes
retrieving events
straightforward
18
Transactions and Calls
Transaction Call
• Mutates state • Read only operation
• Broadcast and mined • Runs “locally”, no broadcast
• Costs ether (”gas” – see later) • No cost
• Asynchronous (returns txn hash) • Synchronous (returns result)
Difference actually in invocation – ethCall vs ethSendTransaction API calls – rather than the contract
19
Ether, Gas, Gas Cost, Fees
• Ether – the cryptocurrency underpinning Ethereum
• Gas – the unit used to measure execution of your transaction
• Gas Cost – the price of one “gas unit” that you are prepared to pay
• Set higher gas cost to get faster confirmation
• Fee – the (gas * gasCost) cost you pay to run your transaction
You provide Ether with your transaction invocation. Gas Cost amount is
deducted and sent to the miners, balance is refunded to you.
https://ethgasstation.info 20
Practical Costs on MainNet in Oct 2018
• Gas is charged for instructions executed and storage used
• Executing a contract costs 21,000 gas + cost of op codes used
• Example: creating Greeter and Mortal costs 279,165 gas to create
• Gas price of 2 Gwei => cost of 0.00053 ETH (~= $0.12 USD)
• Storage costs 20,000 gas per 256bit word (625,000,000 per MB)
• Gas price of 1.9 Gwei => $267.41 per MB (AWS costs $0.10/GB/month)
• Block gas limit of ~8,000,000 => 400 words/block (~12KB)
• Ethereum is orders of magnitude more expensive than cloud
• Test or private networks can obviously sidestep this problem
https://hackernoon.com/ether-purchase-power-df40a38c5a2f 21
Rimba et. al. “Comparing Blockchain and Cloud Services for Business Process Execution” – ICSA 2017
Ethereum Specification – The Yellow Paper
https://github.com/ethereum/yellowpaper 22
Application Design
23
Ethereum Application
DApp
Client Code
24
Java & Ethereum Application
DApp
POJO Transaction Processing
Web3J
API /
User Interface / …
Database Driver
25
Some Key Decisions
• Type of blockchain deployment (public, private, public permissioned)
• Smart contract development environment
• Where is each type of data?
• On the blockchain? On distributed storage? In a database?
• Who trusts which piece?
• What do the smart contracts do? What does Java do?
• How do smart contracts interact with the outside world?
• Identity and key management
26
Communicating with the World - Oracles
Smart Contract
Event E1
write
getE1Events(…)
Oracle Processor
sendTransaction(…)
Data Sources
27
Identity
• Need to manage identity of requests to the blockchain
• Ethereum identity is EC public/private keypairs
• Similar problem to authenticating via middleware in enterprise apps
• Does the application “impersonate” the end-user?
• Does the end user give up their credentials to the application?
• Does the application use an identity server?
• Application can rely on Ethereum node to perform txn signing
• Application can sign locally (requiring private keys in a wallet)
• Needs careful consideration quite early
28
Development
29
Solidity Development
Infura.io
Geth
Cloud
Environments
Ethereum
Nodes IDEs
Ganache
Dev Environment
Tools
Embark
Web3J
Populus Metamask
Etherchain
EthGasStation 30
Java and Solidity Development Example
Java stubs
(.bin files)
32
Example Dev Environment
Solidity source
34
Solidity – Ethereum’s Language
36
Hello World in Solidity
37
Simple Token Contract – Types
38
Simple Token Contract – Allocate Tokens
39
Simple Token Contract – Move Tokens
40
Simple Token Contract – Getters & Fallback
41
Aside: EVM Memory (Storage, Memory, Stack)
43
Building Applications on Ethereum
• Blockchain can provide highly distributed, p2p, resilient data store
and code execution environment – with significant tradeoffs
• Ethereum is a maturing public and private blockchain platform
• Ethereum “dapps” can be integrated into “real” Java applications
• Solidity development is still maturing but tools exist
• Truffle, Embark, Web3J, Metamask, …
• Decide answers to key design questions early
• Environment, data storage, Solidity development pipeline, Java vs Solidity,
external interaction, identity and key management
• Possible to build applications we have never built before
44
Further Information (1)
• Fundamentals
• http://www.righto.com/2014/09/mining-bitcoin-with-pencil-and-paper.html
• https://ethereum.github.io/yellowpaper/paper.pdf
• Network Tools
• https://ethstats.net
• https://ethgasstation.info
• https://etherconverter.online
• https://etherscan.io
45
Further Information (2)
• Developing Solidity Contracts
• https://github.com/ConsenSys/smart-contract-best-practices
• http://solidity.readthedocs.io
• Dev Tools
• Solidity Editors list - https://solidity.readthedocs.io/en/develop
• Metamask - https://metamask.io
• Truffle Framework - http://truffleframework.com
• Embark Framework - https://github.com/embark-framework/embark
• Web3J - https://web3j.io
• Web3.js - https://github.com/ethereum/web3.js
46
Further Information (3)
• Security Tools
• Patterns - https://github.com/OpenZeppelin/zeppelin-solidity
• Lint style tool - https://github.com/duaraghav8/Solium
• External Oracles
• http://www.oraclize.it/
• Data Storage
• https://ipfs.io/
• https://swarm-guide.readthedocs.io
47
Further Information (4)
• Other Interesting Links
• Ethereum reading list - https://github.com/Scanate/EthList
• Solidity reading - https://github.com/bkrem/awesome-solidity
• LLL introduction - https://media.consensys.net/an-introduction-to-lll-for-
ethereum-smart-contract-development-e26e38ea6c23
• Vyper site - https://github.com/ethereum/vyper
• Blockchain comparison -
https://www.nctatechnicalpapers.com/Paper/2017/2017-comparing-
blockchain-implementations
48
Acknowledgements
• Cloud Computing icon by Evgeniy Kozachenko from the Noun Project
https://thenounproject.com/term/cloud-computing/204953
• Blockchain icon on slides 24, 25, 32 from https://www.smartcontractthailand.com
• Blockchain icon on slide 31 from https://www.draglet.com/blockchain-applications/private-or-public-
blockchain
• Other icons licensed from Icon Finder under free to use and commercial licenses
• Logo icons extracted from public websites of the logo owners
49
Thank You
Eoin Woods
Endava
[email protected]
@eoinwoodz
51