当前位置: 首页>>代码示例>>Golang>>正文


Golang compiler.New函数代码示例

本文整理汇总了Golang中github.com/ethereum/go-ethereum/common/compiler.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了New函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Solc

func (self *Ethereum) Solc() (*compiler.Solidity, error) {
	var err error
	if self.solc == nil {
		self.solc, err = compiler.New(self.SolcPath)
	}
	return self.solc, err
}
开发者ID:nilcons-contrib,项目名称:go-ethereum,代码行数:7,代码来源:backend.go

示例2: TestCompileSolidity

func TestCompileSolidity(t *testing.T) {
	t.Skip()

	solc, err := compiler.New("")
	if solc == nil {
		t.Skip("no solidity compiler")
	}
	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`

	jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`

	//expCode := "605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056"
	expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]`
	expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}`
	expDeveloperDoc := `{"methods":{}}`
	expCompilerVersion := `0.9.13`
	expLanguage := "Solidity"
	expLanguageVersion := "0"
	expSource := source

	api := NewEthereumApi(&xeth.XEth{})

	var req RpcRequest
	json.Unmarshal([]byte(jsonstr), &req)

	var response interface{}
	err = api.GetRequestReply(&req, &response)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}
	respjson, err := json.Marshal(response)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	var contract = compiler.Contract{}
	err = json.Unmarshal(respjson, &contract)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	/*
		if contract.Code != expCode {
			t.Errorf("Expected %s got %s", expCode, contract.Code)
		}
	*/
	if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
		t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
	}
	if contract.Info.Language != expLanguage {
		t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
	}
	if contract.Info.LanguageVersion != expLanguageVersion {
		t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
	}
	if contract.Info.CompilerVersion != expCompilerVersion {
		t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
	}

	userdoc, err := json.Marshal(contract.Info.UserDoc)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	abidef, err := json.Marshal(contract.Info.AbiDefinition)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	if string(abidef) != expAbiDefinition {
		t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
	}
	ioutil.WriteFile("/tmp/abidef", []byte(string(abidef)), 0700)
	ioutil.WriteFile("/tmp/expabidef", []byte(expAbiDefinition), 0700)

	if string(userdoc) != expUserDoc {
		t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
	}

	if string(devdoc) != expDeveloperDoc {
		t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
	}
}
开发者ID:hiroshi1tanaka,项目名称:gethkey,代码行数:93,代码来源:api_test.go

示例3: TestContract

func TestContract(t *testing.T) {
	t.Skip("contract testing is implemented with mining in ethash test mode. This takes about 7seconds to run. Unskip and run on demand")
	coinbase := common.HexToAddress(testAddress)
	tmp, repl, ethereum := testREPL(t, func(conf *eth.Config) {
		conf.Etherbase = coinbase
		conf.PowTest = true
	})
	if err := ethereum.Start(); err != nil {
		t.Errorf("error starting ethereum: %v", err)
		return
	}
	defer ethereum.Stop()
	defer os.RemoveAll(tmp)

	reg := registrar.New(repl.xeth)
	_, err := reg.SetGlobalRegistrar("", coinbase)
	if err != nil {
		t.Errorf("error setting HashReg: %v", err)
	}
	_, err = reg.SetHashReg("", coinbase)
	if err != nil {
		t.Errorf("error setting HashReg: %v", err)
	}
	_, err = reg.SetUrlHint("", coinbase)
	if err != nil {
		t.Errorf("error setting HashReg: %v", err)
	}
	/* TODO:
	* lookup receipt and contract addresses by tx hash
	* name registration for HashReg and UrlHint addresses
	* mine those transactions
	* then set once more SetHashReg SetUrlHint
	 */

	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`

	if checkEvalJSON(t, repl, `admin.stopNatSpec()`, `true`) != nil {
		return
	}

	contractInfo, err := ioutil.ReadFile("info_test.json")
	if err != nil {
		t.Fatalf("%v", err)
	}
	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("")
	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 {
		if checkEvalJSON(t, repl, `contract = eth.compile.solidity(source).test`, string(contractInfo)) != nil {
			return
		}
	}

	if checkEvalJSON(t, repl, `contract.code`, `"0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"`) != nil {
		return
	}

	if checkEvalJSON(
		t, repl,
		`contractaddress = eth.sendTransaction({from: primary, data: contract.code})`,
		`"0x46d69d55c3c4b86a924a92c9fc4720bb7bce1d74"`,
	) != nil {
		return
	}

	if !processTxs(repl, t, 8) {
		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);
`
	_, err = repl.re.Run(callSetup)
//.........这里部分代码省略.........
开发者ID:ruflin,项目名称:go-ethereum,代码行数:101,代码来源:js_test.go

示例4: TestContract

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)
//.........这里部分代码省略.........
开发者ID:ssonneborn22,项目名称:go-ethereum,代码行数:101,代码来源:js_test.go

示例5: main

