Skip to content

Commit 15f491e

Browse files
committed
Clean up REPL
1 parent 5817dab commit 15f491e

7 files changed

Lines changed: 141 additions & 166 deletions

File tree

‎cmd/ethereum/cmd.go‎

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
1-
/*
2-
This file is part of go-ethereum
1+
// Copyright (c) 2013-2014, Jeffrey Wilcke. All rights reserved.
2+
//
3+
// This library is free software; you can redistribute it and/or
4+
// modify it under the terms of the GNU General Public
5+
// License as published by the Free Software Foundation; either
6+
// version 2.1 of the License, or (at your option) any later version.
7+
//
8+
// This library is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this library; if not, write to the Free Software
15+
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16+
// MA 02110-1301 USA
317

4-
go-ethereum is free software: you can redistribute it and/or modify
5-
it under the terms of the GNU General Public License as published by
6-
the Free Software Foundation, either version 3 of the License, or
7-
(at your option) any later version.
8-
9-
go-ethereum is distributed in the hope that it will be useful,
10-
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
GNU General Public License for more details.
13-
14-
You should have received a copy of the GNU General Public License
15-
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16-
*/
17-
/**
18-
* @authors
19-
* Jeffrey Wilcke <i@jev.io>
20-
*/
2118
package main
2219

2320
import (
2421
"io/ioutil"
2522
"os"
2623

27-
"github.com/ethereum/go-ethereum/cmd/ethereum/repl"
28-
"github.com/ethereum/go-ethereum/cmd/utils"
2924
"github.com/ethereum/go-ethereum/eth"
3025
"github.com/ethereum/go-ethereum/javascript"
26+
"github.com/ethereum/go-ethereum/xeth"
3127
)
3228

