BitCoin-Minning, What do nonce, POW mean for?π·
Once you know the hash function, the most important idea of ββcryptocurrency, then you need to know about proof of work (POW), which is essential in the mining process. It is the proof that the miner has elaborated this much to find out target hash.
- In the process of proof-of-work (POW), nonce and difficulty arise. Difficulty is a variable that makes it difficult to mine a block. This is easy to accept, but the concept of nonce is hard to grasp. Letβs make assumption like this.
Picking balls which are numbered upto 100 in a pouch
- Put 100 numbered balls into an invisible pouch, and three people A, B, and C draw each and each ball. If a number that satisfies the criteria comes out, it passes, but otherwise, put the ball into a pouch again and repeat the drawing. Here, the conditions below are applied to increase the difficulty.
- A λ
90
λ³΄λ€ μμ μλ₯Ό λ½μΌλ©΄ ν΅κ³Ό (μμ μ¦λͺ )μ λ°μμ€λ€. - B λ
50
λ³΄λ€ μμ μλ₯Ό λ½μμΌ ν΅κ³Ό (μμ μ¦λͺ )μ λ°μμ€λ€. - C λ
5
μ΄νμ μ«μλ₯Ό λ½μμ λλ§ ν΅κ³ΌμμΌ μ€λ€.
λκ° λΉ λ₯Έμκ° λ΄μ 쑰건μ μΆ©μ‘±νκ³ μμ μ λ§μΉ μ μμκΉ? λΉμ°ν Aκ° μλ±ν μ μ νμλ‘ μμ μ λ§μΉ μ μλ€
μλ₯Ό λ€μ΄,
- A λ 5λ² λ§μ 90λ³΄λ€ μμ μλ₯Ό λ½μ ν΅κ³Όνλ€.
- B λ 13λ² λ§μ 50λ³΄λ€ μμ μλ₯Ό λ½μ ν΅κ³Όνλ€.
- C λ 59λ² λ§μ 5λ³΄λ€ μμ μλ₯Ό λ½μ ν΅κ³Όνλ€.
νμ C κ° λ λ§μ νμλ₯Ό μλν΄μΌ λλ κ²μ μλλ€. νλ²μ ν΅κ³Όν μλ μλ€.
νμ§λ§, νλ₯ μ μΌλ‘ C κ° λ λ§μ΄ κ³ μν΄μΌ ν κ²μ΄λ€.
μ¬κΈ°μ,
90 λ³΄λ€ μμ μ
λΌλ쑰건
μ λμ΄λ (difficulty
)- λ°λ³΅μμ
5λ²
μ νμκ° λ Όμ€(nonce
) λ€
nonce λ λͺ λ²μ λ°λ³΅ν΄μ μλ νλμ§, νμμ΄λ©°, μ΄ νμκ° μΌλ§νΌ κ³ μνλμ§λ₯Ό μ
μ¦νλ μμ
μ¦λͺ
(proof_of_work)μ΄λ€. λ
Όμ€λ₯Ό ν¬ν¨ν΄μ, νμ¬μ Hash κ°μ΄ κ³μ°λμΌ νλ©°, μ μΌνκ² λ³κ²½ν μ μλ ν΄μ¬μ
λ ₯κ° ν΅λ‘ NONCE λ³κ²½ μμΌ κ°λ©΄μ ν΄μ¬κ°μ λ°λ³΅μ μΌλ‘ κ³μ°νλ€ λ³΄λ©΄ μ°μ°νκ² μ‘°κ±΄μ λ§μ‘±νλ ν΄μ¬κ°μ μ°ΎμλΈλ€. λΉ λ₯΄κ² κ³μ°ν μ μλ λ₯λ ₯μ κ°μ§μ¬λμκ² κΈ°νκ° λ λ§μ§λ§, μ°Ύμ νλ₯ μ μ°μ° μ κΈ°λ°νκΈ° λλ¬Έμ μ λμ μΈ κ²μ μλλ€ ..μ΄κ±°, λ€ μλΉ¨κ° κ±°μ§λ§μΈκ±° λ€ μμμ£ ? μ¬λ¬λΆ!..
μλ λΈλκ²°κ³Όλ₯Ό λΉκ΅ν΄ 보μ
λμ΄λμ λ°λΌμ, κΈ°μ€λ³΄λ€ μμ ν΄μ¬λ₯Ό κ³μ°νμ λ μλν νμ, λ Όμ€(nonce) λ₯Ό 보μ¬μ€λ€, μΌμ ν¬κΈ° μ΄νμ ν΄μ¬λ₯Ό λ½μλ΄κΈ°κ° μ΄λ €μ΄ λ§νΌ μλνμ λ Όμ€κ° μ¦κ° νλ€.
1
2
3
4
5
6
7
8
9
10
11
{'difficulty': '000',
'hash_present': '000107d0d540f0edf7eb0af657840b51',
'hash_previous': '000005fa8482b821aff9b2ce6103f69e',
'index': 1,
'nonce': 7725,
'timestamp': 1539440895.4122593,
'transaction': [{'amount': 1000, 'recipient': 'Bob', 'sender': 'Alice'},
{'amount': 800, 'recipient': 'Alice', 'sender': 'Scrouge'},
{'amount': 200,
'recipient': 'node_identifier_uid',
'sender': 'coinbase_reward'}]}
- μλνμ(λ Όμ€) = 7,725ν
- λ§μ‘±νλ present_hash κ°μ μ°Ύμμ λ,
- νμμ€ν¬νλ₯Ό μ°κ³ , νμ¬ν΄μ¬κ°μ μΆκ°νλ€
- μ¦, νμμ€ν¬νμ νμ¬ν΄μ¬κ°μ λΊ λΈλμ ν΄μ¬256 ν¨μμ, λ Όμ€κ°κ³Ό κ°μ΄ λ£μΌλ©΄
- νμ¬μ 쑰건μ λ§μ‘±νλ ν΄μ¬κ°μ΄ λμ¨λ€.
- ν΄μ¬ μ λ ₯κ°μμ λ Όμ€λ₯Ό ν¬ν¨ν κ·Έ μ΄λ€κΈμκ° νκΈμλ§ λ°λμ΄λ,
- ν΄μ¬κ°μ 쑰건μμ λ²μ΄λλ€.
- μ¦, νΉμ λ Όμ€κ°μ μ°ΎμμΌλ§, 쑰건μ λ§λ ν΄μ¬κ° μμ μ¦λͺ μ μ΄λ€λΈλ€.
- ν΄μ¬κ°μ 무μμμμ΄κΈ° λλ¬Έμ, λ Όμ€λ₯Ό 1μ© μ¦κ°μν€λ©°, λ°λ³΅ κ³μ°νλ€
- μ΄ κ³Όμ μ΄ λλ€νΌνΉκ³Ό κ°μ λμμ΄λ€.
1
2
3
4
5
6
7
8
9
10
11
{'difficulty': '00000',
'hash_present': '00000da730623b47a1e4d39a4d28ccfe',
'hash_previous': '000005fa8482b821aff9b2ce6103f69e',
'index': 12,
'nonce': 52305,
'timestamp': 1539441010.7054014,
'transaction': [{'amount': 1000, 'recipient': 'Bob', 'sender': 'Alice'},
{'amount': 800, 'recipient': 'Alice', 'sender': 'Scrouge'},
{'amount': 200,
'recipient': 'node_identifier_uid',
'sender': 'coinbase_reward'}]}
- λμ΄λκ° λμλ¦Ώμ μΆκ°λ‘ λ¨μ΄μ§λκ°, μλνμκ° 52,305ν λ‘ λμ΄λ¬λ€.
- 쑰건μ λ§λ ν΄μ¬κ°μ κ³μ°ν μ μλ μ λ ₯κ° λ Όμ€λ₯Ό μ°Ύμλ΄κ³
- 쑰건μ λ§λ ν΄μ¬κ°μ μ°Ύμλ΄λ©΄ μμ μ¦λͺ μ΄ μ΄λ£¨μ΄μ§λ€.
- κ° λΈλμ μ°κ²°κ³ 리λ 쑰건μ λ§λ ν΄μ¬κ°μ κΈ°μ€μΌλ‘ μλ€λ‘ μ°κ²°λμ΄μλ€.
- 쑰건μ λ§λ ν΄μ¬κ°λ₯Ό μ°Ύμμ μ/λ€ λΈλμ μ κ·Όκ²μ΄λ€.
- κΈΈμ΄κ° κΈ΄, μΌλ ¨μ λΈλ‘λ€μ μ/λ€ λ¬΄κ²°μ±μ ν΄μ¬κ°μΌλ‘ μ¦λͺ νλ€.
- λμ΄μ λΈλμ λ΄μ©μ΄ νλλΌλ λ°λλ€λ©΄, λ€μ λΈλμ λͺ¨λ 무ν¨κ° λλ€.
- ν΄νΉμ μν΄ μ€κ°κ°μ λ°κΎΈλ €λ©΄, κ·Έ μ΄ν λΈλμ μμ μ¦λͺ μ λͺ¨λ λ°κΏμΌ νλ€.
- μ΄ κ³Όμ μμ μμ μ¦λͺ μ νλλ°, λ무 λ§μ μκ°μ΄ μμλλ€.
1
2
3
4
5
6
7
8
9
10
11
{'difficulty': '0000000',
'hash_present': '0000000f8fc7d21dc1a4c67f3ec72ac5',
'hash_previous': '000005fa8482b821aff9b2ce6103f69e',
'index': 12,
'nonce': 19760524,
'timestamp': 1539441177.5858967,
'transaction': [{'amount': 1000, 'recipient': 'Bob', 'sender': 'Alice'},
{'amount': 800, 'recipient': 'Alice', 'sender': 'Scrouge'},
{'amount': 200,
'recipient': 'node_identifier_uid',
'sender': 'coinbase_reward'}]}
- λμ΄λκ° λ€μ리 μΆκ°λ‘ λ¨μ΄μ Έμ, μλνμκ° 19,760,524λ² μΌλ‘ λμ΄λ¬λ€.
- λΈλμ΄ λ€λ‘ κ°μλ‘ λμ΄λκ° μ¦κ°νλ©°, μμ μ¦λͺ μ μκ°μ΄ κΈ°νκΈμμ μΌλ‘ λμ΄λ¨.
- μ€κ°λΈλ λ΄μ©μ μμ‘°νλ©΄, μ΄ν λΈλμ λͺ¨λ μμ μ¦λͺ μ λ°κΏμΌ ν΄νΉμ΄ μμ±λλλ°,
- λΈλμ 10λΆλ§λ€ μΆκ°λ‘ μμ±λλ€.
- κ²λ€κ°, μΌλ ¨μ λΈλλͺ¨μ μ 보λ λͺ¨λ λ Έλλ€μ΄ μλμΌλ‘ 곡μ νκ³ μλ€.
- λ°λΌμ, λ Έλκ°μ 곡μ νκ³ μλ λͺ¨λ λΈλμ μΌμκ°μ ν΄νΉ νλκ²μ λΆκ°λ₯ νλ€.
[image ref.]: Understanding How Blockchain Works |
- λΈλν΄μ¬μ μ λ ₯ κ°μ λ³κ²½μν¬ μ μλ μ μΌν λ³μλ λ Όμ€(nonce)λ€.
- λ Όμ€λ₯Ό λ³κ²½νλ©°, ν΄μ¬κ°μ κ³μ°ν΄μ λμ΄λ쑰건μ λ§μ‘±νλ κ°μ΄ λμ¬ λ λ°λ³΅
- λ Όμ€λ 0 μμλΆν° +1μ© μ¦κ°μν€λ©° λ°λ³΅ κ³μ°νκΈ° λλ¬Έμ κ²°κ³Όμ μΌλ‘ κ³μ° νμλ€.
Result of the Block Hash Calculation, Changing difficulty
- λμ΄λλ₯Ό λ°κΏμ κ³μ°ν΄ 보면, κ³μ°μκ°κ³Ό λ°λ³΅νμμμ ν° μ°¨μ΄κ° λλ€.
- Previous hash is β0000β¦β pressent hash starts with β000000β¦β
π’ difficulty related with nonce makes different on execute time. (click to see bigger) |
π₯Well-explained VOD about what does nonce mean for.
- λ Όμ€(nonce)μ μμ μ¦λͺ (POW)μ λν΄μ μμ£Ό μ μ€λͺ ν΄ λμ μμ
λ°λΌνκΈ°λ κ°λ₯~
- YouTube Here : https://www.youtube.com/watch
- Code Here : https://github.com/anders94/blockchain-demo
CODE REVIEW
λμ΄λ(difficulty)λ₯Ό λ°κΏκ°λ©΄μ, λ Όμ€(nonce)λ₯Ό λ³κ²½ ν΄μ κ³μ° ν΄ λ³΄μ.
- λΈλ‘ μμ λͺ¨λ κ° λ€μ μ΄λ―Έ κ²°μ λμ΄ μλ€ (present hash = 0000 β¦.)
- nonce λ§μ΄ μ μΌνκ² minner κ° λ°κΏ μ μλ κ° μ΄λ€.
- λͺ©ν Present hash λ₯Ό μ°ΎκΈ° μν΄ nonceλ₯Ό 1μ© μ¦κ° μν€λ©° Try & Errorλ‘ μ°Ύλλ€.
- μ°μ°ν ν΄μ¬κ°μ 첫μλ¦¬κ° β0000βμΌλ‘ μμνλ λͺ©ν ν΄μ¬λ₯Ό λ°κ²¬ νμ λ,
- μμ μ¦λͺ μ μμ± νλ€. μμ μ¦λͺ (Proof of Work) κ°μ nonce=7725 μ΄λ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import time
import pyprnt
from hashlib import sha256
# difficulty λ₯Ό '0000' μμ 0 μ νλμ© λλ € μ€νμκ°μ λΉκ΅νλ€.
difficulty = '0000'
mining_uid = 'node_identifier_uid'
transactions = [
{
'sender': 'Alice',
'recipient': 'Bob',
'amount': 1000
},
{
'sender': 'Scrouge',
'recipient': 'Alice',
'amount': 800
},
{
'sender': 'coinbase_reward',
'recipient': mining_uid,
'amount': 200
},
]
last_block = {
'index': 12,
'difficulty': difficulty,
'nonce': 0,
'hash_previous': '000005fa8482b821aff9b2ce6103f69e',
'transaction': transactions,
}
def get_hash_w_nonce(last_block, nonce):
""" μ΅κ·ΌλΈλμ λ
Όμ€λ₯Ό λμ
νμ¬ ν΄μ¬κ°μ 리ν΄νλ€"""
last_block['nonce'] = nonce
hash = sha256(str(last_block).encode()).hexdigest()
return hash
def add_header(last_block, block_hash):
""" λΈλμ νμμ€ν¬νμ νμ¬ν΄μ¬λ₯Ό μΆκ°νλ€"""
header = {
'timestamp': time.time(),
'hash_present': block_hash, }
for _key, _val in header.items():
last_block[_key] = _val
return last_block
def proof_of_work(last_block):
nonce = 0
difficulty = last_block['difficulty']
while True:
hash = get_hash_w_nonce(last_block, nonce)
if hash[:len(difficulty)] == difficulty:
hash_present = hash[:32]
add_header(last_block, hash_present)
return last_block
nonce += 1
if __name__ == '__main__':
last_block = proof_of_work(last_block)
pyprnt.prnt(last_block)
Written on ... October 12, 2018