Repository: NethermindEth/juno
Pull request #174 (2022-05-26 14:47:54)

refactor: export `trie.Node`
## Changes:

- Exports the `trie.Node` type so that it can be used outside the trie package.

## Types of changes

- [x] Refactoring (no functional changes, no api changes)

## Testing
**Requires testing**

- [x] Yes
- [ ] No

**In case you checked yes, did you write tests??**

- [x] Yes
- [ ] No


codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
> Merging [#174](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (66c3967) into [main](https://codecov.io/gh/NethermindEth/juno/commit/0a38b0ff2c1defda1b83be320e5ef42748ab2abf?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (0a38b0f) will **increase** coverage by `0.04%`.
> The diff coverage is `100.00%`.

```diff
@@             Coverage Diff             @@
##             main      #174      +/-   ##
===========================================
+ Coverage   99.95%   100.00%   +0.04%     
===========================================
  Files          44        44              
  Lines        4012      4011       -1     
===========================================
+ Hits         4010      4011       +1     
+ Misses          2         0       -2     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (+0.04%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [pkg/trie/node.go](https://codecov.io/gh/NethermindEth/juno/pull/174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3RyaWUvbm9kZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [pkg/trie/trie.go](https://codecov.io/gh/NethermindEth/juno/pull/174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3RyaWUvdHJpZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [pkg/crypto/internal/randutil/randutil.go](https://codecov.io/gh/NethermindEth/juno/pull/174/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL2NyeXB0by9pbnRlcm5hbC9yYW5kdXRpbC9yYW5kdXRpbC5nbw==) | `100.00% <0.00%> (+18.18%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Last update [0a38b0f...66c3967](https://codecov.io/gh/NethermindEth/juno/pull/174?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).


Patch:
diff --git a/pkg/trie/node.go b/pkg/trie/node.go
index c8b8ad35..9091092c 100644
--- a/pkg/trie/node.go
+++ b/pkg/trie/node.go
@@ -7,28 +7,28 @@ import (
 	"github.com/NethermindEth/juno/pkg/crypto/pedersen"
 )
 
-// encoding represents the encoding of a node in a binary tree
+// Encoding represents the Encoding of a node in a binary tree
 // represented by the triplet (length, path, bottom).
-type encoding struct {
+type Encoding struct {
 	Length uint8    `json:"length"`
 	Path   *big.Int `json:"path"`
 	Bottom *big.Int `json:"bottom"`
 }
 
-// node represents a node in a binary tree.
-type node struct {
-	encoding
+// Node represents a Node in a binary tree.
+type Node struct {
+	Encoding
 	Hash *big.Int `json:"hash"`
 }
 
 // bytes returns a JSON byte representation of a node.
-func (n *node) bytes() []byte {
+func (n *Node) bytes() []byte {
 	b, _ := json.Marshal(n)
 	return b
 }
 
 // hash updates the node hash.
-func (n *node) hash() {
+func (n *Node) hash() {
 	if n.Length == 0 {
 		n.Hash = new(big.Int).Set(n.Bottom)
 		return
diff --git a/pkg/trie/trie.go b/pkg/trie/trie.go
index 7ed2b7ee..d26fd791 100644
--- a/pkg/trie/trie.go
+++ b/pkg/trie/trie.go
@@ -88,18 +88,18 @@ func (t *Trie) remove(key []byte) {
 
 // retrieve gets a node from storage and returns true if the node was
 // found.
-func (t *Trie) retrieve(key []byte) (node, bool) {
+func (t *Trie) retrieve(key []byte) (Node, bool) {
 	if len(key) == 0 {
 		key = []byte("root")
 	}
 	b, ok := t.store.Get(key)
 	if !ok {
-		return node{}, false
+		return Node{}, false
 	}
-	var n node
+	var n Node
 	if err := json.Unmarshal(b, &n); err != nil {
 		// notest
-		return node{}, false
+		return Node{}, false
 	}
 	return n, true
 }
@@ -119,16 +119,16 @@ func (t *Trie) diff(key *big.Int) {
 			t.remove(parent)
 		} else {
 			// Overwrite the parent node.
-			n := new(node)
+			n := new(Node)
 
 			// Compute its encoding.
 			switch {
 			case !rightChildIsNotEmpty:
-				n.encoding = encoding{
+				n.Encoding = Encoding{
 					leftChild.Length + 1, leftChild.Path, new(big.Int).Set(leftChild.Bottom),
 				}
 			case !leftChildIsNotEmpty:
-				n.encoding = encoding{
+				n.Encoding = Encoding{
 					rightChild.Length + 1,
 					rightChild.Path.Add(
 						rightChild.Path,
@@ -138,7 +138,7 @@ func (t *Trie) diff(key *big.Int) {
 					new(big.Int).Set(rightChild.Bottom),
 				}
 			default:
-				n.encoding = encoding{
+				n.Encoding = Encoding{
 					0, new(big.Int), pedersen.Digest(leftChild.Hash, rightChild.Hash),
 				}
 			}
@@ -187,8 +187,7 @@ func (t *Trie) Put(key, val *big.Int) {
 	// copy with the bits reversed is used instead.
 	rev := Reversed(key, t.keyLen)
 
-	leaf := node{encoding: encoding{0, new(big.Int), val}}
-	leaf.hash()
+	leaf := Node{Encoding{0, new(big.Int), val}, val}
 	t.commit(Prefix(rev, t.keyLen), leaf.bytes())
 	t.diff(rev)
 }


Test patch:
diff --git a/pkg/trie/trie_test.go b/pkg/trie/trie_test.go
index b480dfaa..66cc9bab 100644
--- a/pkg/trie/trie_test.go
+++ b/pkg/trie/trie_test.go
@@ -194,7 +194,7 @@ func TestPut(t *testing.T) {
 				}
 				t.Fatalf("failed to retrieve value with key %s from database", pre)
 			}
-			var n node
+			var n Node
 			if err := json.Unmarshal(got, &n); err != nil {
 				t.Fatal("failed to unmarshal value from database")
 			}