Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup] Partial cleanup & refactor of different parts of the codebase #44

Merged
merged 43 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
2aeabff
Rough WIP - adding some notes related to kicking off the audit.
Olshansk Feb 8, 2024
b876799
WIP - Going through the extension node documentation
Olshansk Feb 8, 2024
d83d374
WIP - Moved extension node into its own file and removed the resolver…
Olshansk Feb 8, 2024
3cb019e
Split all the node types into different files
Olshansk Feb 8, 2024
562d7fa
Remove the unnecessary resolve helper and rename default value helpers
Olshansk Feb 8, 2024
04853f7
WIP - adding comments to different parts of extension_node.go
Olshansk Feb 8, 2024
6b368c0
Remove the need for the sum workaround function
Olshansk Feb 9, 2024
89f9ceb
WIP - refactoring node hasher's and encoders
Olshansk Feb 9, 2024
1857496
Renamed a few things and converted the examples to proper tests
Olshansk Feb 9, 2024
340d060
Added e2e_review.md and started going through the _node.go files
Olshansk Feb 10, 2024
ae20eab
A lot of things in flux but finished commenting node_encoders
Olshansk Feb 10, 2024
bff8a76
Add prefixLen in node encoders
Olshansk Feb 10, 2024
cd24a78
Interim checkpoint commit - modified lots of code and all tests pass …
Olshansk Feb 11, 2024
03d85ea
Improved how placeholder values are maintained and how the node is re…
Olshansk Feb 11, 2024
ee67740
Moved TrieSpec functions from utils into receiver functions
Olshansk Feb 11, 2024
c263cd7
Moved trie spec into its own file
Olshansk Feb 12, 2024
5e7ed76
chore: add context on hashing algorithms used by the trie
Mar 19, 2024
9e9d9b3
chore: add context on external kvstore writeability
Mar 19, 2024
4831b52
feat: consolidate ClosestProof verification and remove the NilPathHas…
Mar 19, 2024
46dc5c5
feat: reorganise extension node insertion to use separate pointers fo…
Mar 19, 2024
3862345
Old changes
Olshansk Mar 21, 2024
3981639
Merge branch 'main' into audit-remediations
h5law Apr 2, 2024
5199969
Review submittd PR
Olshansk Apr 8, 2024
3fe0d31
Merge with audit-remediations
Olshansk Apr 9, 2024
63643ce
Merge with main
Olshansk May 31, 2024
c96cb4a
Fix tests
Olshansk Jun 3, 2024
47b74de
Self review
Olshansk Jun 3, 2024
92d467e
Revert example
Olshansk Jun 3, 2024
b322e33
Update extension_node.go
Olshansk Jun 4, 2024
186ca40
Update docs/smt.md
Olshansk Jun 4, 2024
2594f1b
Update docs/smt.md
Olshansk Jun 4, 2024
77ba35d
Update hasher.go
Olshansk Jun 4, 2024
a54669b
Before adding helper
Olshansk Jun 4, 2024
9b48300
Added helpers
Olshansk Jun 4, 2024
1a144d1
Reply to harry's comments
Olshansk Jun 4, 2024
3f43640
Added nolint
Olshansk Jun 4, 2024
32c5f24
Update docs/smt.md
Olshansk Jun 5, 2024
5ebf731
Update docs/smt.md
Olshansk Jun 5, 2024
cf7b7d5
Update docs/smt.md
Olshansk Jun 5, 2024
583f57d
Update extension_node.go
Olshansk Jun 5, 2024
eb543a3
Update hasher.go
Olshansk Jun 5, 2024
3d3e3c8
Replied to remaining review comments
Olshansk Jun 5, 2024
db19822
Added TODO
Olshansk Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
# Ignore Goland and JetBrains IDE files
.idea/

# Visual Studio Code
# Ignore vscode files
.vscode
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ check_godoc:

.PHONY: test_all
test_all: ## runs the test suite
go test -v -p 1 ./... -mod=readonly -race
go test -v -p 1 -count=1 ./... -mod=readonly -race

.PHONY: test_badger
test_badger: ## runs the badger KVStore submodule's test suite
go test -v -p 1 ./kvstore/badger/... -mod=readonly -race
go test -v -p 1 -count=1 ./kvstore/badger/... -mod=readonly -race


#####################
Expand Down
2 changes: 1 addition & 1 deletion bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func bulkOperations(t *testing.T, operations int, insert int, update int, delete
if err != nil && err != ErrKeyNotFound {
t.Fatalf("error: %v", err)
}
kv[ki].val = defaultValue
kv[ki].val = defaultEmptyValue
}
}

Expand Down
25 changes: 25 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# FAQ <!-- omit in toc -->

