@@ -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
3337var repllogger = logger .NewLogger ("REPL" )
@@ -38,7 +42,9 @@ type Repl interface {
3842}
3943
4044type 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
5969func (self * JSRepl ) Start () {
@@ -80,7 +90,6 @@ func (self *JSRepl) Start() {
8090func (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+ }
0 commit comments