dump.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package state
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/ethereum/go-ethereum/common"
  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: common.Bytes2Hex(self.trie.Root()),
  21. Accounts: make(map[string]Account),
  22. }
  23. it := self.trie.Iterator()
  24. for it.Next() {
  25. addr := self.trie.GetKey(it.Key)
  26. stateObject := NewStateObjectFromBytes(common.BytesToAddress(addr), it.Value, self.db)
  27. account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
  28. account.Storage = make(map[string]string)
  29. storageIt := stateObject.trie.Iterator()
  30. for storageIt.Next() {
  31. account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
  32. }
  33. world.Accounts[common.Bytes2Hex(addr)] = account
  34. }
  35. return world
  36. }
  37. func (self *StateDB) Dump() []byte {
  38. json, err := json.MarshalIndent(self.RawDump(), "", " ")
  39. if err != nil {
  40. fmt.Println("dump err", err)
  41. }
  42. return json
  43. }
  44. // Debug stuff
  45. func (self *StateObject) CreateOutputForDiff() {
  46. fmt.Printf("%x %x %x %x\n", self.Address(), self.Root(), self.balance.Bytes(), self.nonce)
  47. it := self.trie.Iterator()
  48. for it.Next() {
  49. fmt.Printf("%x %x\n", it.Key, it.Value)
  50. }
  51. }