From e90141a3baa029819161949272adf190d64bd4cd Mon Sep 17 00:00:00 2001 From: Nguyen Ba Tam Date: Wed, 30 Jan 2019 15:45:46 +0700 Subject: [PATCH] clean all state of block signer --- cmd/tomoclean/main.go | 273 ++++++++++++++++++++++++++++++++++++++++++ cmd/utils/flags.go | 8 +- trie/hasher.go | 54 ++++----- trie/iterator.go | 22 ++-- trie/node.go | 78 ++++++------ trie/proof.go | 24 ++-- trie/sync.go | 12 +- trie/trie.go | 76 ++++++------ trie/trie_test.go | 10 +- 9 files changed, 415 insertions(+), 142 deletions(-) create mode 100644 cmd/tomoclean/main.go diff --git a/cmd/tomoclean/main.go b/cmd/tomoclean/main.go new file mode 100644 index 0000000000..991339ee66 --- /dev/null +++ b/cmd/tomoclean/main.go @@ -0,0 +1,273 @@ +package main + +import ( + "flag" + "fmt" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/state" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + "github.com/hashicorp/golang-lru" + "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/util" + "os" + "os/signal" + "runtime" + "sync" + "sync/atomic" + "time" +) + +var ( + dir = flag.String("dir", "", "dir to mainet chain data") + cacheSize = flag.Int("size", 1000000, "dir to mainet chain data") + file = flag.String("file", "", "dir to mainet chain data") +) + +type StateNode struct { + node trie.Node + path []byte +} +type ResultProcessNode struct { + index int + number int + newNodes [17]*StateNode + keys [17]*[]byte +} + +var sercureKey = []byte("secure-key-") +var nWorker = runtime.NumCPU() / 2 +var cleanAddress = []common.Address{common.HexToAddress(common.BlockSigners)} +var cache *lru.Cache +var finish = int32(0) +var running = true +var stateRoots = make(chan *trie.SecureTrie) + +func main() { + flag.Parse() + lddb, _ := ethdb.NewLDBDatabase(*dir, eth.DefaultConfig.DatabaseCache, utils.MakeDatabaseHandles()) + head := core.GetHeadBlockHash(lddb) + currentHeader := core.GetHeader(lddb, head, core.GetBlockNumber(lddb, head)) + tridb := trie.NewDatabase(lddb) + catchEventInterupt(lddb.LDB()) + cache, _ = lru.New(*cacheSize) + go func() { + for i := uint64(1); i <= currentHeader.Number.Uint64(); i++ { + hash := core.GetCanonicalHash(lddb, i) + root := core.GetHeader(lddb, hash, i).Root + trieRoot, err := trie.NewSecure(root, tridb, 0) + if err != nil { + continue + } + fmt.Println(time.Now().Format(time.RFC3339), "Found a trie state root at block ", i, "state root ", root.Hex()) + if running { + stateRoots <- trieRoot + }else { + break + } + } + if running { + close(stateRoots) + } + }() + for trieRoot := range stateRoots { + atomic.StoreInt32(&finish, 1) + if running { + for _, address := range cleanAddress { + enc := trieRoot.Get(address.Bytes()) + var data state.Account + rlp.DecodeBytes(enc, &data) + fmt.Println(time.Now().Format(time.RFC3339), "Start clean state address ", address.Hex(), " at state root ", common.Bytes2Hex(trieRoot.Root()), "state address root", data.Root.Hex()) + signerRoot, err := resolveHash(data.Root[:], lddb.LDB()) + if err != nil { + fmt.Println(time.Now().Format(time.RFC3339), "Not found clean state address ", address.Hex(), " at state root ", common.Bytes2Hex(trieRoot.Root()), "state address root", data.Root.Hex()) + continue + } + batch := new(leveldb.Batch) + list := []*StateNode{&StateNode{node: signerRoot}} + for len(list) > 0 { + newList, total := findNewNodes(list, lddb.LDB(), batch) + list = removeNodesNil(newList, total) + } + fmt.Println(time.Now().Format(time.RFC3339), "Finish clean state address ", address.Hex(), " at state root ", common.Bytes2Hex(trieRoot.Root()), "state address root", data.Root.Hex()) + err = lddb.LDB().Write(batch, nil) + if err != nil { + fmt.Println(time.Now().Format(time.RFC3339), "Write batch leveldb error", err) + os.Exit(1) + } + } + }else { + break + } + atomic.StoreInt32(&finish, 0) + } + fmt.Println(time.Now(), "compact") + lddb.LDB().CompactRange(util.Range{}) + lddb.Close() + fmt.Println(time.Now(), "end") +} + +func removeNodesNil(list [][17]*StateNode, length int) []*StateNode { + results := make([]*StateNode, length) + index := 0 + for _, nodes := range list { + for _, node := range nodes { + if node != nil { + results[index] = node + index++ + } + } + } + return results +} +func catchEventInterupt(db *leveldb.DB) { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for sig := range c { + fmt.Println("catch event interrupt ", sig, running, finish) + running = false + if atomic.LoadInt32(&finish) == 0 { + close(stateRoots) + fmt.Println(time.Now(), "interrupt compact") + db.CompactRange(util.Range{}) + db.Close() + fmt.Println(time.Now(), "interrupt end") + os.Exit(1) + } + } + }() +} +func resolveHash(n trie.HashNode, db *leveldb.DB) (trie.Node, error) { + if cache.Contains(common.BytesToHash(n)) { + return nil, &trie.MissingNodeError{} + } + enc, err := db.Get(n, nil) + if err != nil || enc == nil { + return nil, &trie.MissingNodeError{} + } + return trie.MustDecodeNode(n, enc, 0), nil +} + +func getAllChilds(n StateNode, db *leveldb.DB) ([17]*StateNode, error) { + childs := [17]*StateNode{} + switch node := n.node.(type) { + case *trie.FullNode: + // Full Node, move to the first non-nil child. + for i := 0; i < len(node.Children); i++ { + child := node.Children[i] + if child != nil { + childNode := child + var err error = nil + if _, ok := child.(trie.HashNode); ok { + childNode, err = resolveHash(child.(trie.HashNode), db) + } + if err == nil { + childs[i] = &StateNode{node: childNode, path: append(n.path, byte(i))} + } else if err != nil { + _, ok := err.(*trie.MissingNodeError); + if !ok { + return childs, err + } + } + } + } + case *trie.ShortNode: + // Short Node, return the pointer singleton child + childNode := node.Val + var err error = nil + if _, ok := node.Val.(trie.HashNode); ok { + childNode, err = resolveHash(node.Val.(trie.HashNode), db) + } + if err == nil { + childs[0] = &StateNode{node: childNode, path: append(n.path, node.Key...)} + } else if err != nil { + _, ok := err.(*trie.MissingNodeError); + if !ok { + return childs, err + } + } + } + return childs, nil +} +func processNodes(node StateNode, db *leveldb.DB) ([17]*StateNode, [17]*[]byte, int) { + hash, _ := node.node.Cache() + commonHash := common.BytesToHash(hash) + newNodes := [17]*StateNode{} + keys := [17]*[]byte{} + number := 0 + if !cache.Contains(commonHash) { + childNodes, err := getAllChilds(node, db) + if err != nil { + fmt.Println("Error when get all childs node : ", common.Bytes2Hex(node.path), err) + os.Exit(1) + } + for i, child := range childNodes { + if child != nil { + if _, ok := child.node.(trie.ValueNode); ok { + buf := append(sercureKey, child.path...) + keys[i] = &buf + } else { + hash, _ := child.node.Cache() + var bytes []byte = hash + keys[i] = &bytes + newNodes[i] = child + number++ + } + } + } + cache.Add(commonHash, true) + } + return newNodes, keys, number +} + +func findNewNodes(nodes []*StateNode, db *leveldb.DB, batchlvdb *leveldb.Batch) ([][17]*StateNode, int) { + length := len(nodes) + chunkSize := length / nWorker + if len(nodes)%nWorker != 0 { + chunkSize++ + } + childNodes := make([][17]*StateNode, length) + results := make(chan ResultProcessNode) + wg := sync.WaitGroup{} + wgResults := sync.WaitGroup{} + wg.Add(nWorker) + for i := 0; i < nWorker; i++ { + from := i * chunkSize + to := from + chunkSize + if to > length { + to = length + } + go func(from int, to int) { + for j := from; j < to; j++ { + childs, keys, number := processNodes(*nodes[j], db) + wgResults.Add(1) + go func(result ResultProcessNode) { + results <- result + }(ResultProcessNode{j, number, childs, keys}) + } + wg.Done() + }(from, to) + } + wg.Wait() + total := 0 + go func() { + for result := range results { + childNodes[result.index] = result.newNodes + total = total + result.number + for _, key := range result.keys { + if key != nil { + batchlvdb.Delete(*key) + } + } + wgResults.Done() + } + }() + wgResults.Wait() + close(results) + return childNodes, total +} diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index cdebc9a99f..1b08145659 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -747,9 +747,9 @@ func setIPC(ctx *cli.Context, cfg *node.Config) { } } -// makeDatabaseHandles raises out the number of allowed file handles per process +// MakeDatabaseHandles raises out the number of allowed file handles per process // for tomo and returns half of the allowance to assign to the database. -func makeDatabaseHandles() int { +func MakeDatabaseHandles() int { limit, err := fdlimit.Current() if err != nil { Fatalf("Failed to retrieve file descriptor allowance: %v", err) @@ -1066,7 +1066,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) { cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100 } - cfg.DatabaseHandles = makeDatabaseHandles() + cfg.DatabaseHandles = MakeDatabaseHandles() if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" { Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name) @@ -1212,7 +1212,7 @@ func SetupNetwork(ctx *cli.Context) { func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database { var ( cache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100 - handles = makeDatabaseHandles() + handles = MakeDatabaseHandles() ) name := "chaindata" if ctx.GlobalBool(LightModeFlag.Name) { diff --git a/trie/hasher.go b/trie/hasher.go index 2fc44787ac..cc0f006441 100644 --- a/trie/hasher.go +++ b/trie/hasher.go @@ -53,9 +53,9 @@ func returnHasherToPool(h *hasher) { // hash collapses a node down into a hash node, also returning a copy of the // original node initialized with the computed hash to replace the original one. -func (h *hasher) hash(n node, db *Database, force bool) (node, node, error) { +func (h *hasher) hash(n Node, db *Database, force bool) (Node, Node, error) { // If we're not storing the node, just hashing, use available cached data - if hash, dirty := n.cache(); hash != nil { + if hash, dirty := n.Cache(); hash != nil { if db == nil { return hash, n, nil } @@ -72,23 +72,23 @@ func (h *hasher) hash(n node, db *Database, force bool) (node, node, error) { // Trie not processed yet or needs storage, walk the children collapsed, cached, err := h.hashChildren(n, db) if err != nil { - return hashNode{}, n, err + return HashNode{}, n, err } hashed, err := h.store(collapsed, db, force) if err != nil { - return hashNode{}, n, err + return HashNode{}, n, err } // Cache the hash of the node for later reuse and remove // the dirty flag in commit mode. It's fine to assign these values directly // without copying the node first because hashChildren copies it. - cachedHash, _ := hashed.(hashNode) + cachedHash, _ := hashed.(HashNode) switch cn := cached.(type) { - case *shortNode: + case *ShortNode: cn.flags.hash = cachedHash if db != nil { cn.flags.dirty = false } - case *fullNode: + case *FullNode: cn.flags.hash = cachedHash if db != nil { cn.flags.dirty = false @@ -100,28 +100,28 @@ func (h *hasher) hash(n node, db *Database, force bool) (node, node, error) { // hashChildren replaces the children of a node with their hashes if the encoded // size of the child is larger than a hash, returning the collapsed node as well // as a replacement for the original node with the child hashes cached in. -func (h *hasher) hashChildren(original node, db *Database) (node, node, error) { +func (h *hasher) hashChildren(original Node, db *Database) (Node, Node, error) { var err error switch n := original.(type) { - case *shortNode: - // Hash the short node's child, caching the newly hashed subtree + case *ShortNode: + // Hash the short Node's child, caching the newly hashed subtree collapsed, cached := n.copy(), n.copy() collapsed.Key = hexToCompact(n.Key) cached.Key = common.CopyBytes(n.Key) - if _, ok := n.Val.(valueNode); !ok { + if _, ok := n.Val.(ValueNode); !ok { collapsed.Val, cached.Val, err = h.hash(n.Val, db, false) if err != nil { return original, original, err } } if collapsed.Val == nil { - collapsed.Val = valueNode(nil) // Ensure that nil children are encoded as empty strings. + collapsed.Val = ValueNode(nil) // Ensure that nil children are encoded as empty strings. } return collapsed, cached, nil - case *fullNode: + case *FullNode: // Hash the full node's children, caching the newly hashed subtrees collapsed, cached := n.copy(), n.copy() @@ -132,12 +132,12 @@ func (h *hasher) hashChildren(original node, db *Database) (node, node, error) { return original, original, err } } else { - collapsed.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings. + collapsed.Children[i] = ValueNode(nil) // Ensure that nil children are encoded as empty strings. } } cached.Children[16] = n.Children[16] if collapsed.Children[16] == nil { - collapsed.Children[16] = valueNode(nil) + collapsed.Children[16] = ValueNode(nil) } return collapsed, cached, nil @@ -150,9 +150,9 @@ func (h *hasher) hashChildren(original node, db *Database) (node, node, error) { // store hashes the node n and if we have a storage layer specified, it writes // the key/value pair to it and tracks any node->child references as well as any // node->external trie references. -func (h *hasher) store(n node, db *Database, force bool) (node, error) { +func (h *hasher) store(n Node, db *Database, force bool) (Node, error) { // Don't store hashes or empty nodes. - if _, isHash := n.(hashNode); n == nil || isHash { + if _, isHash := n.(HashNode); n == nil || isHash { return n, nil } // Generate the RLP encoding of the node @@ -164,11 +164,11 @@ func (h *hasher) store(n node, db *Database, force bool) (node, error) { return n, nil // Nodes smaller than 32 bytes are stored inside their parent } // Larger nodes are replaced by their hash and stored in the database. - hash, _ := n.cache() + hash, _ := n.Cache() if hash == nil { h.sha.Reset() h.sha.Write(h.tmp.Bytes()) - hash = hashNode(h.sha.Sum(nil)) + hash = HashNode(h.sha.Sum(nil)) } if db != nil { // We are pooling the trie nodes into an intermediate memory cache @@ -179,13 +179,13 @@ func (h *hasher) store(n node, db *Database, force bool) (node, error) { // Track all direct parent->child node references switch n := n.(type) { - case *shortNode: - if child, ok := n.Val.(hashNode); ok { + case *ShortNode: + if child, ok := n.Val.(HashNode); ok { db.reference(common.BytesToHash(child), hash) } - case *fullNode: + case *FullNode: for i := 0; i < 16; i++ { - if child, ok := n.Children[i].(hashNode); ok { + if child, ok := n.Children[i].(HashNode); ok { db.reference(common.BytesToHash(child), hash) } } @@ -195,13 +195,13 @@ func (h *hasher) store(n node, db *Database, force bool) (node, error) { // Track external references from account->storage trie if h.onleaf != nil { switch n := n.(type) { - case *shortNode: - if child, ok := n.Val.(valueNode); ok { + case *ShortNode: + if child, ok := n.Val.(ValueNode); ok { h.onleaf(child, hash) } - case *fullNode: + case *FullNode: for i := 0; i < 16; i++ { - if child, ok := n.Children[i].(valueNode); ok { + if child, ok := n.Children[i].(ValueNode); ok { h.onleaf(child, hash) } } diff --git a/trie/iterator.go b/trie/iterator.go index 76146c0d64..e30504b151 100644 --- a/trie/iterator.go +++ b/trie/iterator.go @@ -20,7 +20,6 @@ import ( "bytes" "container/heap" "errors" - "github.com/ethereum/go-ethereum/common" ) @@ -60,6 +59,7 @@ type NodeIterator interface { // Next moves the iterator to the next node. If the parameter is false, any child // nodes will be skipped. Next(bool) bool + // Error returns the error status of the iterator. Error() error @@ -86,7 +86,7 @@ type NodeIterator interface { // trie, which can be resumed at a later invocation. type nodeIteratorState struct { hash common.Hash // Hash of the node being iterated (nil if not standalone) - node node // Trie node being iterated + node Node // Trie node being iterated parent common.Hash // Hash of the first full ancestor node (nil if current is the root) index int // Child to be processed next pathlen int // Length of the path to this node @@ -112,7 +112,7 @@ func (e seekError) Error() string { return "seek error: " + e.err.Error() } -func newNodeIterator(trie *Trie, start []byte) NodeIterator { +func NewNodeIterator(trie *Trie, start []byte) NodeIterator { if trie.Hash() == emptyState { return new(nodeIterator) } @@ -141,7 +141,7 @@ func (it *nodeIterator) Leaf() bool { func (it *nodeIterator) LeafBlob() []byte { if len(it.stack) > 0 { - if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { + if node, ok := it.stack[len(it.stack)-1].node.(ValueNode); ok { return []byte(node) } } @@ -150,7 +150,7 @@ func (it *nodeIterator) LeafBlob() []byte { func (it *nodeIterator) LeafKey() []byte { if len(it.stack) > 0 { - if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { + if _, ok := it.stack[len(it.stack)-1].node.(ValueNode); ok { return hexToKeybytes(it.path) } } @@ -250,7 +250,7 @@ func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, er } func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error { - if hash, ok := st.node.(hashNode); ok { + if hash, ok := st.node.(HashNode); ok { resolved, err := tr.resolveHash(hash, path) if err != nil { return err @@ -263,12 +263,12 @@ func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error { func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) { switch node := parent.node.(type) { - case *fullNode: - // Full node, move to the first non-nil child. + case *FullNode: + // Full Node, move to the first non-nil child. for i := parent.index + 1; i < len(node.Children); i++ { child := node.Children[i] if child != nil { - hash, _ := child.cache() + hash, _ := child.Cache() state := &nodeIteratorState{ hash: common.BytesToHash(hash), node: child, @@ -281,10 +281,10 @@ func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Has return state, path, true } } - case *shortNode: + case *ShortNode: // Short node, return the pointer singleton child if parent.index < 0 { - hash, _ := node.Val.cache() + hash, _ := node.Val.Cache() state := &nodeIteratorState{ hash: common.BytesToHash(hash), node: node.Val, diff --git a/trie/node.go b/trie/node.go index a7697fc0c6..4b76f2b559 100644 --- a/trie/node.go +++ b/trie/node.go @@ -27,63 +27,63 @@ import ( var indices = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "[17]"} -type node interface { +type Node interface { fstring(string) string - cache() (hashNode, bool) + Cache() (HashNode, bool) canUnload(cachegen, cachelimit uint16) bool } type ( - fullNode struct { - Children [17]node // Actual trie node data to encode/decode (needs custom encoder) + FullNode struct { + Children [17]Node // Actual trie node data to encode/decode (needs custom encoder) flags nodeFlag } - shortNode struct { + ShortNode struct { Key []byte - Val node + Val Node flags nodeFlag } - hashNode []byte - valueNode []byte + HashNode []byte + ValueNode []byte ) // EncodeRLP encodes a full node into the consensus RLP format. -func (n *fullNode) EncodeRLP(w io.Writer) error { +func (n *FullNode) EncodeRLP(w io.Writer) error { return rlp.Encode(w, n.Children) } -func (n *fullNode) copy() *fullNode { copy := *n; return © } -func (n *shortNode) copy() *shortNode { copy := *n; return © } +func (n *FullNode) copy() *FullNode { copy := *n; return © } +func (n *ShortNode) copy() *ShortNode { copy := *n; return © } // nodeFlag contains caching-related metadata about a node. type nodeFlag struct { - hash hashNode // cached hash of the node (may be nil) + hash HashNode // cached hash of the node (may be nil) gen uint16 // cache generation counter dirty bool // whether the node has changes that must be written to the database } -// canUnload tells whether a node can be unloaded. +// canUnload tells whether a Node can be unloaded. func (n *nodeFlag) canUnload(cachegen, cachelimit uint16) bool { return !n.dirty && cachegen-n.gen >= cachelimit } -func (n *fullNode) canUnload(gen, limit uint16) bool { return n.flags.canUnload(gen, limit) } -func (n *shortNode) canUnload(gen, limit uint16) bool { return n.flags.canUnload(gen, limit) } -func (n hashNode) canUnload(uint16, uint16) bool { return false } -func (n valueNode) canUnload(uint16, uint16) bool { return false } +func (n *FullNode) canUnload(gen, limit uint16) bool { return n.flags.canUnload(gen, limit) } +func (n *ShortNode) canUnload(gen, limit uint16) bool { return n.flags.canUnload(gen, limit) } +func (n HashNode) canUnload(uint16, uint16) bool { return false } +func (n ValueNode) canUnload(uint16, uint16) bool { return false } -func (n *fullNode) cache() (hashNode, bool) { return n.flags.hash, n.flags.dirty } -func (n *shortNode) cache() (hashNode, bool) { return n.flags.hash, n.flags.dirty } -func (n hashNode) cache() (hashNode, bool) { return nil, true } -func (n valueNode) cache() (hashNode, bool) { return nil, true } +func (n *FullNode) Cache() (HashNode, bool) { return n.flags.hash, n.flags.dirty } +func (n *ShortNode) Cache() (HashNode, bool) { return n.flags.hash, n.flags.dirty } +func (n HashNode) Cache() (HashNode, bool) { return nil, true } +func (n ValueNode) Cache() (HashNode, bool) { return nil, true } // Pretty printing. -func (n *fullNode) String() string { return n.fstring("") } -func (n *shortNode) String() string { return n.fstring("") } -func (n hashNode) String() string { return n.fstring("") } -func (n valueNode) String() string { return n.fstring("") } +func (n *FullNode) String() string { return n.fstring("") } +func (n *ShortNode) String() string { return n.fstring("") } +func (n HashNode) String() string { return n.fstring("") } +func (n ValueNode) String() string { return n.fstring("") } -func (n *fullNode) fstring(ind string) string { +func (n *FullNode) fstring(ind string) string { resp := fmt.Sprintf("[\n%s ", ind) for i, node := range n.Children { if node == nil { @@ -94,17 +94,17 @@ func (n *fullNode) fstring(ind string) string { } return resp + fmt.Sprintf("\n%s] ", ind) } -func (n *shortNode) fstring(ind string) string { +func (n *ShortNode) fstring(ind string) string { return fmt.Sprintf("{%x: %v} ", n.Key, n.Val.fstring(ind+" ")) } -func (n hashNode) fstring(ind string) string { +func (n HashNode) fstring(ind string) string { return fmt.Sprintf("<%x> ", []byte(n)) } -func (n valueNode) fstring(ind string) string { +func (n ValueNode) fstring(ind string) string { return fmt.Sprintf("%x ", []byte(n)) } -func mustDecodeNode(hash, buf []byte, cachegen uint16) node { +func MustDecodeNode(hash, buf []byte, cachegen uint16) Node { n, err := decodeNode(hash, buf, cachegen) if err != nil { panic(fmt.Sprintf("node %x: %v", hash, err)) @@ -113,7 +113,7 @@ func mustDecodeNode(hash, buf []byte, cachegen uint16) node { } // decodeNode parses the RLP encoding of a trie node. -func decodeNode(hash, buf []byte, cachegen uint16) (node, error) { +func decodeNode(hash, buf []byte, cachegen uint16) (Node, error) { if len(buf) == 0 { return nil, io.ErrUnexpectedEOF } @@ -133,7 +133,7 @@ func decodeNode(hash, buf []byte, cachegen uint16) (node, error) { } } -func decodeShort(hash, buf, elems []byte, cachegen uint16) (node, error) { +func decodeShort(hash, buf, elems []byte, cachegen uint16) (Node, error) { kbuf, rest, err := rlp.SplitString(elems) if err != nil { return nil, err @@ -146,17 +146,17 @@ func decodeShort(hash, buf, elems []byte, cachegen uint16) (node, error) { if err != nil { return nil, fmt.Errorf("invalid value node: %v", err) } - return &shortNode{key, append(valueNode{}, val...), flag}, nil + return &ShortNode{key, append(ValueNode{}, val...), flag}, nil } r, _, err := decodeRef(rest, cachegen) if err != nil { return nil, wrapError(err, "val") } - return &shortNode{key, r, flag}, nil + return &ShortNode{key, r, flag}, nil } -func decodeFull(hash, buf, elems []byte, cachegen uint16) (*fullNode, error) { - n := &fullNode{flags: nodeFlag{hash: hash, gen: cachegen}} +func decodeFull(hash, buf, elems []byte, cachegen uint16) (*FullNode, error) { + n := &FullNode{flags: nodeFlag{hash: hash, gen: cachegen}} for i := 0; i < 16; i++ { cld, rest, err := decodeRef(elems, cachegen) if err != nil { @@ -169,14 +169,14 @@ func decodeFull(hash, buf, elems []byte, cachegen uint16) (*fullNode, error) { return n, err } if len(val) > 0 { - n.Children[16] = append(valueNode{}, val...) + n.Children[16] = append(ValueNode{}, val...) } return n, nil } const hashLen = len(common.Hash{}) -func decodeRef(buf []byte, cachegen uint16) (node, []byte, error) { +func decodeRef(buf []byte, cachegen uint16) (Node, []byte, error) { kind, val, rest, err := rlp.Split(buf) if err != nil { return nil, buf, err @@ -195,7 +195,7 @@ func decodeRef(buf []byte, cachegen uint16) (node, []byte, error) { // empty node return nil, rest, nil case kind == rlp.String && len(val) == 32: - return append(hashNode{}, val...), rest, nil + return append(HashNode{}, val...), rest, nil default: return nil, nil, fmt.Errorf("invalid RLP string size %d (want 0 or 32)", len(val)) } diff --git a/trie/proof.go b/trie/proof.go index 508e4a6cf4..32738bd867 100644 --- a/trie/proof.go +++ b/trie/proof.go @@ -37,11 +37,11 @@ import ( func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error { // Collect all nodes on the path to key. key = keybytesToHex(key) - nodes := []node{} + nodes := []Node{} tn := t.root for len(key) > 0 && tn != nil { switch n := tn.(type) { - case *shortNode: + case *ShortNode: if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) { // The trie doesn't contain the key. tn = nil @@ -50,11 +50,11 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error { key = key[len(n.Key):] } nodes = append(nodes, n) - case *fullNode: + case *FullNode: tn = n.Children[key[0]] key = key[1:] nodes = append(nodes, n) - case hashNode: + case HashNode: var err error tn, err = t.resolveHash(n, nil) if err != nil { @@ -71,7 +71,7 @@ func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error { // if encoding doesn't work and we're not writing to any database. n, _, _ = hasher.hashChildren(n, nil) hn, _ := hasher.store(n, nil, false) - if hash, ok := hn.(hashNode); ok || i == 0 { + if hash, ok := hn.(HashNode); ok || i == 0 { // If the node's database encoding is a hash (or is the // root node), it becomes a proof element. if fromLevel > 0 { @@ -119,32 +119,32 @@ func VerifyProof(rootHash common.Hash, key []byte, proofDb DatabaseReader) (valu case nil: // The trie doesn't contain the key. return nil, nil, i - case hashNode: + case HashNode: key = keyrest copy(wantHash[:], cld) - case valueNode: + case ValueNode: return cld, nil, i + 1 } } } -func get(tn node, key []byte) ([]byte, node) { +func get(tn Node, key []byte) ([]byte, Node) { for { switch n := tn.(type) { - case *shortNode: + case *ShortNode: if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) { return nil, nil } tn = n.Val key = key[len(n.Key):] - case *fullNode: + case *FullNode: tn = n.Children[key[0]] key = key[1:] - case hashNode: + case HashNode: return key, n case nil: return key, nil - case valueNode: + case ValueNode: return nil, n default: panic(fmt.Sprintf("%T: invalid node: %v", tn, tn)) diff --git a/trie/sync.go b/trie/sync.go index b573a9f732..3e0939fddd 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -248,21 +248,21 @@ func (s *TrieSync) schedule(req *request) { // children retrieves all the missing children of a state trie entry for future // retrieval scheduling. -func (s *TrieSync) children(req *request, object node) ([]*request, error) { +func (s *TrieSync) children(req *request, object Node) ([]*request, error) { // Gather all the children of the node, irrelevant whether known or not type child struct { - node node + node Node depth int } children := []child{} switch node := (object).(type) { - case *shortNode: + case *ShortNode: children = []child{{ node: node.Val, depth: req.depth + len(node.Key), }} - case *fullNode: + case *FullNode: for i := 0; i < 17; i++ { if node.Children[i] != nil { children = append(children, child{ @@ -279,14 +279,14 @@ func (s *TrieSync) children(req *request, object node) ([]*request, error) { for _, child := range children { // Notify any external watcher of a new key/value node if req.callback != nil { - if node, ok := (child.node).(valueNode); ok { + if node, ok := (child.node).(ValueNode); ok { if err := req.callback(node, req.hash); err != nil { return nil, err } } } // If the child references another node, resolve or schedule - if node, ok := (child.node).(hashNode); ok { + if node, ok := (child.node).(HashNode); ok { // Try to resolve the node from the local database hash := common.BytesToHash(node) if _, ok := s.membatch.batch[hash]; ok { diff --git a/trie/trie.go b/trie/trie.go index 31a404e3a0..0f0688a03e 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -66,7 +66,7 @@ type LeafCallback func(leaf []byte, parent common.Hash) error // Trie is not safe for concurrent use. type Trie struct { db *Database - root node + root Node originalRoot common.Hash // Cache generation values. @@ -114,7 +114,7 @@ func New(root common.Hash, db *Database) (*Trie, error) { // NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at // the key after the given start key. func (t *Trie) NodeIterator(start []byte) NodeIterator { - return newNodeIterator(t, start) + return NewNodeIterator(t, start) } // Get returns the value for key stored in the trie. @@ -139,13 +139,13 @@ func (t *Trie) TryGet(key []byte) ([]byte, error) { return value, err } -func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) { +func (t *Trie) tryGet(origNode Node, key []byte, pos int) (value []byte, newnode Node, didResolve bool, err error) { switch n := (origNode).(type) { case nil: return nil, nil, false, nil - case valueNode: + case ValueNode: return n, n, false, nil - case *shortNode: + case *ShortNode: if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) { // key not found in trie return nil, n, false, nil @@ -157,7 +157,7 @@ func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode n.flags.gen = t.cachegen } return value, n, didResolve, err - case *fullNode: + case *FullNode: value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1) if err == nil && didResolve { n = n.copy() @@ -165,7 +165,7 @@ func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode n.Children[key[pos]] = newnode } return value, n, didResolve, err - case hashNode: + case HashNode: child, err := t.resolveHash(n, key[:pos]) if err != nil { return nil, n, true, err @@ -200,7 +200,7 @@ func (t *Trie) Update(key, value []byte) { func (t *Trie) TryUpdate(key, value []byte) error { k := keybytesToHex(key) if len(value) != 0 { - _, n, err := t.insert(t.root, nil, k, valueNode(value)) + _, n, err := t.insert(t.root, nil, k, ValueNode(value)) if err != nil { return err } @@ -215,15 +215,15 @@ func (t *Trie) TryUpdate(key, value []byte) error { return nil } -func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) { +func (t *Trie) insert(n Node, prefix, key []byte, value Node) (bool, Node, error) { if len(key) == 0 { - if v, ok := n.(valueNode); ok { - return !bytes.Equal(v, value.(valueNode)), value, nil + if v, ok := n.(ValueNode); ok { + return !bytes.Equal(v, value.(ValueNode)), value, nil } return true, value, nil } switch n := n.(type) { - case *shortNode: + case *ShortNode: matchlen := prefixLen(key, n.Key) // If the whole key matches, keep this short node as is // and only update the value. @@ -232,10 +232,10 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error if !dirty || err != nil { return false, n, err } - return true, &shortNode{n.Key, nn, t.newFlag()}, nil + return true, &ShortNode{n.Key, nn, t.newFlag()}, nil } // Otherwise branch out at the index where they differ. - branch := &fullNode{flags: t.newFlag()} + branch := &FullNode{flags: t.newFlag()} var err error _, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val) if err != nil { @@ -250,9 +250,9 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error return true, branch, nil } // Otherwise, replace it with a short node leading up to the branch. - return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil + return true, &ShortNode{key[:matchlen], branch, t.newFlag()}, nil - case *fullNode: + case *FullNode: dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value) if !dirty || err != nil { return false, n, err @@ -263,9 +263,9 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error return true, n, nil case nil: - return true, &shortNode{key, value, t.newFlag()}, nil + return true, &ShortNode{key, value, t.newFlag()}, nil - case hashNode: + case HashNode: // We've hit a part of the trie that isn't loaded yet. Load // the node and insert into it. This leaves all child nodes on // the path to the value in the trie. @@ -306,9 +306,9 @@ func (t *Trie) TryDelete(key []byte) error { // delete returns the new root of the trie with key deleted. // It reduces the trie to minimal form by simplifying // nodes on the way up after deleting recursively. -func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { +func (t *Trie) delete(n Node, prefix, key []byte) (bool, Node, error) { switch n := n.(type) { - case *shortNode: + case *ShortNode: matchlen := prefixLen(key, n.Key) if matchlen < len(n.Key) { return false, n, nil // don't replace n on mismatch @@ -325,19 +325,19 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { return false, n, err } switch child := child.(type) { - case *shortNode: + case *ShortNode: // Deleting from the subtrie reduced it to another // short node. Merge the nodes to avoid creating a // shortNode{..., shortNode{...}}. Use concat (which // always creates a new slice) instead of append to // avoid modifying n.Key since it might be shared with // other nodes. - return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil + return true, &ShortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil default: - return true, &shortNode{n.Key, child, t.newFlag()}, nil + return true, &ShortNode{n.Key, child, t.newFlag()}, nil } - case *fullNode: + case *FullNode: dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:]) if !dirty || err != nil { return false, n, err @@ -368,7 +368,7 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { } if pos >= 0 { if pos != 16 { - // If the remaining entry is a short node, it replaces + // If the remaining entry is a short node, it replaces // n and its key gets the missing nibble tacked to the // front. This avoids creating an invalid // shortNode{..., shortNode{...}}. Since the entry @@ -378,25 +378,25 @@ func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) { if err != nil { return false, nil, err } - if cnode, ok := cnode.(*shortNode); ok { + if cnode, ok := cnode.(*ShortNode); ok { k := append([]byte{byte(pos)}, cnode.Key...) - return true, &shortNode{k, cnode.Val, t.newFlag()}, nil + return true, &ShortNode{k, cnode.Val, t.newFlag()}, nil } } // Otherwise, n is replaced by a one-nibble short node // containing the child. - return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil + return true, &ShortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil } // n still contains at least two values and cannot be reduced. return true, n, nil - case valueNode: + case ValueNode: return true, nil, nil case nil: return false, nil, nil - case hashNode: + case HashNode: // We've hit a part of the trie that isn't loaded yet. Load // the node and delete from it. This leaves all child nodes on // the path to the value in the trie. @@ -422,14 +422,14 @@ func concat(s1 []byte, s2 ...byte) []byte { return r } -func (t *Trie) resolve(n node, prefix []byte) (node, error) { - if n, ok := n.(hashNode); ok { +func (t *Trie) resolve(n Node, prefix []byte) (Node, error) { + if n, ok := n.(HashNode); ok { return t.resolveHash(n, prefix) } return n, nil } -func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { +func (t *Trie) resolveHash(n HashNode, prefix []byte) (Node, error) { cacheMissCounter.Inc(1) hash := common.BytesToHash(n) @@ -438,7 +438,7 @@ func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) { if err != nil || enc == nil { return nil, &MissingNodeError{NodeHash: hash, Path: prefix} } - return mustDecodeNode(n, enc, t.cachegen), nil + return MustDecodeNode(n, enc, t.cachegen), nil } // Root returns the root hash of the trie. @@ -450,7 +450,7 @@ func (t *Trie) Root() []byte { return t.Hash().Bytes() } func (t *Trie) Hash() common.Hash { hash, cached, _ := t.hashRoot(nil, nil) t.root = cached - return common.BytesToHash(hash.(hashNode)) + return common.BytesToHash(hash.(HashNode)) } // Commit writes all nodes to the trie's memory database, tracking the internal @@ -465,12 +465,12 @@ func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) { } t.root = cached t.cachegen++ - return common.BytesToHash(hash.(hashNode)), nil + return common.BytesToHash(hash.(HashNode)), nil } -func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (node, node, error) { +func (t *Trie) hashRoot(db *Database, onleaf LeafCallback) (Node, Node, error) { if t.root == nil { - return hashNode(emptyRoot.Bytes()), nil, nil + return HashNode(emptyRoot.Bytes()), nil, nil } h := newHasher(t.cachegen, t.cachelimit, onleaf) defer returnHasherToPool(h) diff --git a/trie/trie_test.go b/trie/trie_test.go index 9972226288..8a8436270a 100644 --- a/trie/trie_test.go +++ b/trie/trie_test.go @@ -469,14 +469,14 @@ func runRandTest(rt randTest) bool { return true } -func checkCacheInvariant(n, parent node, parentCachegen uint16, parentDirty bool, depth int) error { - var children []node +func checkCacheInvariant(n, parent Node, parentCachegen uint16, parentDirty bool, depth int) error { + var children []Node var flag nodeFlag switch n := n.(type) { - case *shortNode: + case *ShortNode: flag = n.flags - children = []node{n.Val} - case *fullNode: + children = []Node{n.Val} + case *FullNode: flag = n.flags children = n.Children[:] default: