aboutsummaryrefslogtreecommitdiffstats
path: root/ethstate/dump.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-08-06 15:53:00 +0800
committerobscuren <geffobscura@gmail.com>2014-08-06 15:53:00 +0800
commitda50c751480da32036f41ccbeb1f292694ca0286 (patch)
treed83eb84c684d0c86d4fcdc20b8acb525b8f923de /ethstate/dump.go
parent4edf7cfb0543555921a79f572c749615c4997ea7 (diff)
downloaddexon-da50c751480da32036f41ccbeb1f292694ca0286.tar
dexon-da50c751480da32036f41ccbeb1f292694ca0286.tar.gz
dexon-da50c751480da32036f41ccbeb1f292694ca0286.tar.bz2
dexon-da50c751480da32036f41ccbeb1f292694ca0286.tar.lz
dexon-da50c751480da32036f41ccbeb1f292694ca0286.tar.xz
dexon-da50c751480da32036f41ccbeb1f292694ca0286.tar.zst
dexon-da50c751480da32036f41ccbeb1f292694ca0286.zip
Added state dump method
Diffstat (limited to 'ethstate/dump.go')
-rw-r--r--ethstate/dump.go47
1 files changed, 47 insertions, 0 deletions
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)
+}