dump.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // DumpAccount represents an account in the state.
  27. type DumpAccount struct {
  28. Balance string `json:"balance"`
  29. Nonce uint64 `json:"nonce"`
  30. Root string `json:"root"`
  31. CodeHash string `json:"codeHash"`
  32. Code string `json:"code,omitempty"`
  33. Storage map[common.Hash]string `json:"storage,omitempty"`
  34. Address *common.Address `json:"address,omitempty"` // Address only present in iterative (line-by-line) mode
  35. SecureKey hexutil.Bytes `json:"key,omitempty"` // If we don't have address, we can output the key
  36. }
  37. // Dump represents the full dump in a collected format, as one large map.
  38. type Dump struct {
  39. Root string `json:"root"`
  40. Accounts map[common.Address]DumpAccount `json:"accounts"`
  41. }
  42. // iterativeDump is a 'collector'-implementation which dump output line-by-line iteratively.
  43. type iterativeDump struct {
  44. *json.Encoder
  45. }
  46. // IteratorDump is an implementation for iterating over data.
  47. type IteratorDump struct {
  48. Root string `json:"root"`
  49. Accounts map[common.Address]DumpAccount `json:"accounts"`
  50. Next []byte `json:"next,omitempty"` // nil if no more accounts
  51. }
  52. // Collector interface which the state trie calls during iteration
  53. type collector interface {
  54. onRoot(common.Hash)
  55. onAccount(common.Address, DumpAccount)
  56. }
  57. func (d *Dump) onRoot(root common.Hash) {
  58. d.Root = fmt.Sprintf("%x", root)
  59. }
  60. func (d *Dump) onAccount(addr common.Address, account DumpAccount) {
  61. d.Accounts[addr] = account
  62. }
  63. func (d *IteratorDump) onRoot(root common.Hash) {
  64. d.Root = fmt.Sprintf("%x", root)
  65. }
  66. func (d *IteratorDump) onAccount(addr common.Address, account DumpAccount) {
  67. d.Accounts[addr] = account
  68. }
  69. func (d iterativeDump) onAccount(addr common.Address, account DumpAccount) {
  70. dumpAccount := &DumpAccount{
  71. Balance: account.Balance,
  72. Nonce: account.Nonce,
  73. Root: account.Root,
  74. CodeHash: account.CodeHash,
  75. Code: account.Code,
  76. Storage: account.Storage,
  77. SecureKey: account.SecureKey,
  78. Address: nil,
  79. }
  80. if addr != (common.Address{}) {
  81. dumpAccount.Address = &addr
  82. }
  83. d.Encode(dumpAccount)
  84. }
  85. func (d iterativeDump) onRoot(root common.Hash) {
  86. d.Encode(struct {
  87. Root common.Hash `json:"root"`
  88. }{root})
  89. }
  90. func (s *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool, start []byte, maxResults int) (nextKey []byte) {
  91. missingPreimages := 0
  92. c.onRoot(s.trie.Hash())
  93. var count int
  94. it := trie.NewIterator(s.trie.NodeIterator(start))
  95. for it.Next() {
  96. var data Account
  97. if err := rlp.DecodeBytes(it.Value, &data); err != nil {
  98. panic(err)
  99. }
  100. account := DumpAccount{
  101. Balance: data.Balance.String(),
  102. Nonce: data.Nonce,
  103. Root: common.Bytes2Hex(data.Root[:]),
  104. CodeHash: common.Bytes2Hex(data.CodeHash),
  105. }
  106. addrBytes := s.trie.GetKey(it.Key)
  107. if addrBytes == nil {
  108. // Preimage missing
  109. missingPreimages++
  110. if excludeMissingPreimages {
  111. continue
  112. }
  113. account.SecureKey = it.Key
  114. }
  115. addr := common.BytesToAddress(addrBytes)
  116. obj := newObject(nil, addr, data)
  117. if !excludeCode {
  118. account.Code = common.Bytes2Hex(obj.Code(s.db))
  119. }
  120. if !excludeStorage {
  121. account.Storage = make(map[common.Hash]string)
  122. storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
  123. for storageIt.Next() {
  124. _, content, _, err := rlp.Split(storageIt.Value)
  125. if err != nil {
  126. log.Error("Failed to decode the value returned by iterator", "error", err)
  127. continue
  128. }
  129. account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
  130. }
  131. }
  132. c.onAccount(addr, account)
  133. count++
  134. if maxResults > 0 && count >= maxResults {
  135. if it.Next() {
  136. nextKey = it.Key
  137. }
  138. break
  139. }
  140. }
  141. if missingPreimages > 0 {
  142. log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
  143. }
  144. return nextKey
  145. }
  146. // RawDump returns the entire state an a single large object
  147. func (s *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
  148. dump := &Dump{
  149. Accounts: make(map[common.Address]DumpAccount),
  150. }
  151. s.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages, nil, 0)
  152. return *dump
  153. }
  154. // Dump returns a JSON string representing the entire state as a single json-object
  155. func (s *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
  156. dump := s.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
  157. json, err := json.MarshalIndent(dump, "", " ")
  158. if err != nil {
  159. fmt.Println("dump err", err)
  160. }
  161. return json
  162. }
  163. // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
  164. func (s *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
  165. s.dump(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages, nil, 0)
  166. }
  167. // IteratorDump dumps out a batch of accounts starts with the given start key
  168. func (s *StateDB) IteratorDump(excludeCode, excludeStorage, excludeMissingPreimages bool, start []byte, maxResults int) IteratorDump {
  169. iterator := &IteratorDump{
  170. Accounts: make(map[common.Address]DumpAccount),
  171. }
  172. iterator.Next = s.dump(iterator, excludeCode, excludeStorage, excludeMissingPreimages, start, maxResults)
  173. return *iterator
  174. }