9 chainjson "github.com/bytom/vapor/encoding/json"
12 // Contract is a compiled Equity contract.
13 type Contract struct {
14 // Name is the contract name.
15 Name string `json:"name"`
17 // Params is the list of contract parameters.
18 Params []*Param `json:"params,omitempty"`
20 // Clauses is the list of contract clauses.
21 Clauses []*Clause `json:"clauses"`
23 // Value is the name of the value locked by the contract.
24 Value ValueInfo `json:"value"`
26 // Body is the optimized bytecode of the contract body. This is not
27 // a complete program! Use instantiate to turn this (plus some
28 // arguments) into a program.
29 Body chainjson.HexBytes `json:"body_bytecode"`
31 // Opcodes is the human-readable string of opcodes corresponding to
33 Opcodes string `json:"body_opcodes,omitempty"`
35 // Recursive tells whether this contract calls itself. (This is
36 // used to select between two possible instantiation options.)
37 Recursive bool `json:"recursive"`
39 // Pre-optimized list of instruction steps, with stack snapshots.
40 Steps []Step `json:"-"`
43 // Param is a contract or clause parameter.
45 // Name is the parameter name.
46 Name string `json:"name"`
48 // Type is the declared parameter type.
49 Type typeDesc `json:"type"`
51 // InferredType, if available, is a more-specific type than Type,
52 // inferred from the logic of the contract.
53 InferredType typeDesc `json:"inferred_type,omitempty"`
56 // Clause is a compiled contract clause.
58 // Name is the clause name.
59 Name string `json:"name"`
61 // Params is the list of clause parameters.
62 Params []*Param `json:"params,omitempty"`
64 statements []statement
66 // BlockHeight is the list of expressions passed to greater()/less() in this
68 BlockHeight []string `json:"blockheight,omitempty"`
70 // HashCalls is the list of hash functions and their arguments used
72 HashCalls []HashCall `json:"hash_calls,omitempty"`
74 // Values is the list of values unlocked or relocked in this clause.
75 Values []ValueInfo `json:"values"`
77 // Contracts is the list of contracts called by this clause.
78 Contracts []string `json:"contracts,omitempty"`
81 // HashCall describes a call to a hash function.
82 type HashCall struct {
83 // HashType is "sha3" or "sha256".
84 HashType string `json:"hash_type"`
86 // Arg is the expression passed to the hash function.
87 Arg string `json:"arg"`
89 // ArgType is the type of Arg.
90 ArgType string `json:"arg_type"`
93 // IfBody describes a if ... else ... struct
94 type IfStatmentBody struct {
98 // else statements body
102 type statement interface {
103 countVarRefs(map[string]int)
106 type defineStatement struct {
111 func (s defineStatement) countVarRefs(counts map[string]int) {
112 s.expr.countVarRefs(counts)
115 type assignStatement struct {
120 func (s assignStatement) countVarRefs(counts map[string]int) {
121 s.expr.countVarRefs(counts)
124 type ifStatement struct {
129 func (s ifStatement) countVarRefs(counts map[string]int) {
130 s.condition.countVarRefs(counts)
133 type verifyStatement struct {
137 func (s verifyStatement) countVarRefs(counts map[string]int) {
138 s.expr.countVarRefs(counts)
141 type lockStatement struct {
142 lockedAmount expression
143 lockedAsset expression
146 // Added as a decoration, used by CHECKOUTPUT
150 func (s lockStatement) countVarRefs(counts map[string]int) {
151 s.lockedAmount.countVarRefs(counts)
152 s.lockedAsset.countVarRefs(counts)
153 s.program.countVarRefs(counts)
156 type unlockStatement struct {
157 unlockedAmount expression
158 unlockedAsset expression
161 func (s unlockStatement) countVarRefs(counts map[string]int) {
162 s.unlockedAmount.countVarRefs(counts)
163 s.unlockedAsset.countVarRefs(counts)
166 type expression interface {
168 typ(*environ) typeDesc
169 countVarRefs(map[string]int)
172 type binaryExpr struct {
173 left, right expression
177 func (e binaryExpr) String() string {
178 return fmt.Sprintf("(%s %s %s)", e.left, e.op.op, e.right)
181 func (e binaryExpr) typ(*environ) typeDesc {
185 func (e binaryExpr) countVarRefs(counts map[string]int) {
186 e.left.countVarRefs(counts)
187 e.right.countVarRefs(counts)
190 type unaryExpr struct {
195 func (e unaryExpr) String() string {
196 return fmt.Sprintf("%s%s", e.op.op, e.expr)
199 func (e unaryExpr) typ(*environ) typeDesc {
203 func (e unaryExpr) countVarRefs(counts map[string]int) {
204 e.expr.countVarRefs(counts)
207 type callExpr struct {
212 func (e callExpr) String() string {
214 for _, a := range e.args {
215 argStrs = append(argStrs, a.String())
217 return fmt.Sprintf("%s(%s)", e.fn, strings.Join(argStrs, ", "))
220 func (e callExpr) typ(env *environ) typeDesc {
221 if b := referencedBuiltin(e.fn); b != nil {
224 if len(e.args) == 1 {
225 switch e.args[0].typ(env) {
229 return sha3PubkeyType
234 if len(e.args) == 1 {
235 switch e.args[0].typ(env) {
239 return sha256PubkeyType
246 if e.fn.typ(env) == predType {
249 if e.fn.typ(env) == contractType {
255 func (e callExpr) countVarRefs(counts map[string]int) {
256 e.fn.countVarRefs(counts)
257 for _, a := range e.args {
258 a.countVarRefs(counts)
264 func (v varRef) String() string {
268 func (e varRef) typ(env *environ) typeDesc {
269 if entry := env.lookup(string(e)); entry != nil {
275 func (e varRef) countVarRefs(counts map[string]int) {
279 type bytesLiteral []byte
281 func (e bytesLiteral) String() string {
282 return "0x" + hex.EncodeToString([]byte(e))
285 func (bytesLiteral) typ(*environ) typeDesc {
289 func (bytesLiteral) countVarRefs(map[string]int) {}
291 type integerLiteral int64
293 func (e integerLiteral) String() string {
294 return strconv.FormatInt(int64(e), 10)
297 func (integerLiteral) typ(*environ) typeDesc {
301 func (integerLiteral) countVarRefs(map[string]int) {}
303 type booleanLiteral bool
305 func (e booleanLiteral) String() string {
312 func (booleanLiteral) typ(*environ) typeDesc {
316 func (booleanLiteral) countVarRefs(map[string]int) {}
318 type listExpr []expression
320 func (e listExpr) String() string {
322 for _, elt := range e {
323 elts = append(elts, elt.String())
325 return fmt.Sprintf("[%s]", strings.Join(elts, ", "))
328 func (listExpr) typ(*environ) typeDesc {
332 func (e listExpr) countVarRefs(counts map[string]int) {
333 for _, elt := range e {
334 elt.countVarRefs(counts)