2015-03-15 08:43:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-25 16:58:52 +02:00
|
|
|
"io/ioutil"
|
2015-04-22 11:59:27 +03:00
|
|
|
"path/filepath"
|
2015-03-15 08:43:48 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2015-04-22 11:59:27 +03:00
|
|
|
"runtime"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-03-15 08:43:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var port = 30300
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
func testJEthRE(t *testing.T) (*jsre, *eth.Ethereum) {
|
|
|
|
tmp, err := ioutil.TempDir("", "geth-test")
|
2015-03-15 08:43:48 +02:00
|
|
|
if err != nil {
|
2015-04-22 11:59:27 +03:00
|
|
|
t.Fatal(err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
defer os.RemoveAll(tmp)
|
2015-03-15 08:43:48 +02:00
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keys"))
|
|
|
|
ethereum, err := eth.New(ð.Config{
|
|
|
|
DataDir: tmp,
|
2015-03-15 08:43:48 +02:00
|
|
|
AccountManager: accounts.NewManager(ks),
|
2015-04-22 11:59:27 +03:00
|
|
|
MaxPeers: 0,
|
2015-03-15 08:43:48 +02:00
|
|
|
Name: "test",
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-04-22 11:59:27 +03:00
|
|
|
t.Fatal("%v", err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
|
2015-04-22 11:59:27 +03:00
|
|
|
repl := newJSRE(ethereum, assetPath, false)
|
|
|
|
return repl, ethereum
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeInfo(t *testing.T) {
|
2015-04-22 11:59:27 +03:00
|
|
|
repl, ethereum := testJEthRE(t)
|
|
|
|
if err := ethereum.Start(); err != nil {
|
|
|
|
t.Fatalf("error starting ethereum: %v", err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
defer ethereum.Stop()
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
want := `{"DiscPort":0,"IP":"0.0.0.0","ListenAddr":"","Name":"test","NodeID":"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","NodeUrl":"enode://00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@0.0.0.0:0","TCPPort":0,"Td":"0"}`
|
|
|
|
checkEvalJSON(t, repl, `admin.nodeInfo()`, want)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccounts(t *testing.T) {
|
2015-04-22 11:59:27 +03:00
|
|
|
repl, ethereum := testJEthRE(t)
|
|
|
|
if err := ethereum.Start(); err != nil {
|
|
|
|
t.Fatalf("error starting ethereum: %v", err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
defer ethereum.Stop()
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
checkEvalJSON(t, repl, `eth.accounts`, `[]`)
|
|
|
|
checkEvalJSON(t, repl, `eth.coinbase`, `"0x"`)
|
2015-03-15 08:43:48 +02:00
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
val, err := repl.re.Run(`admin.newAccount("password")`)
|
2015-03-15 08:43:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
addr := val.String()
|
|
|
|
if !regexp.MustCompile(`0x[0-9a-f]{40}`).MatchString(addr) {
|
|
|
|
t.Errorf("address not hex: %q", addr)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
checkEvalJSON(t, repl, `eth.accounts`, `["` + addr + `"]`)
|
|
|
|
checkEvalJSON(t, repl, `eth.coinbase`, `"` + addr + `"`)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockChain(t *testing.T) {
|
2015-04-22 11:59:27 +03:00
|
|
|
repl, ethereum := testJEthRE(t)
|
|
|
|
if err := ethereum.Start(); err != nil {
|
|
|
|
t.Fatalf("error starting ethereum: %v", err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
defer ethereum.Stop()
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
// get current block dump before export/import.
|
|
|
|
val, err := repl.re.Run("JSON.stringify(admin.debug.dumpBlock())")
|
2015-03-15 08:43:48 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("expected no error, got %v", err)
|
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
beforeExport := val.String()
|
2015-03-15 08:43:48 +02:00
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
// do the export
|
|
|
|
tmp, err := ioutil.TempDir("", "geth-test-export")
|
2015-03-15 08:43:48 +02:00
|
|
|
if err != nil {
|
2015-04-22 11:59:27 +03:00
|
|
|
t.Fatal(err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
tmpfile := filepath.Join(tmp, "export.chain")
|
|
|
|
tmpfileq := strconv.Quote(tmpfile)
|
2015-03-15 08:43:48 +02:00
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
checkEvalJSON(t, repl, `admin.export(` + tmpfileq + `)`, `true`)
|
|
|
|
if _, err := os.Stat(tmpfile); err != nil {
|
|
|
|
t.Fatal(err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
// check import, verify that dumpBlock gives the same result.
|
|
|
|
checkEvalJSON(t, repl, `admin.import(` + tmpfileq + `)`, `true`)
|
|
|
|
checkEvalJSON(t, repl, `admin.debug.dumpBlock()`, beforeExport)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMining(t *testing.T) {
|
2015-04-22 11:59:27 +03:00
|
|
|
repl, ethereum := testJEthRE(t)
|
|
|
|
if err := ethereum.Start(); err != nil {
|
|
|
|
t.Fatalf("error starting ethereum: %v", err)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
defer ethereum.Stop()
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
checkEvalJSON(t, repl, `eth.mining`, `false`)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPC(t *testing.T) {
|
2015-04-22 11:59:27 +03:00
|
|
|
repl, ethereum := testJEthRE(t)
|
|
|
|
if err := ethereum.Start(); err != nil {
|
2015-03-15 08:43:48 +02:00
|
|
|
t.Errorf("error starting ethereum: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer ethereum.Stop()
|
|
|
|
|
2015-04-22 11:59:27 +03:00
|
|
|
checkEvalJSON(t, repl, `admin.startRPC("127.0.0.1", 5004)`, `true`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkEvalJSON(t *testing.T, re *jsre, expr, want string) error {
|
|
|
|
val, err := re.re.Run("JSON.stringify("+ expr + ")")
|
|
|
|
if err == nil && val.String() != want {
|
|
|
|
err = fmt.Errorf("Output mismatch for `%s`:\ngot: %s\nwant: %s", expr, val.String(), want)
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
if err != nil {
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
file = path.Base(file)
|
|
|
|
fmt.Printf("\t%s:%d: %v\n", file, line, err)
|
|
|
|
t.Fail()
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|
2015-04-22 11:59:27 +03:00
|
|
|
return err
|
2015-03-15 08:43:48 +02:00
|
|
|
}
|