dump.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/rlp"
  22. )
  23. type DumpAccount struct {
  24. Balance string `json:"balance"`
  25. Nonce uint64 `json:"nonce"`
  26. Root string `json:"root"`
  27. CodeHash string `json:"codeHash"`
  28. Code string `json:"code"`
  29. Storage map[string]string `json:"storage"`
  30. }
  31. type Dump struct {
  32. Root string `json:"root"`
  33. Accounts map[string]DumpAccount `json:"accounts"`
  34. }
  35. func (self *StateDB) RawDump() Dump {
  36. dump := Dump{
  37. Root: common.Bytes2Hex(self.trie.Root()),
  38. Accounts: make(map[string]DumpAccount),
  39. }
  40. it := self.trie.Iterator()
  41. for it.Next() {
  42. addr := self.trie.GetKey(it.Key)
  43. var data Account
  44. if err := rlp.DecodeBytes(it.Value, &data); err != nil {
  45. panic(err)
  46. }
  47. obj := NewObject(common.BytesToAddress(addr), data, nil)
  48. account := DumpAccount{
  49. Balance: data.Balance.String(),
  50. Nonce: data.Nonce,
  51. Root: common.Bytes2Hex(data.Root[:]),
  52. CodeHash: common.Bytes2Hex(data.CodeHash),
  53. Code: common.Bytes2Hex(obj.Code(self.db)),
  54. Storage: make(map[string]string),
  55. }
  56. storageIt := obj.getTrie(self.db).Iterator()
  57. for storageIt.Next() {
  58. account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
  59. }
  60. dump.Accounts[common.Bytes2Hex(addr)] = account
  61. }
  62. return dump
  63. }
  64. func (self *StateDB) Dump() []byte {
  65. json, err := json.MarshalIndent(self.RawDump(), "", " ")
  66. if err != nil {
  67. fmt.Println("dump err", err)
  68. }
  69. return json
  70. }