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

Faster trie node encoding updated #1974

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions storage/statedb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,7 @@ func (n *cachedNode) rlp() []byte {
if node, ok := n.node.(rawNode); ok {
return node
}
blob, err := rlp.EncodeToBytes(n.node)
if err != nil {
panic(err)
}
return blob
return nodeToBytes(n.node)
}

// obj returns the decoded and expanded trie node, either directly from the cache,
Expand Down
36 changes: 21 additions & 15 deletions storage/statedb/hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type hasherOpts struct {

type hasher struct {
hasherOpts
tmp sliceBuffer
sha KeccakState
tmp sliceBuffer
sha KeccakState
encbuf rlp.EncoderBuffer
}

// KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports
Expand All @@ -65,8 +66,9 @@ func (b *sliceBuffer) Reset() {
var hasherPool = sync.Pool{
New: func() interface{} {
return &hasher{
tmp: make(sliceBuffer, 0, 550), // cap is as large as a full fullNode.
sha: sha3.NewKeccak256().(KeccakState),
tmp: make(sliceBuffer, 0, 550), // cap is as large as a full fullNode.
sha: sha3.NewKeccak256().(KeccakState),
encbuf: rlp.NewEncoderBuffer(nil),
}
},
}
Expand Down Expand Up @@ -206,11 +208,9 @@ func (h *hasher) store(n node, db *Database, force bool, onRoot bool) (node, uin
// Calculate lenEncoded if not set
if hash == nil || lenEncoded == 0 {
// Generate the RLP encoding of the node for database storing
h.tmp.Reset()
if err := rlp.Encode(&h.tmp, h.nodeForStoring(n)); err != nil {
panic("encode error: " + err.Error())
}
lenEncoded = uint16(len(h.tmp))
h.nodeForStoring(n).encode(h.encbuf)
enc := h.encodedBytes()
lenEncoded = uint16(len(enc))
}
if lenEncoded < 32 && !force {
return n, lenEncoded // Nodes smaller than 32 bytes are stored inside their parent
Expand All @@ -219,11 +219,9 @@ func (h *hasher) store(n node, db *Database, force bool, onRoot bool) (node, uin
// Calculate hash if not set
if hash == nil {
// Generate the RLP encoding of the node for Merkle hashing
h.tmp.Reset()
if err := rlp.Encode(&h.tmp, h.nodeForHashing(n)); err != nil {
panic("encode error: " + err.Error())
}
hash = h.makeHashNode(h.tmp, onRoot)
h.nodeForHashing(n).encode(h.encbuf)
enc := h.encodedBytes()
hash = h.hashData(enc, onRoot)
}

if db != nil {
Expand Down Expand Up @@ -253,7 +251,7 @@ func (h *hasher) store(n node, db *Database, force bool, onRoot bool) (node, uin
return hash, lenEncoded
}

func (h *hasher) makeHashNode(data []byte, onRoot bool) hashNode {
func (h *hasher) hashData(data []byte, onRoot bool) hashNode {
var hash common.Hash
h.sha.Reset()
h.sha.Write(data)
Expand All @@ -265,6 +263,14 @@ func (h *hasher) makeHashNode(data []byte, onRoot bool) hashNode {
}
}

// encodedBytes returns the result of the last encoding operation on h.encbuf.
// This also resets the encoder buffer.
func (h *hasher) encodedBytes() []byte {
h.tmp = h.encbuf.AppendToBytes(h.tmp[:0])
h.encbuf.Reset(nil)
return h.tmp
}

func (h *hasher) nodeForHashing(original node) node {
return unextendNode(original, false)
}
Expand Down
14 changes: 4 additions & 10 deletions storage/statedb/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type node interface {
fstring(string) string
cache() (hashNode, bool)
lenEncoded() uint16
encode(w rlp.EncoderBuffer)
}

type (
Expand All @@ -57,16 +58,9 @@ var nilValueNode = valueNode(nil)

// EncodeRLP encodes a full node into the consensus RLP format.
func (n *fullNode) EncodeRLP(w io.Writer) error {
var nodes [17]node

for i, child := range &n.Children {
if child != nil {
nodes[i] = child
} else {
nodes[i] = nilValueNode
}
}
return rlp.Encode(w, nodes)
eb := rlp.NewEncoderBuffer(w)
n.encode(eb)
return eb.Flush()
}

func (n *fullNode) copy() *fullNode { copy := *n; return &copy }
Expand Down
91 changes: 91 additions & 0 deletions storage/statedb/node_enc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Modifications Copyright 2023 The klaytn Authors
// Copyright 2022 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from trie/node_enc.go (2023/09/19).
// Modified and improved for the klaytn development.

package statedb

import (
"github.com/klaytn/klaytn/rlp"
)

func nodeToBytes(n node) []byte {
w := rlp.NewEncoderBuffer(nil)
n.encode(w)
result := w.ToBytes()
w.Flush()
return result
}

func (n *fullNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
for _, c := range n.Children {
if c != nil {
c.encode(w)
} else {
w.Write(rlp.EmptyString)
}
}
w.ListEnd(offset)
}

func (n *shortNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
w.WriteBytes(n.Key)
if n.Val != nil {
n.Val.encode(w)
} else {
w.Write(rlp.EmptyString)
}
w.ListEnd(offset)
}

func (n hashNode) encode(w rlp.EncoderBuffer) {
w.WriteBytes(n)
}

func (n valueNode) encode(w rlp.EncoderBuffer) {
w.WriteBytes(n)
}

func (n rawFullNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
for _, c := range n {
if c != nil {
c.encode(w)
} else {
w.Write(rlp.EmptyString)
}
}
w.ListEnd(offset)
}

func (n *rawShortNode) encode(w rlp.EncoderBuffer) {
offset := w.List()
w.WriteBytes(n.Key)
if n.Val != nil {
n.Val.encode(w)
} else {
w.Write(rlp.EmptyString)
}
w.ListEnd(offset)
}

func (n rawNode) encode(w rlp.EncoderBuffer) {
w.Write(n)
}
2 changes: 1 addition & 1 deletion storage/statedb/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDB ProofDBWriter) error {
// hash is for the merkle proof. hash = Keccak(rlp.Encode(nodeForHashing(n)))
enc, _ := rlp.EncodeToBytes(hasher.nodeForHashing(n))
if !ok {
hash = hasher.makeHashNode(enc, false)
hash = hasher.hashData(enc, false)
}
dbKey := database.TrieNodeKey(common.BytesToExtHash(hash))
proofDB.WriteMerkleProof(dbKey, enc)
Expand Down