This commit is contained in:
zsfelfoldi 2015-06-02 16:56:10 +02:00
commit 2f404edffc
4 changed files with 86 additions and 10 deletions

View file

@ -220,7 +220,7 @@ func (js *jsre) httpLoadScript(call otto.FunctionCall) otto.Value {
fmt.Println(err) fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()
} }
script, err := ds.Get(uri, "") script, err := js.ds.Get(uri, "")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.FalseValue() return otto.FalseValue()
@ -251,7 +251,7 @@ func (js *jsre) httpGet(call otto.FunctionCall) otto.Value {
return otto.UndefinedValue() return otto.UndefinedValue()
} }
} }
resp, err := ds.Get(uri, path) resp, err := js.ds.Get(uri, path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.UndefinedValue() return otto.UndefinedValue()
@ -854,7 +854,7 @@ func (js *jsre) getContractInfo(call otto.FunctionCall) otto.Value {
return otto.FalseValue() return otto.FalseValue()
} }
infoDoc, err := natspec.FetchDocsForContract(addr, js.xeth, ds) infoDoc, err := natspec.FetchDocsForContract(addr, js.xeth, js.ds)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return otto.UndefinedValue() return otto.UndefinedValue()

View file

@ -65,6 +65,7 @@ func (r dumbterm) PasswordPrompt(p string) (string, error) {
func (r dumbterm) AppendHistory(string) {} func (r dumbterm) AppendHistory(string) {}
type jsre struct { type jsre struct {
ds *docserver.DocServer
re *re.JSRE re *re.JSRE
ethereum *eth.Ethereum ethereum *eth.Ethereum
xeth *xeth.XEth xeth *xeth.XEth
@ -82,6 +83,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool
if f == nil { if f == nil {
f = js f = js
} }
js.ds = docserver.New("/")
js.xeth = xeth.New(ethereum, f) js.xeth = xeth.New(ethereum, f)
js.wait = js.xeth.UpdateState() js.wait = js.xeth.UpdateState()
js.re = re.New(libPath) js.re = re.New(libPath)
@ -90,7 +92,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool
js.adminBindings() js.adminBindings()
if bzzEnabled { if bzzEnabled {
// register the swarm rountripper with the bzz scheme on the docserver // register the swarm rountripper with the bzz scheme on the docserver
ds.RegisterProtocol("bzz", &bzz.RoundTripper{Port: bzzPort}) js.ds.RegisterProtocol("bzz", &bzz.RoundTripper{Port: bzzPort})
// swarm js bindings // swarm js bindings
bzz.NewJSApi(js.re, js.ethereum.Swarm) bzz.NewJSApi(js.re, js.ethereum.Swarm)
js.ethereum.Swarm.Resolver = resolver.New(xeth.New(ethereum, f)) js.ethereum.Swarm.Resolver = resolver.New(xeth.New(ethereum, f))
@ -156,11 +158,9 @@ var net = web3.net;
js.re.Eval(resolver.GlobalRegistrar + "registrar = GlobalRegistrar.at(\"" + resolver.GlobalRegistrarAddr + "\");") js.re.Eval(resolver.GlobalRegistrar + "registrar = GlobalRegistrar.at(\"" + resolver.GlobalRegistrarAddr + "\");")
} }
var ds = docserver.New("/")
func (self *jsre) ConfirmTransaction(tx string) bool { func (self *jsre) ConfirmTransaction(tx string) bool {
if self.ethereum.NatSpec { if self.ethereum.NatSpec {
notice := natspec.GetNotice(self.xeth, tx, ds) notice := natspec.GetNotice(self.xeth, tx, self.ds)
fmt.Println(notice) fmt.Println(notice)
answer, _ := self.Prompt("Confirm Transaction [y/n]") answer, _ := self.Prompt("Confirm Transaction [y/n]")
return strings.HasPrefix(strings.Trim(answer, " "), "y") return strings.HasPrefix(strings.Trim(answer, " "), "y")

68
cmd/geth/js_bzz_test.go Normal file
View file

@ -0,0 +1,68 @@
package main
import (
// "io/ioutil"
"os"
// "path"
// "runtime"
"testing"
// "github.com/ethereum/go-ethereum/bzz"
// "github.com/ethereum/go-ethereum/common/resolver"
"github.com/ethereum/go-ethereum/eth"
)
func bzzREPL(t *testing.T, port string) (string, *testjethre, *eth.Ethereum) {
return testREPL(t, func(c *eth.Config) {
c.Bzz = true
c.BzzPort = port
})
}
func TestBzzUploadDownload(t *testing.T) {
tmp, repl, ethereum := bzzREPL(t, "")
if err := ethereum.Start(); err != nil {
t.Fatalf("error starting ethereum: %v", err)
}
defer ethereum.Stop()
defer os.RemoveAll(tmp)
_ = repl
}
func TestBzzPutGet(t *testing.T) {
tmp, repl, ethereum := bzzREPL(t, "")
if err := ethereum.Start(); err != nil {
t.Fatalf("error starting ethereum: %v", err)
}
defer ethereum.Stop()
defer os.RemoveAll(tmp)
if checkEvalJSON(t, repl, `hash = bzz.put("console.log(\"hello from console\")", "application/javascript")`, `"97f1b7c7ea12468fd37c262383b9aa862d0cfbc4fc7218652374679fc5cf40cd"`) != nil {
return
}
want := `{"content":"console.log(\"hello from console\")","contentType":"application/javascript","size":"33","status":"0"}`
if checkEvalJSON(t, repl, `bzz.get(hash)`, want) != nil {
return
}
}
// the server can be initialized only once per test session !
// until we implement a stoppable http server
// further http tests will need to make sure the correct server is running
func TestHTTP(t *testing.T) {
tmp, repl, ethereum := bzzREPL(t, "8500")
if err := ethereum.Start(); err != nil {
t.Fatalf("error starting ethereum: %v", err)
}
defer ethereum.Stop()
defer os.RemoveAll(tmp)
if checkEvalJSON(t, repl, `hash = bzz.put("6*7", "application/javascript")`, `"97f1b7c7ea12468fd37c262383b9aa862d0cfbc4fc7218652374679fc5cf40cd"`) != nil {
return
}
if checkEvalJSON(t, repl, `http.get("bzz://"+hash)`, `"6*7"`) != nil {
return
}
if checkEvalJSON(t, repl, `http.loadScript("bzz://"+hash)`, `42`) != nil {
return
}
}

View file

@ -62,6 +62,10 @@ func (self *testjethre) ConfirmTransaction(tx string) bool {
} }
func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) {
return testREPL(t, nil)
}
func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *eth.Ethereum) {
tmp, err := ioutil.TempDir("", "geth-test") tmp, err := ioutil.TempDir("", "geth-test")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -72,7 +76,7 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) {
ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keystore")) ks := crypto.NewKeyStorePlain(filepath.Join(tmp, "keystore"))
am := accounts.NewManager(ks) am := accounts.NewManager(ks)
ethereum, err := eth.New(&eth.Config{ conf := &eth.Config{
NodeKey: testNodeKey, NodeKey: testNodeKey,
DataDir: tmp, DataDir: tmp,
AccountManager: am, AccountManager: am,
@ -80,7 +84,11 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) {
Name: "test", Name: "test",
SolcPath: testSolcPath, SolcPath: testSolcPath,
PowTest: true, PowTest: true,
}) }
if config != nil {
config(conf)
}
ethereum, err := eth.New(conf)
if err != nil { if err != nil {
t.Fatal("%v", err) t.Fatal("%v", err)
} }
@ -103,7 +111,7 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) {
assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
ds := docserver.New("/") ds := docserver.New("/")
tf := &testjethre{ds: ds} tf := &testjethre{ds: ds}
repl := newJSRE(ethereum, assetPath, "", false, "", false, tf) repl := newJSRE(ethereum, assetPath, "", conf.Bzz, conf.BzzPort, false, tf)
tf.jsre = repl tf.jsre = repl
return tmp, tf, ethereum return tmp, tf, ethereum
} }