33-
func InitJsConsole(ethereum *eth.Ethereum) {
34-
repl := ethrepl.NewJSRepl(ethereum)
35-
go repl.Start()
36-
utils.RegisterInterrupt(func(os.Signal) {
37-
repl.Stop()
38-
})
39-
}
40-
4129
func ExecJsFile(ethereum *eth.Ethereum, InputFile string) {
4230
file, err := os.Open(InputFile)
4331
if err != nil {
@@ -47,9 +35,6 @@ func ExecJsFile(ethereum *eth.Ethereum, InputFile string) {
4735
if err != nil {
4836
clilogger.Fatalln(err)
4937
}
50-
re := javascript.NewJSRE(ethereum)
51-
utils.RegisterInterrupt(func(os.Signal) {
52-
re.Stop()
53-
})
38+
re := javascript.NewJSRE(xeth.New(ethereum))
5439
re.Run(string(content))
5540
}

‎cmd/ethereum/flags.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func Init() {
120120
flag.BoolVar(&StartMining, "mine", false, "start mining")
121121
flag.BoolVar(&StartJsConsole, "js", false, "launches javascript console")
122122
flag.BoolVar(&PrintVersion, "version", false, "prints version number")
123+
flag.IntVar(&MinerThreads, "minerthreads", runtime.NumCPU(), "number of miner threads")
123124

124125
// Network stuff
125126
var (

‎cmd/ethereum/main.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"runtime"
2727
"time"
2828

29+
"github.com/ethereum/go-ethereum/cmd/ethereum/repl"
2930
"github.com/ethereum/go-ethereum/cmd/utils"
3031
"github.com/ethereum/go-ethereum/core/types"
3132
"github.com/ethereum/go-ethereum/eth"
@@ -137,7 +138,12 @@ func main() {
137138
}
138139

139140
if StartJsConsole {
140-
InitJsConsole(ethereum)
141+
repl := ethrepl.NewJSRepl(ethereum)
142+
repl.Start()
143+
utils.RegisterInterrupt(func(os.Signal) {
144+
repl.Stop()
145+
})
146+
141147
} else if len(InputFile) > 0 {
142148
ExecJsFile(ethereum, InputFile)
143149
}

‎cmd/ethereum/repl/repl.go‎

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ import (
2424
"os"
2525
"path"
2626

27+
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/eth"
2829
"github.com/ethereum/go-ethereum/ethutil"
2930
"github.com/ethereum/go-ethereum/javascript"
3031
"github.com/ethereum/go-ethereum/logger"
32+
"github.com/ethereum/go-ethereum/state"
33+
"github.com/ethereum/go-ethereum/xeth"
34+
"github.com/obscuren/otto"
3135
)
3236

3337
var repllogger = logger.NewLogger("REPL")
@@ -38,7 +42,9 @@ type Repl interface {
3842
}
3943

4044
type JSRepl struct {
41-
re *javascript.JSRE
45+
re *javascript.JSRE
46+
ethereum *eth.Ethereum
47+
xeth *xeth.XEth
4248

4349
prompt string
4450

@@ -53,7 +59,11 @@ func NewJSRepl(ethereum *eth.Ethereum) *JSRepl {
5359
panic(err)
5460
}
5561

56-
return &JSRepl{re: javascript.NewJSRE(ethereum), prompt: "> ", history: hist}
62+
xeth := xeth.New(ethereum)
63+
repl := &JSRepl{re: javascript.NewJSRE(xeth), xeth: xeth, ethereum: ethereum, prompt: "> ", history: hist}
64+
repl.initStdFuncs()
65+
66+
return repl
5767
}
5868

5969
func (self *JSRepl) Start() {
@@ -80,7 +90,6 @@ func (self *JSRepl) Start() {
8090
func (self *JSRepl) Stop() {
8191
if self.running {
8292
self.running = false
83-
self.re.Stop()
8493
repllogger.Infoln("exit JS Console")
8594
self.history.Close()
8695
}
@@ -101,3 +110,92 @@ func (self *JSRepl) parseInput(code string) {
101110

102111
self.PrintValue(value)
103112
}
113+
114+
func (self *JSRepl) initStdFuncs() {
115+
t, _ := self.re.Vm.Get("eth")
116+
eth := t.Object()
117+
eth.Set("connect", self.connect)
118+
eth.Set("stopMining", self.stopMining)
119+
eth.Set("startMining", self.startMining)
120+
eth.Set("dump", self.dump)
121+
eth.Set("export", self.export)
122+
}
123+
124+
/*
125+
* The following methods are natively implemented javascript functions
126+
*/
127+
128+
func (self *JSRepl) dump(call otto.FunctionCall) otto.Value {
129+
var block *types.Block
130+
131+
if len(call.ArgumentList) > 0 {
132+
if call.Argument(0).IsNumber() {
133+
num, _ := call.Argument(0).ToInteger()
134+
block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num))
135+
} else if call.Argument(0).IsString() {
136+
hash, _ := call.Argument(0).ToString()
137+
block = self.ethereum.ChainManager().GetBlock(ethutil.Hex2Bytes(hash))
138+
} else {
139+
fmt.Println("invalid argument for dump. Either hex string or number")
140+
}
141+
142+
if block == nil {
143+
fmt.Println("block not found")
144+
145+
return otto.UndefinedValue()
146+
}
147+
148+
} else {
149+
block = self.ethereum.ChainManager().CurrentBlock()
150+
}
151+
152+
statedb := state.New(block.Root(), self.ethereum.Db())
153+
154+
v, _ := self.re.Vm.ToValue(statedb.RawDump())
155+
156+
return v
157+
}
158+
159+
func (self *JSRepl) stopMining(call otto.FunctionCall) otto.Value {
160+
self.xeth.Miner().Stop()
161+
162+
return otto.TrueValue()
163+
}
164+
165+
func (self *JSRepl) startMining(call otto.FunctionCall) otto.Value {
166+
self.xeth.Miner().Start()
167+
return otto.TrueValue()
168+
}
169+
170+
func (self *JSRepl) connect(call otto.FunctionCall) otto.Value {
171+
nodeURL, err := call.Argument(0).ToString()
172+
if err != nil {
173+
return otto.FalseValue()
174+
}
175+
if err := self.ethereum.SuggestPeer(nodeURL); err != nil {
176+
return otto.FalseValue()
177+
}
178+
return otto.TrueValue()
179+
}
180+
181+
func (self *JSRepl) export(call otto.FunctionCall) otto.Value {
182+
if len(call.ArgumentList) == 0 {
183+
fmt.Println("err: require file name")
184+
return otto.FalseValue()
185+
}
186+
187+
fn, err := call.Argument(0).ToString()
188+
if err != nil {
189+
fmt.Println(err)
190+
return otto.FalseValue()
191+
}
192+
193+
data := self.ethereum.ChainManager().Export()
194+
195+
if err := ethutil.WriteFile(fn, data); err != nil {
196+
fmt.Println(err)
197+
return otto.FalseValue()
198+
}
199+
200+
return otto.TrueValue()
201+
}

‎eth/protocol.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
ProtocolVersion = 55
18+
ProtocolVersion = 56
1919
NetworkId = 0
2020
ProtocolLength = uint64(8)
2121
ProtocolMaxMsgSize = 10 * 1024 * 1024
@@ -250,6 +250,7 @@ func (self *ethProtocol) handle() error {
250250
return self.protoError(ErrDecode, "msg %v: %v", msg, err)
251251
}
252252
hash := request.Block.Hash()
253+
fmt.Println("received block: %x", hash)
253254
_, chainHead, _ := self.chainManager.Status()
254255

255256
jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{

0 commit comments

Comments
 (0)