dump.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package state
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. "github.com/ethereum/go-ethereum/trie"
  25. )
  26. // DumpCollector interface which the state trie calls during iteration
  27. type DumpCollector interface {
  28. // OnRoot is called with the state root
  29. OnRoot(common.Hash)
  30. // OnAccount is called once for each account in the trie
  31. OnAccount(common.Address, DumpAccount)
  32. }
  33. // DumpAccount represents an account in the state.
  34. type DumpAccount struct {
  35. Balance string `json:"balance"`
  36. Nonce uint64 `json:"nonce"`
  37. Root string `json:"root"`
  38. CodeHash string `json:"codeHash"`
  39. Code string `json:"code,omitempty"`
  40. Storage map[common.Hash]string `json:"storage,omitempty"`
  41. Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
  42. SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
  43. }
  44. // Dump represents the full dump in a collected format, as one large map.
  45. type Dump struct {
  46. Root string `json:"root"`
  47. Accounts map[common.Address]DumpAccount `json:"accounts"`
  48. }
  49. // OnRoot implements DumpCollector interface
  50. func (d *Dump) OnRoot(root common.Hash) {
  51. d.Root = fmt.Sprintf("%x", root)
  52. }
  53. // OnAccount implements DumpCollector interface
  54. func (d *Dump) OnAccount(addr common.Address, account DumpAccount) {
  55. d.Accounts[addr] = account
  56. }
  57. // IteratorDump is an implementation for iterating over data.
  58. type IteratorDump struct {
  59. Root string `json:"root"`
  60. Accounts map[common.Address]DumpAccount `json:"accounts"`
  61. Next []byte `json:"next,omitempty"` // nil if no more accounts
  62. }
  63. // OnRoot implements DumpCollector interface
  64. func (d *IteratorDump) OnRoot(root common.Hash) {
  65. d.Root = fmt.Sprintf("%x", root)
  66. }
  67. // OnAccount implements DumpCollector interface
  68. func (d *IteratorDump) OnAccount(addr common.Address, account DumpAccount) {
  69. d.Accounts[addr] = account
  70. }
  71. // iterativeDump is a DumpCollector-implementation which dumps output line-by-line iteratively.
  72. type iterativeDump struct {
  73. *json.Encoder
  74. }
  75. // OnAccount implements DumpCollector interface
  76. func (d iterativeDump) OnAccount(addr common.Address, account DumpAccount) {
  77. dumpAccount := &DumpAccount{
  78. Balance: account.Balance,
  79. Nonce: account.Nonce,
  80. Root: account.Root,
  81. CodeHash: account.CodeHash,
  82. Code: account.Code,
  83. Storage: account.Storage,
  84. SecureKey: account.SecureKey,
  85. Address: nil,
  86. }
  87. if addr != (common.Address{}) {
  88. dumpAccount.Address = &addr
  89. }
  90. d.Encode(dumpAccount)
  91. }
  92. // OnRoot implements DumpCollector interface
  93. func (d iterativeDump) OnRoot(root common.Hash) {
  94. d.Encode(struct {
  95. Root common.Hash `json:"root"`
  96. }{root})
  97. }
  98. func (s *StateDB) DumpToCollector(c DumpCollector, excludeCode, excludeStorage, excludeMissingPreimages bool, start []byte, maxResults int) (nextKey []byte) {
  99. missingPreimages := 0
  100. c.OnRoot(s.trie.Hash())
  101. var count int
  102. it := trie.NewIterator(s.trie.NodeIterator(start))
  103. for it.Next() {
  104. var data Account
  105. if err := rlp.DecodeBytes(it.Value, &data); err != nil {
  106. panic(err)
  107. }
  108. account := DumpAccount{
  109. Balance: data.Balance.String(),
  110. Nonce: data.Nonce,
  111. Root: common.Bytes2Hex(data.Root[:]),
  112. CodeHash: common.Bytes2Hex(data.CodeHash),
  113. }
  114. addrBytes := s.trie.GetKey(it.Key)
  115. if addrBytes == nil {
  116. // Preimage missing
  117. missingPreimages++
  118. if excludeMissingPreimages {
  119. continue
  120. }
  121. account.SecureKey = it.Key
  122. }
  123. addr := common.BytesToAddress(addrBytes)
  124. obj := newObject(s, addr, data)
  125. if !excludeCode {
  126. account.Code = common.Bytes2Hex(obj.Code(s.db))
  127. }
  128. if !excludeStorage {
  129. account.Storage = make(map[common.Hash]string)
  130. storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
  131. for storageIt.Next() {
  132. _, content, _, err := rlp.Split(storageIt.Value)
  133. if err != nil {
  134. log.Error("Failed to decode the value returned by iterator", "error", err)
  135. continue
  136. }
  137. account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
  138. }
  139. }
  140. c.OnAccount(addr, account)
  141. count++
  142. if maxResults > 0 && count >= maxResults {
  143. if it.Next() {
  144. nextKey = it.Key
  145. }
  146. break
  147. }
  148. }
  149. if missingPreimages > 0 {
  150. log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
  151. }
  152. return nextKey
  153. }
  154. // RawDump returns the entire state an a single large object
  155. func (s *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
  156. dump := &Dump{
  157. Accounts: make(map[common.Address]DumpAccount),
  158. }
  159. s.DumpToCollector(dump, excludeCode, excludeStorage, excludeMissingPreimages, nil, 0)
  160. return *dump
  161. }
  162. // Dump returns a JSON string representing the entire state as a single json-object
  163. func (s *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
  164. dump := s.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
  165. json, err := json.MarshalIndent(dump, "", " ")
  166. if err != nil {
  167. fmt.Println("Dump err", err)
  168. }
  169. return json
  170. }
  171. func (self *StateDB) DumpAddress(address common.Address) (DumpAccount, bool) {
  172. if !self.Exist(address) {
  173. return DumpAccount{}, false
  174. }
  175. obj := self.getStateObject(address)
  176. account := DumpAccount{
  177. Balance: obj.data.Balance.String(),
  178. Nonce: obj.data.Nonce,
  179. Root: common.Bytes2Hex(obj.data.Root[:]),
  180. CodeHash: common.Bytes2Hex(obj.data.CodeHash),
  181. Code: common.Bytes2Hex(obj.Code(self.db)),
  182. Storage: make(map[common.Hash]string),
  183. }
  184. noDataIssue := true
  185. storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
  186. for storageIt.Next() {
  187. _, content, _, err := rlp.Split(storageIt.Value)
  188. if err != nil {
  189. noDataIssue = false
  190. log.Error("Failed to decode the value returned by iterator", "error", err)
  191. break
  192. }
  193. account.Storage[common.BytesToHash(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
  194. }
  195. return account, noDataIssue
  196. }
  197. // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
  198. func (s *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
  199. s.DumpToCollector(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages, nil, 0)
  200. }
  201. // IteratorDump dumps out a batch of accounts starts with the given start key
  202. func (s *StateDB) IteratorDump(excludeCode, excludeStorage, excludeMissingPreimages bool, start []byte, maxResults int) IteratorDump {
  203. iterator := &IteratorDump{
  204. Accounts: make(map[common.Address]DumpAccount),
  205. }
  206. iterator.Next = s.DumpToCollector(iterator, excludeCode, excludeStorage, excludeMissingPreimages, start, maxResults)
  207. return *iterator
  208. }