dump.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package state
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/ethereum/go-ethereum/ethutil"
  6. )
  7. type Account struct {
  8. Balance string `json:"balance"`
  9. Nonce uint64 `json:"nonce"`
  10. Root string `json:"root"`
  11. CodeHash string `json:"codeHash"`
  12. Storage map[string]string `json:"storage"`
  13. }
  14. type World struct {
  15. Root string `json:"root"`
  16. Accounts map[string]Account `json:"accounts"`
  17. }
  18. func (self *StateDB) RawDump() World {
  19. world := World{
  20. Root: ethutil.Bytes2Hex(self.trie.Root()),
  21. Accounts: make(map[string]Account),
  22. }
  23. it := self.trie.Iterator()
  24. for it.Next() {
  25. stateObject := NewStateObjectFromBytes(it.Key, it.Value, self.db)
  26. account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
  27. account.Storage = make(map[string]string)
  28. storageIt := stateObject.State.trie.Iterator()
  29. for storageIt.Next() {
  30. account.Storage[ethutil.Bytes2Hex(storageIt.Key)] = ethutil.Bytes2Hex(storageIt.Value)
  31. }
  32. world.Accounts[ethutil.Bytes2Hex(it.Key)] = account
  33. }
  34. return world
  35. }
  36. func (self *StateDB) Dump() []byte {
  37. json, err := json.MarshalIndent(self.RawDump(), "", " ")
  38. if err != nil {
  39. fmt.Println("dump err", err)
  40. }
  41. return json
  42. }
  43. // Debug stuff
  44. func (self *StateObject) CreateOutputForDiff() {
  45. fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.nonce)
  46. it := self.State.trie.Iterator()
  47. for it.Next() {
  48. fmt.Printf("%x %x\n", it.Key, it.Value)
  49. }
  50. }