- [History](#history)
- [Fork](#fork)
- [Implementation](#implementation)
- [What's the story behind Extension Node Implementation?](#whats-the-story-behind-extension-node-implementation)

This documentation is meant to capture common questions that come up and act
as a supplement or secondary reference to the primary documentation.

## History

### Fork

This library was originally forked off of [celestiaorg/smt](https://github.com/celestiaorg/smt)
which was archived on Feb 27th, 2023.

## Implementation

### What's the story behind Extension Node Implementation?

The [SMT extension node](./smt.md#extension-nodes) is very similar to that of
Ethereum's [Modified Merkle Patricia Trie](https://ethereum.org/developers/docs/data-structures-and-encoding/patricia-merkle-trie).

A quick primer on it can be found in this [5P;1R post](https://olshansky.substack.com/p/5p1r-ethereums-modified-merkle-patricia).
205 changes: 103 additions & 102 deletions docs/merkle-sum-trie.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

<!-- toc -->

- [Overview](#overview)
- [Implementation](#implementation)
* [Sum Encoding](#sum-encoding)
* [Digests](#digests)
* [Visualisations](#visualisations)
+ [General Trie Structure](#general-trie-structure)
+ [Binary Sum Digests](#binary-sum-digests)
- [Sum](#sum)
- [Roots](#roots)
- [Nil Values](#nil-values)
- [Sparse Merkle Sum Trie (smst)](#sparse-merkle-sum-trie-smst)
- [Overview](#overview)
- [Implementation](#implementation)
- [Sum Encoding](#sum-encoding)
- [Digests](#digests)
- [Visualizations](#visualizations)
- [General Trie Structure](#general-trie-structure)
- [Binary Sum Digests](#binary-sum-digests)
- [Sum](#sum)
- [Roots](#roots)
- [Nil Values](#nil-values)

<!-- tocstop -->

Expand Down Expand Up @@ -64,34 +65,34 @@ The golang `encoding/binary` package is used to encode the sum with
`binary.BigEndian.PutUint64(sumBz[:], sum)` into a byte array `sumBz`.

In order for the SMST to include the sum into a leaf node the SMT the SMST
initialises the SMT with the `WithValueHasher(nil)` option so that the SMT does
initializes the SMT with the `WithValueHasher(nil)` option so that the SMT does
**not** hash any values. The SMST will then hash the value and append the sum
bytes to the end of the hashed value, using whatever `ValueHasher` was given to
the SMST on initialisation.
the SMST on initialization.

```mermaid
graph TD
subgraph KVS[Key-Value-Sum]
K1["Key: foo"]
K2["Value: bar"]
K3["Sum: 10"]
end
subgraph SMST[SMST]
SS1[ValueHasher: SHA256]
subgraph SUM["SMST.Update()"]
SU1["valueHash = ValueHasher(Value)"]
SU2["sumBytes = binary(Sum)"]
SU3["valueHash = append(valueHash, sumBytes...)"]
end
end
subgraph SMT[SMT]
SM1[ValueHasher: nil]
subgraph UPD["SMT.Update()"]
U2["SMT.nodeStore.Set(Key, valueHash)"]
end
end
KVS --"Key + Value + Sum"--> SMST
SMST --"Key + valueHash"--> SMT
subgraph KVS[Key-Value-Sum]
K1["Key: foo"]
K2["Value: bar"]
K3["Sum: 10"]
end
subgraph SMST[SMST]
SS1[ValueHasher: SHA256]
subgraph SUM["SMST.Update()"]
SU1["valueHash = ValueHasher(Value)"]
SU2["sumBytes = binary(Sum)"]
SU3["valueHash = append(valueHash, sumBytes...)"]
end
end
subgraph SMT[SMT]
SM1[ValueHasher: nil]
subgraph UPD["SMT.Update()"]
U2["SMT.nodeStore.Set(Key, valueHash)"]
end
end
KVS --"Key + Value + Sum"--> SMST
SMST --"Key + valueHash"--> SMT
```

### Digests
Expand Down Expand Up @@ -128,10 +129,10 @@ Therefore for the following node types, the digests are computed as follows:
This means that with a hasher such as `sha256.New()` whose hash size is
`32 bytes`, the digest of any node will be `40 bytes` in length.

### Visualisations
### Visualizations

The following diagrams are representations of how the trie and its components
can be visualised.
can be visualized.

#### General Trie Structure

Expand All @@ -142,45 +143,45 @@ nodes as an extra field.

```mermaid
graph TB
subgraph Root
A1["Digest: Hash(Hash(Path+H1)+Hash(H2+(Hash(H3+H4)))+Binary(20))+Binary(20)"]
subgraph Root
A1["Digest: Hash(Hash(Path+H1)+Hash(H2+(Hash(H3+H4)))+Binary(20))+Binary(20)"]
A2[Sum: 20]
end
subgraph BI[Inner Node]
B1["Digest: Hash(H2+(Hash(H3+H4))+Binary(12))+Binary(12)"]
end
subgraph BI[Inner Node]
B1["Digest: Hash(H2+(Hash(H3+H4))+Binary(12))+Binary(12)"]
B2[Sum: 12]
end
subgraph BE[Extension Node]
B3["Digest: Hash(Path+H1+Binary(8))+Binary(8)"]
end
subgraph BE[Extension Node]
B3["Digest: Hash(Path+H1+Binary(8))+Binary(8)"]
B4[Sum: 8]
end
subgraph CI[Inner Node]
C1["Digest: Hash(H3+H4+Binary(7))+Binary(7)"]
end
subgraph CI[Inner Node]
C1["Digest: Hash(H3+H4+Binary(7))+Binary(7)"]
C2[Sum: 7]
end
subgraph CL[Leaf Node]
C3[Digest: H2]
end
subgraph CL[Leaf Node]
C3[Digest: H2]
C4[Sum: 5]
end
subgraph DL1[Leaf Node]
D1[Digest: H3]
end
subgraph DL1[Leaf Node]
D1[Digest: H3]
D2[Sum: 4]
end
subgraph DL2[Leaf Node]
D3[Digest: H4]
end
subgraph DL2[Leaf Node]
D3[Digest: H4]
D4[Sum: 3]
end
subgraph EL[Leaf Node]
E1[Digest: H1]
end
subgraph EL[Leaf Node]
E1[Digest: H1]
E2[Sum: 8]
end
Root-->|0| BE
Root-->|1| BI
BI-->|0| CL
BI-->|1| CI
CI-->|0| DL1
CI-->|1| DL2
BE-->EL
end
Root-->|0| BE
Root-->|1| BI
BI-->|0| CL
BI-->|1| CI
CI-->|0| DL1
CI-->|1| DL2
BE-->EL
```

#### Binary Sum Digests
Expand All @@ -192,56 +193,56 @@ exception of the leaf nodes where the sum is shown as part of its value.

```mermaid
graph TB
subgraph RI[Inner Node]
RIA["Root Hash: Hash(D6+D7+Binary(18))+Binary(18)"]
subgraph RI[Inner Node]
RIA["Root Hash: Hash(D6+D7+Binary(18))+Binary(18)"]
RIB[Sum: 15]
end
subgraph I1[Inner Node]
I1A["D7: Hash(D1+D5+Binary(11))+Binary(11)"]
end
subgraph I1[Inner Node]
I1A["D7: Hash(D1+D5+Binary(11))+Binary(11)"]
I1B[Sum: 11]
end
subgraph I2[Inner Node]
I2A["D6: Hash(D3+D4+Binary(7))+Binary(7)"]
end
subgraph I2[Inner Node]
I2A["D6: Hash(D3+D4+Binary(7))+Binary(7)"]
I2B[Sum: 7]
end
subgraph L1[Leaf Node]
L1A[Path: 0b0010000]
L1B["Value: 0x01+Binary(6)"]
end
subgraph L1[Leaf Node]
L1A[Path: 0b0010000]
L1B["Value: 0x01+Binary(6)"]
L1C["H1: Hash(Path+Value+Binary(6))"]
L1D["D1: H1+Binary(6)"]
end
subgraph L3[Leaf Node]
L3A[Path: 0b1010000]
L3B["Value: 0x03+Binary(3)"]
end
subgraph L3[Leaf Node]
L3A[Path: 0b1010000]
L3B["Value: 0x03+Binary(3)"]
L3C["H3: Hash(Path+Value+Binary(3))"]
L3D["D3: H3+Binary(3)"]
end
subgraph L4[Leaf Node]
L4A[Path: 0b1100000]
L4B["Value: 0x04+Binary(4)"]
end
subgraph L4[Leaf Node]
L4A[Path: 0b1100000]
L4B["Value: 0x04+Binary(4)"]
L4C["H4: Hash(Path+Value+Binary(4))"]
L4D["D4: H4+Binary(4)"]
end
subgraph E1[Extension Node]
E1A[Path: 0b01100101]
E1B["Path Bounds: [2, 6)"]
end
subgraph E1[Extension Node]
E1A[Path: 0b01100101]
E1B["Path Bounds: [2, 6)"]
E1C[Sum: 5]
E1D["H5: Hash(Path+PathBounds+D2+Binary(5))"]
E1E["D5: H5+Binary(5)"]
end
subgraph L2[Leaf Node]
L2A[Path: 0b01100101]
L2B["Value: 0x02+Binary(5)"]
end
subgraph L2[Leaf Node]
L2A[Path: 0b01100101]
L2B["Value: 0x02+Binary(5)"]
L2C["H2: Hash(Path+Value+Hex(5))+Binary(5)"]
L2D["D2: H2+Binary(5)"]
end
RI -->|0| I1
RI -->|1| I2
I1 -->|0| L1
I1 -->|1| E1
E1 --> L2
I2 -->|0| L3
I2 -->|1| L4
end
RI -->|0| I1
RI -->|1| I2
I1 -->|0| L1
I1 -->|1| E1
E1 --> L2
I2 -->|0| L3
I2 -->|1| L4
```

## Sum
Expand Down
Loading
Loading