Skip to content

Commit 3e6a0fa

Browse files
committed
state,main: add '--nocode' and '--nostorage' to dump cmd
1 parent 4883ce4 commit 3e6a0fa

File tree

8 files changed

+37
-19
lines changed

8 files changed

+37
-19
lines changed

cmd/evm/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func runCmd(ctx *cli.Context) error {
197197

198198
if ctx.GlobalBool(DumpFlag.Name) {
199199
statedb.IntermediateRoot(true)
200-
fmt.Println(string(statedb.Dump()))
200+
fmt.Println(string(statedb.Dump(false, false)))
201201
}
202202

203203
if memProfilePath := ctx.GlobalString(MemProfileFlag.Name); memProfilePath != "" {

cmd/evm/staterunner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func stateTestCmd(ctx *cli.Context) error {
9999
// Test failed, mark as so and dump any state to aid debugging
100100
result.Pass, result.Error = false, err.Error()
101101
if ctx.GlobalBool(DumpFlag.Name) && state != nil {
102-
dump := state.RawDump()
102+
dump := state.RawDump(false, false)
103103
result.State = &dump
104104
}
105105
}

cmd/geth/chaincmd.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ Remove blockchain and state databases`,
136136
utils.CacheFlag,
137137
utils.LightModeFlag,
138138
utils.IterativeOutputFlag,
139+
utils.ExcludeCodeFlag,
140+
utils.ExcludeStorageFlag,
139141
},
140142
Category: "BLOCKCHAIN COMMANDS",
141143
Description: `
@@ -387,10 +389,12 @@ func dump(ctx *cli.Context) error {
387389
if err != nil {
388390
utils.Fatalf("could not create new state: %v", err)
389391
}
392+
excludeCode := ctx.GlobalIsSet(utils.ExcludeCodeFlag.Name)
393+
excludeStorage := ctx.GlobalIsSet(utils.ExcludeStorageFlag.Name)
390394
if ctx.GlobalIsSet(utils.IterativeOutputFlag.Name) {
391-
state.IterativeDump()
395+
state.IterativeDump(excludeCode, excludeStorage)
392396
} else {
393-
fmt.Printf("%s\n", state.Dump())
397+
fmt.Printf("%s\n", state.Dump(excludeCode, excludeStorage))
394398
}
395399
}
396400
}

cmd/geth/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ var (
147147

148148
dumpFlags = []cli.Flag{
149149
utils.IterativeOutputFlag,
150+
utils.ExcludeCodeFlag,
151+
utils.ExcludeStorageFlag,
150152
}
151153
)
152154

cmd/utils/flags.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ var (
168168
Name: "iterative",
169169
Usage: "Print streaming json iteratively as json objects, delimited by lines",
170170
}
171+
ExcludeStorageFlag = cli.BoolFlag{
172+
Name: "nostorage",
173+
Usage: "When dumping state, do not include storage (saves db lookups)",
174+
}
175+
ExcludeCodeFlag = cli.BoolFlag{
176+
Name: "nocode",
177+
Usage: "When dumping state, do not include code (saves db lookups)",
178+
}
171179
defaultSyncMode = eth.DefaultConfig.SyncMode
172180
SyncModeFlag = TextMarshalerFlag{
173181
Name: "syncmode",

core/state/dump.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (self *IterativeDump) onRoot(root common.Hash) {
104104
})
105105
}
106106

107-
func (self *StateDB) performDump(c collector) {
107+
func (self *StateDB) performDump(c collector, excludeCode, excludeStorage bool) {
108108

109109
c.onRoot(self.trie.Hash())
110110

@@ -122,29 +122,33 @@ func (self *StateDB) performDump(c collector) {
122122
Nonce: data.Nonce,
123123
Root: common.Bytes2Hex(data.Root[:]),
124124
CodeHash: common.Bytes2Hex(data.CodeHash),
125-
Code: common.Bytes2Hex(obj.Code(self.db)),
126-
Storage: make(map[string]string),
127125
}
128-
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
129-
for storageIt.Next() {
130-
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
126+
if !excludeCode {
127+
account.Code = common.Bytes2Hex(obj.Code(self.db))
128+
}
129+
if !excludeStorage {
130+
account.Storage = make(map[string]string)
131+
storageIt := trie.NewIterator(obj.getTrie(self.db).NodeIterator(nil))
132+
for storageIt.Next() {
133+
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
134+
}
131135
}
132136
c.onAccount(common.Bytes2Hex(addr), account)
133137
}
134138
}
135139

136140
// RawDump returns the entire state an a single large object
137-
func (self *StateDB) RawDump() Dump {
141+
func (self *StateDB) RawDump(excludeCode, excludeStorage bool) Dump {
138142

139143
dump := newCollectingDump()
140-
self.performDump(dump)
144+
self.performDump(dump, excludeCode, excludeStorage)
141145
return *dump
142146
}
143147

144148
// Dump returns a JSON string representing the entire state as a single json-object
145-
func (self *StateDB) Dump() []byte {
149+
func (self *StateDB) Dump(excludeCode, excludeStorage bool) []byte {
146150
dump := newCollectingDump()
147-
self.performDump(dump)
151+
self.performDump(dump, excludeCode, excludeStorage)
148152
json, err := json.MarshalIndent(dump, "", " ")
149153
if err != nil {
150154
fmt.Println("dump err", err)
@@ -153,6 +157,6 @@ func (self *StateDB) Dump() []byte {
153157
}
154158

155159
// IterativeDump dumps out accounts as json-objects, delimited by linebreaks on stdout
156-
func (self *StateDB) IterativeDump() {
157-
self.performDump(newIterativeDump(os.Stdout))
160+
func (self *StateDB) IterativeDump(excludeCode, excludeStorage bool) {
161+
self.performDump(newIterativeDump(os.Stdout), excludeCode, excludeStorage)
158162
}

core/state/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (s *StateSuite) TestDump(c *checker.C) {
5151
s.state.Commit(false)
5252

5353
// check that dump contains the state objects that are in trie
54-
got := string(s.state.Dump())
54+
got := string(s.state.Dump(false, false))
5555
want := `{
5656
"root": "71edff0130dd2385947095001c73d9e28d862fc286fca2b922ca6f6f3cddfdd2",
5757
"accounts": {

eth/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
310310
// both the pending block as well as the pending state from
311311
// the miner and operate on those
312312
_, stateDb := api.eth.miner.Pending()
313-
return stateDb.RawDump(), nil
313+
return stateDb.RawDump(false, false), nil
314314
}
315315
var block *types.Block
316316
if blockNr == rpc.LatestBlockNumber {
@@ -325,7 +325,7 @@ func (api *PublicDebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error
325325
if err != nil {
326326
return state.Dump{}, err
327327
}
328-
return stateDb.RawDump(), nil
328+
return stateDb.RawDump(false, false), nil
329329
}
330330

331331
// PrivateDebugAPI is the collection of Ethereum full node APIs exposed over

0 commit comments

Comments
 (0)