1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
package main
import (
"fmt"
"github.com/obscuren/otto"
"os"
"path"
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth"
)
var port = 30300
func testJEthRE(t *testing.T) (repl *jsre, ethereum *eth.Ethereum, err error) {
os.RemoveAll("/tmp/eth/")
err = os.MkdirAll("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/", os.ModePerm)
if err != nil {
t.Errorf("%v", err)
return
}
err = os.MkdirAll("/tmp/eth/data", os.ModePerm)
if err != nil {
t.Errorf("%v", err)
return
}
// FIXME: this does not work ATM
ks := crypto.NewKeyStorePlain("/tmp/eth/keys")
common.WriteFile("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d",
[]byte(`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`))
port++
ethereum, err = eth.New(ð.Config{
DataDir: "/tmp/eth",
AccountManager: accounts.NewManager(ks),
Port: fmt.Sprintf("%d", port),
MaxPeers: 10,
Name: "test",
})
if err != nil {
t.Errorf("%v", err)
return
}
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
repl = newJSRE(ethereum, assetPath)
return
}
func TestNodeInfo(t *testing.T) {
repl, ethereum, err := testJEthRE(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
}
defer ethereum.Stop()
val, err := repl.re.Run("admin.nodeInfo()")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
exp, err := val.Export()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
nodeInfo, ok := exp.(*eth.NodeInfo)
if !ok {
t.Errorf("expected nodeInfo, got %v", err)
}
exp = "test"
got := nodeInfo.Name
if exp != got {
t.Errorf("expected %v, got %v", exp, got)
}
exp = 30301
port := nodeInfo.DiscPort
if exp != port {
t.Errorf("expected %v, got %v", exp, port)
}
exp = 30301
port = nodeInfo.TCPPort
if exp != port {
t.Errorf("expected %v, got %v", exp, port)
}
}
func TestAccounts(t *testing.T) {
repl, ethereum, err := testJEthRE(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
}
defer ethereum.Stop()
val, err := repl.re.Run("eth.coinbase")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
pp, err := repl.re.PrettyPrint(val)
if err != nil {
t.Errorf("%v", err)
}
if !val.IsString() {
t.Errorf("incorrect type, expected string, got %v: %v", val, pp)
}
strVal, _ := val.ToString()
expected := "0xe273f01c99144c438695e10f24926dc1f9fbf62d"
if strVal != expected {
t.Errorf("incorrect result, expected %s, got %v", expected, strVal)
}
val, err = repl.re.Run(`admin.newAccount("password")`)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
addr, err := val.ToString()
if err != nil {
t.Errorf("expected string, got %v", err)
}
val, err = repl.re.Run("eth.accounts")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
exp, err := val.Export()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
addrs, ok := exp.([]string)
if !ok {
t.Errorf("expected []string, got %v", err)
}
if len(addrs) != 2 || (addr != addrs[0][2:] && addr != addrs[1][2:]) {
t.Errorf("expected addrs == [<default>, <new>], got %v (%v)", addrs, addr)
}
}
func TestBlockChain(t *testing.T) {
repl, ethereum, err := testJEthRE(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
}
defer ethereum.Stop()
// should get current block
val0, err := repl.re.Run("admin.dumpBlock()")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
fn := "/tmp/eth/data/blockchain.0"
_, err = repl.re.Run("admin.export(\"" + fn + "\")")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if _, err = os.Stat(fn); err != nil {
t.Errorf("expected no error on file, got %v", err)
}
_, err = repl.re.Run("admin.import(\"" + fn + "\")")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
var val1 otto.Value
// should get current block
val1, err = repl.re.Run("admin.dumpBlock()")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
// FIXME: neither != , nor reflect.DeepEqual works, doing string comparison
v0 := fmt.Sprintf("%v", val0)
v1 := fmt.Sprintf("%v", val1)
if v0 != v1 {
t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0)
}
}
func TestMining(t *testing.T) {
repl, ethereum, err := testJEthRE(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
}
defer ethereum.Stop()
val, err := repl.re.Run("eth.mining")
if err != nil {
t.Errorf("expected no error, got %v", err)
}
var mining bool
mining, err = val.ToBoolean()
if err != nil {
t.Errorf("expected boolean, got %v", err)
}
if mining {
t.Errorf("expected false (not mining), got true")
}
}
func TestRPC(t *testing.T) {
repl, ethereum, err := testJEthRE(t)
if err != nil {
t.Errorf("error creating jsre, got %v", err)
return
}
err = ethereum.Start()
if err != nil {
t.Errorf("error starting ethereum: %v", err)
return
}
defer ethereum.Stop()
val, err := repl.re.Run(`admin.startRPC("127.0.0.1", 5004)`)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
success, _ := val.ToBoolean()
if !success {
t.Errorf("expected true (started), got false")
}
}
|