Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
Principals and Practice of Cryptocurrencies
Cornell CS 5437, Spring 2016
Simulation
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
2
Example β Gossip
β’ A messages is generated at one machine
β’ Message processing time: random 0 - 1sec
β’ After processing, node chooses random neighbor and sends message
β’ Sending time: random 0 β 1sec
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
3
Why A Simulation?
Formal analysis
Working system
experiment
Simulation Proof of concept
experiment
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
4
Why A Simulation?
Formal analysis
Working system experiment
Simulation Proof of concept experiment
β’ How complex a model? β’ Over-detailed --> overly complicated β’ Insufficient details --> inaccurate (or wrong)
Formal Analysis Simulation Experiment
Simple models Complex models Real world
Basic understanding, parameter interaction
Per-run results βcheap runs
Per-run results βexpensive runs
Scale to arbitrary system parameters
Cheap scaling Expensive scaling
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
5
Event Driven Simulation
Time-driven simulation: second by second
Event-driven simulation: event by event
Which is more accurate?
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
6
Time Event
1 Message π arrived at node π
1.3 Message π arrived at node π
Event Driven Simulation
Time-driven simulation: second by second
Event-driven simulation: event by event
No tradeoff between efficiency
and accuracy
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
7
Process-Oriented Simulation
β’ Use an object per entity β’ Store individual object state, and let object react to events
Time Entity Event
1 node π Message π arrived
1.3 node π Message π arrived
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
8
InputIncludes both model details and runtime inputs
β’ Synthetic, e.g., β’ Topology of huge system β’ Input arrival times
β’ Traces, measurement-based, e.g., β’ Input arrival times β’ Processing times
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
9
Output Data collection: β’ Output as much data as possible (probably in a log), but not
too much β it can easily explode. For example: β’ Message arrival time at each node:
Can then calculate 90th percentile propagation time without re-running
β’ But not every send / processing-start event
Tips: β’ Output meaningful data during long runs β’ Output execution parameters in log
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
10
Executions Multiple executions β’ Each with different random inputs β’ Warmup
β’ Do not consider for statistics β’ E.g., when multiple messages are propagated together,
let queues stabilize β’ Or a single long run, divided to sections
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
11
Restart and Avoiding It
β’ Memoizeβ’ Carefully while youβre debuggingβ¦
β’ Checkpoint β’ For crash handlingβ’ If you decide continue, to avoid restart
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
Probability
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
13
Probability
β’ An experiment produces a random results, πβ’ The sample space Ξ© is all possible results, π β Ξ©β’ An Event is a subset of the sample space, πΈ β Ξ©
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
15
Probability β’ Every event πΈ has a probability 0 β€ π πΈ β€ 1β’ The conditional probability of A given B is the probability of A given
the B is true; π π΄ π΅ =π π΄β©π΅
π π΅
β’ A random variable is a function from the sample space to a discrete or continuous range
β’ A random variable has a Cumulative Distribution Function (CDF): πΉπ π₯ = π π β€ π₯
πΉπ π₯π₯βββ
0, πΉπ π₯π₯ββ
1β’ A discrete random variable has a probability per value β’ A continuous random variable has a probability distribution function
ππ π₯ = πΉπβ² π₯
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
16
Probability β’ The mean of a variable π is
πΈ π = ββ
β
π₯ππ π₯ ππ₯
β’ The variance of a variable π is var π = πΈ π β πΈ π 2 = πΈ π2 β πΈ π 2
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
17
Example β Bernoulli distribution
β’ Bernoulli distribution: E.g., due to a biased coin toss, resulting in heads (π1) or tails (π2)
π = 1 heads0 tails
π π = 1 = π, π π = 0 = 1 β ππΈ π = 1 β π + 0 β 1 β π = πvar π = π 1 β π
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
18
Example β Normal Distribution E.g., height, measurement errors
π(π, π2)
π π₯ =1
π 2ππβπ₯βπ 2
2π2
πΈ π = πvar π = π2
The standard normal distribution is π(0, 1).
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
19
Example β Normal Distribution The law of large numbers: The average of Independent and Identically Distributed (IID) random variables converges to the mean of the distribution they are sampled from.
The central limit theorem: the average of IID random variables is approximately normally distributed.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
20
Example β Exponential Distribution
E.g., interval between phone calls
π π₯ = ππβππ₯ π₯ β₯ 00 otherwise
πΉ π₯ = 1 β πβππ₯ π₯ β₯ 00 otherwise
πΈ π = 1/πvar π = 1/π2
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
21
Memorylessness
π· πΏ > π + π|πΏ > π =π π > π + π‘ β© π > π‘
π π > π‘
=π π > π + π‘
π π > π‘=1 β πΉ(π + π‘)
1 β πΉ(π‘)=πβπ π +π‘
πβππ‘
= πβππ = 1 β πΉ π = π· πΏ > π
For an exponential random variable, πΉ π₯ = 1 β πβππ₯:
It doesnβt matter how long you have been waiting for the bus
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
22
Psuedo Random Number Generator
β’ No real randomness β’ PRNG has state and output
β’ State changes on every output request (loops, but usually not an issue)
β’ Can be reset with given seed
1 >>> import random
2 >>> random.random()
3 0.28651040107085957
4 >>> random.random()
5 0.1796791064694051
6 >>> random.seed(42)
7 >>> random.random()
8 0.6394267984578837
9 >>> random.random()
10 0.02501075522266693
11 >>> random.seed(42)
12 >>> random.random()
13 0.6394267984578837
14 >>> random.random()
15 0.02501075522266693
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
23
Use Object PRNG
1 >>> randA = random.Random(42)
2 >>> randB = random.Random(42)
3 >>> randA.random()
4 0.6394267984578837
5 >>> randB.random()
6 0.6394267984578837
7 >>> randA.random()
8 0.025010755222666936
9 >>> randB.random()
10 0.025010755222666936
β’ Program modular β’ Reset global PRNG in
each module? β’ No β object PRNG per
module β’ Seed moduleβs PRNG
on init
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
24
PRNG for Simulation
β’ Different seeds for different runs β’ Reproducibility (mostly for debugging)
β’ Manually change between runs β’ Seed time, but record seed for reproduction
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
25
Inverse Transform Sampling
0.5
3
1
CDF
CDF-1
1
3π π‘ = 0.5 1 β€ π‘ < 30 otherwise
πΉ π‘ = 0 π‘ < 1
0.5(π‘ β 1) 1 β€ π‘ < 31 π‘ β₯ 3
πΉβ1 π’ = 1 + 2π’
>> randA = random.Random()
>> x = 1 + 2 * randA.random()
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
26
Statistical Analysis
Given a finite set of measurements
β’ Estimate the properties of the sampled space β’ Estimate the estimation accuracy
Stop when the accuracy is sufficient.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
27
Statistical Analysis Take a sample of size π {π₯_π, 1 β€ π β€ π} of independent measurements. E.g., simulation propagation times from π runs.
Sample are taken from a population with probability distribution with mean π and variance π2. (π and π are unknown)
β’ The sample mean is:
π₯ =1
π
π=1
π
π₯π
β’ The sample variance is:
π2 =1
π β 1
π=1
π
π₯π β π₯ 2
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
28
Statistical Analysis But the sample mean is a random variable.
The mean of π₯ is π.
So π₯ is an estimator of π
β’ π₯ is not π, and β’ π is not π
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
30
Statistical Analysis What is the variance of π₯?
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
33
Statistical Analysis What is the variance of π₯?
var π₯ =π2
π
More samples --> smaller variance --> π₯ probably closer to π
But we can only take a finite number of samples π. And we donβt have π, only π. So we need to estimate π2.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
34
Statistical Analysis What is the mean of π2?
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
35
Statistical Analysis
π2 =1
π β 1
π=1
π
π₯π β π₯ 2 =1
π β 1
π=1
π
π₯π β1
π
π
π₯π
2
=
=1
π β 1
π=1
π
(π₯πβπ) β1
π
π
(π₯πβπ)
2
=
=1
π β 1
π
π₯π β π 2 +1
π2
π
(π₯πβπ)
2
β2
π(π₯πβπ)
π
(π₯πβπ) =
=1
π β 1
π
π₯π β π 2 +1
π(π β 1)
π
(π₯πβπ)
2
β2
π π β 1
π
(π₯πβπ)
π
(π₯πβπ) =
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
36
Statistical Analysis
1
π β 1
π
π₯π β π 2 +1
π(π β 1)
π
(π₯πβπ)
2
β2
π π β 1
π
(π₯πβπ)
π
(π₯πβπ) =
=1
π β 1
π
π₯π β π 2 β1
π(π β 1)
π
π₯π β π 2 +
π
πβ π
(π₯π β π)(π₯π β π) =
=1
π
π
π₯π β π 2 β1
π(π β 1)
π
πβ π
(π₯π β π)(π₯π β π)
πΈ π2 =1
π
π
πΈ π₯π β π 2 β1
π π β 1
π
πβ π
πΈ π₯π β π π₯π β π = π2
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
37
Statistical Analysis What is the mean of π2? π2.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
38
Confidence Interval With the standard normal distribution π(0, 1), define π§πΌ/2 to be the
point for which the integral to the right is πΌ/2. (tables online)
π βzπΌ/2 β€ π§ β€ π§πΌ/2 = 1 β πΌ
0 π§πΌ/2
Area is πΌ/2
For example, for πΌ = 0.05, π§0.025 = 1.96
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
39
ConfidenceSo how accurate is the estimate π₯? If the π₯πβs are normally distributed: π₯π = π(π, π), let
π = π₯ β π
ππ
π is normally distributed π = π 0, 1 . So
π βπ§πΌ/2 β€ π = π₯ β π
ππ β€ π§πΌ/2 = 1 β πΌ
π π₯ βzπΌ/2π
πβ€ π β€ π₯ +
zπΌ/2π
π= 1 β πΌ
For confidence level 1 β πΌ,
the confidence interval is π₯ βzπΌ/2π
π, π₯ +
zπΌ/2π
π
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
40
Confidence Interval β’ Since we donβt have π, we approximate with S. β’ Although the π₯πβs are not necessarily normally distributed, according
to the central limit theorem, itβs a good approximation for their sum. Thumb rule: π β₯ 30.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
41
Example
Index Value
1 1.9
2 2.0
3 1.9
4 2.0
5 2.1
6 1.9
7 2.1
8 2.1
9 2.1
10 2.1
π₯ =1
π
π=1
π
π₯π = 2.05
π2 =1
π β 1
π=1
π
π₯π β π₯ 2 = 0.009
π§0.05/2 = 1.96
π₯ Β± π§0.05/2π
π= 1.99,2.11
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
42
Statistical Analysis
Given a finite set of measurements
β’ Estimate the properties of the sampled space β’ Estimate the estimation accuracy
Stop when the accuracy is sufficient. E.g., the confidence interval is of length 0.01sec with a confidence level of 95%.
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
Difficulty and Block Interval
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
44
Bitcoinβs Block Difficulty
β’ Hash (SHA2562) of legal block is smaller than a target β’ Target is a 256bit value β’ Stored as a 32bit field called bits in blocks
0x180BC409 0BC409 β 28 18β3
Bits: Target
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
45
Bitcoinβs Block Difficulty
β’ Hash (SHA2562) of legal block is smaller than a target β’ Target is a 256bit value β’ Stored as a 32bit field called bits in blocks β’ Largest target (targetmax) is defined by bits 0x1d00ffff:
0x00000000FFFF0000000000000000000000000000000000000000000000000000
β’ Difficulty is defined with respect to the largest target:
difficulty =largest target
target
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
46
Bitcoinβs Block Difficulty
How long does it take to find a value smaller than 0x00000000FFFF0000000000000000000000000000000000000000000000000000
Or simply: 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
?
But wait β the nonce field sizeβ¦
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
47
Bitcoinβs Block DifficultyHow is the target adjusted?
β’ Once every 2016 blocks (2016/7/24/6 = 2 weeks) β’ 2016 blocks ago was π‘0β’ Last block was π‘πβ’ Total time is Ξ = π‘π β π‘0 seconds
β’ Difficulty before π·πππβ’ Estimate of total hashes calculated: 2016 Γ 232π·πππ
We want to find π·πππ€ such that it will take the system 10 minutes on average to find a block. HW
Co
rnel
l CS
54
37
Β© It
tay
Eyal
, 20
16
50
Agenda
β’ Difficulty calculation β’ Time to find a block β’ Difficulty automatic tuning β’ Minimum of two exponentials and fair mining