OSDN Git Service

equity compiler tool (#18)
[bytom/equity.git] / vendor / github.com / bytom / protocol / vm / runlimit.go
1 package vm
2
3 import (
4         "fmt"
5 )
6
7 const (
8         //base runlimit gas
9         GasBaseStep     int64 = 1
10         GasLowSlowStep  int64 = 2
11         GasSlowStep     int64 = 10
12         GasHighSlowStep int64 = 17
13         GasLowMidStep   int64 = 32
14         GasMidStep      int64 = 33
15         GasHighMidStep  int64 = 41
16         GasLowFastStep  int64 = 296
17         GasFastStep     int64 = 881
18
19         GasData      int64 = 9
20         GasPushData1 int64 = 138
21         GasPushData2 int64 = 32779
22         GasPushData4 int64 = 2147483661
23
24         GasUnderBaseStep     int64 = -4
25         GasUnderLowSlowStep  int64 = -5
26         GasUnderSlowStep     int64 = -7
27         GasUnderHighSlowStep int64 = -8
28         GasUnderLowMidStep   int64 = -14
29         GasUnderMidStep      int64 = -15
30         GasUnderHighMidStep  int64 = -21
31         GasUnderLowFastStep  int64 = -30
32         GasUnderFastStep     int64 = -37
33
34         //mutable factor
35         ConfigLength   int64 = 64
36         ConfigDistance int64 = 8
37         ConfigNumPub   int64 = 3
38         ConfigNumSig   int64 = 2
39
40         //mutable runlimit gas
41         GasMutBaseStep     int64 = 1 + ConfigLength
42         GasMutLowSlowStep  int64 = 9 + ConfigLength
43         GasMutSlowStep     int64 = -6 + ConfigLength
44         GasMutHighSlowStep int64 = -6 - ConfigLength
45         GasMutLowMidStep   int64 = -7 - ConfigLength
46         GasMutMidStep      int64 = -71 - ConfigLength
47         GasMutHighMidStep  int64 = 18 + 2*ConfigLength
48         GasMutLowFastStep  int64 = 26 + 3*ConfigLength
49         GasMutFastStep     int64 = -14 - 2*ConfigLength
50
51         GasDifferBaseStep int64 = -7 + ConfigDistance
52         GasDifferSlowStep int64 = -7 - ConfigDistance
53         GasDifferMidStep  int64 = -12 - ConfigDistance
54         GasDifferFastStep int64 = -28 - ConfigDistance
55
56         GasMultiSig int64 = -63 + 984*ConfigNumPub - 72*ConfigNumSig
57 )
58
59 type gasInfo struct {
60         Op   Op
61         name string
62         Gas  int64
63 }
64
65 var (
66         gasop = [256]gasInfo{
67                 // data pushing
68                 OP_FALSE: {OP_FALSE, "FALSE", GasSlowStep},
69                 OP_1:     {OP_1, "1", GasSlowStep},
70
71                 // sic: the PUSHDATA ops all share an implementation
72                 OP_PUSHDATA1: {OP_PUSHDATA1, "PUSHDATA1", GasPushData1},
73                 OP_PUSHDATA2: {OP_PUSHDATA2, "PUSHDATA2", GasPushData2},
74                 OP_PUSHDATA4: {OP_PUSHDATA4, "PUSHDATA4", GasPushData4},
75
76                 OP_1NEGATE: {OP_1NEGATE, "1NEGATE", GasHighSlowStep},
77
78                 OP_NOP: {OP_NOP, "NOP", GasBaseStep},
79
80                 // control flow
81                 OP_JUMP:   {OP_JUMP, "JUMP", GasBaseStep},
82                 OP_JUMPIF: {OP_JUMPIF, "JUMPIF", GasUnderMidStep},
83
84                 OP_VERIFY: {OP_VERIFY, "VERIFY", GasUnderHighSlowStep},
85                 OP_FAIL:   {OP_FAIL, "FAIL", GasBaseStep},
86
87                 OP_TOALTSTACK:   {OP_TOALTSTACK, "TOALTSTACK", GasLowSlowStep},
88                 OP_FROMALTSTACK: {OP_FROMALTSTACK, "FROMALTSTACK", GasLowSlowStep},
89                 OP_2DROP:        {OP_2DROP, "2DROP", GasMutFastStep},
90                 OP_2DUP:         {OP_2DUP, "2DUP", GasMutHighMidStep},
91                 OP_3DUP:         {OP_3DUP, "3DUP", GasMutLowFastStep},
92                 OP_2OVER:        {OP_2OVER, "2OVER", GasMutHighMidStep},
93                 OP_2ROT:         {OP_2ROT, "2ROT", GasLowSlowStep},
94                 OP_2SWAP:        {OP_2SWAP, "2SWAP", GasLowSlowStep},
95                 OP_IFDUP:        {OP_IFDUP, "IFDUP", GasBaseStep},
96                 OP_DEPTH:        {OP_DEPTH, "DEPTH", GasHighSlowStep},
97                 OP_DROP:         {OP_DROP, "DROP", GasMutLowMidStep},
98                 OP_DUP:          {OP_DUP, "DUP", GasMutLowSlowStep},
99                 OP_NIP:          {OP_NIP, "NIP", GasMutLowMidStep},
100                 OP_OVER:         {OP_OVER, "OVER", GasMutLowSlowStep},
101                 OP_PICK:         {OP_PICK, "PICK", GasMutSlowStep},
102                 OP_ROLL:         {OP_ROLL, "ROLL", GasUnderLowMidStep},
103                 OP_ROT:          {OP_ROT, "ROT", GasLowSlowStep},
104                 OP_SWAP:         {OP_SWAP, "SWAP", GasBaseStep},
105                 OP_TUCK:         {OP_TUCK, "TUCK", GasMutLowSlowStep},
106
107                 OP_CAT:         {OP_CAT, "CAT", GasUnderBaseStep},
108                 OP_SUBSTR:      {OP_SUBSTR, "SUBSTR", GasDifferFastStep},
109                 OP_LEFT:        {OP_LEFT, "LEFT", GasDifferMidStep},
110                 OP_RIGHT:       {OP_RIGHT, "RIGHT", GasDifferMidStep},
111                 OP_SIZE:        {OP_SIZE, "SIZE", GasHighSlowStep},
112                 OP_CATPUSHDATA: {OP_CATPUSHDATA, "CATPUSHDATA", GasUnderBaseStep},
113
114                 OP_INVERT:      {OP_INVERT, "INVERT", GasMutBaseStep},
115                 OP_AND:         {OP_AND, "AND", GasDifferSlowStep},
116                 OP_OR:          {OP_OR, "OR", GasDifferBaseStep},
117                 OP_XOR:         {OP_XOR, "XOR", GasDifferBaseStep},
118                 OP_EQUAL:       {OP_EQUAL, "EQUAL", GasMutHighSlowStep},
119                 OP_EQUALVERIFY: {OP_EQUALVERIFY, "EQUALVERIFY", GasMutLowMidStep},
120
121                 OP_1ADD:               {OP_1ADD, "1ADD", GasLowSlowStep},
122                 OP_1SUB:               {OP_1SUB, "1SUB", GasLowSlowStep},
123                 OP_2MUL:               {OP_2MUL, "2MUL", GasLowSlowStep},
124                 OP_2DIV:               {OP_2DIV, "2DIV", GasLowSlowStep},
125                 OP_NEGATE:             {OP_NEGATE, "NEGATE", GasLowSlowStep},
126                 OP_ABS:                {OP_ABS, "ABS", GasLowSlowStep},
127                 OP_NOT:                {OP_NOT, "NOT", GasUnderLowSlowStep},
128                 OP_0NOTEQUAL:          {OP_0NOTEQUAL, "0NOTEQUAL", GasUnderLowSlowStep},
129                 OP_ADD:                {OP_ADD, "ADD", GasUnderLowMidStep},
130                 OP_SUB:                {OP_SUB, "SUB", GasUnderLowMidStep},
131                 OP_MUL:                {OP_MUL, "MUL", GasUnderLowMidStep},
132                 OP_DIV:                {OP_DIV, "DIV", GasUnderLowMidStep},
133                 OP_MOD:                {OP_MOD, "MOD", GasUnderLowMidStep},
134                 OP_LSHIFT:             {OP_LSHIFT, "LSHIFT", GasUnderLowMidStep},
135                 OP_RSHIFT:             {OP_RSHIFT, "RSHIFT", GasUnderLowMidStep},
136                 OP_BOOLAND:            {OP_BOOLAND, "BOOLAND", GasUnderSlowStep},
137                 OP_BOOLOR:             {OP_BOOLOR, "BOOLOR", GasUnderSlowStep},
138                 OP_NUMEQUAL:           {OP_NUMEQUAL, "NUMEQUAL", GasUnderHighMidStep},
139                 OP_NUMEQUALVERIFY:     {OP_NUMEQUALVERIFY, "NUMEQUALVERIFY", GasUnderLowFastStep},
140                 OP_NUMNOTEQUAL:        {OP_NUMNOTEQUAL, "NUMNOTEQUAL", GasUnderHighMidStep},
141                 OP_LESSTHAN:           {OP_LESSTHAN, "LESSTHAN", GasUnderHighMidStep},
142                 OP_GREATERTHAN:        {OP_GREATERTHAN, "GREATERTHAN", GasUnderHighMidStep},
143                 OP_LESSTHANOREQUAL:    {OP_LESSTHANOREQUAL, "LESSTHANOREQUAL", GasUnderHighMidStep},
144                 OP_GREATERTHANOREQUAL: {OP_GREATERTHANOREQUAL, "GREATERTHANOREQUAL", GasUnderHighMidStep},
145                 OP_MIN:                {OP_MIN, "MIN", GasUnderLowMidStep},
146                 OP_MAX:                {OP_MAX, "MAX", GasUnderLowMidStep},
147                 OP_WITHIN:             {OP_WITHIN, "WITHIN", GasUnderFastStep},
148
149                 OP_SHA256:        {OP_SHA256, "SHA256", GasLowMidStep},
150                 OP_SHA3:          {OP_SHA3, "SHA3", GasLowMidStep},
151                 OP_CHECKSIG:      {OP_CHECKSIG, "CHECKSIG", GasFastStep},
152                 OP_CHECKMULTISIG: {OP_CHECKMULTISIG, "CHECKMULTISIG", GasMultiSig},
153                 OP_TXSIGHASH:     {OP_TXSIGHASH, "TXSIGHASH", GasLowFastStep},
154
155                 OP_CHECKOUTPUT: {OP_CHECKOUTPUT, "CHECKOUTPUT", GasMutMidStep},
156                 OP_ASSET:       {OP_ASSET, "ASSET", GasHighMidStep},
157                 OP_AMOUNT:      {OP_AMOUNT, "AMOUNT", GasHighSlowStep},
158                 OP_PROGRAM:     {OP_PROGRAM, "PROGRAM", GasMutLowSlowStep},
159                 //OP_MINTIME:     {OP_MINTIME, "MINTIME", GasHighSlowStep},
160                 //OP_MAXTIME:     {OP_MAXTIME, "MAXTIME", GasHighSlowStep},
161                 //OP_TXDATA:      {OP_TXDATA, "TXDATA", GasHighMidStep},
162                 //OP_ENTRYDATA:   {OP_ENTRYDATA, "ENTRYDATA", GasHighMidStep},
163                 OP_INDEX:    {OP_INDEX, "INDEX", GasHighSlowStep},
164                 OP_ENTRYID:  {OP_ENTRYID, "ENTRYID", GasHighMidStep},
165                 OP_OUTPUTID: {OP_OUTPUTID, "OUTPUTID", GasHighMidStep},
166                 //OP_NONCE:       {OP_NONCE, "NONCE", GasHighMidStep},
167                 OP_BLOCKHEIGHT: {OP_BLOCKHEIGHT, "BLOCKHEIGHT", GasHighSlowStep},
168
169                 OP_CHECKPREDICATE: {OP_CHECKPREDICATE, "CHECKPREDICATE", GasMidStep},
170         }
171
172         gasByName map[string]gasInfo
173 )
174
175 const (
176         GasAmount    int64 = 8
177         GasAsset     int64 = 32
178         GasBoolean   int64 = 1
179         GasHash      int64 = 32
180         GasInteger   int64 = 8
181         GasProgram   int64 = 64
182         GasPublicKey int64 = 32
183         GasSignature int64 = 64
184         GasString    int64 = 32
185         GasTime      int64 = 8
186
187         //push parament factor
188         ContractParams int64 = 9
189         ClauseParams   int64 = 8
190 )
191
192 var paramGasMap = map[string]int64{
193         "Amount":    GasAmount,
194         "Asset":     GasAsset,
195         "Boolean":   GasBoolean,
196         "Hash":      GasHash,
197         "Integer":   GasInteger,
198         "Program":   GasProgram,
199         "PublicKey": GasPublicKey,
200         "Signature": GasSignature,
201         "String":    GasString,
202         "Time":      GasTime,
203 }
204
205 func InitGas() {
206         for i := 1; i <= 75; i++ {
207                 gasop[i] = gasInfo{Op(i), fmt.Sprintf("DATA_%d", i), GasData + int64(i)}
208         }
209
210         for i := uint8(0); i <= 15; i++ {
211                 op := uint8(OP_1) + i
212                 gasop[op] = gasInfo{Op(op), fmt.Sprintf("%d", i+1), GasSlowStep}
213         }
214
215         gasByName = make(map[string]gasInfo)
216         for _, info := range gasop {
217                 gasByName[info.name] = info
218         }
219         gasByName["0"] = gasop[OP_FALSE]
220         gasByName["TRUE"] = gasop[OP_1]
221
222         gasop[OP_0] = gasop[OP_FALSE]
223         gasop[OP_TRUE] = gasop[OP_1]
224
225         for i := 0; i <= 255; i++ {
226                 if gasop[i].name == "" {
227                         gasop[i] = gasInfo{Op(i), fmt.Sprintf("NOPx%02x", i), GasBaseStep}
228                 }
229         }
230 }
231
232 func GetGas(op Op) int64 {
233         return gasop[op].Gas
234 }
235
236 func GetContractParamGas(typename string) int64 {
237         paramGas, ok := paramGasMap[typename]
238         if !ok {
239                 return -1
240         }
241
242         return paramGas + ContractParams
243 }
244
245 func GetClauseParamGas(typename string) int64 {
246         paramGas, ok := paramGasMap[typename]
247         if !ok {
248                 return -1
249         }
250
251         return paramGas + ClauseParams
252 }