// also called by admin.contractInfo.get func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, http *docserver.DocServer) (content []byte, err error) { // retrieve contract hash from state codehex := xeth.CodeAt(contractAddress) codeb := xeth.CodeAtBytes(contractAddress) if codehex == "0x" { err = fmt.Errorf("contract (%v) not found", contractAddress) return } codehash := common.BytesToHash(crypto.Sha3(codeb)) // set up nameresolver with natspecreg + urlhint contract addresses res := resolver.New(xeth) // resolve host via HashReg/UrlHint Resolver uri, hash, err := res.KeyToUrl(codehash) if err != nil { return } // get content via http client and authenticate content using hash content, err = http.GetAuthContent(uri, hash) if err != nil { return } return }
func New(xeth *xeth.XEth, tx string, http *docserver.DocServer) (self *NatSpec, err error) { // extract contract address from tx var obj map[string]json.RawMessage err = json.Unmarshal([]byte(tx), &obj) if err != nil { return } var tmp []map[string]string err = json.Unmarshal(obj["params"], &tmp) if err != nil { return } contractAddress := tmp[0]["to"] // retrieve contract hash from state if !xeth.IsContract(contractAddress) { err = fmt.Errorf("NatSpec error: contract not found") return } codehex := xeth.CodeAt(contractAddress) codeHash := common.BytesToHash(crypto.Sha3(common.Hex2Bytes(codehex[2:]))) // parse out host/domain // set up nameresolver with natspecreg + urlhint contract addresses res := resolver.New( xeth, resolver.URLHintContractAddress, resolver.HashRegContractAddress, ) // resolve host via HashReg/UrlHint Resolver uri, hash, err := res.KeyToUrl(codeHash) if err != nil { return } // get content via http client and authenticate content using hash content, err := http.GetAuthContent(uri, hash) if err != nil { return } // get abi, userdoc var obj2 map[string]json.RawMessage err = json.Unmarshal(content, &obj2) if err != nil { return } abi := []byte(obj2["abi"]) userdoc := []byte(obj2["userdoc"]) self, err = NewWithDocs(abi, userdoc, tx) return }
func (self *testFrontend) testResolver() *resolver.Resolver { return resolver.New(self.xeth, resolver.URLHintContractAddress, resolver.HashRegContractAddress) }
func TestContract(t *testing.T) { t.Skip() tmp, repl, ethereum := testJEthRE(t) if err := ethereum.Start(); err != nil { t.Errorf("error starting ethereum: %v", err) return } 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` + ` return a * 7;\n` + ` }\n` + `}\n` checkEvalJSON(t, repl, `admin.contractInfo.stop()`, `true`) 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 solc is found with right version, test it, otherwise read from file sol, err := compiler.New("") if err != nil { t.Logf("solc not found: mocking contract compilation step") } else if sol.Version() != solcVersion { t.Logf("WARNING: solc different version found (%v, test written for %v, may need to update)", sol.Version(), solcVersion) } if err != nil { info, err := ioutil.ReadFile("info_test.json") if err != nil { t.Fatalf("%v", err) } _, err = repl.re.Run(`contract = JSON.parse(` + strconv.Quote(string(info)) + `)`) if err != nil { t.Errorf("%v", err) } } else { checkEvalJSON(t, repl, `contract = eth.compile.solidity(source).test`, string(contractInfo)) } checkEvalJSON(t, repl, `contract.code`, `"0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"`) checkEvalJSON( t, repl, `contractaddress = eth.sendTransaction({from: primary, data: contract.code })`, `"0x5dcaace5982778b409c524873b319667eba5d074"`, ) 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) } // 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) } txc, repl.xeth = repl.xeth.ApplyTestTxs(repl.stateDb, coinbase, txc) 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":"0x5dcaace5982778b409c524873b319667eba5d074","data": "0xc6888fa10000000000000000000000000000000000000000000000000000000000000006"}]}` if repl.lastConfirm != expNotice { t.Errorf("incorrect confirmation message: expected %v, got %v", expNotice, repl.lastConfirm) } var contenthash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"` if sol != nil { modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`)) _ = modContractInfo // contenthash = crypto.Sha3(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) } checkEvalJSON(t, repl, `admin.contractInfo.start()`, `true`) // update state txc, repl.xeth = repl.xeth.ApplyTestTxs(repl.stateDb, coinbase, txc) 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) } }