Tomasz Drwięga
@tomusdrw
Parity Technologies
How do they work?
Part 1: Blocks & Transactions
Part 2: Smart Contracts
List of changes
List of changes
List of changes
Metadata
/ Previous state
Metadata
/ Previous state
Metadata
/ Previous state
Genesis State
Blockchain
Immutable data structure containing all the changes that were applied in any point in time
Blockchain
Hashes - prevent tampering (e.g. KECCAK256)
Signatures - authorize the actions (e.g. ECDSA)
Parent = hash(B0) Timestamp = 150..000 Number = 1
Hash = hash(B1)
transfer(A, B, 5)
sig(A)
transfer(C, B, 1)
sig(C)
Parent = hash(B2) Timestamp = 150..000 Number = 2
Hash = hash(B1)
transfer(B, A, 5)
sig(B)
sig(Authority1)
hash(B0)
hash(B1)
sig(Authority2)
hash(B2)
sig(Authority1)
Proof of Authority
We only accept blocks signed by a hardcoded list of authorities.
Blocks need to be signed in turns at most 1 block per 3 seconds.
Difficulty=2 Sol.=0b001.. SolvedBy=A
hash(B0)
hash(B1)
hash(B2)
Proof of Work
We only accept blocks with a solution to a puzzle.
The difficulty of the puzzle can be adjusted to have a stable rate of new blocks.
Difficulty=4 Sol.=0b00001.. SolvedBy=B
Difficulty=3 Sol.=0b0001.. SolvedBy=A
(Boot) Node 1
Node 2
New Node
Hey! Could you give me all your peers?
(Boot) Node 1
Node 2
New Node
Hey! Send me all them blocks, will ya?
Block 5
Block 4
Block 0
(Boot) Node 1
Node 2
New Node
Hey! I've got a transaction to include in block.
Block 5
Block 5
Block 5
transfer(N, B, 5)
sig(N)
(Boot) Node 1
Node 2
New Node
Block 5
Block 5
Block 5
transfer(N, B, 5)
sig(N)
transfer(N, B, 5)
sig(N)
Cool, I'm mining and will include the tx for a small fee.
(Boot) Node 1
Node 2
New Node
Block 6
Block 5
Block 5
transfer(N, B, 5)
sig(N)
transfer(N, B, 5)
sig(N)
Block 6
Managed to mine new block, here it is guys!
(Boot) Node 1
Node 2
New Node
Block 6
Block 6
Block 6
Canonical Chain
What if two different blocks are produced with the same parent hash?
Which one should you choose?
Block 1
Block 2
Block 3
Block 3
Fork
Canonical Chain
We use "the longest" chain.
Ethereum re-organizes to a chain with the highest difficulty.
Block 1
Block 2
Block 3
Block 3
Block 4
Take away note: The latest state you see can sometimes be reverted - wait for confirmations.
Bitcoin is the first public blockchain ever.
Block Time | 10 minutes |
Consensus | Proof of Work - hashcash |
State | Account Balances of BTC / UTXO* |
Transactions | Value Transfers* |
Launched | 2009 |
Block Reward | 12BTC (halving) ~ 21M total coins |
Ethereum is the World Computer
Block Time | 14 seconds |
Consensus | Proof of Work - ethash* |
State | Arbitrary |
Transactions | Turing-complete / programmable |
Launched | 2015 |
Block Reward | 5ETH (+uncles) ~ Unlimited coins |
Blockchains allow for trustless transactions between multiple parties.
$ parity ui --chain=dev --mode=offline --ws-origins=all
# or
$ parity ui --chain=dev --network-id=<random-number> --ws-origins=all
$ cd ~/.local/share/io.parity.ethereum/dapps
# ~/Library/Application Support/io.parity.ethereum/dapps
# %AppData%\Parity\Ethereum\dapps
$ git clone --depth=1 -b built https://github.com/tomusdrw/etherdisplay
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getBlockByNumber",
"params": ["latest", false]
}
# apt install httpie
$ http localhost:8545 jsonrpc=2.0 id=1 method=eth_getBlockByNumber params:='["latest",false]'
# Or with CURL
$ curl localhost:8545 -H "Content-Type:application/json" -X POST --data \
'{"jsonrpc":"2.0","id":1,"method":"eth_getBlockByNumber","params":["latest",false]}'
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"author": "0x05a56e2d52c817161883f50c441c3228cfe54d9f",
"difficulty": "0x3ff800000",
"extraData": "0x476574682f76312e302e302f6c696e75782f676f312e342e32",
"gasLimit": "0x1388",
"gasUsed": "0x0",
"hash": "0x88e96d4537bea4d9c05d12549907b32561d3bf31f45aae734cdc119f13406cb6",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"miner": "0x05a56e2d52c817161883f50c441c3228cfe54d9f",
"mixHash": "0x969b900de27b6ac6a67742365dd65f55a0526c41fd18e1b16f1a1215c2e66f59",
"nonce": "0x539bd4979fef1ec4",
"number": "0x1",
"parentHash": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"sealFields": [
"0xa0969b900de27b6ac6a67742365dd65f55a0526c41fd18e1b16f1a1215c2e66f59",
"0x88539bd4979fef1ec4"
],
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x219",
"stateRoot": "0xd67e4d450343046425ae4271474353857ab860dbc0a1dde64b41b5cd3a532bf3",
"timestamp": "0x55ba4224",
"totalDifficulty": "0x7ff800000",
"transactions": [],
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"uncles": []
}
}
Documentation of all methods:
https://github.com/paritytech/parity/wiki/JSONRPC-eth-module
// Returns a block data
// and transactions in it
eth_getBlockByNumber(
<block-number>,
<include-transactions>,
);
// Returns a balance of an account.
eth_getBalance(
<address>,
);
Smart Contract is like an agent behind a regular address.
pragma solidity ^0.4.11;
contract Burn {
uint256 public value;
address public owner;
function Burn() public payable {
value = msg.value;
owner = msg.sender;
}
}
pragma solidity ^0.4.11;
contract Parity {
uint256 public value;
address public owner;
function export() payable {
value += msg.value;
owner = msg.sender;
}
}
0xc0de15de4d... at 0x123..456
binary at /usr/bin/parity
$ parity export blocks
from: 0x456..def
to: 0x123..456
value: 5 * 10^18 wei (5 ETH)
gas: 100,000
gasPrice: 4 * 10^9 wei (4 shannon)
nonce: 0
data: 0x444...
("call function export")
0x123456... (Transaction RLP)
pragma solidity ^0.4.17;
contract Lock {
uint256 public value;
address public owner;
function Lock() public payable {
value = msg.value;
owner = msg.sender;
}
function withdraw() public {
require(msg.sender == owner);
msg.sender.transfer(value);
}
}
pragma solidity ^0.4.17;
contract Lock {
mapping(address => uint256) public locked;
function lock() public payable {
locked[msg.sender] = msg.value;
}
function unlock() public {
var value = locked[msg.sender];
require(value != 0);
delete locked[msg.sender];
msg.sender.transfer(value);
}
}
Can you spot a bug in this contract?
contract SimpleToken { // TODO Use SafeMath!
uint256 constant rate = 1000;
event Transfer(address indexed _from, address indexed _to, uint _value);
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => uint256) public lockTime;
function buyTokens() payable {
var tokens = msg.value * rate;
var unlockAt = block.number + 256 * 24; // 24h
totalSupply += tokens;
balanceOf[msg.sender] += tokens;
lockTime[msg.sender] = unlockAt;
}
function burnTokens(uint256 _value) {
var tokens = _value * rate;
require(block.number >= lockTime[msg.sender]);
require(balanceOf[msg.sender] >= tokens);
balanceOf[msg.sender] -= tokens;
totalSupply -= tokens;
delete lockTime[msg.sender];
msg.sender.transfer(_value);
}
function transfer(address _to, uint _value) returns (bool success) {
if (block.number < lockTime[msg.sender]) return false;
if (balanceOf[msg.sender] < _value) return false;
if (_value == 0 || balanceOf[_to] + _value <= balanceOf[_to]) return false;
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
}
}
https://theethereum.wiki/w/index.php/ERC20_Token_Standard
// https://github.com/ethereum/EIPs/issues/20
contract ERC20 {
function totalSupply()
constant returns (uint totalSupply);
function balanceOf(address _owner)
constant returns (uint balance);
function transfer(address _to, uint _value)
returns (bool success);
function transferFrom(address _from, address _to, uint _value)
returns (bool success);
function approve(address _spender, uint _value)
returns (bool success);
function allowance(address _owner, address _spender)
constant returns (uint remaining);
event Transfer(
address indexed _from, address indexed _to, uint _value
);
event Approval(
address indexed _owner, address indexed _spender, uint _value
);
}
contract Purchase {
uint public value;
address public seller;
address public buyer;
function Purchase(uint _value) {
seller = msg.sender;
value = _value;
}
/// Confirm the purchase as buyer.
function confirmPurchase() payable {
require(msg.value == value);
buyer = msg.sender;
}
/// Confirm that you (the buyer) received the item.
function confirmReceived() {
require(msg.sender == buyer);
seller.transfer(this.balance);
}
}
contract Purchase {
uint public value;
address public seller;
address public buyer;
// Seller needs to deposit double the value of the item.
function Purchase() payable {
seller = msg.sender;
value = msg.value / 2;
require(value * 2 == msg.value);
}
function abort() {
require(msg.sender == seller);
require(buyer == 0);
seller.transfer(this.balance);
}
/// Confirm the purchase as buyer. Deposit double the value of the item.
function confirmPurchase() payable {
require(msg.value == 2 * value);
buyer = msg.sender;
}
/// Confirm that you (the buyer) received the item.
function confirmReceived() {
require(msg.sender == buyer);
buyer.transfer(value); // transfer half of the deposit
seller.transfer(this.balance); // transfer the entire deposit
delete value;
}
}
How to do a blind auction?
Not really working that well currently, reading state of the blockchain is difficult.
Most likely we will need a "centralized" server anyway.
(Good thing being is that we can provide some proofs that the data has not been tampered)
Good:
Parity-specific technology. Allows to securely store sensitive/encrypted data and give access only to specific parties.
Example: Company A stores a document. Access to the document is given to Company B. B can fetch the document and decrypt it using some fancy math.
(NOTE: A does not need to provide the same document encrypted specifically for B!)
Example:
Accounting:
In other words there are two types of coins:
Option 1: Put everything on the blockchain
and allow anyone to participate - exchanges can be provided by anyone, but requires some internal currency.
Option 2: Separate company allowed to issue/unlock coins. At the end of the month Service Provider gets a bill - how much it needs to pay for generated coins.