Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
change to code
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-kr committed Jun 13, 2023
1 parent 7a0e606 commit f7dde3b
Showing 1 changed file with 115 additions and 10 deletions.
125 changes: 115 additions & 10 deletions KIPs/kip-111.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Each node in the State Trie defines the 32-byte hash of the value as the key, an

아래 그림은 Account 175의 잔고가 27에서 45로 변경될때 state tire와 block간의 연결관계를 그림으로 나타낸 것이다.
![state trie diagram](../assets/kip-111/state_trie_diagram.png)
위 그림에서 붉은 노드는 새로운 데이터로 변경되었다. 붉은 노드를 삭제해도 current block의 정보를 처리하는데는 아무런 문제를 일으키지 않는다. 하지만 기존의 Hash를 사용할 경우 어느 노드에서 붉은 노드를 참조하고 있는지 알수 없어 삭제할 수가 없었다. reference count 정보를 추가하고 관리할 수도 있겠지만 여러가지 이유(multi thread, 엄청난 노드수)로 쉽지 않다.
A new ExtHash is used in the State trie to remove the problem of multiple trie-nodes referencing a single trie-node. ExtHash is a Hash type that is created by adding a 6-byte serial number to the legacy hash. As a result, no trie-node with the same ExtHash can exist. Data is increased because data redundancy is eliminated, but it is judged to be at a manageable level as a result of the experiment.
위 그림에서 붉은 노드는 새로운 데이터로 변경되었다. pruning의 관점에서 봤을때 붉은 노드를 삭제해도 current block의 정보를 처리하는데는 아무런 문제를 일으키지 않는다. 하지만 기존의 Hash은 중복 referenctr가 발생하기 때문에 어느 노드에서 붉은 노드를 참조하고 있는지 알수 없어 삭제할 수가 없었다. reference count 정보를 추가하고 관리할 수도 있겠지만 여러가지 이유(multi thread, 엄청난 노드수)로 쉽지 않다.
A new ExtHash is used in the State trie to remove the problem of multiple trie-nodes referencing a single trie-node. ExtHash is a Hash type that is created by adding a 7-byte serial number to the legacy hash. As a result, no trie-node with the same ExtHash can exist. Data is increased because data redundancy is eliminated, but it is judged to be at a manageable level as a result of the experiment.
The biggest reason for introducing ExtHash is to eliminate multiple references. Many of the pruning methods that have been tried so far have had to stop the system or be accompanied by a lot of IO load due to multiple reference problems. The slight data overhead added to ExtHash allows pruning to proceed without system downtime and without worrying about IO load.


Expand Down Expand Up @@ -54,31 +54,136 @@ h. MPT - Merkle Patrica Trie


### Trie 구조 정의
State Trie Node를 다음과 같이 표현할 수 있다.
* 모든 Server Node에서 statedb pruning을 사용하는 것은 아니다.
ExtHash는 State Trie Node에서만 사용한다. Header, Body, Transaction, Reciept등은 중복문제가 발생하지 않기 때문에 32byte hash를 사용한다.
* State Trie Node는 32byte Hash와 39byte ExtHash 중 1가지만 사용된다.
* 기존과 동일하게 동작하는 Server Node에서는 32byte Hash를 사용한다.
* statedb pruning을 사용하는 Server Node에서는 38byte Hash를 사용한다.
* statedb pruning을 사용하는 Server Node에서는 39byte Hash를 사용한다.
```go
Ref = byte32(Hash) or byte38(ExtHash)
ref_32 = 32 byte - Hash
ref_39 = 39 byte - ExtHash
Ref = ref_32 or ref_39
Leaf = [ path, val ]
Extension = [ path, Ref ]
Branch = [ Ref, Ref, ..., Ref, val ]
Account = [ balance, nonce, storageRoot Ref, codeHash H ]
```

