From ecbed5901a84ce41f0b29bcaf78da50ab4f698e3 Mon Sep 17 00:00:00 2001 From: bxq2011hust Date: Thu, 4 Jan 2024 17:19:26 +0800 Subject: [PATCH] abigen support smbin flag and remove useless code --- v3/abi/bind/bind.go | 3 +- v3/abi/bind/template.go | 25 +- v3/cmd/abigen/main.go | 23 +- v3/cmd/commandline/cns.go | 94 ------ v3/cmd/commandline/commands.go | 6 +- v3/examples/hello_world/wrapper/HelloWorld.go | 21 +- v3/examples/parallelok/parallelOk.go | 24 +- v3/precompiled/cns/cns.go | 269 ------------------ v3/precompiled/cns/cns_service.go | 162 ----------- v3/precompiled/cns/cns_service_test.go | 172 ----------- 10 files changed, 87 insertions(+), 712 deletions(-) delete mode 100644 v3/cmd/commandline/cns.go delete mode 100644 v3/precompiled/cns/cns.go delete mode 100644 v3/precompiled/cns/cns_service.go delete mode 100644 v3/precompiled/cns/cns_service_test.go diff --git a/v3/abi/bind/bind.go b/v3/abi/bind/bind.go index a07b60c2..846f0422 100644 --- a/v3/abi/bind/bind.go +++ b/v3/abi/bind/bind.go @@ -48,7 +48,7 @@ const ( // to be used as is in client code, but rather as an intermediate struct which // enforces compile time type safety and naming convention opposed to having to // manually maintain hard coded strings that break on runtime. -func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, smcrypto bool) (string, error) { +func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string]string, pkg string, lang Lang, libs map[string]string, aliases map[string]string, smcrypto bool, smBytecodes []string) (string, error) { var ( // contracts is the map of each individual contract requested binding contracts = make(map[string]*tmplContract) @@ -170,6 +170,7 @@ func Bind(types []string, abis []string, bytecodes []string, fsigs []map[string] Type: capitalise(types[i]), InputABI: strings.Replace(strippedABI, "\"", "\\\"", -1), InputBin: strings.TrimPrefix(strings.TrimSpace(bytecodes[i]), "0x"), + InputSMBin: strings.TrimPrefix(strings.TrimSpace(smBytecodes[i]), "0x"), Constructor: evmABI.Constructor, Calls: calls, Transacts: transacts, diff --git a/v3/abi/bind/template.go b/v3/abi/bind/template.go index 5e0e7f1b..44634b8d 100644 --- a/v3/abi/bind/template.go +++ b/v3/abi/bind/template.go @@ -31,6 +31,7 @@ type tmplContract struct { Type string // Type name of the main contract binding InputABI string // JSON ABI used as the input to generate the binding from InputBin string // Optional EVM bytecode used to denetare deploy code from + InputSMBin string // Optional EVM bytecode used to denetare deploy code from FuncSigs map[string]string // Optional map: string signature -> 4-byte signature Constructor abi.Method // Contract constructor for deploy parametrization Calls map[string]*tmplMethod // Contract calls that only read state data @@ -87,6 +88,7 @@ const tmplSourceGo = ` package {{.Package}} import ( + "fmt" "math/big" "strings" @@ -131,6 +133,7 @@ var ( {{if .InputBin}} // {{.Type}}Bin is the compiled bytecode used for deploying new contracts. var {{.Type}}Bin = "0x{{.InputBin}}" + var {{.Type}}SMBin = "0x{{.InputSMBin}}" // Deploy{{.Type}} deploys a new contract, binding an instance of {{.Type}} to it. func Deploy{{.Type}}(auth *bind.TransactOpts, backend bind.ContractBackend {{range .Constructor.Inputs}}, {{.Name}} {{bindtype .Type $structs}}{{end}}) (common.Address, *types.Receipt, *{{.Type}}, error) { @@ -142,7 +145,16 @@ var ( {{decapitalise $name}}Addr, _, _, _ := Deploy{{capitalise $name}}(auth, backend) {{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1) {{end}} - address, receipt, contract, err := bind.DeployContract(auth, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex({{.Type}}SMBin) + } else { + bytecode = common.FromHex({{.Type}}Bin) + } + if len(bytecode) == 0 { + return common.Address{}, nil, nil, fmt.Errorf("cannot deploy empty bytecode") + } + address, receipt, contract, err := bind.DeployContract(auth, parsed, bytecode, backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) if err != nil { return common.Address{}, nil, nil, err } @@ -158,7 +170,16 @@ var ( {{decapitalise $name}}Addr, _, _, _ := AsyncDeploy{{capitalise $name}}(auth, backend) {{$contract.Type}}Bin = strings.Replace({{$contract.Type}}Bin, "__${{$pattern}}$__", {{decapitalise $name}}Addr.String()[2:], -1) {{end}} - tx, err := bind.AsyncDeployContract(auth, handler, parsed, common.FromHex({{.Type}}Bin), backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex({{.Type}}SMBin) + } else { + bytecode = common.FromHex({{.Type}}Bin) + } + if len(bytecode) == 0 { + return nil, fmt.Errorf("cannot deploy empty bytecode") + } + tx, err := bind.AsyncDeployContract(auth, handler, parsed, bytecode, backend {{range .Constructor.Inputs}}, {{.Name}}{{end}}) if err != nil { return nil, err } diff --git a/v3/cmd/abigen/main.go b/v3/cmd/abigen/main.go index e4972124..d0b7b17f 100644 --- a/v3/cmd/abigen/main.go +++ b/v3/cmd/abigen/main.go @@ -45,11 +45,15 @@ var ( // Flags needed by abigen abiFlag = &cli.StringFlag{ Name: "abi", - Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN", + Usage: "Path to the Solidity contract ABI json to bind, - for STDIN", } binFlag = &cli.StringFlag{ Name: "bin", - Usage: "Path to the Ethereum contract bytecode (generate deploy method)", + Usage: "Path to the Solidity contract bytecode (generate deploy method)", + } + smBinFlag = &cli.StringFlag{ + Name: "smbin", + Usage: "Path to the Solidity contract sm crypto bytecode (generate deploy method)", } typeFlag = &cli.StringFlag{ Name: "type", @@ -101,6 +105,7 @@ func init() { langFlag, aliasFlag, cryptoFlag, + smBinFlag, } app.Action = abigen } @@ -127,6 +132,7 @@ func abigen(c *cli.Context) error { var ( abis []string bins []string + smBins []string types []string sigs []map[string]string libs = make(map[string]string) @@ -160,6 +166,17 @@ func abigen(c *cli.Context) error { } bins = append(bins, string(bin)) + var smBin []byte + if smBinFile := c.String(smBinFlag.Name); smBinFile != "" { + if smBin, err = os.ReadFile(smBinFile); err != nil { + utils.Fatalf("Failed to read input sm crypto bytecode: %v", err) + } + if strings.Contains(string(smBin), "//") { + utils.Fatalf("Contract has additional library references, please use other mode(e.g. --combined-json) to catch library infos") + } + } + smBins = append(smBins, string(smBin)) + kind := c.String(typeFlag.Name) if kind == "" { kind = c.String(pkgFlag.Name) @@ -232,7 +249,7 @@ func abigen(c *cli.Context) error { } } // Generate the contract binding - code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases, smcrypto) + code, err := bind.Bind(types, abis, bins, sigs, c.String(pkgFlag.Name), lang, libs, aliases, smcrypto, smBins) if err != nil { utils.Fatalf("Failed to generate ABI binding: %v", err) } diff --git a/v3/cmd/commandline/cns.go b/v3/cmd/commandline/cns.go deleted file mode 100644 index 1cd25a70..00000000 --- a/v3/cmd/commandline/cns.go +++ /dev/null @@ -1,94 +0,0 @@ -package commandline - -import ( - "fmt" - - "github.com/FISCO-BCOS/go-sdk/v3/precompiled/cns" - "github.com/spf13/cobra" -) - -var queryCNS = &cobra.Command{ - Use: "queryCNS", - Short: "[name] [version] Query CNS information by contract name and contract version", - Long: `Query CNS information by contract name and contract version. -Arguments: - [name]: string - [version]: string - -For example: - - [queryCNS] [HelloWorld] [1.0] - -For more information please refer: - - https://fisco-bcos-documentation.readthedocs.io/zh_CN/latest/docs/manual/console.html#querycns`, - Args: cobra.RangeArgs(1, 2), - Run: func(cmd *cobra.Command, args []string) { - var contractName string - var version string - var infos []cns.Info - var err error - cnsService, err := cns.NewCnsService(RPC) - if err != nil { - fmt.Printf("queryCNS failed, cns.NewCnsService err: %v\n", err) - return - } - contractName = args[0] - if len(args) == 1 { - infos, err = cnsService.QueryCnsByName(contractName) - } else { - version = args[1] - infos, err = cnsService.QueryCnsByNameAndVersion(contractName, version) - } - if err != nil { - fmt.Printf("queryCNS failed, cnsService.QueryCnsByName or cnsService.QueryCnsByNameAndVersion err: %v\n", err) - return - } - if len(args) == 1 && len(infos) == 0 { - fmt.Printf("contract %v is not existed\n", args[0]) - } else if len(args) == 2 && len(infos) == 0 { - fmt.Printf("contract %v, version %v is not existed\n", args[0], args[1]) - } - // abi info is not printed - for i := 0; i < len(infos); i++ { - fmt.Println("name: " + infos[i].Name + ", version: " + infos[i].Version + ", address: " + infos[i].Address) - } - }, -} - -var getAddressByContractNameAndVersion = &cobra.Command{ - Use: "getAddressByContractNameAndVersion", - Short: "[name] [version] Get address by contract name and version", - Long: `Get address by contract name and version. -Arguments: - [name]: string - [version]: string - -For example: - - [getAddressByContractNameAndVersion] [HelloWorld] [1.0] -`, - Args: cobra.ExactArgs(2), - Run: func(cmd *cobra.Command, args []string) { - var contractName string - var version string - var err error - cnsService, err := cns.NewCnsService(RPC) - if err != nil { - fmt.Printf("getAddressByContractNameAndVersion failed, cns.NewCnsService err: %v\n", err) - return - } - contractName = args[0] - version = args[1] - address, err := cnsService.GetAddressByContractNameAndVersion(contractName, version) - if err != nil { - fmt.Printf("getAddressByContractNameAndVersion failed, cnsService.GetAddressByContractNameAndVersion err: %v\n", err) - return - } - fmt.Println(address.Hex()) - }, -} - -func init() { - rootCmd.AddCommand(queryCNS, getAddressByContractNameAndVersion) -} diff --git a/v3/cmd/commandline/commands.go b/v3/cmd/commandline/commands.go index 18e287e2..b054ea95 100644 --- a/v3/cmd/commandline/commands.go +++ b/v3/cmd/commandline/commands.go @@ -676,9 +676,9 @@ func init() { // will be global for your application. // rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is the project directory ./config.ini)") - rootCmd.Flags().StringVarP(&cfgFile, "privateKeyPath", "p", "", "private key file path of pem format") - rootCmd.Flags().BoolVarP(&smCrypto, "smCrypto", "s", false, "use smCrypto or not, default is false") - rootCmd.Flags().BoolVarP(&disableSsl, "disableSsl", "d", false, "switch off ssl or not, default use ssl") + rootCmd.PersistentFlags().StringVarP(&cfgFile, "privateKeyPath", "p", "", "private key file path of pem format") + rootCmd.PersistentFlags().BoolVarP(&smCrypto, "smCrypto", "s", false, "use smCrypto or not, default is false") + rootCmd.PersistentFlags().BoolVarP(&disableSsl, "disableSsl", "d", false, "switch off ssl or not, default use ssl") rootCmd.PersistentFlags().StringVarP(&groupID, "groupID", "g", "group0", "groupID of FISCO BCOS chain") rootCmd.PersistentFlags().StringVarP(&nodeEndpoint, "nodeEndpoint", "n", "127.0.0.1:20200", "node endpoint, default is 127.0.0.1:20200") rootCmd.PersistentFlags().StringVarP(&certPath, "certPath", "c", "./conf", "cert path, default is ./conf, should contain ca.crt, sdk.crt, sdk.key") diff --git a/v3/examples/hello_world/wrapper/HelloWorld.go b/v3/examples/hello_world/wrapper/HelloWorld.go index 8f854f3a..7a48b946 100644 --- a/v3/examples/hello_world/wrapper/HelloWorld.go +++ b/v3/examples/hello_world/wrapper/HelloWorld.go @@ -30,14 +30,22 @@ const HelloWorldABI = "[{\"inputs\":[{\"internalType\":\"string\",\"name\":\"ini // HelloWorldBin is the compiled bytecode used for deploying new contracts. var HelloWorldBin = "0x60806040523480156200001157600080fd5b50604051620009fd380380620009fd8339818101604052810190620000379190620002ac565b80600090805190602001906200004f9291906200005f565b5060006001819055505062000362565b8280546200006d906200032c565b90600052602060002090601f016020900481019282620000915760008555620000dd565b82601f10620000ac57805160ff1916838001178555620000dd565b82800160010185558215620000dd579182015b82811115620000dc578251825591602001919060010190620000bf565b5b509050620000ec9190620000f0565b5090565b5b808211156200010b576000816000905550600101620000f1565b5090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b62000178826200012d565b810181811067ffffffffffffffff821117156200019a57620001996200013e565b5b80604052505050565b6000620001af6200010f565b9050620001bd82826200016d565b919050565b600067ffffffffffffffff821115620001e057620001df6200013e565b5b620001eb826200012d565b9050602081019050919050565b60005b8381101562000218578082015181840152602081019050620001fb565b8381111562000228576000848401525b50505050565b6000620002456200023f84620001c2565b620001a3565b90508281526020810184848401111562000264576200026362000128565b5b62000271848285620001f8565b509392505050565b600082601f83011262000291576200029062000123565b5b8151620002a38482602086016200022e565b91505092915050565b600060208284031215620002c557620002c462000119565b5b600082015167ffffffffffffffff811115620002e657620002e56200011e565b5b620002f48482850162000279565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200034557607f821691505b602082108114156200035c576200035b620002fd565b5b50919050565b61068b80620003726000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80634ed3885e1461004657806354fd4d50146100765780636d4ce63c14610094575b600080fd5b610060600480360381019061005b9190610387565b6100b2565b60405161006d919061046d565b60405180910390f35b61007e6101dd565b60405161008b91906104a8565b60405180910390f35b61009c6101e3565b6040516100a9919061046d565b60405180910390f35b606060008080546100c2906104f2565b80601f01602080910402602001604051908101604052809291908181526020018280546100ee906104f2565b801561013b5780601f106101105761010080835404028352916020019161013b565b820191906000526020600020905b81548152906001019060200180831161011e57829003601f168201915b50505050509050838360009190610153929190610275565b50600180546101629190610553565b6001819055503373ffffffffffffffffffffffffffffffffffffffff163273ffffffffffffffffffffffffffffffffffffffff167fc3bf5911f8e0476e774566ef3fa1259f04156ba5c61ea5ff35c0201390381f9686866001546040516101cb93929190610623565b60405180910390a38091505092915050565b60015481565b6060600080546101f2906104f2565b80601f016020809104026020016040519081016040528092919081815260200182805461021e906104f2565b801561026b5780601f106102405761010080835404028352916020019161026b565b820191906000526020600020905b81548152906001019060200180831161024e57829003601f168201915b5050505050905090565b828054610281906104f2565b90600052602060002090601f0160209004810192826102a357600085556102ea565b82601f106102bc57803560ff19168380011785556102ea565b828001600101855582156102ea579182015b828111156102e95782358255916020019190600101906102ce565b5b5090506102f791906102fb565b5090565b5b808211156103145760008160009055506001016102fc565b5090565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f84011261034757610346610322565b5b8235905067ffffffffffffffff81111561036457610363610327565b5b6020830191508360018202830111156103805761037f61032c565b5b9250929050565b6000806020838503121561039e5761039d610318565b5b600083013567ffffffffffffffff8111156103bc576103bb61031d565b5b6103c885828601610331565b92509250509250929050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561040e5780820151818401526020810190506103f3565b8381111561041d576000848401525b50505050565b6000601f19601f8301169050919050565b600061043f826103d4565b61044981856103df565b93506104598185602086016103f0565b61046281610423565b840191505092915050565b600060208201905081810360008301526104878184610434565b905092915050565b6000819050919050565b6104a28161048f565b82525050565b60006020820190506104bd6000830184610499565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061050a57607f821691505b6020821081141561051e5761051d6104c3565b5b50919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061055e8261048f565b91506105698361048f565b9250817f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038313600083121516156105a4576105a3610524565b5b817f80000000000000000000000000000000000000000000000000000000000000000383126000831216156105dc576105db610524565b5b828201905092915050565b82818337600083830152505050565b600061060283856103df565b935061060f8385846105e7565b61061883610423565b840190509392505050565b6000604082019050818103600083015261063e8185876105f6565b905061064d6020830184610499565b94935050505056fea2646970667358221220f474bd1d28e84751caca4356bb3cca5453b846289fe3aed4ecbc8cd022fb484464736f6c634300080b0033" +// HelloWorldSMBin is the compiled sm crypto bytecode used for deploying new contracts. +var HelloWorldSMBin = "0x60806040523480156200001157600080fd5b50604051620009fd380380620009fd8339818101604052810190620000379190620002ac565b80600090805190602001906200004f9291906200005f565b5060006001819055505062000362565b8280546200006d906200032c565b90600052602060002090601f016020900481019282620000915760008555620000dd565b82601f10620000ac57805160ff1916838001178555620000dd565b82800160010185558215620000dd579182015b82811115620000dc578251825591602001919060010190620000bf565b5b509050620000ec9190620000f0565b5090565b5b808211156200010b576000816000905550600101620000f1565b5090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b62000178826200012d565b810181811067ffffffffffffffff821117156200019a57620001996200013e565b5b80604052505050565b6000620001af6200010f565b9050620001bd82826200016d565b919050565b600067ffffffffffffffff821115620001e057620001df6200013e565b5b620001eb826200012d565b9050602081019050919050565b60005b8381101562000218578082015181840152602081019050620001fb565b8381111562000228576000848401525b50505050565b6000620002456200023f84620001c2565b620001a3565b90508281526020810184848401111562000264576200026362000128565b5b62000271848285620001f8565b509392505050565b600082601f83011262000291576200029062000123565b5b8151620002a38482602086016200022e565b91505092915050565b600060208284031215620002c557620002c462000119565b5b600082015167ffffffffffffffff811115620002e657620002e56200011e565b5b620002f48482850162000279565b91505092915050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200034557607f821691505b602082108114156200035c576200035b620002fd565b5b50919050565b61068b80620003726000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063299f7f9d146100465780633590b49f14610064578063ad56e1dc14610094575b600080fd5b61004e6100b2565b60405161005b91906103b1565b60405180910390f35b61007e60048036038101906100799190610442565b610144565b60405161008b91906103b1565b60405180910390f35b61009c61026f565b6040516100a991906104a8565b60405180910390f35b6060600080546100c1906104f2565b80601f01602080910402602001604051908101604052809291908181526020018280546100ed906104f2565b801561013a5780601f1061010f5761010080835404028352916020019161013a565b820191906000526020600020905b81548152906001019060200180831161011d57829003601f168201915b5050505050905090565b60606000808054610154906104f2565b80601f0160208091040260200160405190810160405280929190818152602001828054610180906104f2565b80156101cd5780601f106101a2576101008083540402835291602001916101cd565b820191906000526020600020905b8154815290600101906020018083116101b057829003601f168201915b505050505090508383600091906101e5929190610275565b50600180546101f49190610553565b6001819055503373ffffffffffffffffffffffffffffffffffffffff163273ffffffffffffffffffffffffffffffffffffffff167f292b41e0d55affa2665ff1997c27450654b980f63cba971653b8c113829478ff868660015460405161025d93929190610623565b60405180910390a38091505092915050565b60015481565b828054610281906104f2565b90600052602060002090601f0160209004810192826102a357600085556102ea565b82601f106102bc57803560ff19168380011785556102ea565b828001600101855582156102ea579182015b828111156102e95782358255916020019190600101906102ce565b5b5090506102f791906102fb565b5090565b5b808211156103145760008160009055506001016102fc565b5090565b600081519050919050565b600082825260208201905092915050565b60005b83811015610352578082015181840152602081019050610337565b83811115610361576000848401525b50505050565b6000601f19601f8301169050919050565b600061038382610318565b61038d8185610323565b935061039d818560208601610334565b6103a681610367565b840191505092915050565b600060208201905081810360008301526103cb8184610378565b905092915050565b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b60008083601f840112610402576104016103dd565b5b8235905067ffffffffffffffff81111561041f5761041e6103e2565b5b60208301915083600182028301111561043b5761043a6103e7565b5b9250929050565b60008060208385031215610459576104586103d3565b5b600083013567ffffffffffffffff811115610477576104766103d8565b5b610483858286016103ec565b92509250509250929050565b6000819050919050565b6104a28161048f565b82525050565b60006020820190506104bd6000830184610499565b92915050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061050a57607f821691505b6020821081141561051e5761051d6104c3565b5b50919050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600061055e8261048f565b91506105698361048f565b9250817f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038313600083121516156105a4576105a3610524565b5b817f80000000000000000000000000000000000000000000000000000000000000000383126000831216156105dc576105db610524565b5b828201905092915050565b82818337600083830152505050565b60006106028385610323565b935061060f8385846105e7565b61061883610367565b840190509392505050565b6000604082019050818103600083015261063e8185876105f6565b905061064d6020830184610499565b94935050505056fea264697066735822122038302801bb6b6822b2bd3c1004fd5a921a20673a852170b48171e828e6fec02664736f6c634300080b0033" + // DeployHelloWorld deploys a new contract, binding an instance of HelloWorld to it. func DeployHelloWorld(auth *bind.TransactOpts, backend bind.ContractBackend, initValue string) (common.Address, *types.Receipt, *HelloWorld, error) { parsed, err := abi.JSON(strings.NewReader(HelloWorldABI)) if err != nil { return common.Address{}, nil, nil, err } - - address, receipt, contract, err := bind.DeployContract(auth, parsed, common.FromHex(HelloWorldBin), backend, initValue) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex(HelloWorldSMBin) + } else { + bytecode = common.FromHex(HelloWorldBin) + } + address, receipt, contract, err := bind.DeployContract(auth, parsed, bytecode, backend, initValue) if err != nil { return common.Address{}, nil, nil, err } @@ -49,8 +57,13 @@ func AsyncDeployHelloWorld(auth *bind.TransactOpts, handler func(*types.Receipt, if err != nil { return nil, err } - - tx, err := bind.AsyncDeployContract(auth, handler, parsed, common.FromHex(HelloWorldBin), backend, initValue) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex(HelloWorldSMBin) + } else { + bytecode = common.FromHex(HelloWorldBin) + } + tx, err := bind.AsyncDeployContract(auth, handler, parsed, bytecode, backend, initValue) if err != nil { return nil, err } diff --git a/v3/examples/parallelok/parallelOk.go b/v3/examples/parallelok/parallelOk.go index db65e09b..671ab990 100644 --- a/v3/examples/parallelok/parallelOk.go +++ b/v3/examples/parallelok/parallelOk.go @@ -4,6 +4,7 @@ package main import ( + "fmt" "math/big" "strings" @@ -29,6 +30,7 @@ const TransferABI = "[{\"conflictFields\":[{\"kind\":3,\"slot\":0,\"value\":[0]} // TransferBin is the compiled bytecode used for deploying new contracts. var TransferBin = "0x608060405234801561001057600080fd5b50610679806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c806335ee5f87146100515780638a42ebe9146100815780639b80b0501461009d578063fad42f87146100b9575b600080fd5b61006b60048036038101906100669190610369565b6100d5565b60405161007891906103cb565b60405180910390f35b61009b60048036038101906100969190610412565b6100fc565b005b6100b760048036038101906100b2919061046e565b610123565b005b6100d360048036038101906100ce919061046e565b610192565b005b600080826040516100e69190610573565b9081526020016040518091039020549050919050565b8060008360405161010d9190610573565b9081526020016040518091039020819055505050565b806000846040516101349190610573565b9081526020016040518091039020600082825461015191906105b9565b92505081905550806000836040516101699190610573565b9081526020016040518091039020600082825461018691906105ed565b92505081905550505050565b806000846040516101a39190610573565b908152602001604051809103902060008282546101c091906105b9565b92505081905550806000836040516101d89190610573565b908152602001604051809103902060008282546101f591906105ed565b92505081905550606481111561020a57600080fd5b505050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102768261022d565b810181811067ffffffffffffffff821117156102955761029461023e565b5b80604052505050565b60006102a861020f565b90506102b4828261026d565b919050565b600067ffffffffffffffff8211156102d4576102d361023e565b5b6102dd8261022d565b9050602081019050919050565b82818337600083830152505050565b600061030c610307846102b9565b61029e565b90508281526020810184848401111561032857610327610228565b5b6103338482856102ea565b509392505050565b600082601f8301126103505761034f610223565b5b81356103608482602086016102f9565b91505092915050565b60006020828403121561037f5761037e610219565b5b600082013567ffffffffffffffff81111561039d5761039c61021e565b5b6103a98482850161033b565b91505092915050565b6000819050919050565b6103c5816103b2565b82525050565b60006020820190506103e060008301846103bc565b92915050565b6103ef816103b2565b81146103fa57600080fd5b50565b60008135905061040c816103e6565b92915050565b6000806040838503121561042957610428610219565b5b600083013567ffffffffffffffff8111156104475761044661021e565b5b6104538582860161033b565b9250506020610464858286016103fd565b9150509250929050565b60008060006060848603121561048757610486610219565b5b600084013567ffffffffffffffff8111156104a5576104a461021e565b5b6104b18682870161033b565b935050602084013567ffffffffffffffff8111156104d2576104d161021e565b5b6104de8682870161033b565b92505060406104ef868287016103fd565b9150509250925092565b600081519050919050565b600081905092915050565b60005b8381101561052d578082015181840152602081019050610512565b8381111561053c576000848401525b50505050565b600061054d826104f9565b6105578185610504565b935061056781856020860161050f565b80840191505092915050565b600061057f8284610542565b915081905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006105c4826103b2565b91506105cf836103b2565b9250828210156105e2576105e161058a565b5b828203905092915050565b60006105f8826103b2565b9150610603836103b2565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156106385761063761058a565b5b82820190509291505056fea26469706673582212209c3a15397c5fc2d0668cca2ef1bce80e3b8124697ac44a54a13f74f9e30961d164736f6c634300080b0033" +var TransferSMBin = "0x608060405234801561001057600080fd5b50610679806100206000396000f3fe608060405234801561001057600080fd5b506004361061004c5760003560e01c8063612d2bff14610051578063ab71bf091461006d578063cd93c25d14610089578063f2f4ee6d146100b9575b600080fd5b61006b6004803603810190610066919061039f565b6100d5565b005b6100876004803603810190610082919061039f565b610144565b005b6100a3600480360381019061009e919061042a565b6101c1565b6040516100b09190610482565b60405180910390f35b6100d360048036038101906100ce919061049d565b6101e8565b005b806000846040516100e69190610573565b9081526020016040518091039020600082825461010391906105b9565b925050819055508060008360405161011b9190610573565b9081526020016040518091039020600082825461013891906105ed565b92505081905550505050565b806000846040516101559190610573565b9081526020016040518091039020600082825461017291906105b9565b925050819055508060008360405161018a9190610573565b908152602001604051809103902060008282546101a791906105ed565b9250508190555060648111156101bc57600080fd5b505050565b600080826040516101d29190610573565b9081526020016040518091039020549050919050565b806000836040516101f99190610573565b9081526020016040518091039020819055505050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6102768261022d565b810181811067ffffffffffffffff821117156102955761029461023e565b5b80604052505050565b60006102a861020f565b90506102b4828261026d565b919050565b600067ffffffffffffffff8211156102d4576102d361023e565b5b6102dd8261022d565b9050602081019050919050565b82818337600083830152505050565b600061030c610307846102b9565b61029e565b90508281526020810184848401111561032857610327610228565b5b6103338482856102ea565b509392505050565b600082601f8301126103505761034f610223565b5b81356103608482602086016102f9565b91505092915050565b6000819050919050565b61037c81610369565b811461038757600080fd5b50565b60008135905061039981610373565b92915050565b6000806000606084860312156103b8576103b7610219565b5b600084013567ffffffffffffffff8111156103d6576103d561021e565b5b6103e28682870161033b565b935050602084013567ffffffffffffffff8111156104035761040261021e565b5b61040f8682870161033b565b92505060406104208682870161038a565b9150509250925092565b6000602082840312156104405761043f610219565b5b600082013567ffffffffffffffff81111561045e5761045d61021e565b5b61046a8482850161033b565b91505092915050565b61047c81610369565b82525050565b60006020820190506104976000830184610473565b92915050565b600080604083850312156104b4576104b3610219565b5b600083013567ffffffffffffffff8111156104d2576104d161021e565b5b6104de8582860161033b565b92505060206104ef8582860161038a565b9150509250929050565b600081519050919050565b600081905092915050565b60005b8381101561052d578082015181840152602081019050610512565b8381111561053c576000848401525b50505050565b600061054d826104f9565b6105578185610504565b935061056781856020860161050f565b80840191505092915050565b600061057f8284610542565b915081905092915050565b7fb95aa35500000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006105c482610369565b91506105cf83610369565b9250828210156105e2576105e161058a565b5b828203905092915050565b60006105f882610369565b915061060383610369565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156106385761063761058a565b5b82820190509291505056fea26469706673582212204f8ebd1bd8a95fec67c4a519537e11316b2dd90a6d972b04ef627aac78dc982f64736f6c634300080b0033" // DeployTransfer deploys a new contract, binding an instance of Transfer to it. func DeployTransfer(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Receipt, *Transfer, error) { @@ -37,7 +39,16 @@ func DeployTransfer(auth *bind.TransactOpts, backend bind.ContractBackend) (comm return common.Address{}, nil, nil, err } - address, receipt, contract, err := bind.DeployContract(auth, parsed, common.FromHex(TransferBin), backend) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex(TransferSMBin) + } else { + bytecode = common.FromHex(TransferBin) + } + if len(bytecode) == 0 { + return common.Address{}, nil, nil, fmt.Errorf("cannot deploy empty bytecode") + } + address, receipt, contract, err := bind.DeployContract(auth, parsed, bytecode, backend) if err != nil { return common.Address{}, nil, nil, err } @@ -50,7 +61,16 @@ func AsyncDeployTransfer(auth *bind.TransactOpts, handler func(*types.Receipt, e return nil, err } - tx, err := bind.AsyncDeployContract(auth, handler, parsed, common.FromHex(TransferBin), backend) + var bytecode []byte + if backend.SMCrypto() { + bytecode = common.FromHex(TransferSMBin) + } else { + bytecode = common.FromHex(TransferBin) + } + if len(bytecode) == 0 { + return nil, fmt.Errorf("cannot deploy empty bytecode") + } + tx, err := bind.AsyncDeployContract(auth, handler, parsed, bytecode, backend) if err != nil { return nil, err } diff --git a/v3/precompiled/cns/cns.go b/v3/precompiled/cns/cns.go deleted file mode 100644 index 9f7dc15f..00000000 --- a/v3/precompiled/cns/cns.go +++ /dev/null @@ -1,269 +0,0 @@ -// Code generated - DO NOT EDIT. -// This file is a generated binding and any manual changes will be lost. - -package cns - -import ( - "math/big" - "strings" - - "github.com/FISCO-BCOS/go-sdk/v3/abi" - "github.com/FISCO-BCOS/go-sdk/v3/abi/bind" - "github.com/FISCO-BCOS/go-sdk/v3/types" - ethereum "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/common" -) - -// Reference imports to suppress errors if they are not otherwise used. -var ( - _ = big.NewInt - _ = strings.NewReader - _ = ethereum.NotFound - _ = abi.U256 - _ = bind.Bind - _ = common.Big1 -) - -// CnsABI is the input ABI used to generate the binding from. -const CnsABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"}],\"name\":\"selectByName\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"}],\"name\":\"selectByNameAndVersion\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"},{\"name\":\"addr\",\"type\":\"string\"},{\"name\":\"abi\",\"type\":\"string\"}],\"name\":\"insert\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"version\",\"type\":\"string\"}],\"name\":\"getContractAddress\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]" - -// Cns is an auto generated Go binding around a Solidity contract. -type Cns struct { - CnsCaller // Read-only binding to the contract - CnsTransactor // Write-only binding to the contract - CnsFilterer // Log filterer for contract events -} - -// CnsCaller is an auto generated read-only Go binding around a Solidity contract. -type CnsCaller struct { - contract *bind.BoundContract // Generic contract wrapper for the low level calls -} - -// CnsTransactor is an auto generated write-only Go binding around a Solidity contract. -type CnsTransactor struct { - contract *bind.BoundContract // Generic contract wrapper for the low level calls -} - -// CnsFilterer is an auto generated log filtering Go binding around a Solidity contract events. -type CnsFilterer struct { - contract *bind.BoundContract // Generic contract wrapper for the low level calls -} - -// CnsSession is an auto generated Go binding around a Solidity contract, -// with pre-set call and transact options. -type CnsSession struct { - Contract *Cns // Generic contract binding to set the session for - CallOpts bind.CallOpts // Call options to use throughout this session - TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session -} - -// CnsCallerSession is an auto generated read-only Go binding around a Solidity contract, -// with pre-set call options. -type CnsCallerSession struct { - Contract *CnsCaller // Generic contract caller binding to set the session for - CallOpts bind.CallOpts // Call options to use throughout this session -} - -// CnsTransactorSession is an auto generated write-only Go binding around a Solidity contract, -// with pre-set transact options. -type CnsTransactorSession struct { - Contract *CnsTransactor // Generic contract transactor binding to set the session for - TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session -} - -// CnsRaw is an auto generated low-level Go binding around a Solidity contract. -type CnsRaw struct { - Contract *Cns // Generic contract binding to access the raw methods on -} - -// CnsCallerRaw is an auto generated low-level read-only Go binding around a Solidity contract. -type CnsCallerRaw struct { - Contract *CnsCaller // Generic read-only contract binding to access the raw methods on -} - -// CnsTransactorRaw is an auto generated low-level write-only Go binding around a Solidity contract. -type CnsTransactorRaw struct { - Contract *CnsTransactor // Generic write-only contract binding to access the raw methods on -} - -// NewCns creates a new instance of Cns, bound to a specific deployed contract. -func NewCns(address common.Address, backend bind.ContractBackend) (*Cns, error) { - contract, err := bindCns(address, backend, backend, backend) - if err != nil { - return nil, err - } - return &Cns{CnsCaller: CnsCaller{contract: contract}, CnsTransactor: CnsTransactor{contract: contract}, CnsFilterer: CnsFilterer{contract: contract}}, nil -} - -// NewCnsCaller creates a new read-only instance of Cns, bound to a specific deployed contract. -func NewCnsCaller(address common.Address, caller bind.ContractCaller) (*CnsCaller, error) { - contract, err := bindCns(address, caller, nil, nil) - if err != nil { - return nil, err - } - return &CnsCaller{contract: contract}, nil -} - -// NewCnsTransactor creates a new write-only instance of Cns, bound to a specific deployed contract. -func NewCnsTransactor(address common.Address, transactor bind.ContractTransactor) (*CnsTransactor, error) { - contract, err := bindCns(address, nil, transactor, nil) - if err != nil { - return nil, err - } - return &CnsTransactor{contract: contract}, nil -} - -// NewCnsFilterer creates a new log filterer instance of Cns, bound to a specific deployed contract. -func NewCnsFilterer(address common.Address, filterer bind.ContractFilterer) (*CnsFilterer, error) { - contract, err := bindCns(address, nil, nil, filterer) - if err != nil { - return nil, err - } - return &CnsFilterer{contract: contract}, nil -} - -// bindCns binds a generic wrapper to an already deployed contract. -func bindCns(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { - parsed, err := abi.JSON(strings.NewReader(CnsABI)) - if err != nil { - return nil, err - } - return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil -} - -// Call invokes the (constant) contract method with params as input values and -// sets the output to result. The result type might be a single field for simple -// returns, a slice of interfaces for anonymous returns and a struct for named -// returns. -func (_Cns *CnsRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { - return _Cns.Contract.CnsCaller.contract.Call(opts, result, method, params...) -} - -// Transfer initiates a plain transaction to move funds to the contract, calling -// its default method if one is available. -func (_Cns *CnsRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.CnsTransactor.contract.Transfer(opts) -} - -// Transact invokes the (paid) contract method with params as input values. -func (_Cns *CnsRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.CnsTransactor.contract.Transact(opts, method, params...) -} - -// Call invokes the (constant) contract method with params as input values and -// sets the output to result. The result type might be a single field for simple -// returns, a slice of interfaces for anonymous returns and a struct for named -// returns. -func (_Cns *CnsCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { - return _Cns.Contract.contract.Call(opts, result, method, params...) -} - -// Transfer initiates a plain transaction to move funds to the contract, calling -// its default method if one is available. -func (_Cns *CnsTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.contract.Transfer(opts) -} - -// Transact invokes the (paid) contract method with params as input values. -func (_Cns *CnsTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.contract.Transact(opts, method, params...) -} - -// GetContractAddress is a free data retrieval call binding the contract method 0xf85f8126. -// -// Solidity: function getContractAddress(string name, string version) constant returns(address) -func (_Cns *CnsCaller) GetContractAddress(opts *bind.CallOpts, name string, version string) (common.Address, error) { - var ( - ret0 = new(common.Address) - ) - out := ret0 - err := _Cns.contract.Call(opts, out, "getContractAddress", name, version) - return *ret0, err -} - -// GetContractAddress is a free data retrieval call binding the contract method 0xf85f8126. -// -// Solidity: function getContractAddress(string name, string version) constant returns(address) -func (_Cns *CnsSession) GetContractAddress(name string, version string) (common.Address, error) { - return _Cns.Contract.GetContractAddress(&_Cns.CallOpts, name, version) -} - -// GetContractAddress is a free data retrieval call binding the contract method 0xf85f8126. -// -// Solidity: function getContractAddress(string name, string version) constant returns(address) -func (_Cns *CnsCallerSession) GetContractAddress(name string, version string) (common.Address, error) { - return _Cns.Contract.GetContractAddress(&_Cns.CallOpts, name, version) -} - -// SelectByName is a free data retrieval call binding the contract method 0x819a3d62. -// -// Solidity: function selectByName(string name) constant returns(string) -func (_Cns *CnsCaller) SelectByName(opts *bind.CallOpts, name string) (string, error) { - var ( - ret0 = new(string) - ) - out := ret0 - err := _Cns.contract.Call(opts, out, "selectByName", name) - return *ret0, err -} - -// SelectByName is a free data retrieval call binding the contract method 0x819a3d62. -// -// Solidity: function selectByName(string name) constant returns(string) -func (_Cns *CnsSession) SelectByName(name string) (string, error) { - return _Cns.Contract.SelectByName(&_Cns.CallOpts, name) -} - -// SelectByName is a free data retrieval call binding the contract method 0x819a3d62. -// -// Solidity: function selectByName(string name) constant returns(string) -func (_Cns *CnsCallerSession) SelectByName(name string) (string, error) { - return _Cns.Contract.SelectByName(&_Cns.CallOpts, name) -} - -// SelectByNameAndVersion is a free data retrieval call binding the contract method 0x897f0251. -// -// Solidity: function selectByNameAndVersion(string name, string version) constant returns(string) -func (_Cns *CnsCaller) SelectByNameAndVersion(opts *bind.CallOpts, name string, version string) (string, error) { - var ( - ret0 = new(string) - ) - out := ret0 - err := _Cns.contract.Call(opts, out, "selectByNameAndVersion", name, version) - return *ret0, err -} - -// SelectByNameAndVersion is a free data retrieval call binding the contract method 0x897f0251. -// -// Solidity: function selectByNameAndVersion(string name, string version) constant returns(string) -func (_Cns *CnsSession) SelectByNameAndVersion(name string, version string) (string, error) { - return _Cns.Contract.SelectByNameAndVersion(&_Cns.CallOpts, name, version) -} - -// SelectByNameAndVersion is a free data retrieval call binding the contract method 0x897f0251. -// -// Solidity: function selectByNameAndVersion(string name, string version) constant returns(string) -func (_Cns *CnsCallerSession) SelectByNameAndVersion(name string, version string) (string, error) { - return _Cns.Contract.SelectByNameAndVersion(&_Cns.CallOpts, name, version) -} - -// Insert is a paid mutator transaction binding the contract method 0xa216464b. -// -// Solidity: function insert(string name, string version, string addr, string abi) returns(uint256) -func (_Cns *CnsTransactor) Insert(opts *bind.TransactOpts, name string, version string, addr string, abi string) (*types.Transaction, *types.Receipt, error) { - return _Cns.contract.Transact(opts, "insert", name, version, addr, abi) -} - -// Insert is a paid mutator transaction binding the contract method 0xa216464b. -// -// Solidity: function insert(string name, string version, string addr, string abi) returns(uint256) -func (_Cns *CnsSession) Insert(name string, version string, addr string, abi string) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.Insert(&_Cns.TransactOpts, name, version, addr, abi) -} - -// Insert is a paid mutator transaction binding the contract method 0xa216464b. -// -// Solidity: function insert(string name, string version, string addr, string abi) returns(uint256) -func (_Cns *CnsTransactorSession) Insert(name string, version string, addr string, abi string) (*types.Transaction, *types.Receipt, error) { - return _Cns.Contract.Insert(&_Cns.TransactOpts, name, version, addr, abi) -} diff --git a/v3/precompiled/cns/cns_service.go b/v3/precompiled/cns/cns_service.go deleted file mode 100644 index 8f4813e2..00000000 --- a/v3/precompiled/cns/cns_service.go +++ /dev/null @@ -1,162 +0,0 @@ -package cns - -import ( - "encoding/json" - "fmt" - - "github.com/FISCO-BCOS/go-sdk/v3/abi/bind" - "github.com/FISCO-BCOS/go-sdk/v3/client" - "github.com/FISCO-BCOS/go-sdk/v3/precompiled" - "github.com/FISCO-BCOS/go-sdk/v3/types" - "github.com/ethereum/go-ethereum/common" -) - -// cns precompiled contract error code -const ( - versionLengthOverflow int64 = -51201 - addressAndVersionExist int64 = -51200 -) - -// getErrorMessage returns the message of error code -func getErrorMessage(errorCode int64) string { - var message string - switch errorCode { - case versionLengthOverflow: - message = "version length overflow" - case addressAndVersionExist: - message = "address and version exist" - default: - message = "" - } - return message -} - -// errorCodeToError judges whether the error code represents an error -func errorCodeToError(errorCode int64) error { - var errorCodeMessage string - errorCodeMessage = precompiled.GetCommonErrorCodeMessage(errorCode) - if errorCodeMessage != "" { - return fmt.Errorf("error code: %v, error code message: %v", errorCode, errorCodeMessage) - } - errorCodeMessage = getErrorMessage(errorCode) - if errorCodeMessage != "" { - return fmt.Errorf("error code: %v, error code message: %v", errorCode, errorCodeMessage) - } - return nil -} - -// Info is used for the CNSService -type Info struct { - Name string `json:"name"` - Version string `json:"version"` - Address string `json:"address"` - Abi string `json:"abi"` -} - -// Service is a precompile contract service. -type Service struct { - cns *Cns - cnsAuth *bind.TransactOpts - client *client.Client -} - -const maxVersionLength = 40 - -// contract address -var cnsPrecompileAddress = common.HexToAddress("0x0000000000000000000000000000000000001004") - -// NewCnsService returns ptr of Service -func NewCnsService(client *client.Client) (*Service, error) { - instance, err := NewCns(cnsPrecompileAddress, client) - if err != nil { - return nil, fmt.Errorf("construct Service failed: %+v", err) - } - auth := client.GetTransactOpts() - return &Service{cns: instance, cnsAuth: auth, client: client}, nil -} - -// SelectByName returns the cns information according to the name string. -func (service *Service) SelectByName(name string) (string, error) { - opts := &bind.CallOpts{From: service.cnsAuth.From} - cnsName, err := service.cns.SelectByName(opts, name) - if err != nil { - return "", fmt.Errorf("service SelectByName failed: %+v", err) - } - return cnsName, nil -} - -// SelectByNameAndVersion returns the cns information according to the name string and version string. -func (service *Service) SelectByNameAndVersion(name string, version string) (string, error) { - opts := &bind.CallOpts{From: service.cnsAuth.From} - cnsName, err := service.cns.SelectByNameAndVersion(opts, name, version) - if err != nil { - return "", fmt.Errorf("service SelectByNameAndVersion failed: %+v", err) - } - return cnsName, nil -} - -// GetAddressByContractNameAndVersion returns the contract address. -func (service *Service) GetAddressByContractNameAndVersion(contractName, version string) (common.Address, error) { - opts := &bind.CallOpts{From: service.cnsAuth.From} - address, err := service.cns.GetContractAddress(opts, contractName, version) - if err != nil { - return common.Address{}, fmt.Errorf("service GetAddressByContractNameAndVersion failed: %+v", err) - } - return address, nil -} - -// RegisterCns registers a contract for its CNS. -func (service *Service) RegisterCns(name string, version string, address common.Address, abi string) (int64, error) { - if len(version) > maxVersionLength { - return precompiled.DefaultErrorCode, fmt.Errorf("version string length exceeds the maximum limit") - } - _, receipt, err := service.cns.Insert(service.cnsAuth, name, version, address.String(), abi) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("service RegisterCns failed: %+v", err) - } - return parseReturnValue(receipt, "insert") -} - -// QueryCnsByName returns the CNS info according to the CNS name -func (service *Service) QueryCnsByName(name string) ([]Info, error) { - cnsInfo, err := service.SelectByName(name) - if err != nil { - return nil, err - } - // json unmarshal - var infos []Info - if err := json.Unmarshal([]byte(cnsInfo), &infos); err != nil { - return nil, fmt.Errorf("unmarshal the Info failed") - } - return infos, nil -} - -// QueryCnsByNameAndVersion returns the CNS info according to the name and version -func (service *Service) QueryCnsByNameAndVersion(name string, version string) ([]Info, error) { - cnsInfo, err := service.SelectByNameAndVersion(name, version) - if err != nil { - return nil, err - } - // json unmarshal - var infos []Info - if err := json.Unmarshal([]byte(cnsInfo), &infos); err != nil { - return nil, fmt.Errorf("unmarshal the Info failed") - } - return infos, nil -} - -func parseReturnValue(receipt *types.Receipt, name string) (int64, error) { - errorMessage := receipt.GetErrorMessage() - if errorMessage != "" { - return int64(receipt.GetStatus()), fmt.Errorf("receipt.Status err: %v", errorMessage) - } - bigNum, err := precompiled.ParseBigIntFromOutput(receipt) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("ParseBigIntFromOutput failed, err: %v, txHah: %v", err, receipt.TransactionHash) - } - errorCode, err := precompiled.BigIntToInt64(bigNum) - if err != nil { - return precompiled.DefaultErrorCode, fmt.Errorf("parseReturnValue failed, err: %v, txHah: %v", err, receipt.TransactionHash) - } - return errorCode, errorCodeToError(errorCode) -} diff --git a/v3/precompiled/cns/cns_service_test.go b/v3/precompiled/cns/cns_service_test.go deleted file mode 100644 index f61cb756..00000000 --- a/v3/precompiled/cns/cns_service_test.go +++ /dev/null @@ -1,172 +0,0 @@ -package cns - -import ( - "context" - "encoding/hex" - "os" - "testing" - - "github.com/FISCO-BCOS/go-sdk/v3/client" - "github.com/ethereum/go-ethereum/common" -) - -func getClient(t *testing.T) *client.Client { - privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58") - config := &client.Config{IsSMCrypto: false, GroupID: "group0", - PrivateKey: privateKey, Host: "127.0.0.1", Port: 20200, TLSCaFile: "./ca.crt", TLSKeyFile: "./sdk.key", TLSCertFile: "./sdk.crt"} - c, err := client.DialContext(context.Background(), config) - if err != nil { - t.Fatalf("Dial to %s:%d failed of %v", config.Host, config.Port, err) - } - return c -} - -func getService(t *testing.T) { - c := getClient(t) - newService, err := NewCnsService(c) - if err != nil { - t.Fatalf("init CnsService failed: %+v", err) - } - service = newService -} - -const ( - standardOutput = 1 - name = "store" - version = "5.0" - address = "0x0626918C51A1F36c7ad4354BB1197460A533a2B9" - testABI = `[ - { - "constant": true, - "inputs": [ - { - "name": "", - "type": "bytes32" - } - ], - "name": "items", - "outputs": [ - { - "name": "", - "type": "bytes32" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": true, - "inputs": [], - "name": "version", - "outputs": [ - { - "name": "", - "type": "string" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "key", - "type": "bytes32" - }, - { - "name": "value", - "type": "bytes32" - } - ], - "name": "setItem", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "inputs": [ - { - "name": "_version", - "type": "string" - } - ], - "payable": false, - "stateMutability": "nonpayable", - "type": "constructor" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": false, - "name": "key", - "type": "bytes32" - }, - { - "indexed": false, - "name": "value", - "type": "bytes32" - } - ], - "name": "ItemSet", - "type": "event" - } - ]` -) - -var ( - service *Service -) - -func TestMain(m *testing.M) { - getService(&testing.T{}) - exitCode := m.Run() - os.Exit(exitCode) -} - -func TestRegisterCns(t *testing.T) { - result, err := service.RegisterCns(name, version, common.HexToAddress(address), testABI) - if err != nil { - t.Fatalf("Service RegisterCns failed: %+v\n", err) - } - if result != standardOutput { - t.Fatalf("TestRegisterCns failed, the result %v is inconsistent with \"%v\"", result, standardOutput) - } - t.Logf("TestRegisterCns result: %v", result) -} - -func TestGetAddressByContractNameAndVersion(t *testing.T) { - addr, err := service.GetAddressByContractNameAndVersion(name, version) - if err != nil { - t.Fatalf("GetAddressByContractNameAndVersion failed: %v", err) - } - if addr.Hex() != address { - t.Fatalf("GetAddressByContractNameAndVersion failed, the result %v is inconsistent with \"0626918c51a1f36c7ad4354bb1197460a533a2b9\"", addr.Hex()) - } -} - -func TestQueryCnsByNameAndVersion(t *testing.T) { - cnsInfo, err := service.QueryCnsByNameAndVersion(name, version) - if err != nil { - t.Fatalf("QueryCnsByNameAndVersion failed: %v\n", err) - } - if len(cnsInfo) != 1 { - t.Fatalf("QueryCnsByNameAndVersion failed, the length of cnsInfo \"%v\" is inconsistent with 1", len(cnsInfo)) - } - // t.Logf("QueryCnsByNameAndVersion: %v", cnsInfo[0]) -} - -func TestQueryCnsByName(t *testing.T) { - cnsInfoByName, err := service.QueryCnsByName(name) - if err != nil { - t.Fatalf("QueryCnsByName failed: %v\n", err) - } - if len(cnsInfoByName) != 1 { - t.Fatalf("QueryCnsByNameAndVersion failed, the length of cnsInfoByName \"%v\" is inconsistent with 1", len(cnsInfoByName)) - } - // t.Logf("QueryCnsByName: %v", cnsInfoByName[0]) -}