Skip to content

Commit

Permalink
Unexport node's Encoding field
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Dec 13, 2021
1 parent 89bf885 commit 23560c6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion internal/trie/node/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Branch struct {
// from the node stored in the database.
dirty bool
hashDigest []byte
Encoding []byte
encoding []byte
// generation is incremented on every trie Snapshot() call.
// Nodes that are part of the trie are then gradually updated
// to have a matching generation number as well, if they are
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/branch_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func (b *Branch) hash(digestBuffer io.Writer) (err error) {
// Encode encodes a branch with the encoding specified at the top of this package
// to the buffer given.
func (b *Branch) Encode(buffer Buffer) (err error) {
if !b.dirty && b.Encoding != nil {
_, err = buffer.Write(b.Encoding)
if !b.dirty && b.encoding != nil {
_, err = buffer.Write(b.encoding)
if err != nil {
return fmt.Errorf("cannot write stored encoding to buffer: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Test_Branch_Encode(t *testing.T) {
}{
"clean branch with encoding": {
branch: &Branch{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writes: []writeCall{
{ // stored encoding
Expand All @@ -32,7 +32,7 @@ func Test_Branch_Encode(t *testing.T) {
},
"write error for clean branch with encoding": {
branch: &Branch{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writes: []writeCall{
{ // stored encoding
Expand Down
8 changes: 4 additions & 4 deletions internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (b *Branch) Copy() Node {
Value: nil,
dirty: b.dirty,
hashDigest: make([]byte, len(b.hashDigest)),
Encoding: make([]byte, len(b.Encoding)),
encoding: make([]byte, len(b.encoding)),
generation: b.generation,
}
copy(cpy.Key, b.Key)
Expand All @@ -26,7 +26,7 @@ func (b *Branch) Copy() Node {
}

copy(cpy.hashDigest, b.hashDigest)
copy(cpy.Encoding, b.Encoding)
copy(cpy.encoding, b.encoding)
return cpy
}

Expand All @@ -43,12 +43,12 @@ func (l *Leaf) Copy() Node {
Value: make([]byte, len(l.Value)),
dirty: l.dirty,
hashDigest: make([]byte, len(l.hashDigest)),
Encoding: make([]byte, len(l.Encoding)),
encoding: make([]byte, len(l.encoding)),
generation: l.generation,
}
copy(cpy.Key, l.Key)
copy(cpy.Value, l.Value)
copy(cpy.hashDigest, l.hashDigest)
copy(cpy.Encoding, l.Encoding)
copy(cpy.encoding, l.encoding)
return cpy
}
12 changes: 6 additions & 6 deletions internal/trie/node/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// SetEncodingAndHash sets the encoding and hash slices
// given to the branch. Note it does not copy them, so beware.
func (b *Branch) SetEncodingAndHash(enc, hash []byte) {
b.Encoding = enc
b.encoding = enc
b.hashDigest = hash
}

Expand All @@ -30,8 +30,8 @@ func (b *Branch) GetHash() []byte {
// If the encoding is less than 32 bytes, the hash returned
// is the encoding and not the hash of the encoding.
func (b *Branch) EncodeAndHash() (encoding, hash []byte, err error) {
if !b.dirty && b.Encoding != nil && b.hashDigest != nil {
return b.Encoding, b.hashDigest, nil
if !b.dirty && b.encoding != nil && b.hashDigest != nil {
return b.encoding, b.hashDigest, nil
}

buffer := pools.EncodingBuffers.Get().(*bytes.Buffer)
Expand All @@ -45,9 +45,9 @@ func (b *Branch) EncodeAndHash() (encoding, hash []byte, err error) {

bufferBytes := buffer.Bytes()

b.Encoding = make([]byte, len(bufferBytes))
copy(b.Encoding, bufferBytes)
encoding = b.Encoding // no need to copy
b.encoding = make([]byte, len(bufferBytes))
copy(b.encoding, bufferBytes)
encoding = b.encoding // no need to copy

if buffer.Len() < 32 {
b.hashDigest = make([]byte, len(bufferBytes))
Expand Down
2 changes: 1 addition & 1 deletion internal/trie/node/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Leaf struct {
// from the node stored in the database.
dirty bool
hashDigest []byte
Encoding []byte
encoding []byte
encodingMu sync.RWMutex
// generation is incremented on every trie Snapshot() call.
// Nodes that are part of the trie are then gradually updated
Expand Down
20 changes: 10 additions & 10 deletions internal/trie/node/leaf_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// given to the branch. Note it does not copy them, so beware.
func (l *Leaf) SetEncodingAndHash(enc, hash []byte) {
l.encodingMu.Lock()
l.Encoding = enc
l.encoding = enc
l.encodingMu.Unlock()
l.hashDigest = hash
}
Expand All @@ -38,9 +38,9 @@ func (l *Leaf) GetHash() []byte {
// is the encoding and not the hash of the encoding.
func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
l.encodingMu.RLock()
if !l.IsDirty() && l.Encoding != nil && l.hashDigest != nil {
if !l.IsDirty() && l.encoding != nil && l.hashDigest != nil {
l.encodingMu.RUnlock()
return l.Encoding, l.hashDigest, nil
return l.encoding, l.hashDigest, nil
}
l.encodingMu.RUnlock()

Expand All @@ -58,10 +58,10 @@ func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
l.encodingMu.Lock()
// TODO remove this copying since it defeats the purpose of `buffer`
// and the sync.Pool.
l.Encoding = make([]byte, len(bufferBytes))
copy(l.Encoding, bufferBytes)
l.encoding = make([]byte, len(bufferBytes))
copy(l.encoding, bufferBytes)
l.encodingMu.Unlock()
encoding = l.Encoding // no need to copy
encoding = l.encoding // no need to copy

if len(bufferBytes) < 32 {
l.hashDigest = make([]byte, len(bufferBytes))
Expand All @@ -87,8 +87,8 @@ func (l *Leaf) EncodeAndHash() (encoding, hash []byte, err error) {
// NodeHeader | Extra partial key length | Partial Key | Value
func (l *Leaf) Encode(buffer Buffer) (err error) {
l.encodingMu.RLock()
if !l.dirty && l.Encoding != nil {
_, err = buffer.Write(l.Encoding)
if !l.dirty && l.encoding != nil {
_, err = buffer.Write(l.encoding)
l.encodingMu.RUnlock()
if err != nil {
return fmt.Errorf("cannot write stored encoding to buffer: %w", err)
Expand Down Expand Up @@ -122,8 +122,8 @@ func (l *Leaf) Encode(buffer Buffer) (err error) {
// and the sync.Pool.
l.encodingMu.Lock()
defer l.encodingMu.Unlock()
l.Encoding = make([]byte, buffer.Len())
copy(l.Encoding, buffer.Bytes())
l.encoding = make([]byte, buffer.Len())
copy(l.encoding, buffer.Bytes())
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions internal/trie/node/leaf_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Leaf_Encode(t *testing.T) {
}{
"clean leaf with encoding": {
leaf: &Leaf{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writes: []writeCall{
{
Expand All @@ -37,7 +37,7 @@ func Test_Leaf_Encode(t *testing.T) {
},
"write error for clean leaf with encoding": {
leaf: &Leaf{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writes: []writeCall{
{
Expand Down Expand Up @@ -153,7 +153,7 @@ func Test_Leaf_Encode(t *testing.T) {
} else {
require.NoError(t, err)
}
assert.Equal(t, testCase.expectedEncoding, testCase.leaf.Encoding)
assert.Equal(t, testCase.expectedEncoding, testCase.leaf.encoding)
})
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ func Test_Leaf_hash(t *testing.T) {
}{
"small leaf buffer write error": {
leaf: &Leaf{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writeCall: true,
write: writeCall{
Expand All @@ -217,7 +217,7 @@ func Test_Leaf_hash(t *testing.T) {
},
"small leaf success": {
leaf: &Leaf{
Encoding: []byte{1, 2, 3},
encoding: []byte{1, 2, 3},
},
writeCall: true,
write: writeCall{
Expand All @@ -226,7 +226,7 @@ func Test_Leaf_hash(t *testing.T) {
},
"leaf hash sum buffer write error": {
leaf: &Leaf{
Encoding: []byte{
encoding: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
Expand All @@ -250,7 +250,7 @@ func Test_Leaf_hash(t *testing.T) {
},
"leaf hash sum success": {
leaf: &Leaf{
Encoding: []byte{
encoding: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
Expand Down

0 comments on commit 23560c6

Please sign in to comment.