func main() {
	// Parse and ensure all needed inputs are specified
	flag.Parse()

	if *abiFlag == "" && *solFlag == "" {
		fmt.Printf("No contract ABI (--abi) or Solidity source (--sol) specified\n")
		os.Exit(-1)
	} else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && *solFlag != "" {
		fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity source (--sol) flag\n")
		os.Exit(-1)
	}
	if *pkgFlag == "" {
		fmt.Printf("No destination Go package specified (--pkg)\n")
		os.Exit(-1)
	}
	// If the entire solidity code was specified, build and bind based on that
	var (
		abis  []string
		bins  []string
		types []string
	)
	if *solFlag != "" {
		// Generate the list of types to exclude from binding
		exclude := make(map[string]bool)
		for _, kind := range strings.Split(*excFlag, ",") {
			exclude[strings.ToLower(kind)] = true
		}
		// Build the Solidity source into bindable components
		solc, err := compiler.New(*solcFlag)
		if err != nil {
			fmt.Printf("Failed to locate Solidity compiler: %v\n", err)
			os.Exit(-1)
		}
		source, err := ioutil.ReadFile(*solFlag)
		if err != nil {
			fmt.Printf("Failed to read Soldity source code: %v\n", err)
			os.Exit(-1)
		}
		contracts, err := solc.Compile(string(source))
		if err != nil {
			fmt.Printf("Failed to build Solidity contract: %v\n", err)
			os.Exit(-1)
		}
		// Gather all non-excluded contract for binding
		for name, contract := range contracts {
			if exclude[strings.ToLower(name)] {
				continue
			}
			abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
			abis = append(abis, string(abi))
			bins = append(bins, contract.Code)
			types = append(types, name)
		}
	} else {
		// Otherwise load up the ABI, optional bytecode and type name from the parameters
		abi, err := ioutil.ReadFile(*abiFlag)
		if err != nil {
			fmt.Printf("Failed to read input ABI: %v\n", err)
			os.Exit(-1)
		}
		abis = append(abis, string(abi))

		bin := []byte{}
		if *binFlag != "" {
			if bin, err = ioutil.ReadFile(*binFlag); err != nil {
				fmt.Printf("Failed to read input bytecode: %v\n", err)
				os.Exit(-1)
			}
		}
		bins = append(bins, string(bin))

		kind := *typFlag
		if kind == "" {
			kind = *pkgFlag
		}
		types = append(types, kind)
	}
	// Generate the contract binding
	code, err := bind.Bind(types, abis, bins, *pkgFlag)
	if err != nil {
		fmt.Printf("Failed to generate ABI binding: %v\n", err)
		os.Exit(-1)
	}
	// Either flush it out to a file or display on the standard output
	if *outFlag == "" {
		fmt.Printf("%s\n", code)
		return
	}
	if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
		fmt.Printf("Failed to write ABI binding: %v\n", err)
		os.Exit(-1)
	}
}
开发者ID:Raskal8,项目名称:go-ethereum,代码行数:93,代码来源:main.go

示例6: TestCompileSolidity

func TestCompileSolidity(t *testing.T) {

	solc, err := compiler.New("")
	if solc == nil {
		t.Skip("no solc found: skip")
	} else if solc.Version() != solcVersion {
		t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion)
	}
	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`

	jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`

	expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
	expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]`
	expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}`
	expDeveloperDoc := `{"methods":{}}`
	expCompilerVersion := solc.Version()
	expLanguage := "Solidity"
	expLanguageVersion := "0"
	expSource := source

	eth := &eth.Ethereum{}
	xeth := xeth.NewTest(eth, nil)
	api := NewEthApi(xeth, eth, codec.JSON)

	var rpcRequest shared.Request
	json.Unmarshal([]byte(jsonstr), &rpcRequest)

	response, err := api.CompileSolidity(&rpcRequest)
	if err != nil {
		t.Errorf("Execution failed, %v", err)
	}

	respjson, err := json.Marshal(response)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	var contracts = make(map[string]*compiler.Contract)
	err = json.Unmarshal(respjson, &contracts)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	if len(contracts) != 1 {
		t.Errorf("expected one contract, got %v", len(contracts))
	}

	contract := contracts["test"]

	if contract.Code != expCode {
		t.Errorf("Expected \n%s got \n%s", expCode, contract.Code)
	}

	if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
		t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
	}

	if contract.Info.Language != expLanguage {
		t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
	}

	if contract.Info.LanguageVersion != expLanguageVersion {
		t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
	}

	if contract.Info.CompilerVersion != expCompilerVersion {
		t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
	}

	userdoc, err := json.Marshal(contract.Info.UserDoc)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	abidef, err := json.Marshal(contract.Info.AbiDefinition)
	if err != nil {
		t.Errorf("expected no error, got %v", err)
	}

	if string(abidef) != expAbiDefinition {
		t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
	}

	if string(userdoc) != expUserDoc {
		t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
	}

	if string(devdoc) != expDeveloperDoc {
		t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
//.........这里部分代码省略.........
开发者ID:j4ustin,项目名称:go-ethereum,代码行数:101,代码来源:api_test.go


注:本文中的github.com/ethereum/go-ethereum/common/compiler.New函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。