### Merkel Patricia Trie - 32 byte Hash
위에서 정의를 바탕으로 32 byte Hash를 사용해서 Merkel Patricia trie를 간단히 표현해 보면 아래와 같이 표현할 수 있다.
Account 정보는 다음과 같다. ( 참조(https://ethereum.stackexchange.com/questions/39915/ethereum-merkle-patricia-trie-extension-node) 문서에서 중복 참조가 일어나도록 약간만 수정 )
a711355 : 45.0 ETH
a77d337 : 1.00 WEI
a7f9363 : 1.10 ETH
a77d397 : 1.00 WEI
```go
Diagram-1
Root("acec...e3c5") - Extension0 [ "a7", "72fd...b753" ]
|
Branch0 [ 0:N, 1:"7323...25a7", 2:N, ..., 7:"945e...f7af", 8:N, ..., f:"6c8d...5660", val ]
/ | \
Leaf0 [ "1355", 45.0 ETH ] Extension1 [ "d3", "127f...6d00" ] Leaf1 [ "9365", 1.10 ETH ]
|
Branch1 [ 0:N, ..., 3:"5597...7d27", ..., 9:"5597...7d27", ..., f:N, val ]
\ /
Leaf2 [ "7", 1.00 WEI ]
```

### Merkel Patricia Trie - 39 byte ExtHash
위 32 byte Hash를 39 byte ExtHash 사용해서 Merkel Patricia trie를 간단히 표현해 보면 아래와 같이 표현할 수 있다.
Account 정보는 위와 동일하다.
편의상 xxx1, xxx2식의 표현이 추가된 7바이트를 의미한다.
```go
Diagram-2
Root("acec...e3c5") - Extension0 [ "a7", "72fd...b753"xxx7 ]
|
Branch0 [ 0:N, 1:"7323...25a7"xxx6, 2:N, ..., 7:"945e...f7af"xxx5, 8:N, ..., f:"6c8d...5660"xxx4, val ]
/ | \
Leaf0 [ "1355", 45.0 ETH ] Extension1 [ "d3", "127f...6d00"xxx3 ] Leaf1 [ "9365", 1.10 ETH ]
|
Branch1 [ 0:N, ..., 3:"5597...7d27"xxx2, ..., 9:"5597...7d27"xxx1, ..., f:N, val ]
/ \
Leaf2 [ "7", 1.00 WEI ] Leaf3 [ "7", 1.00 WEI ]
```

### Get the same MPT Root Hash from ExtHash

ExtHash is a hash with 6 bytes added to the original hash. This changes the Root Hash value when calculating the hash of MPT (Merkle Patricia Trie). This can cause a collision with nodes (servers) or past versions of the hash that do not use ExtHash. To solve this problem, when calculating the hash of MPT, you can get the same result as before by removing the last 6 bytes from ExtHash and calculating it. In this section, we will describe the process of getting the MPT Root Hash using the original Hash by changing ExtHash to Hash.
ExtHash is a hash with 7 bytes added to the original hash. This changes the Root Hash value when calculating the hash of MPT (Merkle Patricia Trie). This can cause a collision with nodes (servers) or past versions of the hash that do not use ExtHash. To solve this problem, when calculating the hash of MPT, you can get the same result as before by removing the last 7 bytes from ExtHash and calculating it. In this section, we will describe the process of getting the MPT Root Hash using the original Hash by changing ExtHash to Hash.

#### ExtHash에서 기존Hash 얻기 - step by step
Diagram-1,2는 Hash와 ExtHash와의 차이점을 보이기 위한 그림이며 Hash가 계산된 완성본의 형태이다.
원래의 Hash가 계산되기 전상태의 메모리상의 MPT를 표현하면 아래처럼 표현할 수 있다.
```go
Diagram-3
Root - Extension0 [ "a7", Branch0 ]
|
Branch0 [ 0:N, 1:Leaf0, 2:N, ..., 7:Extension1, 8:N, ..., f:Leaf1, val ]
/ | \
Leaf0 [ "1355", 45.0 ETH ] Extension1 [ "d3", Branch1 ] Leaf1 [ "9365", 1.10 ETH ]
|
Branch1 [ 0:N, ..., 3:Leaf2, ..., 9:Leaf3, ..., f:N, val ]
/ \
Leaf2 [ "7", 1.00 WEI ] Leaf3 [ "7", 1.00 WEI ]
```
Hash계산은 아래에서 부터 위로 검사하기 때문에 Leaf3에서 위쪽으로 계산합니다.
* Leaf3는 ref_39가 존재하지 않음으로 그냥 Hash를 계산하고 random count "xxx1"을 붙입니다.
Leaf3의 ref_39는 "5597...7d27"xxx1이 되며 Branch1의 9번 child를 "5597...7d27"xxx1으로 업데이트 합니다.
* Leaf2도 Leaf3와 동일하지만 이번에는 random count가 "xxx2"가 됩니다.
Leaf2의 ref_39는 "5597...7d27"xxx2가 되며 Branch1의 3번 child를 "5597...7d27"xxx2로 업데이트 합니다.
* Branch1에는 위 Leaf3, Leaf2에서 업데이트된 2개의 ref_39가 존재합니다. Hash를 계산하기 전에 ref_39를 ref_32로 잠시 변환합니다.
그러면 Branch1의 value는 [ 0:N, ..., 3:"5597...7d27", ..., 9:"5597...7d27", ..., f:N, val ] 이 되며, Diagram-1의 Branch1과 동일한 Value가 되고 Hash를 계산하면 "127f...6d00"가 되고 random count "xxx3"을 붙입니다.









<!-- Diagram-2를 Key : Value형식으로 정리하면 아래처럼 표현할 수 있습니다.
```go
"acec...e3c5" : Extension0 - [ "a7", "72fd...b753"xxx1 ]
"72fd...b753"xxx1 : Branch0 - [ 0:N, 1:"7323...25a7"xxx2, 2:N, ..., 7:"945e...f7af"xxx3, 8:N, ..., f:"6c8d...5660"xxx4, val ]
"7323...25a7"xxx2 : Leaf0 - [ "1355", 45.0 ETH ]
"945e...f7af"xxx3 : Extension1 - [ "d3", "127f...6d00"xxx5 ]
"6c8d...5660"xxx4 : Leaf1 - [ "9365", 1.10 ETH ]
"127f...6d00"xxx5 : Branch1 - [ 0:N, ..., 3:"5597...7d27"xxx6, ..., 9:"5597...7d27"xxx7, ..., f:N, val ]
"5597...7d27"xxx6 : Leaf2 - [ "7", 1.00 WEI ]
"5597...7d27"xxx7 : Leaf3 - [ "7", 1.00 WEI ]
``` -->



Hash계산은 아래에서 부터 위로 검사하기 때문에 Leaf3에서 위쪽으로 계산합니다.
* Leaf3의 ref_39에서 마지만 xxx7만 제거하면 원래 Hash("5597...7d27")를 얻을 수 있습니다.
* Leaf2도 Leaf3과 같은 방븝으로 기존 Hash("5597...7d27")을 얻습니다.
* Branch1



Leaf3의 ref_39는 "5597...7d27"xxx7 이며, Leaf2의 ref_39는 "5597...7d27"xxx6 입니다.








#### Merkle Patricia Trie
아래 그림은 기존 MPT에서 value에서 Hash를 구하고 서로를 참조하는 Root Node, Child Node, Leaf Node간의 연결 관계를 나타낸 그림이다.
![Legacy Merkle Patricia Trie diagram](../assets/kip-111/legacy_trie_diagram.png)

아래 그림은 ExtHash를 사용한 MPT의 연결 관계를 나타낸 그림이다.
* Root Node에서 Child Node, Leaf Node를 참조할때는 ExtHash를 사용해서 연결된다.
* 노드의 정합성을 검증하기 위한 Hash를 구할때는 ExtHash의 마지막 6자리를 제외하고 Hash를 계산해서 기존 MPT와 동일한 Hash를 얻을 수 있다.
* 노드의 정합성을 검증하기 위한 Hash를 구할때는 ExtHash의 마지막 7자리를 제외하고 Hash를 계산해서 기존 MPT와 동일한 Hash를 얻을 수 있다.
![ExtHash Merkle Patricia Trie diagram](../assets/kip-111/extHash_trie_diagram.png)







#### ExtHash Filter
The ExtHashFilter function works as follows.
All trie-nodes in MPT are of type fullNode or shorNode.
Expand Down Expand Up @@ -139,7 +244,7 @@ func storeByHash( n node ) error {
// use ExtHash process
func storeByExtHash( n node ) error {
data = rlp.encode(n)
hash = keccak256(data) + 6 byte random count
hash = keccak256(data) + 7 byte random count

err = db.put(hashm data)
return errr
Expand Down

0 comments on commit f7dde3b

Please sign in to comment.