9 chainjson "github.com/bytom/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 string `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 // Reqs is the list of requirements (from the clause's "requires"
66 Reqs []*ClauseReq `json:"reqs,omitempty"`
68 statements []statement
70 // BlockHeight is the list of expressions passed to greater()/less() in this
72 BlockHeight []string `json:"blockheight,omitempty"`
74 // HashCalls is the list of hash functions and their arguments used
76 HashCalls []HashCall `json:"hash_calls,omitempty"`
78 // Values is the list of values unlocked or relocked in this clause.
79 Values []ValueInfo `json:"values"`
81 // Contracts is the list of contracts called by this clause.
82 Contracts []string `json:"contracts,omitempty"`
85 // HashCall describes a call to a hash function.
86 type HashCall struct {
87 // HashType is "sha3" or "sha256".
88 HashType string `json:"hash_type"`
90 // Arg is the expression passed to the hash function.
91 Arg string `json:"arg"`
93 // ArgType is the type of Arg.
94 ArgType string `json:"arg_type"`
97 // ClauseReq describes a payment requirement of a clause (one of the
98 // things after the "requires" keyword).
99 type ClauseReq struct {
100 Name string `json:"name"`
102 assetExpr, amountExpr expression
104 // Asset is the expression describing the required asset.
105 Asset string `json:"asset"`
107 // Amount is the expression describing the required amount.
108 Amount string `json:"amount"`
111 type statement interface {
112 countVarRefs(map[string]int)
115 type verifyStatement struct {
119 func (s verifyStatement) countVarRefs(counts map[string]int) {
120 s.expr.countVarRefs(counts)
123 type lockStatement struct {
127 // Added as a decoration, used by CHECKOUTPUT
131 func (s lockStatement) countVarRefs(counts map[string]int) {
132 s.locked.countVarRefs(counts)
133 s.program.countVarRefs(counts)
136 type unlockStatement struct {
140 func (s unlockStatement) countVarRefs(counts map[string]int) {
141 s.expr.countVarRefs(counts)
144 type expression interface {
146 typ(*environ) typeDesc
147 countVarRefs(map[string]int)
150 type binaryExpr struct {
151 left, right expression
155 func (e binaryExpr) String() string {
156 return fmt.Sprintf("(%s %s %s)", e.left, e.op.op, e.right)
159 func (e binaryExpr) typ(*environ) typeDesc {
163 func (e binaryExpr) countVarRefs(counts map[string]int) {
164 e.left.countVarRefs(counts)
165 e.right.countVarRefs(counts)
168 type unaryExpr struct {
173 func (e unaryExpr) String() string {
174 return fmt.Sprintf("%s%s", e.op.op, e.expr)
177 func (e unaryExpr) typ(*environ) typeDesc {
181 func (e unaryExpr) countVarRefs(counts map[string]int) {
182 e.expr.countVarRefs(counts)
185 type callExpr struct {
190 func (e callExpr) String() string {
192 for _, a := range e.args {
193 argStrs = append(argStrs, a.String())
195 return fmt.Sprintf("%s(%s)", e.fn, strings.Join(argStrs, ", "))
198 func (e callExpr) typ(env *environ) typeDesc {
199 if b := referencedBuiltin(e.fn); b != nil {
202 if len(e.args) == 1 {
203 switch e.args[0].typ(env) {
207 return sha3PubkeyType
212 if len(e.args) == 1 {
213 switch e.args[0].typ(env) {
217 return sha256PubkeyType
224 if e.fn.typ(env) == predType {
227 if e.fn.typ(env) == contractType {
233 func (e callExpr) countVarRefs(counts map[string]int) {
234 e.fn.countVarRefs(counts)
235 for _, a := range e.args {
236 a.countVarRefs(counts)
242 func (v varRef) String() string {
246 func (e varRef) typ(env *environ) typeDesc {
247 if entry := env.lookup(string(e)); entry != nil {
253 func (e varRef) countVarRefs(counts map[string]int) {
257 type bytesLiteral []byte
259 func (e bytesLiteral) String() string {
260 return "0x" + hex.EncodeToString([]byte(e))
263 func (bytesLiteral) typ(*environ) typeDesc {
267 func (bytesLiteral) countVarRefs(map[string]int) {}
269 type integerLiteral int64
271 func (e integerLiteral) String() string {
272 return strconv.FormatInt(int64(e), 10)
275 func (integerLiteral) typ(*environ) typeDesc {
279 func (integerLiteral) countVarRefs(map[string]int) {}
281 type booleanLiteral bool
283 func (e booleanLiteral) String() string {
290 func (booleanLiteral) typ(*environ) typeDesc {
294 func (booleanLiteral) countVarRefs(map[string]int) {}
296 type listExpr []expression
298 func (e listExpr) String() string {
300 for _, elt := range e {
301 elts = append(elts, elt.String())
303 return fmt.Sprintf("[%s]", strings.Join(elts, ", "))
306 func (listExpr) typ(*environ) typeDesc {
310 func (e listExpr) countVarRefs(counts map[string]int) {
311 for _, elt := range e {
312 elt.countVarRefs(counts)