dump.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. )
  22. type Account struct {
  23. Balance string `json:"balance"`
  24. Nonce uint64 `json:"nonce"`
  25. Root string `json:"root"`
  26. CodeHash string `json:"codeHash"`
  27. Code string `json:"code"`
  28. Storage map[string]string `json:"storage"`
  29. }
  30. type World struct {
  31. Root string `json:"root"`
  32. Accounts map[string]Account `json:"accounts"`
  33. }
  34. func (self *StateDB) RawDump() World {
  35. world := World{
  36. Root: common.Bytes2Hex(self.trie.Root()),
  37. Accounts: make(map[string]Account),
  38. }
  39. it := self.trie.Iterator()
  40. for it.Next() {
  41. addr := self.trie.GetKey(it.Key)
  42. stateObject, err := DecodeObject(common.BytesToAddress(addr), self.db, it.Value)
  43. if err != nil {
  44. panic(err)
  45. }
  46. account := Account{
  47. Balance: stateObject.balance.String(),
  48. Nonce: stateObject.nonce,
  49. Root: common.Bytes2Hex(stateObject.Root()),
  50. CodeHash: common.Bytes2Hex(stateObject.codeHash),
  51. Code: common.Bytes2Hex(stateObject.Code()),
  52. Storage: make(map[string]string),
  53. }
  54. storageIt := stateObject.trie.Iterator()
  55. for storageIt.Next() {
  56. account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
  57. }
  58. world.Accounts[common.Bytes2Hex(addr)] = account
  59. }
  60. return world
  61. }
  62. func (self *StateDB) Dump() []byte {
  63. json, err := json.MarshalIndent(self.RawDump(), "", " ")
  64. if err != nil {
  65. fmt.Println("dump err", err)
  66. }
  67. return json
  68. }
  69. // Debug stuff
  70. func (self *StateObject) CreateOutputForDiff() {
  71. fmt.Printf("%x %x %x %x\n", self.Address(), self.Root(), self.balance.Bytes(), self.nonce)
  72. it := self.trie.Iterator()
  73. for it.Next() {
  74. fmt.Printf("%x %x\n", it.Key, it.Value)
  75. }
  76. }