CodeChain Deep Dive
June 27, 2018
Table of Contents
Consensus
PoW vs BFT
| PoW | BFT |
Safety (= Finality) | Not Guaranteed | Guaranteed |
Liveness | Guaranteed | Guaranteed in Partial Synchronous Network |
Openness | Excellent | Limited |
Energy usage | Inefficient | Efficient |
Throughput | Low | High |
Latency | High | Low |
CodeChain Consensus
Sharding
Scalability Problem
Scalability Solution
Sharding
root
account
shard1
shard2
account
shard1
asset
asset
shard2
asset
asset
Participants
Collect transactions
Create Parcel
Collect signatures from Validators
Validate transactions in Parcel
Gateway
Shard Validator
Beacon
3. Signed Parcel
1. Unsigned Parcel
2. Signature
Execute Parcel(top-level transaction)
Mental model
validator
validator
validator
validator
validator
validator
Shard-level
Consensus
node
node
node
node
node
node
Top-level
Consensus
node
node
node
node
node
node
Top-level
Consensus
Threshold Signature
validator
validator
validator
validator
validator
Shard Validator
Storage
Merkleized UTXO
Key Idea
Benefits
Asset Transfer in Merkleized UTXO 1/3
‘A’000000...FEFE - Asset A
asset type
amount
lock script hash
Block 100
StateRoot
0xAAAA...
Branch
Branch
Branch
Branch
Branch
...
...
...
...
...
Asset Transfer in Merkleized UTXO 2/3
Block 100
‘A’0000…FDFD - Asset B
asset type
amount
lock script hash
Block 101
AssetTransfer
A → B
‘A’000000...FEFE - Asset A
asset type
amount
lock script hash
StateRoot
0xAAAA...
Branch
Branch
Branch
Branch
Branch
...
...
...
...
...
Asset Transfer in Merkleized UTXO 3/3
Block 100
Branch
Branch
Get “Asset A” from StateRoot 0xBBBB… returns null
‘A’0000…FDFD - Asset B
asset type
amount
lock script hash
Block 101
AssetTransfer
A → B
‘A’000000...FEFE - Asset A
asset type
amount
lock script hash
StateRoot
0xAAAA...
Branch
Branch
Branch
Branch
Branch
...
...
...
...
...
StateRoot
0xBBBB...
Transactions
CodeChain Transactions
Mint
Transfer
Asset
Gold
Silver
Silver
Gold
Scheme
Sending CodeChain Transaction
CodeChain Network
Gateway
Asset owner
Asset owner
Asset owner
AssetTransfer
Node
Node
Node
Node
ChangeShardState
Game Service Example - AssetMint
AssetMint
output 1
999999 sword
owner A
Game server mints new item
create
AssetScheme
* metadata
* amount
Game Gateway
account 0xDE..
Send transaction to the gateway
Game server
owner A
Game Service Example - AssetMint
AssetMint
output 1
999999 sword
owner A
AssetScheme
* metadata
* amount
Game Gateway
account 0xDE..
verify transaction and create parcel
Game Service Example - AssetMint
Parcel
Transactions
AssetMint
Game Gateway
account 0xDE..
Fee, Signature, ...
...
...
Send signed parcel
create parcel and sign
CodeChain node
CodeChain Network
Node
Node
Node
Node
Game Service Example - AssetTransfer
Game client
asset owner B
A user buys a sword with 5 gold from NPC
AssetTransfer
input 0
20 gold
owner B
output 0
999998 swords
owner A
input 1
999999 sword
owner A
output 1
1 sword
owner B
output 2
5 gold
owner A
output 3
15 gold
owner B
sign
sign
Game server
owner A
Game Service Example - AssetTransfer
AssetTransfer
input 0
20 gold
owner B
input 1
999999 sword
owner A
Game Gateway
account 0xDE..
Send transaction to the gateway
output 0
999998 swords
owner A
output 1
1 sword
owner B
output 2
5 gold
owner A
output 3
15 gold
owner B
Game server
owner A
Game Service Example - AssetTransfer
Parcel
Transactions
AssetTransfer
Game Gateway
account 0xDE..
...
...
Send signed parcel
verify transaction and
create parcel
CodeChain node
Fee, Signature, ...
Scripting
CodeChain Script
Burn
TX0
TX1
BURNT!
CodeChain Asset Transfer
Lock Script Hash
Parameters
Lock Script
Unlock Script
Output
Input
Lock script Hash
Lock script
BLAKE256
Lock script
Run CCVM
Unlock script
Sanity Checker
Unlock script
Initial State
Parameters
Success
Fail
Burn
Synchronization
Full Block Synchronization
?
?
?
Snapshot Synchronization
R
snapshot
chunk
Head
Network
Network Extensions
Node
Session
Initiator
P2P
Connection
Extensions
Node
Node
Node
Peer Discovery
Session Initiation
Connection Request
Node
Node
Connection Allowed
ECDH Request
ECDH Allowed
ECDH
Share secret key
Create a session
Connection Request
Node
Node
Connection Allowed
Set shared secret with RPC
Share session without network
Set shared secret with RPC
Share secret key
Create a session
Authentication
head | version | 0x05 | extension-name | extension-version |
body | aes256(extension-message, session-key) | |||
signature | blake2b(head.body, session-key) |
head | version | 0x06 | extension-name | extension-version |
body | extension-message | |||
signature | blake2b(head.body, session-key) |
Encryption
head | version | 0x05 | extension-name | extension-version |
body | aes256(extension-message, session-key) | |||
signature | blake2b(head.body, session-key) |
head | version | 0x06 | extension-name | extension-version |
body | extension-message | |||
signature | blake2b(head.body, session-key) |
Extension API
pub trait Extension {
fn name(&self) -> String;
fn need_encryption(&self) -> bool;
fn versions(&self) -> Vec<Version>;
fn on_initialize(&self, api: Arc<Api>);
fn on_node_added(
&self, _node: &NodeId, version: Version);
fn on_node_removed(&self, _node: &NodeId);
fn on_message(
&self, _node: &NodeId, _message: &[u8]);
fn on_timeout(&self, _timer: TimerToken);
fn on_local_message(&self, _message: &[u8]);
}
pub trait Api {
fn send(
&self, node: &NodeId, message: &Vec<u8>);
fn set_timer(
&self, timer: TimerToken, d: Duration)
-> Result<()>;
fn set_timer_once(� &self, timer: TimerToken, d: Duration)
-> Result<()>;
fn clear_timer(&self, timer: TimerToken)
-> Result<()>;
fn send_local_message(
&self, message: &Encodable);
}
Q & A
CodeChain
Home
GitHub
https://github.com/CodeChain-io/codechain
Gitter
https://gitter.im/CodeChain-io/codechain
Blog