mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-09 05:23:47 +00:00
* induce zktrie * refactoring zktrie * fix crash issue in logger * renaming JSON field * unify hash scheme * goimport and mod lint * backward compatible with go 1.17 * lints * add option on genesis file * corrections according to the reviews * trivial fixes: ValueKey entry, key in prove nodes * fixing for the proof fix ... * avoiding panic before loading stateDb in genesis setup * revert ExtraData.StateList json annotation for compatibility * fix goimports lint * fix goimports lint * better encoding for leaf node * fix proof's printing issue, add handling on coinbase * update genesis, and rule out snapshot in zktrie mode * update readme and lint * fix an issue Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: HAOYUatHZ <haoyu@protonmail.com>
86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package trie
|
|
|
|
import (
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/scroll-tech/go-ethereum/ethdb"
|
|
)
|
|
|
|
// TODO: we should refactor codes, so ZktrieDatabase and Database become two implementation of a
|
|
// interface later, making codes less surprising..
|
|
// ZktrieDatabase Database adaptor
|
|
type ZktrieDatabase struct {
|
|
db *Database
|
|
prefix []byte
|
|
}
|
|
|
|
func NewZktrieDatabase(diskdb ethdb.KeyValueStore) *ZktrieDatabase {
|
|
return &ZktrieDatabase{db: NewDatabase(diskdb), prefix: []byte{}}
|
|
}
|
|
|
|
// adhoc wrapper...
|
|
func NewZktrieDatabaseFromTriedb(db *Database) *ZktrieDatabase {
|
|
db.Zktrie = true
|
|
return &ZktrieDatabase{db: db, prefix: []byte{}}
|
|
}
|
|
|
|
// Put saves a key:value into the Storage
|
|
func (l *ZktrieDatabase) Put(k, v []byte) error {
|
|
l.db.lock.Lock()
|
|
l.db.rawDirties.Put(Concat(l.prefix, k[:]), v)
|
|
l.db.lock.Unlock()
|
|
return nil
|
|
}
|
|
|
|
// Get retrieves a value from a key in the Storage
|
|
func (l *ZktrieDatabase) Get(key []byte) ([]byte, error) {
|
|
concatKey := Concat(l.prefix, key[:])
|
|
l.db.lock.RLock()
|
|
value, ok := l.db.rawDirties.Get(concatKey)
|
|
l.db.lock.RUnlock()
|
|
if ok {
|
|
return value, nil
|
|
}
|
|
v, err := l.db.diskdb.Get(concatKey)
|
|
if err == leveldb.ErrNotFound {
|
|
return nil, ErrNotFound
|
|
}
|
|
return v, err
|
|
}
|
|
|
|
// Iterate implements the method Iterate of the interface Storage
|
|
func (l *ZktrieDatabase) Iterate(f func([]byte, []byte) (bool, error)) error {
|
|
iter := l.db.diskdb.NewIterator(l.prefix, nil)
|
|
defer iter.Release()
|
|
for iter.Next() {
|
|
localKey := iter.Key()[len(l.prefix):]
|
|
if cont, err := f(localKey, iter.Value()); err != nil {
|
|
return err
|
|
} else if !cont {
|
|
break
|
|
}
|
|
}
|
|
iter.Release()
|
|
return iter.Error()
|
|
}
|
|
|
|
// Close implements the method Close of the interface Storage
|
|
func (l *ZktrieDatabase) Close() {
|
|
// FIXME: is this correct?
|
|
if err := l.db.diskdb.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// List implements the method List of the interface Storage
|
|
func (l *ZktrieDatabase) List(limit int) ([]KV, error) {
|
|
ret := []KV{}
|
|
err := l.Iterate(func(key []byte, value []byte) (bool, error) {
|
|
ret = append(ret, KV{K: Clone(key), V: Clone(value)})
|
|
if len(ret) == limit {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
})
|
|
return ret, err
|
|
}
|