Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
qianbin committed Nov 17, 2023
1 parent 16c5d34 commit ffdb2a1
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 424 deletions.
26 changes: 13 additions & 13 deletions muxdb/internal/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (t *Trie) makeDedupedNodeKey(dst []byte, seq sequence, path []byte) []byte
// newDatabase creates a database instance for low-level trie construction.
func (t *Trie) newDatabase() trie.Database {
var (
thisHash []byte
// thisHash []byte
thisSeq sequence
thisPath []byte
keyBuf []byte
Expand Down Expand Up @@ -136,17 +136,17 @@ func (t *Trie) newDatabase() trie.Database {
}
}

defer func() {
if err == nil && !t.ext.IsNonCrypto() {
// to ensure the node is correct, we need to verify the node hash.
// TODO: later can skip this step
if ok, err1 := trie.VerifyNodeHash(blob[len(dst):], thisHash); err1 != nil {
err = errors.Wrap(err1, "verify node hash")
} else if !ok {
err = errors.New("node hash checksum error")
}
}
}()
// defer func() {
// if err == nil && !t.ext.IsNonCrypto() {
// // to ensure the node is correct, we need to verify the node hash.
// // TODO: later can skip this step
// if ok, err1 := trie.VerifyNodeHash(blob[len(dst):], thisHash); err1 != nil {
// err = errors.Wrap(err1, "verify node hash")
// } else if !ok {
// err = errors.New("node hash checksum error")
// }
// }
// }()

// query in db
snapshot := t.back.Store.Snapshot()
Expand All @@ -173,7 +173,7 @@ func (t *Trie) newDatabase() trie.Database {
}
}),
databaseKeyEncodeFunc(func(hash []byte, seq uint64, path []byte) []byte {
thisHash = hash
// thisHash = hash
thisSeq = sequence(seq)
thisPath = path
return nil
Expand Down
14 changes: 7 additions & 7 deletions muxdb/muxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,17 @@ func Open(path string, options *Options) (*MuxDB, error) {
options.TrieNodeCacheSizeMB,
options.TrieRootCacheCapacity)

trieLeafBank := trie.NewLeafBank(
engine,
trieLeafBankSpace,
options.TrieLeafBankSlotCapacity)
// trieLeafBank := trie.NewLeafBank(
// engine,
// trieLeafBankSpace,
// options.TrieLeafBankSlotCapacity)

return &MuxDB{
engine: engine,
trieBackend: &trie.Backend{
Store: engine,
Cache: trieCache,
LeafBank: trieLeafBank,
Store: engine,
Cache: trieCache,
// LeafBank: trieLeafBank,
HistSpace: trieHistSpace,
DedupedSpace: trieDedupedSpace,
HistPtnFactor: cfg.HistPtnFactor,
Expand Down
13 changes: 11 additions & 2 deletions trie/extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ func (n Node) Hash() (hash thor.Bytes32) {

// SeqNum returns the node's sequence number. 0 is returned if the node is dirty.
func (n Node) SeqNum() uint64 {
if n.node != nil {
return n.node.seqNum()
switch n := n.node.(type) {
case *shortNode:
if h := n.flags.hash; h != nil {
return h.seq
}
case *fullNode:
if h := n.flags.hash; h != nil {
return h.seq
}
case *hashNode:
return n.seq
}
return 0
}
Expand Down
125 changes: 108 additions & 17 deletions trie/fast_node_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,150 @@ import (

func (n *fullNode) encode(e *lowrlp.Encoder, nonCrypto bool) {
off := e.List()
for _, c := range n.Children {
for i, c := range n.Children {
if c != nil {
c.encode(e, nonCrypto)
if h, dirty, _ := c.cache(); h != nil && !dirty {
h.encode(e, nonCrypto)
} else {
c.encode(e, nonCrypto)
}
} else {
e.EncodeEmptyString()
if i == 16 {
e.EncodeEmptyList()
} else {
e.EncodeEmptyString()
}
}
}
e.ListEnd(off)
}

func (n *fullNode) encodeTrailing(e *lowrlp.Encoder) {
func (n *fullNode) encodeConsensus(e *lowrlp.Encoder) {
off := e.List()
for _, c := range n.Children {
if c != nil {
c.encodeTrailing(e)
if h, _, _ := c.cache(); h != nil {
h.encodeConsensus(e)
} else {
c.encodeConsensus(e)
}
} else {
e.EncodeEmptyString()
}
}
e.ListEnd(off)
}

func (n *shortNode) encode(e *lowrlp.Encoder, nonCrypto bool) {
off := e.List()
e.EncodeString(n.Key)
e.EncodeString(hexToCompact(n.Key))
if n.Val != nil {
n.Val.encode(e, nonCrypto)
if h, dirty, _ := n.Val.cache(); h != nil && !dirty {
h.encode(e, nonCrypto)
} else {
n.Val.encode(e, nonCrypto)
}
} else {
e.EncodeEmptyString()
e.EncodeEmptyList()
}
e.ListEnd(off)
}

func (n *shortNode) encodeTrailing(e *lowrlp.Encoder) {
func (n *shortNode) encodeConsensus(e *lowrlp.Encoder) {
off := e.List()
e.EncodeString(hexToCompact(n.Key))
if n.Val != nil {
n.Val.encodeTrailing(e)
if h, _, _ := n.Val.cache(); h != nil {
h.encodeConsensus(e)
} else {
n.Val.encodeConsensus(e)
}
} else {
e.EncodeEmptyString()
}
e.ListEnd(off)
}

func (n *hashNode) encode(e *lowrlp.Encoder, nonCrypto bool) {
if nonCrypto {
e.EncodeString(nonCryptoNodeHashPlaceholder)
e.EncodeUint(n.seq)
} else {
e.EncodeString(n.Hash[:])
var tmp [hashLen + 8]byte
copy(tmp[:], n.Hash[:])
size := putint(tmp[hashLen:], n.seq)
e.EncodeString(tmp[:hashLen+size])
}
}

func (n *hashNode) encodeTrailing(e *lowrlp.Encoder) {
e.EncodeUint(n.seq)
func (n *hashNode) encodeConsensus(e *lowrlp.Encoder) {
e.EncodeString(n.Hash[:])
}

func (n *valueNode) encode(e *lowrlp.Encoder, nonCrypto bool) {
off := e.List()
e.EncodeString(n.Value)
e.EncodeString(n.meta)
e.ListEnd(off)
}

func (n *valueNode) encodeConsensus(e *lowrlp.Encoder) {

e.EncodeString(n.Value)
}

func (n *valueNode) encodeTrailing(e *lowrlp.Encoder) {
if len(n.Value) > 0 {
e.EncodeString(n.meta)
func putint(b []byte, i uint64) (size int) {
switch {
case i < (1 << 8):
b[0] = byte(i)
return 1
case i < (1 << 16):
b[0] = byte(i >> 8)
b[1] = byte(i)
return 2
case i < (1 << 24):
b[0] = byte(i >> 16)
b[1] = byte(i >> 8)
b[2] = byte(i)
return 3
case i < (1 << 32):
b[0] = byte(i >> 24)
b[1] = byte(i >> 16)
b[2] = byte(i >> 8)
b[3] = byte(i)
return 4
case i < (1 << 40):
b[0] = byte(i >> 32)
b[1] = byte(i >> 24)
b[2] = byte(i >> 16)
b[3] = byte(i >> 8)
b[4] = byte(i)
return 5
case i < (1 << 48):
b[0] = byte(i >> 40)
b[1] = byte(i >> 32)
b[2] = byte(i >> 24)
b[3] = byte(i >> 16)
b[4] = byte(i >> 8)
b[5] = byte(i)
return 6
case i < (1 << 56):
b[0] = byte(i >> 48)
b[1] = byte(i >> 40)
b[2] = byte(i >> 32)
b[3] = byte(i >> 24)
b[4] = byte(i >> 16)
b[5] = byte(i >> 8)
b[6] = byte(i)
return 7
default:
b[0] = byte(i >> 56)
b[1] = byte(i >> 48)
b[2] = byte(i >> 40)
b[3] = byte(i >> 32)
b[4] = byte(i >> 24)
b[5] = byte(i >> 16)
b[6] = byte(i >> 8)
b[7] = byte(i)
return 8
}
}
Loading

0 comments on commit ffdb2a1

Please sign in to comment.