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

Implement stackTrie #1504

Merged
merged 16 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 12 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: 5 additions & 1 deletion storage/statedb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func (n rawNode) canUnload(uint16, uint16) bool { panic("this should never end u
func (n rawNode) cache() (hashNode, bool) { panic("this should never end up in a live trie") }
func (n rawNode) fstring(ind string) string { panic("this should never end up in a live trie") }
func (n rawNode) lenEncoded() uint16 { panic("this should never end up in a live trie") }
func (n rawNode) EncodeRLP(w io.Writer) error {
_, err := w.Write([]byte(n))
return err
}

// rawFullNode represents only the useful data content of a full node, with the
// caches and flags stripped out to minimize its data database. This type honors
Expand Down Expand Up @@ -232,7 +236,7 @@ func gatherChildren(n node, children *[]common.Hash) {
case hashNode:
*children = append(*children, common.BytesToHash(n))

case valueNode, nil:
case valueNode, nil, rawNode:

default:
panic(fmt.Sprintf("unknown node type: %T", n))
Expand Down
20 changes: 18 additions & 2 deletions storage/statedb/derive_sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,25 @@ import (
type DeriveShaOrig struct{}

func (d DeriveShaOrig) DeriveSha(list types.DerivableList) common.Hash {
trie := NewStackTrie(nil)
trie.Reset()
jeongkyun-oh marked this conversation as resolved.
Show resolved Hide resolved
keybuf := new(bytes.Buffer)
trie := new(Trie)
for i := 0; i < list.Len(); i++ {

// StackTrie requires values to be inserted in increasing
// hash order, which is not the order that `list` provides
// hashes in. This insertion sequence ensures that the
// order is correct.
for i := 1; i < list.Len() && i <= 0x7f; i++ {
keybuf.Reset()
rlp.Encode(keybuf, uint(i))
trie.Update(keybuf.Bytes(), list.GetRlp(i))
}
if list.Len() > 0 {
keybuf.Reset()
rlp.Encode(keybuf, uint(0))
trie.Update(keybuf.Bytes(), list.GetRlp(0))
}
for i := 0x80; i < list.Len(); i++ {
keybuf.Reset()
rlp.Encode(keybuf, uint(i))
trie.Update(keybuf.Bytes(), list.GetRlp(i))
Expand Down
29 changes: 29 additions & 0 deletions storage/statedb/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ func hexToCompact(hex []byte) []byte {
return buf
}

// hexToCompactInPlace places the compact key in input buffer, returning the length
// needed for the representation
func hexToCompactInPlace(hex []byte) int {
var (
hexLen = len(hex) // length of the hex input
firstByte = byte(0)
)
// Check if we have a terminator there
if hexLen > 0 && hex[hexLen-1] == 16 {
firstByte = 1 << 5
hexLen-- // last part was the terminator, ignore that
}
var (
binLen = hexLen/2 + 1
ni = 0 // index in hex
bi = 1 // index in bin (compact)
)
if hexLen&1 == 1 {
firstByte |= 1 << 4 // odd flag
firstByte |= hex[0] // first nibble is contained in the first byte
ni++
}
for ; ni < hexLen; bi, ni = bi+1, ni+2 {
hex[bi] = hex[ni]<<4 | hex[ni+1]
}
hex[0] = firstByte
return binLen
}

func compactToHex(compact []byte) []byte {
if len(compact) == 0 {
return compact
Expand Down