dump.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 *State) Dump() []byte {
  19. world := World{
  20. Root: ethutil.Bytes2Hex(self.Trie.GetRoot()),
  21. Accounts: make(map[string]Account),
  22. }
  23. self.Trie.NewIterator().Each(func(key string, value *ethutil.Value) {
  24. stateObject := NewStateObjectFromBytes([]byte(key), value.Bytes())
  25. account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.Nonce, Root: ethutil.Bytes2Hex(stateObject.Root()), CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
  26. account.Storage = make(map[string]string)
  27. stateObject.EachStorage(func(key string, value *ethutil.Value) {
  28. value.Decode()
  29. account.Storage[ethutil.Bytes2Hex([]byte(key))] = ethutil.Bytes2Hex(value.Bytes())
  30. })
  31. world.Accounts[ethutil.Bytes2Hex([]byte(key))] = account
  32. })
  33. json, err := json.MarshalIndent(world, "", " ")
  34. if err != nil {
  35. fmt.Println("dump err", err)
  36. }
  37. return json
  38. }
  39. // Debug stuff
  40. func (self *StateObject) CreateOutputForDiff() {
  41. fmt.Printf("%x %x %x %x\n", self.Address(), self.State.Root(), self.balance.Bytes(), self.Nonce)
  42. self.EachStorage(func(addr string, value *ethutil.Value) {
  43. fmt.Printf("%x %x\n", addr, value.Bytes())
  44. })
  45. }