forked from mirror/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
10 years ago
|
package state
|
||
10 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethutil"
|
||
10 years ago
|
)
|
||
|
|
||
|
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"`
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *State) Dump() []byte {
|
||
10 years ago
|
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())
|
||
|
|
||
10 years ago
|
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.Nonce, CodeHash: ethutil.Bytes2Hex(stateObject.codeHash)}
|
||
10 years ago
|
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)
|
||
|
}
|
||
|
|
||
10 years ago
|
return json
|
||
10 years ago
|
}
|