Skip to content

Commit f87501b

Browse files
bas-vkBas van Kervel
authored andcommitted
added batch support to console and attach actions
1 parent 3ff272b commit f87501b

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

cmd/geth/js.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,25 @@ func (self *jsre) loadAutoCompletion() {
220220
}
221221
}
222222

223+
func (self *jsre) batch(statement string) {
224+
val, err := self.re.Run(statement)
225+
226+
if err != nil {
227+
fmt.Printf("error: %v", err)
228+
} else if val.IsDefined() && val.IsObject() {
229+
obj, _ := self.re.Get("ret_result")
230+
fmt.Printf("%v", obj)
231+
} else if val.IsDefined() {
232+
fmt.Printf("%v", val)
233+
}
234+
235+
if self.atexit != nil {
236+
self.atexit()
237+
}
238+
239+
self.re.Stop(false)
240+
}
241+
223242
// show summary of current geth instance
224243
func (self *jsre) welcome() {
225244
self.re.Eval(`console.log('instance: ' + web3.version.client);`)

cmd/geth/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
255255
utils.IPCDisabledFlag,
256256
utils.IPCApiFlag,
257257
utils.IPCPathFlag,
258+
utils.ExecFlag,
258259
utils.WhisperEnabledFlag,
259260
utils.VMDebugFlag,
260261
utils.ProtocolVersionFlag,
@@ -337,8 +338,12 @@ func attach(ctx *cli.Context) {
337338
true,
338339
nil)
339340

340-
repl.welcome()
341-
repl.interactive()
341+
if ctx.GlobalString(utils.ExecFlag.Name) != "" {
342+
repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
343+
} else {
344+
repl.welcome()
345+
repl.interactive()
346+
}
342347
}
343348

344349
func console(ctx *cli.Context) {
@@ -368,8 +373,12 @@ func console(ctx *cli.Context) {
368373
nil,
369374
)
370375

371-
repl.welcome()
372-
repl.interactive()
376+
if ctx.GlobalString(utils.ExecFlag.Name) != "" {
377+
repl.batch(ctx.GlobalString(utils.ExecFlag.Name))
378+
} else {
379+
repl.welcome()
380+
repl.interactive()
381+
}
373382

374383
ethereum.Stop()
375384
ethereum.WaitForShutdown()

cmd/utils/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ var (
227227
Usage: "Filename for IPC socket/pipe",
228228
Value: DirectoryString{common.DefaultIpcPath()},
229229
}
230+
ExecFlag = cli.StringFlag{
231+
Name: "exec",
232+
Usage: "Execute javascript statement (only in combination with console/attach)",
233+
}
230234
// Network Settings
231235
MaxPeersFlag = cli.IntFlag{
232236
Name: "maxpeers",

rpc/api/api_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package api
33
import (
44
"testing"
55

6-
"github.com/ethereum/go-ethereum/rpc/codec"
76
"encoding/json"
87
"strconv"
8+
99
"github.com/ethereum/go-ethereum/common/compiler"
10-
"github.com/ethereum/go-ethereum/rpc/shared"
1110
"github.com/ethereum/go-ethereum/eth"
11+
"github.com/ethereum/go-ethereum/rpc/codec"
12+
"github.com/ethereum/go-ethereum/rpc/shared"
1213
"github.com/ethereum/go-ethereum/xeth"
1314
)
1415

@@ -58,11 +59,11 @@ func TestCompileSolidity(t *testing.T) {
5859
t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion)
5960
}
6061
source := `contract test {\n` +
61-
" /// @notice Will multiply `a` by 7." + `\n` +
62-
` function multiply(uint a) returns(uint d) {\n` +
63-
` return a * 7;\n` +
64-
` }\n` +
65-
`}\n`
62+
" /// @notice Will multiply `a` by 7." + `\n` +
63+
` function multiply(uint a) returns(uint d) {\n` +
64+
` return a * 7;\n` +
65+
` }\n` +
66+
`}\n`
6667

6768
jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`
6869

rpc/api/eth.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func (self *ethApi) Sign(req *shared.Request) (interface{}, error) {
250250
return v, nil
251251
}
252252

253-
254253
func (self *ethApi) PushTx(req *shared.Request) (interface{}, error) {
255254
args := new(NewDataArgs)
256255
if err := self.codec.Decode(req.Params, &args); err != nil {

rpc/api/eth_args.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -227,32 +227,32 @@ func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
227227
}
228228

229229
type NewDataArgs struct {
230-
Data string
230+
Data string
231231
}
232232

233233
func (args *NewDataArgs) UnmarshalJSON(b []byte) (err error) {
234-
var obj []interface{}
234+
var obj []interface{}
235235

236-
if err := json.Unmarshal(b, &obj); err != nil {
237-
return shared.NewDecodeParamError(err.Error())
238-
}
236+
if err := json.Unmarshal(b, &obj); err != nil {
237+
return shared.NewDecodeParamError(err.Error())
238+
}
239239

240-
// Check for sufficient params
241-
if len(obj) < 1 {
242-
return shared.NewInsufficientParamsError(len(obj), 1)
243-
}
240+
// Check for sufficient params
241+
if len(obj) < 1 {
242+
return shared.NewInsufficientParamsError(len(obj), 1)
243+
}
244244

245-
data, ok := obj[0].(string)
246-
if !ok {
247-
return shared.NewInvalidTypeError("data", "not a string")
248-
}
249-
args.Data = data
245+
data, ok := obj[0].(string)
246+
if !ok {
247+
return shared.NewInvalidTypeError("data", "not a string")
248+
}
249+
args.Data = data
250250

251-
if len(args.Data) == 0 {
252-
return shared.NewValidationError("data", "is required")
253-
}
251+
if len(args.Data) == 0 {
252+
return shared.NewValidationError("data", "is required")
253+
}
254254

255-
return nil
255+
return nil
256256
}
257257

258258
type NewSigArgs struct {

0 commit comments

Comments
 (0)