From da50c751480da32036f41ccbeb1f292694ca0286 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 6 Aug 2014 09:53:00 +0200 Subject: Added state dump method --- ethstate/dump.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ethstate/dump.go (limited to 'ethstate/dump.go') diff --git a/ethstate/dump.go b/ethstate/dump.go new file mode 100644 index 000000000..2406dfc49 --- /dev/null +++ b/ethstate/dump.go @@ -0,0 +1,47 @@ +package ethstate + +import ( + "encoding/json" + "fmt" + + "github.com/ethereum/eth-go/ethutil" +) + +type Account struct { + Balance string `json:"balance"` + Nonce uint64 `json:"nonce"` + CodeHash string `json:"codeHash"` + Storage map[string]string `json:"storage"` +} + +type World struct { + Root string `json:"root"` + Accounts map[string]Account `json:"accounts"` +} + +func (self *State) Dump() string { + world := World{ + Root: ethutil.Bytes2Hex(self.Trie.Root.([]byte)), + Accounts: make(map[string]Account), + } + + self.Trie.NewIterator().Each(func(key string, value *ethutil.Value) { + stateObject := NewStateObjectFromBytes([]byte(key), value.Bytes()) + + account := Account{Balance: stateObject.Balance.String(), Nonce: stateObject.Nonce, CodeHash: ethutil.Bytes2Hex(stateObject.CodeHash)} + account.Storage = make(map[string]string) + + stateObject.EachStorage(func(key string, value *ethutil.Value) { + value.Decode() + account.Storage[ethutil.Bytes2Hex([]byte(key))] = ethutil.Bytes2Hex(value.Bytes()) + }) + world.Accounts[ethutil.Bytes2Hex([]byte(key))] = account + }) + + json, err := json.MarshalIndent(world, "", " ") + if err != nil { + fmt.Println("dump err", err) + } + + return string(json) +} -- cgit v1.2.3 From 793e666f36e512bceb0d92d0a8dfe1d7c508c0e2 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 17 Aug 2014 12:42:32 +0200 Subject: Dump bytes instead of strings --- ethstate/dump.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ethstate/dump.go') diff --git a/ethstate/dump.go b/ethstate/dump.go index 2406dfc49..be60a05fc 100644 --- a/ethstate/dump.go +++ b/ethstate/dump.go @@ -19,7 +19,7 @@ type World struct { Accounts map[string]Account `json:"accounts"` } -func (self *State) Dump() string { +func (self *State) Dump() []byte { world := World{ Root: ethutil.Bytes2Hex(self.Trie.Root.([]byte)), Accounts: make(map[string]Account), @@ -43,5 +43,5 @@ func (self *State) Dump() string { fmt.Println("dump err", err) } - return string(json) + return json } -- cgit v1.2.3