Repository: NethermindEth/juno
Pull request #171 (2022-05-26 12:38:06)

refactor: export trie utility functions
## Changes:

- Export trie utility functions to allow them to be used outside the trie
package.

## Types of changes

- [x] Refactoring (no functional changes, no api changes)

## Testing

**Requires testing**

- [ ] Yes
- [x] No

**In case you checked yes, did you write tests??**

- [x] Yes
- [ ] No

codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/171?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
> Merging [#171](https://codecov.io/gh/NethermindEth/juno/pull/171?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (e54e7ad) into [main](https://codecov.io/gh/NethermindEth/juno/commit/3e71279632d82689e5af03e26693ca5c58a2376e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (3e71279) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##              main      #171   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           36        36           
  Lines         3771      3767    -4     
=========================================
- Hits          3771      3767    -4     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

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/171?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [pkg/trie/trie.go](https://codecov.io/gh/NethermindEth/juno/pull/171/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/trie/utils.go](https://codecov.io/gh/NethermindEth/juno/pull/171/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL3RyaWUvdXRpbHMuZ28=) | `100.00% <100.00%> (ø)` | |
| [pkg/crypto/weierstrass/weierstrass.go](https://codecov.io/gh/NethermindEth/juno/pull/171/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL2NyeXB0by93ZWllcnN0cmFzcy93ZWllcnN0cmFzcy5nbw==) | `100.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/171?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/171?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Last update [3e71279...e54e7ad](https://codecov.io/gh/NethermindEth/juno/pull/171?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/trie.go b/pkg/trie/trie.go
index 7991d014..7ed2b7ee 100644
--- a/pkg/trie/trie.go
+++ b/pkg/trie/trie.go
@@ -110,7 +110,7 @@ func (t *Trie) retrieve(key []byte) (node, bool) {
 // encoding and hashes otherwise.
 func (t *Trie) diff(key *big.Int) {
 	for height := t.keyLen - 1; height >= 0; height-- {
-		parent := prefix(key, height)
+		parent := Prefix(key, height)
 
 		leftChild, leftChildIsNotEmpty := t.retrieve(append(parent, 48 /* "0" */))
 		rightChild, rightChildIsNotEmpty := t.retrieve(append(parent, 49 /* "1" */))
@@ -157,9 +157,9 @@ func (t *Trie) Delete(key *big.Int) {
 	// The internal representation of big.Int has the least significant
 	// bit in the 0th position but this algorithm assumes the oppose so
 	// a copy with the bits reversed is used instead.
-	rev := reversed(key, t.keyLen)
+	rev := Reversed(key, t.keyLen)
 
-	t.remove(prefix(rev, t.keyLen))
+	t.remove(Prefix(rev, t.keyLen))
 	t.diff(rev)
 }
 
@@ -168,7 +168,7 @@ func (t *Trie) Get(key *big.Int) (*big.Int, bool) {
 	// The internal representation of big.Int has the least significant
 	// bit in the 0th position but this algorithm assumes the opposite so
 	// a copy with the bits reversed is passed into the function.
-	node, ok := t.retrieve(prefix(reversed(key, t.keyLen), t.keyLen))
+	node, ok := t.retrieve(Prefix(Reversed(key, t.keyLen), t.keyLen))
 	if !ok {
 		return nil, false
 	}
@@ -185,11 +185,11 @@ func (t *Trie) Put(key, val *big.Int) {
 	// The internal representation of big.Int has the least significant
 	// bit in the 0th position but this algorithm assumes the oppose so a
 	// copy with the bits reversed is used instead.
-	rev := reversed(key, t.keyLen)
+	rev := Reversed(key, t.keyLen)
 
 	leaf := node{encoding: encoding{0, new(big.Int), val}}
 	leaf.hash()
-	t.commit(prefix(rev, t.keyLen), leaf.bytes())
+	t.commit(Prefix(rev, t.keyLen), leaf.bytes())
 	t.diff(rev)
 }
 
diff --git a/pkg/trie/utils.go b/pkg/trie/utils.go
index 6e234e4f..9ef89bdf 100644
--- a/pkg/trie/utils.go
+++ b/pkg/trie/utils.go
@@ -6,9 +6,9 @@ import (
 	"strconv"
 )
 
-// prefix returns a byte representation of a binary key up to a given
+// Prefix returns a byte representation of a binary key up to a given
 // height in the tree.
-func prefix(key *big.Int, height int) []byte {
+func Prefix(key *big.Int, height int) []byte {
 	var buf bytes.Buffer
 	for i := 0; i < height; i++ {
 		buf.WriteString(strconv.FormatUint(uint64(key.Bit(i)), 10))
@@ -16,13 +16,13 @@ func prefix(key *big.Int, height int) []byte {
 	return buf.Bytes()
 }
 
-// reversed returns a copy of a *big.Int where the bits are in reverse
+// Reversed returns a copy of a *big.Int where the bits are in reverse
 // order up to a certain length n. For example, a 4-bit representation
 // of the number 11 as a *big.Int would be 0b1101, this function returns
 // a *big.Int represented by the number 0b1011 which is 13. This
 // function is generally used in situations where the most significant
 // bit has to be in the 0th position.
-func reversed(x *big.Int, n int) *big.Int {
+func Reversed(x *big.Int, n int) *big.Int {
 	rev := new(big.Int)
 	for i, j := n-1, 0; j < n; i, j = i-1, j+1 {
 		rev.SetBit(rev, j, x.Bit(i))


Test patch:
diff --git a/pkg/trie/trie_test.go b/pkg/trie/trie_test.go
index 91fb6e89..b480dfaa 100644
--- a/pkg/trie/trie_test.go
+++ b/pkg/trie/trie_test.go
@@ -185,7 +185,7 @@ func TestPut(t *testing.T) {
 	for _, test := range tests {
 		t.Run(fmt.Sprintf("put(%#v, %#v)", test.key, test.val), func(t *testing.T) {
 			trie.Put(test.key, test.val)
-			pre := prefix(reversed(test.key, testKeyLen), testKeyLen)
+			pre := Prefix(Reversed(test.key, testKeyLen), testKeyLen)
 			got, ok := db.Get(pre)
 			if !ok {
 				// A key with a value 0 is deleted.