dump.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. 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. Storage map[string]string `json:"storage"`
  28. }
  29. type World struct {
  30. Root string `json:"root"`
  31. Accounts map[string]Account `json:"accounts"`
  32. }
  33. func (self *StateDB) RawDump() World {
  34. world := World{
  35. Root: common.Bytes2Hex(self.trie.Root()),
  36. Accounts: make(map[string]Account),
  37. }
  38. it := self.trie.Iterator()
  39. for it.Next() {
  40. addr := self.trie.GetKey(it.Key)
  41. stateObject := NewStateObjectFromBytes(common.BytesToAddress(addr), it.Value, self.db)
  42. account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
  43. account.Storage = make(map[string]string)
  44. storageIt := stateObject.trie.Iterator()
  45. for storageIt.Next() {
  46. account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
  47. }
  48. world.Accounts[common.Bytes2Hex(addr)] = account
  49. }
  50. return world
  51. }
  52. func (self *StateDB) Dump() []byte {
  53. json, err := json.MarshalIndent(self.RawDump(), "", " ")
  54. if err != nil {
  55. fmt.Println("dump err", err)
  56. }
  57. return json
  58. }
  59. // Debug stuff
  60. func (self *StateObject) CreateOutputForDiff() {
  61. fmt.Printf("%x %x %x %x\n", self.Address(), self.Root(), self.balance.Bytes(), self.nonce)
  62. it := self.trie.Iterator()
  63. for it.Next() {
  64. fmt.Printf("%x %x\n", it.Key, it.Value)
  65. }
  66. }