bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 19쪽 중 8쪽
verifiable for observers, and
the custodian can change the rules at any time, or be censored by powerful outsiders.
7.1.1 Distributed/shared version of events
In digital ‘shared’ money, many computers each have a record of every currency transaction. When
a new transaction is made on one computer it is broadcast to the other computers, and accepted
if it follows predefined rules.
Users only benefit from coins when other users accept them in exchange, and users only accept
coins they feel are legitimate. To maximize the utility of their coins, users are naturally inclined
to settle on one commonly accepted rule-set, without the presence of a central authority. 2
Rule 1: Money can only be created in clearly defined scenarios.
Rule 2: Transactions spend money that already exists.
Rule 3: A person can only spend a piece of money once.
Rule 4: Only the person who owns a piece of money can spend it.
Rule 5: Transactions output money equal to the money spent.
Rule 6: Transactions are formatted correctly.
Rules 2-6 are covered by the transaction scheme discussed in Chapter 6, which adds the fungibility
and privacy-related benefits of ambiguous signing, anonymous receipt of funds, and unreadable
amount transfers. We explain Rule 1 later in this chapter. 3 Transactions use cryptography, so we
call their content a cryptocurrency.
If two computers receive different legitimate transactions spending the same money before they
have a chance to send the information to each other, how do they decide which is correct? There
is a ‘fork’ in the currency, because two different copies that follow the same rules exist.
Clearly the earliest legitimate transaction spending a piece of money should be canonical. This is
easier said than done. As we will see, obtaining consensus for transaction histories constitutes the
raison d’ˆ etre of blockchain technology.
2 In political science this is called a Schelling Point [64], social minima, or social contract.
3 In commodity money like gold these rules are met by physical reality.
CHAPTER 7. THE MONERO BLOCKCHAIN 57
7.1.2 Simple blockchain
First we need all computers, henceforth referred to as nodes, to agree on the order of transactions.
Let’s say a currency started with a ‘genesis’ declaration: “Let the SampleCoin begin!”. We call
this message a ‘block’, and its block hash is
BHG =H(“Let the SampleCoin begin!”)
Every time a node receives some transactions, they use hashes of those transactions, TH , like
messages, along with the previous block’s hash, and compute new block hashes
BH 1 =H(BHG, TH 1, TH 2,... )
BH 2 =H(BH 1, TH 3, TH 4,... )
And so on, publishing each new block of messages as it’s made. Each new block references the
previous, most recently published block. In this way a clear order of events extends/chains all the
way back to the genesis message. We have a very simple ‘blockchain’. 4
Nodes can include a timestamp in their blocks to aid record keeping. If most nodes are honest with
timestamps then the blockchain provides a decent picture of when each transaction was recorded.
If different blocks referencing the same previous block are published at the same time, then the
network of nodes will fork as each node receives one of the new blocks before the other (for
simplicity, imagine about half the nodes end up with each side of the fork).
7.2 Difficulty
If nodes can publish new blocks whenever they want, the network might fracture and diverge into
many different, equally legitimate, chains. Say it takes 30 seconds to make sure everyone in the
network gets a new block. What if new blocks are sent out every 31, 15 seconds, 10 seconds, etc?
We can control how fast the entire network makes new blocks. If the time it takes to make a new
block is much higher than the time for the previous block to reach most nodes, the network will
tend to remain intact.
7.2.1 Mining a block
The output of a cryptographic hash function is uniformly distributed and apparently independent
of the input. This means, given a potential input, its hash is equally likely to be every single
possible output. Furthermore, it takes a certain amount of time to compute a single hash.
4 A blockchain is technically a ‘directed acyclic graph’ (DAG), with Bitcoin-style blockchains a one-dimensional
variant. DAGs contain a finite number of nodes and one-directional edges (vectors) connecting nodes. If you start
at one node, you will never loop back to it no matter what path you take. [6]
CHAPTER 7. THE MONERO BLOCKCHAIN 58
Let’s imagine a hash functionHi(x) which outputs a number from 1 to 100:Hi(x)∈DR{1,..., 100}.5
Given some x,Hi(x) selects the same ‘random’ number from {1,..., 100} every time you calculate
it. It takes 1 minute to calculate Hi(x).
Say we are given a message m, and told to find a ‘nonce’ n (some integer) such that Hi(m,n )
outputs a number less than or equal to the targett = 5 (i.e. Hi(m,n )∈{ 1,..., 5}).
Since only 1/20th of outputs fromHi(x) will meet the target, it should take around 20 guesses of
n to find one that works (and hence 20 minutes of computing time).
Searching for a useful nonce is called mining, and publishing the message with its nonce is a proof
of work because it proves we found a useful nonce (even if we were lucky and found it with just one
hash, or even blindly published a good nonce), which anyone can verify by computing Hi(m,n ).
Now say we have a hash function for generating proofs of work, HPoW ∈DR{0,...,m}, where m is
its maximum possible output. Given a message m (a block of information), a nonce n to mine,
and a target t, we can define the expected average number of hashes, the difficulty d, like this:
d =m/t. If src/crypto-
note basic/
diffi-
culty.cpp
check
hash()
HPoW (m,n )∗d≤m, thenHPoW (m,n )≤t and n is acceptable.6
With smaller targets the difficulty rises and it takes a computer more and more hashes, and
therefore longer and longer periods of time, to find useful nonces. 7
7.2.2 Mining speed
Assume all nodes are mining at the same time, but quit on their ‘current’ block when they receive
a new one from the network. They immediately start mining a fresh block that references the new
one.
Suppose we collect a bunch b of recent blocks from the blockchain (say, with index u∈{ 1,...,b})
which each had a difficulty du. For now, assume the nodes who mined them were honest, so each
block timestamp TSu is accurate. 8 The total time between the earliest block and most recent
block is totalTime =TSb−TS 1. The approximate number of hashes it took to mine all the blocks
is totalDifficulty = ∑udu.
Now we can guess how fast the network, with all its nodes, can compute hashes. If the actual
speed didn’t change much while the bunch of blocks was being produced, it should be effectively 9
hashSpeed≈ totalDifficulty/totalTime
5 We use∈DR to say the output is deterministically random.
6 In Monero only difficulties are recorded/computed since HPoW (m,n )∗d≤m doesn’t need t.
7 Mining and verifying are asymmetric since it takes the same time to verify a proof of work (one computation
of the proof of work algorithm) no matter what the difficulty is.
8 Timestamps are determined when a miner starts mining a block, so they are likely to lag behind the actual
publication moment. The next block starts mining right away, so the timestamp that appears after a given block
indicates how long miners spent on it.
9 If node 1 tries nonce n = 23 and later node 2 also tries n = 23, node 2’s effort is wasted because the network
already ‘knows’ n = 23 doesn’t work (otherwise node 1 would have published that block). The network’s effective
hash rate depends on how fast it hashes unique nonces for a given block of messages. As we will see, since miners
include a miner transaction with one-time address Ko∈ER Zl (ER = effectively random) in their blocks, blocks are
always unique between miners except with negligible probability, so trying the same nonces doesn’t matter.
CHAPTER 7. THE MONERO BLOCKCHAIN 59
If we want to set the target time to mine new blocks so blocks are produced at a rate
(one block)/(target time), then we calculate how many hashes it should take for the network to
spend that amount of time mining. Note: we round up so the difficulty never equals zero.
newDifficulty = hashSpeed∗ targetTime
There is no guarantee the next block will take newDifficulty amount of total network hashes to
mine, but over time and many blocks and constantly re-calibrating, the difficulty will track with
the network’s real hash speed and blocks will tend to take targetTime.10
7.2.3 Consensus: largest cumulative difficulty
Now we can resolve conflicts between chain forks.
By convention, the chain with highest cumulative difficulty (from all blocks in the chain), and
therefore with most work (network hashes) spent constructing, is considered the real, legitimate
version. If a chain splits and each fork has the same cumulative difficulty, nodes continue mining
on the branch they received first. When one branch gets ahead of the other they discard (‘orphan’)
the weaker branch.
If nodes wish to change or upgrade the basic protocol, i.e. the set of rules a node considers when
deciding if a blockchain copy or new block is legitimate, they may easily do so by forking the
chain. Whether the new branch has any impact on users depends on how many nodes switch and
how much software infrastructure is modified. 11
For an attacker to convince honest nodes to alter the transaction history, perhaps in order to
respend/unspend funds, he must create a chain fork (on the current protocol) with higher total
difficulty than the main chain (which meanwhile continues to grow). This is very hard to do unless
you control over 50% of the network hash speed and can outwork other miners. [97]
7.2.4 Mining in Monero
To make sure chain forks are on an even footing, we don’t sample the most recent blocks (for
calculating new difficulties), instead lagging our bunch b byl. For example, if there are 29 blocks
in the chain (blocks 1,..., 29),b = 10, andl = 5, we sample blocks 15-24 in order to compute block
30’s difficulty.
If mining nodes are dishonest they can manipulate timestamps so new difficulties don’t match
the network’s real hash speed. We get around this by sorting timestamps chronologically, then
10 If we assume network hash rate is constantly, gradually, increasing, then since new difficulties depend on past
hashes (i.e. before the hash rate increased a tiny bit) we should expect actual block times to, on average, be slightly
less than targetTime. The effect of this on the emission schedule (Section 7.3.1) could be canceled out by penalties
from increasing block weights, which we explore in Section 7.3.3.
11 Monero developers have successfully changed src/hardforks/
hardforks.cpp
mainnet hard
forks[]
its protocol 11 times, with nearly all users and miners adopting
each fork: v1 April 18, 2014 (genesis version) [127]; v2 March 2016; v3 September 2016; v4 January 2017; v5 April
2017; v6 September 2017; v7 April 2018; v8 and v9 October 2018; v10 and v11 March 2019; v12 November 2019.
The core git repository’s README contains a summary of protocol changes in each version.
CHAPTER 7. THE MONERO BLOCKCHAIN 60
chopping off the firsto outliers and lasto outliers. Now we have a ‘window’ of blocks w =b−2∗o.
From the previous example, if o = 3 and timestamps are honest then we would chop blocks 15-17
and 22-24, leaving blocks 18-21 to compute block 30’s difficulty from.
Before chopping outliers we sorted timestamps, but only timestamps. Block difficulties are left
unsorted. We use the cumulative difficulty for each block, which is that block’s difficulty plus the
difficulty of all previous blocks in the chain.
Using
src/crypto-
note core/
block-
chain.cpp
get diff-
iculty for
next
block()
the chopped arrays of w sorted timestamps and unsorted cumulative difficulties (indexed
from 1,...,w ), we define
totalTime = choppedSortedTimestamps[w]− choppedSortedTimestamps[1]
totalDifficulty = choppedCumulativeDifficulties[w]− choppedCumulativeDifficulties[1]
In src/crypto-
note config.h
Monero the target time is 120 seconds (2 minutes), l = 15 (30 mins), b = 720 (one day), and
o = 60 (2 hours). 12,13
Block difficulties are not stored in the blockchain, so someone downloading a copy of the blockchain
and verifying all blocks are legitimate needs to recalculate difficulties from recorded timestamps.
There src/crypto-
note basic/
diffi-
culty.cpp
next diff-
iculty()
are a few rules to consider for the first b +l = 735 blocks.
Rule 1 : Ignore the genesis block (block 0, with d = 1) completely. Blocks 1 and 2 have
d = 1.
Rule 2: Before chopping off outliers, try to get the window w to compute totals from.
Rule 3: After w blocks, chop off high and low outliers, scaling the amount chopped until
b blocks. If the amount of previous blocks (minus w) is odd, remove one more low outlier
than high.
Rule 4: After b blocks, sample the earliest b blocks untilb+l blocks, after which everything
proceeds normally - lagging by l.
Monero proof of work (PoW)
Monero src/crypto-
note basic/
cryptonote
tx utils.cpp
get block
longhash()
has used a few different proof of work hash algorithms (with 32 byte outputs) in different
protocol versions. The original, known as Cryptonight, was designed to be relatively inefficient on
GPU, FPGA, and ASIC architectures [124] compared to standard hash functions like SHA256. In
April 2018 (v7 of the protocol), new blocks were required to begin using a slightly modified variant
that countered the advent of Cryptonight ASICs [29]. Another slight variant, named Cryptonight
V2, was implemented in October 2018 (v8) [11], and Cryptonight-R (based on Cryptonight but
with more substantial changes than just a tweak) started being used for new blocks in March 2019
(v10) [12]. A src/crypto/
rx-slow-
hash.c
radical new proof of work called RandomX [70] was designed and made mandatory
for new blocks in November 2019 (v12) with the intention of long-term ASIC resistance [15].
12 In March 2016 (v2 of the protocol), Monero changed from 1 minute target block times to 2 minute target block
times [14]. Other difficulty parameters have always been the same.
13 Monero’s difficulty algorithm may be suboptimal compared to state of the art algorithms [144]. Fortunately it
is ‘fairly resilient to selfish mining’ [51], an essential feature.
CHAPTER 7. THE MONERO BLOCKCHAIN 61
7.3 Money supply
There are two basic mechanisms for creating money in a blockchain-based cryptocurrency.
First, the currency’s creators can conjure coins and distribute them to people in the genesis
message. This is often called an ‘airdrop’. Sometimes creators give themselves a large amount in
a so-called ‘pre-mine’. [20]
Second, the currency can be automatically distributed as reward for mining a block, much like
mining for gold. There are two types here. In the Bitcoin model the total possible supply is
capped. Block rewards slowly decline to zero, after which no more money is ever made. In the
inflation model supply increases indefinitely.
Monero is based on a currency known as Bytecoin that had a sizeable pre-mine, followed by block
rewards [13]. Monero had no pre-mine, and as we will see, its block rewards slowly decline to a
small amount after which all new blocks reward that same amount, making Monero inflationary.
7.3.1 Block reward
Block miners, before mining for a nonce, make a ‘miner transaction’ with no inputs and at least
one output.14 The total output amount is equal to the block reward, plus transaction fees from all
transactions to be included in the block, and is communicated in clear text. Nodes who receive a
mined block must verify src/crypto-
note core/
block-
chain.cpp
validate
miner
trans-
action()
the block reward is correct, and can calculate the current money supply
by summing all past block rewards together.
Besides distributing money, block rewards incentivize mining. If there were no block rewards
(and no other mechanism), why would anyone mine new blocks? Perhaps altruism or curiosity.
However, few miners makes it easy for a malicious actor to assemble >50% of the network’s hash
rate and easily rewrite recent chain history. 15 This is also why in Monero block rewards do not
fall all the way to zero.
With block rewards, competition between miners drives total hash rate up until the marginal cost
of adding more hash rate is higher than the marginal reward of obtaining that proportion of mined
blocks (which appear at a constant rate) (plus some premiums like risk and opportunity cost).
This means as a cryptocurrency becomes more valuable, its total hash rate will increase and it
becomes progressively more difficult and expensive to gather >50%.
14 A miner transaction can have any number of outputs, although currently the core implementation is only able
to make one. Moreover, unlike normal transactions there are no explicit restrictions on miner transaction weight.
They are functionally limited by the maximum block weight.
15 As an attacker gets higher shares of the hash rate (beyond 50%), it takes less time to rewrite older and
older blocks. Given a block x days old, owned hash speed v, and honest hash speed vh (v > vh), it will take
y =x∗ (vh/(v−vh)) days to rewrite.
CHAPTER 7. THE MONERO BLOCKCHAIN 62
Bit shifting
Bit shifting is used for calculating the base block reward (as we will see in Section 7.3.3, the actual
block reward can sometimes be reduced below the base amount).
Suppose we have an integer A = 13 with bit representation [1101]. If we shift the bits of A down
by 2 using the bitwise shift right operator, denoted A >> 2, we get [0011].01, which equals 3.25.
In reality that last part gets thrown away - ‘shifted’ into oblivion, leaving us with [0011] = 3. 16
Calculating base block reward for Monero
Let’s call the current total money supply M, and the ‘limit’ of the money supply L = 2 64− 1
(in binary it is [11....11], with 64 bits). 17 In the beginning of Monero the base block reward was
B = (L-M) >> 20. If M = 0, then, in decimal format,
L = 18, 446, 744, 073, 709, 551, 615
B0 = (L− 0)>> 20 = 17, 592, 186, 044, 415
These numbers are in ‘atomic units’ - 1 atomic unit of Monero can’t be divided. Clearly atomic
units are ridiculous - L is over 18 quintillion! We can divide everything by 1012 to move the decimal
point over, giving us the standard units of Monero (a.k.a. XMR, Monero’s so-called ‘stock ticker’).
L
1012 = 18, 446, 744.073709551615
B0 = (L− 0)>> 201012 = 17.592186044415
And there it is, the very first block reward, dispersed to pseudonymous thankful for today (who
was responsible for starting the Monero project) in Monero’s genesis block [127], was about 17.6
Moneroj! See Appendix C to confirm this for yourself. 18
As blocks are mined M grows, lowering subsequent block rewards. Initially (since the genesis
block in April 2014) Monero blocks were mined once per minute, but in March 2016, it became
two minutes per block [14]. To keep the rate of money creation, i.e. the ‘emission schedule’, 19
the same, block rewards were doubled. This just means, after the change, we use (L-M) >> 19
instead of >> 20 for new blocks. Currently the base block reward is src/crypto-
note basic/
cryptonote
basic
impl.cpp
get block
reward()
B = (L−M)>> 191012
7.3.2 Dynamic block weight
It would be nice to mine every new transaction into a block right away. What if someone submits
a lot of transactions maliciously? The blockchain, storing every transaction, would quickly grow
enormous.
16 Bitwise shift right by n bits is equivalent to integer division by 2 n.
17 Perhaps now it is clear why range proofs (Section 5.5) limit transaction amounts to 64 bits.
18 Monero amounts are stored in atomic-unit format in the blockchain.
19 For an interesting comparison of Monero and Bitcoin’s emission schedules see [19].
CHAPTER 7. THE MONERO BLOCKCHAIN 63
One mitigation is a fixed block size (in bytes), so the number of transactions per block is limited.
What if honest transaction volume rises? Each transaction author would bid for a spot in new
blocks by offering fees to miners. Miners would focus on min프로젝트로 돌아가기