diff --git a/cmd/geth/admin.go b/cmd/geth/admin.go index 45decfa7f5..20654c0cfd 100644 --- a/cmd/geth/admin.go +++ b/cmd/geth/admin.go @@ -59,6 +59,7 @@ func (js *jsre) adminBindings() { cinfo.Set("stop", js.stopNatSpec) cinfo.Set("newRegistry", js.newRegistry) cinfo.Set("get", js.getContractInfo) + cinfo.Set("saveInfo", js.saveInfo) cinfo.Set("register", js.register) cinfo.Set("registerUrl", js.registerUrl) // cinfo.Set("verify", js.verify) @@ -678,12 +679,18 @@ func (js *jsre) waitForBlocks(call otto.FunctionCall) otto.Value { } if args == 0 { - height = js.xeth.CurrentBlock().Number() - height.Add(height, common.Big1) + height = new(big.Int).Add(js.xeth.CurrentBlock().Number(), common.Big1) } + height, err = waitForBlocks(js.wait, height, timer) + if err != nil { + return otto.UndefinedValue() + } + v, _ := call.Otto.ToValue(height.Uint64()) + return v +} - wait := js.wait - js.wait <- height +func waitForBlocks(wait chan *big.Int, height *big.Int, timer <-chan time.Time) (newHeight *big.Int, err error) { + wait <- height select { case <-timer: // if times out make sure the xeth loop does not block @@ -693,11 +700,10 @@ func (js *jsre) waitForBlocks(call otto.FunctionCall) otto.Value { case <-wait: } }() - return otto.UndefinedValue() - case height = <-wait: + return nil, fmt.Errorf("timeout") + case newHeight = <-wait: } - v, _ := call.Otto.ToValue(height.Uint64()) - return v + return } func (js *jsre) sleep(call otto.FunctionCall) otto.Value { @@ -729,23 +735,49 @@ func (js *jsre) setSolc(call otto.FunctionCall) otto.Value { } func (js *jsre) register(call otto.FunctionCall) otto.Value { - if len(call.ArgumentList) != 4 { - fmt.Println("requires 4 arguments: admin.contractInfo.register(fromaddress, contractaddress, contract, filename)") - return otto.UndefinedValue() + if len(call.ArgumentList) != 3 { + fmt.Println("requires 3 arguments: admin.contractInfo.register(fromaddress, contractaddress, contenthash)") + return otto.FalseValue() } sender, err := call.Argument(0).ToString() if err != nil { fmt.Println(err) - return otto.UndefinedValue() + return otto.FalseValue() } address, err := call.Argument(1).ToString() if err != nil { fmt.Println(err) - return otto.UndefinedValue() + return otto.FalseValue() } - raw, err := call.Argument(2).Export() + contentHashHex, err := call.Argument(2).ToString() + if err != nil { + fmt.Println(err) + return otto.FalseValue() + } + + // sender and contract address are passed as hex strings + codeb := js.xeth.CodeAtBytes(address) + codeHash := common.BytesToHash(crypto.Sha3(codeb)) + contentHash := common.HexToHash(contentHashHex) + registry := resolver.New(js.xeth) + + _, err = registry.RegisterContentHash(common.HexToAddress(sender), codeHash, contentHash) + if err != nil { + fmt.Println(err) + return otto.FalseValue() + } + + return otto.TrueValue() +} + +func (js *jsre) saveInfo(call otto.FunctionCall) otto.Value { + if len(call.ArgumentList) != 2 { + fmt.Println("requires 2 arguments: admin.contractInfo.saveInfo(contract.info, filename)") + return otto.UndefinedValue() + } + raw, err := call.Argument(0).Export() if err != nil { fmt.Println(err) return otto.UndefinedValue() @@ -755,36 +787,20 @@ func (js *jsre) register(call otto.FunctionCall) otto.Value { fmt.Println(err) return otto.UndefinedValue() } - var contract compiler.Contract - err = json.Unmarshal(jsonraw, &contract) + var contractInfo compiler.ContractInfo + err = json.Unmarshal(jsonraw, &contractInfo) if err != nil { fmt.Println(err) return otto.UndefinedValue() } - filename, err := call.Argument(3).ToString() + filename, err := call.Argument(1).ToString() if err != nil { fmt.Println(err) return otto.UndefinedValue() } - contenthash, err := compiler.ExtractInfo(&contract, filename) - if err != nil { - fmt.Println(err) - return otto.UndefinedValue() - } - // sender and contract address are passed as hex strings - codeb := js.xeth.CodeAtBytes(address) - codehash := common.BytesToHash(crypto.Sha3(codeb)) - - if err != nil { - fmt.Println(err) - return otto.UndefinedValue() - } - - registry := resolver.New(js.xeth) - - _, err = registry.RegisterContentHash(common.HexToAddress(sender), codehash, contenthash) + contenthash, err := compiler.SaveInfo(&contractInfo, filename) if err != nil { fmt.Println(err) return otto.UndefinedValue() @@ -796,7 +812,7 @@ func (js *jsre) register(call otto.FunctionCall) otto.Value { func (js *jsre) registerUrl(call otto.FunctionCall) otto.Value { if len(call.ArgumentList) != 3 { - fmt.Println("requires 3 arguments: admin.contractInfo.register(fromaddress, contenthash, filename)") + fmt.Println("requires 3 arguments: admin.contractInfo.registerUrl(fromaddress, contenthash, url)") return otto.FalseValue() } sender, err := call.Argument(0).ToString() @@ -818,7 +834,6 @@ func (js *jsre) registerUrl(call otto.FunctionCall) otto.Value { } registry := resolver.New(js.xeth) - _, err = registry.RegisterUrl(common.HexToAddress(sender), common.HexToHash(contenthash), url) if err != nil { fmt.Println(err) @@ -830,7 +845,7 @@ func (js *jsre) registerUrl(call otto.FunctionCall) otto.Value { func (js *jsre) getContractInfo(call otto.FunctionCall) otto.Value { if len(call.ArgumentList) != 1 { - fmt.Println("requires 1 argument: admin.contractInfo.register(contractaddress)") + fmt.Println("requires 1 argument: admin.contractInfo.get(contractaddress)") return otto.FalseValue() } addr, err := call.Argument(0).ToString() diff --git a/cmd/geth/js.go b/cmd/geth/js.go index 24daf6f583..216c5e3500 100644 --- a/cmd/geth/js.go +++ b/cmd/geth/js.go @@ -85,6 +85,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool js.xeth = xeth.New(ethereum, f) js.wait = js.xeth.UpdateState() js.re = re.New(libPath) + // js.apiBindings(js.xeth) js.apiBindings(f) js.adminBindings() if bzzEnabled { @@ -113,6 +114,7 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, bzzEnabled bool func (js *jsre) apiBindings(f xeth.Frontend) { xe := xeth.New(js.ethereum, f) + // func (js *jsre) apiBindings(xe *xeth.XEth) { ethApi := rpc.NewEthereumApi(xe) jeth := rpc.NewJeth(ethApi, js.re) diff --git a/cmd/geth/js_test.go b/cmd/geth/js_test.go index d6d4e05fd0..95a9b97afc 100644 --- a/cmd/geth/js_test.go +++ b/cmd/geth/js_test.go @@ -3,12 +3,14 @@ package main import ( "fmt" "io/ioutil" + "math/big" "os" "path/filepath" "regexp" "runtime" "strconv" "testing" + "time" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/common" @@ -17,7 +19,6 @@ import ( "github.com/ethereum/go-ethereum/common/natspec" "github.com/ethereum/go-ethereum/common/resolver" "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth" ) @@ -41,7 +42,6 @@ var ( type testjethre struct { *jsre - stateDb *state.StateDB lastConfirm string ds *docserver.DocServer } @@ -79,6 +79,7 @@ func testJEthRE(t *testing.T) (string, *testjethre, *eth.Ethereum) { MaxPeers: 0, Name: "test", SolcPath: testSolcPath, + PowTest: true, }) if err != nil { t.Fatal("%v", err) @@ -101,19 +102,12 @@ 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") ds := docserver.New("/") - tf := &testjethre{ds: ds, stateDb: ethereum.ChainManager().State().Copy()} + tf := &testjethre{ds: ds} repl := newJSRE(ethereum, assetPath, "", false, "", false, tf) tf.jsre = repl return tmp, tf, ethereum } -// this line below is needed for transaction to be applied to the state in testing -// the heavy lifing is done in XEth.ApplyTestTxs -// this is fragile, overwriting xeth will result in -// process leaking since xeth loops cannot quit safely -// should be replaced by proper mining with testDAG for easy full integration tests -// txc, self.xeth = self.xeth.ApplyTestTxs(self.xeth.repl.stateDb, coinbase, txc) - func TestNodeInfo(t *testing.T) { t.Skip("broken after p2p update") tmp, repl, ethereum := testJEthRE(t) @@ -257,12 +251,9 @@ func TestContract(t *testing.T) { defer ethereum.Stop() defer os.RemoveAll(tmp) - var txc uint64 coinbase := common.HexToAddress(testAddress) resolver.New(repl.xeth).CreateContracts(coinbase) - // time.Sleep(1000 * time.Millisecond) - // checkEvalJSON(t, repl, `eth.getBlock("pending", true).transactions.length`, `2`) source := `contract test {\n` + " /// @notice Will multiply `a` by 7." + `\n` + ` function multiply(uint a) returns(uint d) {\n` + @@ -270,14 +261,20 @@ func TestContract(t *testing.T) { ` }\n` + `}\n` - checkEvalJSON(t, repl, `admin.contractInfo.stop()`, `true`) + if checkEvalJSON(t, repl, `admin.contractInfo.stop()`, `true`) != nil { + return + } contractInfo, err := ioutil.ReadFile("info_test.json") if err != nil { t.Fatalf("%v", err) } - checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"`+testAddress+`"`) - checkEvalJSON(t, repl, `source = "`+source+`"`, `"`+source+`"`) + if checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"`+testAddress+`"`) != nil { + return + } + if checkEvalJSON(t, repl, `source = "`+source+`"`, `"`+source+`"`) != nil { + return + } // if solc is found with right version, test it, otherwise read from file sol, err := compiler.New("") @@ -297,71 +294,156 @@ func TestContract(t *testing.T) { t.Errorf("%v", err) } } else { - checkEvalJSON(t, repl, `contract = eth.compile.solidity(source).test`, string(contractInfo)) + if checkEvalJSON(t, repl, `contract = eth.compile.solidity(source).test`, string(contractInfo)) != nil { + return + } } - checkEvalJSON(t, repl, `contract.code`, `"0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"`) + if checkEvalJSON(t, repl, `contract.code`, `"0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"`) != nil { + return + } - checkEvalJSON( + if checkEvalJSON( t, repl, - `contractaddress = eth.sendTransaction({from: primary, data: contract.code })`, + `contractaddress = eth.sendTransaction({from: primary, data: contract.code, gasPrice:"1000", gas:"100000", amount:100 })`, `"0x291293d57e0a0ab47effe97c02577f90d9211567"`, - ) + ) != nil { + return + } + + if !processTxs(repl, t, 7) { + return + } callSetup := `abiDef = JSON.parse('[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]'); Multiply7 = eth.contract(abiDef); multiply7 = Multiply7.at(contractaddress); ` - // time.Sleep(1500 * time.Millisecond) _, err = repl.re.Run(callSetup) if err != nil { t.Errorf("unexpected error setting up contract, got %v", err) + return } - // checkEvalJSON(t, repl, `eth.getBlock("pending", true).transactions.length`, `3`) - - // why is this sometimes failing? - // checkEvalJSON(t, repl, `multiply7.multiply.call(6)`, `42`) expNotice := "" if repl.lastConfirm != expNotice { t.Errorf("incorrect confirmation message: expected %v, got %v", expNotice, repl.lastConfirm) + return } - txc, repl.xeth = repl.xeth.ApplyTestTxs(repl.stateDb, coinbase, txc) + if checkEvalJSON(t, repl, `admin.contractInfo.start()`, `true`) != nil { + return + } + if checkEvalJSON(t, repl, `multiply7.multiply.sendTransaction(6, { from: primary })`, `undefined`) != nil { + return + } + + if !processTxs(repl, t, 1) { + return + } - checkEvalJSON(t, repl, `admin.contractInfo.start()`, `true`) - checkEvalJSON(t, repl, `multiply7.multiply.sendTransaction(6, { from: primary, gas: "1000000", gasPrice: "100000" })`, `undefined`) expNotice = `About to submit transaction (no NatSpec info found for contract: content hash not found for '0x87e2802265838c7f14bb69eecd2112911af6767907a702eeaa445239fb20711b'): {"params":[{"to":"0x291293d57e0a0ab47effe97c02577f90d9211567","data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"}]}` if repl.lastConfirm != expNotice { - t.Errorf("incorrect confirmation message: expected %v, got %v", expNotice, repl.lastConfirm) + t.Errorf("incorrect confirmation message: expected\n%v, got\n%v", expNotice, repl.lastConfirm) + return } - var contenthash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"` - if sol != nil { + var contentHash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"` + if sol != nil && solcVersion != sol.Version() { modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`)) - _ = modContractInfo - // contenthash = crypto.Sha3(modContractInfo) + fmt.Printf("modified contractinfo:\n%s\n", modContractInfo) + contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"` } - checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) - checkEvalJSON(t, repl, `contenthash = admin.contractInfo.register(primary, contractaddress, contract, filename)`, contenthash) - checkEvalJSON(t, repl, `admin.contractInfo.registerUrl(primary, contenthash, "file://"+filename)`, `true`) - if err != nil { - t.Errorf("unexpected error registering, got %v", err) + if checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) != nil { + return + } + if checkEvalJSON(t, repl, `contentHash = admin.contractInfo.saveInfo(contract.info, filename)`, contentHash) != nil { + return + } + if checkEvalJSON(t, repl, `admin.contractInfo.register(primary, contractaddress, contentHash)`, `true`) != nil { + return + } + if checkEvalJSON(t, repl, `admin.contractInfo.registerUrl(primary, contentHash, "file://"+filename)`, `true`) != nil { + return } - checkEvalJSON(t, repl, `admin.contractInfo.start()`, `true`) + if checkEvalJSON(t, repl, `admin.contractInfo.start()`, `true`) != nil { + return + } - // update state - txc, repl.xeth = repl.xeth.ApplyTestTxs(repl.stateDb, coinbase, txc) + if !processTxs(repl, t, 3) { + return + } + + if checkEvalJSON(t, repl, `multiply7.multiply.sendTransaction(6, { from: primary })`, `undefined`) != nil { + return + } + + if !processTxs(repl, t, 1) { + return + } - checkEvalJSON(t, repl, `multiply7.multiply.sendTransaction(6, { from: primary, gas: "1000000", gasPrice: "100000" })`, `undefined`) expNotice = "Will multiply 6 by 7." if repl.lastConfirm != expNotice { - t.Errorf("incorrect confirmation message: expected %v, got %v", expNotice, repl.lastConfirm) + t.Errorf("incorrect confirmation message: expected\n%v, got\n%v", expNotice, repl.lastConfirm) + return } } +func pendingTransactions(repl *testjethre, t *testing.T) (txc int64, err error) { + txs := repl.ethereum.TxPool().GetTransactions() + return int64(len(txs)), nil +} + +func processTxs(repl *testjethre, t *testing.T, expTxc int) bool { + var txc int64 + var err error + for i := 0; i < 50; i++ { + txc, err = pendingTransactions(repl, t) + if err != nil { + t.Errorf("unexpected error checking pending transactions: %v", err) + return false + } + if expTxc < int(txc) { + t.Errorf("too many pending transactions: expected %v, got %v", expTxc, txc) + return false + } else if expTxc == int(txc) { + break + } + time.Sleep(100 * time.Millisecond) + } + if int(txc) != expTxc { + t.Errorf("incorrect number of pending transactions, expected %v, got %v", expTxc, txc) + return false + } + + err = repl.ethereum.StartMining(runtime.NumCPU()) + if err != nil { + t.Errorf("unexpected error mining: %v", err) + return false + } + defer repl.ethereum.StopMining() + + timer := time.NewTimer(100 * time.Second) + height := new(big.Int).Add(repl.xeth.CurrentBlock().Number(), big.NewInt(1)) + _, err = waitForBlocks(repl.wait, height, timer.C) + if err != nil { + t.Errorf("error mining transactions: %v", err) + return false + } + txc, err = pendingTransactions(repl, t) + if err != nil { + t.Errorf("unexpected error checking pending transactions: %v", err) + return false + } + if txc != 0 { + t.Errorf("%d trasactions were not mined", txc) + return false + } + return true +} + func checkEvalJSON(t *testing.T, re *testjethre, expr, want string) error { val, err := re.re.Run("JSON.stringify(" + expr + ")") if err == nil && val.String() != want { diff --git a/common/compiler/solidity.go b/common/compiler/solidity.go index caf86974e5..4d5ffb473e 100644 --- a/common/compiler/solidity.go +++ b/common/compiler/solidity.go @@ -185,12 +185,12 @@ func (sol *Solidity) Compile(source string) (contracts map[string]*Contract, err return } -func ExtractInfo(contract *Contract, filename string) (contenthash common.Hash, err error) { - contractInfo, err := json.Marshal(contract.Info) +func SaveInfo(info *ContractInfo, filename string) (contenthash common.Hash, err error) { + infojson, err := json.Marshal(info) if err != nil { return } - contenthash = common.BytesToHash(crypto.Sha3(contractInfo)) - err = ioutil.WriteFile(filename, contractInfo, 0600) + contenthash = common.BytesToHash(crypto.Sha3(infojson)) + err = ioutil.WriteFile(filename, infojson, 0600) return } diff --git a/common/compiler/solidity_test.go b/common/compiler/solidity_test.go index 46f733e59d..7864283c65 100644 --- a/common/compiler/solidity_test.go +++ b/common/compiler/solidity_test.go @@ -72,19 +72,15 @@ func TestNoCompiler(t *testing.T) { } } -func TestExtractInfo(t *testing.T) { - var cinfo ContractInfo - err := json.Unmarshal([]byte(info), &cinfo) +func TestSaveInfo(t *testing.T) { + var cinfo *ContractInfo + err := json.Unmarshal([]byte(info), cinfo) if err != nil { t.Errorf("%v", err) } - contract := &Contract{ - Code: "", - Info: cinfo, - } filename := "/tmp/solctest.info.json" os.Remove(filename) - cinfohash, err := ExtractInfo(contract, filename) + cinfohash, err := SaveInfo(cinfo, filename) if err != nil { t.Errorf("error extracting info: %v", err) } diff --git a/common/resolver/resolver.go b/common/resolver/resolver.go index c45ee19828..b2400fe57e 100644 --- a/common/resolver/resolver.go +++ b/common/resolver/resolver.go @@ -36,8 +36,8 @@ var ( const ( txValue = "0" - txGas = "100000" - txGasPrice = "1000000000000" + txGas = "" + txGasPrice = "" ) func abiSignature(s string) string { @@ -284,7 +284,7 @@ func (self *Resolver) RegisterUrl(address common.Address, hash common.Hash, url // registers key -> conenthash in HashReg and // registers contenthash -> url in UrlHint // creates 3 transaction using address as sender -// transactions need to obe mined to take effect +// transactions need to be mined to take effect func (self *Resolver) RegisterAddrWithUrl(address common.Address, codehash, dochash common.Hash, url string) (txh string, err error) { _, err = self.RegisterContentHash(address, codehash, dochash) diff --git a/core/transaction_pool.go b/core/transaction_pool.go index c896488d1e..879c329249 100644 --- a/core/transaction_pool.go +++ b/core/transaction_pool.go @@ -231,6 +231,9 @@ func (tp *TxPool) GetTransaction(hash common.Hash) *types.Transaction { } func (self *TxPool) GetTransactions() (txs types.Transactions) { + self.checkQueue() + self.validatePool() + self.mu.RLock() defer self.mu.RUnlock() diff --git a/eth/backend.go b/eth/backend.go index cdac2a8b8f..17c9da829c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -290,6 +290,7 @@ func New(config *Config) (*Ethereum, error) { } if config.PowTest { + glog.V(logger.Info).Infof("ethash used in test mode") eth.pow, err = ethash.NewForTesting() if err != nil { return nil, err