dump.go 5.3 KB

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