OSDN Git Service

new repo
[bytom/vapor.git] / equity / equity / main.go
1 package main
2
3 import (
4         "bufio"
5         "encoding/hex"
6         "fmt"
7         "os"
8         "runtime"
9
10         "github.com/spf13/cobra"
11
12         "github.com/vapor/equity/compiler"
13         equityutil "github.com/vapor/equity/equity/util"
14 )
15
16 const (
17         strBin      string = "bin"
18         strShift    string = "shift"
19         strInstance string = "instance"
20 )
21
22 var (
23         bin      = false
24         shift    = false
25         instance = false
26 )
27
28 func init() {
29         equityCmd.PersistentFlags().BoolVar(&bin, strBin, false, "Binary of the contracts in hex.")
30         equityCmd.PersistentFlags().BoolVar(&shift, strShift, false, "Function shift of the contracts.")
31         equityCmd.PersistentFlags().BoolVar(&instance, strInstance, false, "Object of the Instantiated contracts.")
32 }
33
34 func main() {
35         runtime.GOMAXPROCS(runtime.NumCPU())
36         if err := equityCmd.Execute(); err != nil {
37                 os.Exit(0)
38         }
39 }
40
41 var equityCmd = &cobra.Command{
42         Use:     "equity <input_file>",
43         Short:   "equity commandline compiler",
44         Example: "equity contract_name [contract_args...] --bin --instance",
45         Args:    cobra.RangeArgs(1, 100),
46         Run: func(cmd *cobra.Command, args []string) {
47                 if len(args) < 1 {
48                         cmd.Usage()
49                 }
50
51                 contractFile, err := os.Open(args[0])
52                 if err != nil {
53                         fmt.Printf("An error [%v] occurred on opening the file, please check whether the file exists or can be accessed.\n", err)
54                         os.Exit(0)
55                 }
56                 defer contractFile.Close()
57
58                 reader := bufio.NewReader(contractFile)
59                 contracts, err := compiler.Compile(reader)
60                 if err != nil {
61                         fmt.Println("Compile contract failed:", err)
62                         os.Exit(0)
63                 }
64
65                 if len(contracts) == 0 {
66                         fmt.Println("The contract is empty!")
67                         os.Exit(0)
68                 }
69
70                 // Print the result for all contracts
71                 for i, contract := range contracts {
72                         fmt.Printf("======= %v =======\n", contract.Name)
73                         if bin {
74                                 fmt.Println("Binary:")
75                                 fmt.Printf("%v\n\n", hex.EncodeToString(contract.Body))
76                         }
77
78                         if shift {
79                                 fmt.Println("Clause shift:")
80                                 clauseMap, err := equityutil.Shift(contract)
81                                 if err != nil {
82                                         fmt.Println("Statistics contract clause shift error:", err)
83                                         os.Exit(0)
84                                 }
85
86                                 for clause, shift := range clauseMap {
87                                         fmt.Printf("    %s:  %v\n", clause, shift)
88                                 }
89                                 fmt.Printf("\nNOTE: \n    If the contract contains only one clause, Users don't need clause selector when unlock contract." +
90                                         "\n    Furthermore, there is no signification for ending clause shift except for display.\n\n")
91                         }
92
93                         if instance {
94                                 if i != len(contracts)-1 {
95                                         continue
96                                 }
97
98                                 fmt.Println("Instantiated program:")
99                                 if len(args)-1 < len(contract.Params) {
100                                         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))
101                                         usage := fmt.Sprintf("Usage:\n  equity %s", args[0])
102                                         for _, param := range contract.Params {
103                                                 usage = usage + " <" + param.Name + ">"
104                                         }
105                                         fmt.Printf("%s\n\n", usage)
106                                         os.Exit(0)
107                                 }
108
109                                 contractArgs, err := equityutil.ConvertArguments(contract, args[1:len(contract.Params)+1])
110                                 if err != nil {
111                                         fmt.Println("Convert arguments into contract parameters error:", err)
112                                         os.Exit(0)
113                                 }
114
115                                 instantProg, err := equityutil.InstantiateContract(contract, contractArgs)
116                                 if err != nil {
117                                         fmt.Println("Instantiate contract error:", err)
118                                         os.Exit(0)
119                                 }
120                                 fmt.Printf("%v\n\n", hex.EncodeToString(instantProg))
121                         }
122                 }
123         },
124 }