-
Notifications
You must be signed in to change notification settings - Fork 182
/
mpt_viewer.go
78 lines (69 loc) · 2.39 KB
/
mpt_viewer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package mpt
import (
"fmt"
"log"
ethcmn "github.com/ethereum/go-ethereum/common"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/trie"
"github.com/okex/exchain/libs/cosmos-sdk/server"
"github.com/okex/exchain/libs/cosmos-sdk/store/mpt"
"github.com/spf13/cobra"
)
func mptViewerCmd(ctx *server.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "mptviewer",
Args: cobra.ExactArgs(1),
Short: "iterate mpt store (acc, evm)",
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkValidKey(args[0])
},
Run: func(cmd *cobra.Command, args []string) {
log.Printf("--------- iterate %s data start ---------\n", args[0])
switch args[0] {
case accStoreKey:
iterateAccMpt(ctx)
case evmStoreKey:
iterateEvmMpt(ctx)
}
log.Printf("--------- iterate %s data end ---------\n", args[0])
},
}
return cmd
}
func iterateAccMpt(ctx *server.Context) {
accMptDb := mpt.InstanceOfMptStore()
heightBytes, err := accMptDb.TrieDB().DiskDB().Get(mpt.KeyPrefixAccLatestStoredHeight)
panicError(err)
rootHash, err := accMptDb.TrieDB().DiskDB().Get(append(mpt.KeyPrefixAccRootMptHash, heightBytes...))
panicError(err)
accTrie, err := accMptDb.OpenTrie(ethcmn.BytesToHash(rootHash))
panicError(err)
fmt.Println("accTrie root hash:", accTrie.Hash())
itr := trie.NewIterator(accTrie.NodeIterator(nil))
for itr.Next() {
fmt.Printf("%s: %s\n", ethcmn.Bytes2Hex(itr.Key), ethcmn.Bytes2Hex(itr.Value))
}
}
func iterateEvmMpt(ctx *server.Context) {
evmMptDb := mpt.InstanceOfMptStore()
hhash, err := evmMptDb.TrieDB().DiskDB().Get(mpt.KeyPrefixAccLatestStoredHeight)
panicError(err)
rootHash, err := evmMptDb.TrieDB().DiskDB().Get(append(mpt.KeyPrefixEvmRootMptHash, hhash...))
panicError(err)
evmTrie, err := evmMptDb.OpenTrie(ethcmn.BytesToHash(rootHash))
panicError(err)
fmt.Println("evmTrie root hash:", evmTrie.Hash())
var stateRoot ethcmn.Hash
itr := trie.NewIterator(evmTrie.NodeIterator(nil))
for itr.Next() {
addr := ethcmn.BytesToAddress(evmTrie.GetKey(itr.Key))
addrHash := ethcrypto.Keccak256Hash(addr[:])
stateRoot.SetBytes(itr.Value)
contractTrie := getStorageTrie(evmMptDb, addrHash, stateRoot)
fmt.Println(addr.String(), contractTrie.Hash())
cItr := trie.NewIterator(contractTrie.NodeIterator(nil))
for cItr.Next() {
fmt.Printf("%s: %s\n", ethcmn.Bytes2Hex(cItr.Key), ethcmn.Bytes2Hex(cItr.Value))
}
}
}