From: oysheng <33340252+oysheng@users.noreply.github.com> Date: Fri, 15 Mar 2019 06:58:29 +0000 (+0800) Subject: optimise equity commandline (#36) X-Git-Tag: v0.1.1 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=d1eb7abcc69dcb93f0dc8eba145599322d6cdc67;p=bytom%2Fequity.git optimise equity commandline (#36) * optimise equity commandline * modify instance contract sentence * optimise test * optimise Makefile --- diff --git a/Makefile b/Makefile index 924761a..f0dc949 100644 --- a/Makefile +++ b/Makefile @@ -22,13 +22,13 @@ release: equity cd target && cp -f equity $(EQUITY_RELEASE).exe cd target && md5sum $(EQUITY_RELEASE).exe > $(EQUITY_RELEASE).md5 cd target && zip $(EQUITY_RELEASE).zip $(EQUITY_RELEASE).exe $(EQUITY_RELEASE).md5 - cd target && rm -f $(EQUITY_RELEASE).exe $(EQUITY_RELEASE).md5 + cd target && rm -f equity $(EQUITY_RELEASE).exe $(EQUITY_RELEASE).md5 else release: equity cd target && cp -f equity $(EQUITY_RELEASE) cd target && md5sum $(EQUITY_RELEASE) > $(EQUITY_RELEASE).md5 cd target && tar -czf $(EQUITY_RELEASE).tgz $(EQUITY_RELEASE) $(EQUITY_RELEASE).md5 - cd target && rm -f $(EQUITY_RELEASE) $(EQUITY_RELEASE).md5 + cd target && rm -f equity $(EQUITY_RELEASE) $(EQUITY_RELEASE).md5 endif release-all: clean diff --git a/compiler/equitytest/FixedLimitCollect b/compiler/equitytest/FixedLimitCollect index 6bef9b6..fc60c1d 100644 --- a/compiler/equitytest/FixedLimitCollect +++ b/compiler/equitytest/FixedLimitCollect @@ -10,12 +10,10 @@ contract FixedLimitCollect(assetDeposited: Asset, bankerKey: PublicKey) locks billAmount of billAsset { clause collect(amountDeposited: Amount, saver: Program) { verify below(dueBlockHeight) - verify amountDeposited/100000000 > 0 && amountDeposited <= billAmount && totalAmountBill <= totalAmountCapital + verify amountDeposited <= billAmount && totalAmountBill <= totalAmountCapital define sAmountDeposited: Integer = amountDeposited/100000000 define sTotalAmountBill: Integer = totalAmountBill/100000000 verify sAmountDeposited > 0 && sTotalAmountBill > 0 - define gain: Integer = totalAmountCapital*sAmountDeposited/sTotalAmountBill - verify gain > 0 if amountDeposited < billAmount { lock amountDeposited of assetDeposited with FixedLimitProfit(billAsset, totalAmountBill, totalAmountCapital, expireBlockHeight, additionalBlockHeight, banker, bankerKey) lock amountDeposited of billAsset with saver diff --git a/compiler/equitytest/FixedLimitProfit b/compiler/equitytest/FixedLimitProfit index 0c13283..253fda5 100644 --- a/compiler/equitytest/FixedLimitProfit +++ b/compiler/equitytest/FixedLimitProfit @@ -11,7 +11,7 @@ contract FixedLimitProfit(assetBill: Asset, define sTotalAmountBill: Integer = totalAmountBill/100000000 verify sAmountBill > 0 && sTotalAmountBill > 0 && amountBill < totalAmountBill define gain: Integer = totalAmountCapital*sAmountBill/sTotalAmountBill - verify gain > 0 + verify gain > 0 && gain <= capitalAmount if gain < capitalAmount { lock amountBill of assetBill with banker lock gain of capitalAsset with saver diff --git a/compiler/equitytest/equity_test.go b/compiler/equitytest/equity_test.go index b8f4edb..84c3645 100644 --- a/compiler/equitytest/equity_test.go +++ b/compiler/equitytest/equity_test.go @@ -29,11 +29,11 @@ func TestCompileContract(t *testing.T) { }, { "./FixedLimitCollect", - "597a64370200005479cda069c35b790400e1f5059600a05c797ba19a53795579a19a695a790400e1f5059653790400e1f505967800a07800a09a6955797b957c9600a069c35b797c9f9161645b010000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec1696332020000005b79c2547951005e79895d79895c79895b7989597989587989537a894ca4587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c1696343020000547acd9f69587a587aae7cac", + "597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac", }, { "./FixedLimitProfit", - "587a64980000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c967600a069c3787c9f91616481000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696393000000005b795479515b79c16951c3c2515d79c16963a4000000557acd9f69577a577aae7cac", + "587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac", }, } diff --git a/equity/main.go b/equity/main.go index 367e8bc..62d2734 100644 --- a/equity/main.go +++ b/equity/main.go @@ -52,95 +52,100 @@ var equityCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { if version { version := compiler.VersionWithCommit(compiler.GitCommit) + fmt.Println("equity, the equity compiler commandline interface") fmt.Println("Version:", version) os.Exit(0) } if len(args) < 1 { cmd.Usage() - } - - contractFile, err := os.Open(args[0]) - if err != nil { - fmt.Printf("An error [%v] occurred on opening the file, please check whether the file exists or can be accessed.\n", err) os.Exit(0) } - defer contractFile.Close() - reader := bufio.NewReader(contractFile) - contracts, err := compiler.Compile(reader) - if err != nil { - fmt.Println("Compile contract failed:", err) - os.Exit(0) + if err := handleCompiled(args); err != nil { + os.Exit(-1) } + }, +} - if len(contracts) == 0 { - fmt.Println("The contract is empty!") - os.Exit(0) +func handleCompiled(args []string) error { + contractFile, err := os.Open(args[0]) + if err != nil { + fmt.Printf("An error [%v] occurred on opening the file, please check whether the file exists or can be accessed.\n", err) + return err + } + defer contractFile.Close() + + reader := bufio.NewReader(contractFile) + contracts, err := compiler.Compile(reader) + if err != nil { + fmt.Println("Compile contract failed:", err) + return err + } + + // Print the result for all contracts + for i, contract := range contracts { + fmt.Printf("======= %v =======\n", contract.Name) + if bin { + fmt.Println("Binary:") + fmt.Printf("%v\n\n", hex.EncodeToString(contract.Body)) } - // Print the result for all contracts - for i, contract := range contracts { - fmt.Printf("======= %v =======\n", contract.Name) - if bin { - fmt.Println("Binary:") - fmt.Printf("%v\n\n", hex.EncodeToString(contract.Body)) + if shift { + fmt.Println("Clause shift:") + clauseMap, err := equ.Shift(contract) + if err != nil { + fmt.Println("Statistics contract clause shift error:", err) + return err } - if shift { - fmt.Println("Clause shift:") - clauseMap, err := equ.Shift(contract) - if err != nil { - fmt.Println("Statistics contract clause shift error:", err) - os.Exit(0) - } - - for clause, shift := range clauseMap { - fmt.Printf(" %s: %v\n", clause, shift) - } - fmt.Printf("\nNOTE: \n If the contract contains only one clause, Users don't need clause selector when unlock contract." + - "\n Furthermore, there is no signification for ending clause shift except for display.\n\n") + for clause, shift := range clauseMap { + fmt.Printf(" %s: %v\n", clause, shift) } + fmt.Printf("\nNOTE: \n If the contract contains only one clause, Users don't need clause selector when unlock contract." + + "\n Furthermore, there is no signification for ending clause shift except for display.\n\n") + } - if instance { - if i != len(contracts)-1 { - continue - } + if instance { + if i != len(contracts)-1 { + continue + } - fmt.Println("Instantiated program:") - if len(args)-1 < len(contract.Params) { - fmt.Printf("Error: The number of input arguments %d is less than the number of contract parameters %d\n", len(args)-1, len(contract.Params)) - usage := fmt.Sprintf("Usage:\n equity %s", args[0]) - for _, param := range contract.Params { - usage = usage + " <" + param.Name + ">" - } - fmt.Printf("%s\n\n", usage) - os.Exit(0) + fmt.Println("Instantiated program:") + if len(args)-1 < len(contract.Params) { + fmt.Printf("Error: The number of input arguments %d is less than the number of contract parameters %d\n", len(args)-1, len(contract.Params)) + usage := fmt.Sprintf("Usage:\n equity %s", args[0]) + for _, param := range contract.Params { + usage = usage + " <" + param.Name + ">" } + fmt.Printf("%s\n\n", usage) + return err + } - contractArgs, err := equ.ConvertArguments(contract, args[1:len(contract.Params)+1]) - if err != nil { - fmt.Println("Convert arguments into contract parameters error:", err) - os.Exit(0) - } + contractArgs, err := equ.ConvertArguments(contract, args[1:len(contract.Params)+1]) + if err != nil { + fmt.Println("Convert arguments into contract parameters error:", err) + return err + } - instantProg, err := equ.InstantiateContract(contract, contractArgs) - if err != nil { - fmt.Println("Instantiate contract error:", err) - os.Exit(0) - } - fmt.Printf("%v\n\n", hex.EncodeToString(instantProg)) + instantProg, err := equ.InstantiateContract(contract, contractArgs) + if err != nil { + fmt.Println("Instantiate contract error:", err) + return err } + fmt.Printf("%v\n\n", hex.EncodeToString(instantProg)) + } - if ast { - fmt.Println("Ast:") - rawData, err := equ.JSONMarshal(contract, true) - if err != nil { - fmt.Println("Marshal the struct of contract to json error:", err) - os.Exit(0) - } - fmt.Println(string(rawData)) + if ast { + fmt.Println("Ast:") + rawData, err := equ.JSONMarshal(contract, true) + if err != nil { + fmt.Println("Marshal the struct of contract to json error:", err) + return err } + fmt.Println(string(rawData)) } - }, + } + + return nil }