dump.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // Collector interface which the state trie calls during iteration
  47. type collector interface {
  48. onRoot(common.Hash)
  49. onAccount(common.Address, DumpAccount)
  50. }
  51. func (d *Dump) onRoot(root common.Hash) {
  52. d.Root = fmt.Sprintf("%x", root)
  53. }
  54. func (d *Dump) onAccount(addr common.Address, account DumpAccount) {
  55. d.Accounts[addr] = account
  56. }
  57. func (d iterativeDump) onAccount(addr common.Address, account DumpAccount) {
  58. dumpAccount := &DumpAccount{
  59. Balance: account.Balance,
  60. Nonce: account.Nonce,
  61. Root: account.Root,
  62. CodeHash: account.CodeHash,
  63. Code: account.Code,
  64. Storage: account.Storage,
  65. SecureKey: account.SecureKey,
  66. Address: nil,
  67. }
  68. if addr != (common.Address{}) {
  69. dumpAccount.Address = &addr
  70. }
  71. d.Encode(dumpAccount)
  72. }
  73. func (d iterativeDump) onRoot(root common.Hash) {
  74. d.Encode(struct {
  75. Root common.Hash `json:"root"`
  76. }{root})
  77. }
  78. func (s *StateDB) dump(c collector, excludeCode, excludeStorage, excludeMissingPreimages bool) {
  79. emptyAddress := (common.Address{})
  80. missingPreimages := 0
  81. c.onRoot(s.trie.Hash())
  82. it := trie.NewIterator(s.trie.NodeIterator(nil))
  83. for it.Next() {
  84. var data Account
  85. if err := rlp.DecodeBytes(it.Value, &data); err != nil {
  86. panic(err)
  87. }
  88. addr := common.BytesToAddress(s.trie.GetKey(it.Key))
  89. obj := newObject(nil, addr, data)
  90. account := DumpAccount{
  91. Balance: data.Balance.String(),
  92. Nonce: data.Nonce,
  93. Root: common.Bytes2Hex(data.Root[:]),
  94. CodeHash: common.Bytes2Hex(data.CodeHash),
  95. }
  96. if emptyAddress == addr {
  97. // Preimage missing
  98. missingPreimages++
  99. if excludeMissingPreimages {
  100. continue
  101. }
  102. account.SecureKey = it.Key
  103. }
  104. if !excludeCode {
  105. account.Code = common.Bytes2Hex(obj.Code(s.db))
  106. }
  107. if !excludeStorage {
  108. account.Storage = make(map[common.Hash]string)
  109. storageIt := trie.NewIterator(obj.getTrie(s.db).NodeIterator(nil))
  110. for storageIt.Next() {
  111. _, content, _, err := rlp.Split(storageIt.Value)
  112. if err != nil {
  113. log.Error("Failed to decode the value returned by iterator", "error", err)
  114. continue
  115. }
  116. account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
  117. }
  118. }
  119. c.onAccount(addr, account)
  120. }
  121. if missingPreimages > 0 {
  122. log.Warn("Dump incomplete due to missing preimages", "missing", missingPreimages)
  123. }
  124. }
  125. // RawDump returns the entire state an a single large object
  126. func (s *StateDB) RawDump(excludeCode, excludeStorage, excludeMissingPreimages bool) Dump {
  127. dump := &Dump{
  128. Accounts: make(map[common.Address]DumpAccount),
  129. }
  130. s.dump(dump, excludeCode, excludeStorage, excludeMissingPreimages)
  131. return *dump
  132. }
  133. // Dump returns a JSON string representing the entire state as a single json-object
  134. func (s *StateDB) Dump(excludeCode, excludeStorage, excludeMissingPreimages bool) []byte {
  135. dump := s.RawDump(excludeCode, excludeStorage, excludeMissingPreimages)
  136. json, err := json.MarshalIndent(dump, "", " ")
  137. if err != nil {
  138. fmt.Println("dump err", err)
  139. }
  140. return json
  141. }
  142. // IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
  143. func (s *StateDB) IterativeDump(excludeCode, excludeStorage, excludeMissingPreimages bool, output *json.Encoder) {
  144. s.dump(iterativeDump{output}, excludeCode, excludeStorage, excludeMissingPreimages)
  145. }