9 chainjson "github.com/bytom/encoding/json"
10 "github.com/bytom/errors"
11 "github.com/bytom/protocol/vm"
12 "github.com/bytom/protocol/vm/vmutil"
15 // ValueInfo describes how a blockchain value is used in a contract
17 type ValueInfo struct {
18 // Name is the clause's name for this value.
19 Name string `json:"name"`
21 // Program is the program expression used to the lock the value, if
22 // the value is locked with "lock." If it's unlocked with "unlock"
23 // instead, this is empty.
24 Program string `json:"program,omitempty"`
26 // Asset is the expression describing the asset type the value must
27 // have, as it appears in a clause's "requires" section. If this is
28 // the contract value instead, this is empty.
29 Asset string `json:"asset,omitempty"`
31 // Amount is the expression describing the amount the value must
32 // have, as it appears in a clause's "requires" section. If this is
33 // the contract value instead, this is empty.
34 Amount string `json:"amount,omitempty"`
37 // ContractArg is an argument with which to instantiate a contract as
38 // a program. Exactly one of B, I, and S should be supplied.
39 type ContractArg struct {
40 B *bool `json:"boolean,omitempty"`
41 I *int64 `json:"integer,omitempty"`
42 S *chainjson.HexBytes `json:"string,omitempty"`
45 // Compile parses a sequence of Equity contracts from the supplied reader
46 // and produces Contract objects containing the compiled bytecode and
47 // other analysis. If argMap is non-nil, it maps contract names to
48 // lists of arguments with which to instantiate them as programs, with
49 // the results placed in the contract's Program field. A contract
50 // named in argMap but not found in the input is silently ignored.
51 func Compile(r io.Reader) ([]*Contract, error) {
52 inp, err := ioutil.ReadAll(r)
54 return nil, errors.Wrap(err, "reading input")
56 contracts, err := parse(inp)
58 return nil, errors.Wrap(err, "parse error")
61 globalEnv := newEnviron(nil)
62 for _, k := range keywords {
63 globalEnv.add(k, nilType, roleKeyword)
65 for _, b := range builtins {
66 globalEnv.add(b.name, nilType, roleBuiltin)
69 // All contracts must be checked for recursiveness before any are
71 for _, contract := range contracts {
72 contract.Recursive = checkRecursive(contract)
75 for _, contract := range contracts {
76 err = globalEnv.addContract(contract)
82 for _, contract := range contracts {
83 err = compileContract(contract, globalEnv)
85 return nil, errors.Wrap(err, "compiling contract")
87 for _, clause := range contract.Clauses {
88 for _, stmt := range clause.statements {
89 switch s := stmt.(type) {
91 valueInfo := ValueInfo{
92 Amount: s.lockedAmount.String(),
93 Asset: s.lockedAsset.String(),
94 Program: s.program.String(),
97 clause.Values = append(clause.Values, valueInfo)
98 case *unlockStatement:
99 valueInfo := ValueInfo{
100 Amount: contract.Value.Amount,
101 Asset: contract.Value.Asset,
103 clause.Values = append(clause.Values, valueInfo)
109 return contracts, nil
112 func Instantiate(body []byte, params []*Param, recursive bool, args []ContractArg) ([]byte, error) {
113 if len(args) != len(params) {
114 return nil, fmt.Errorf("got %d argument(s), want %d", len(args), len(params))
117 // typecheck args against param types
118 for i, param := range params {
121 case amountType, intType:
123 return nil, fmt.Errorf("type mismatch in arg %d (want integer)", i)
125 case assetType, hashType, progType, pubkeyType, sigType, strType:
127 return nil, fmt.Errorf("type mismatch in arg %d (want string)", i)
131 return nil, fmt.Errorf("type mismatch in arg %d (want boolean)", i)
136 b := vmutil.NewBuilder()
138 for i := len(args) - 1; i >= 0; i-- {
155 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
157 b.AddOp(vm.OP_DEPTH).AddOp(vm.OP_OVER)
159 // <argN> <argN-1> ... <arg1> DEPTH <body> 0 CHECKPREDICATE
164 b.AddOp(vm.OP_CHECKPREDICATE)
168 func compileContract(contract *Contract, globalEnv *environ) error {
171 if len(contract.Clauses) == 0 {
172 return fmt.Errorf("empty contract")
174 env := newEnviron(globalEnv)
175 for _, p := range contract.Params {
176 err = env.add(p.Name, p.Type, roleContractParam)
182 // value is spilt with valueAmount and valueAsset
183 if err = env.add(contract.Value.Amount, amountType, roleContractValue); err != nil {
186 if err = env.add(contract.Value.Asset, assetType, roleContractValue); err != nil {
190 for _, c := range contract.Clauses {
191 err = env.add(c.Name, nilType, roleClause)
197 err = prohibitSigParams(contract)
201 err = requireAllParamsUsedInClauses(contract.Params, contract.Clauses)
208 if len(contract.Clauses) > 1 {
209 stk = stk.add("<clause selector>")
212 for i := len(contract.Params) - 1; i >= 0; i-- {
213 p := contract.Params[i]
214 stk = stk.add(p.Name)
217 if contract.Recursive {
218 stk = stk.add(contract.Name)
222 sequence := 0 // sequence is used to count the number of ifStatements
224 if len(contract.Clauses) == 1 {
225 err = compileClause(b, stk, contract, env, contract.Clauses[0], &sequence)
230 if len(contract.Params) > 0 {
231 // A clause selector is at the bottom of the stack. Roll it to the
233 n := len(contract.Params)
234 if contract.Recursive {
237 stk = b.addRoll(stk, n) // stack: [<clause params> <contract params> [<maybe contract body>] <clause selector>]
243 for i := len(contract.Clauses) - 1; i >= 2; i-- {
244 stk = b.addDup(stk) // stack: [... <clause selector> <clause selector>]
245 stk = b.addInt64(stk, int64(i)) // stack: [... <clause selector> <clause selector> <i>]
246 stk = b.addNumEqual(stk, fmt.Sprintf("(<clause selector> == %d)", i)) // stack: [... <clause selector> <i == clause selector>]
247 stk = b.addJumpIf(stk, contract.Clauses[i].Name) // stack: [... <clause selector>]
248 stk2 = stk // stack starts here for clauses 2 through N-1
252 stk = b.addJumpIf(stk, contract.Clauses[1].Name) // consumes the clause selector
254 // no jump needed for clause 0
256 for i, clause := range contract.Clauses {
258 // Clauses 0 and 1 have no clause selector on top of the
259 // stack. Clauses 2 and later do.
263 b.addJumpTarget(stk, clause.Name)
269 err = compileClause(b, stk, contract, env, clause, &sequence)
271 return errors.Wrapf(err, "compiling clause \"%s\"", clause.Name)
273 b.forgetPendingVerify()
274 if i < len(contract.Clauses)-1 {
275 b.addJump(stk, "_end")
278 b.addJumpTarget(stk, "_end")
281 opcodes := optimize(b.opcodes())
282 prog, err := vm.Assemble(opcodes)
288 contract.Opcodes = opcodes
290 contract.Steps = b.steps()
295 func compileClause(b *builder, contractStk stack, contract *Contract, env *environ, clause *Clause, sequence *int) error {
298 // copy env to leave outerEnv unchanged
299 env = newEnviron(env)
300 for _, p := range clause.Params {
301 err = env.add(p.Name, p.Type, roleClauseParam)
307 if err = assignIndexes(clause); err != nil {
312 for _, p := range clause.Params {
313 // NOTE: the order of clause params is not reversed, unlike
314 // contract params (and also unlike the arguments to Equity
316 stk = stk.add(p.Name)
318 stk = stk.addFromStack(contractStk)
320 // a count of the number of times each variable is referenced
321 counts := make(map[string]int)
322 for _, s := range clause.statements {
323 if stmt, ok := s.(*defineStatement); ok && stmt.expr == nil {
327 s.countVarRefs(counts)
328 if stmt, ok := s.(*ifStatement); ok {
329 for _, trueStmt := range stmt.body.trueBody {
330 trueStmt.countVarRefs(counts)
333 for _, falseStmt := range stmt.body.falseBody {
334 falseStmt.countVarRefs(counts)
339 for _, stat := range clause.statements {
340 if stk, err = compileStatement(b, stk, contract, env, clause, counts, stat, sequence); err != nil {
345 err = requireAllValuesDisposedOnce(contract, clause)
349 err = typeCheckClause(contract, clause, env)
353 err = requireAllParamsUsedInClause(clause.Params, clause)
361 func compileStatement(b *builder, stk stack, contract *Contract, env *environ, clause *Clause, counts map[string]int, stat statement, sequence *int) (stack, error) {
363 switch stmt := stat.(type) {
365 // sequence add 1 when the statement is ifStatement
367 strSequence := fmt.Sprintf("%d", *sequence)
369 // compile condition expression
370 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.condition)
372 return stk, errors.Wrapf(err, "in check condition of ifStatement in clause \"%s\"", clause.Name)
375 // jump to falseBody when condition is false, while the JUMPIF instruction will be run success when
376 // the value of dataStack is true, therefore add this check
377 conditionExpr := stk.str
378 stk = b.addBoolean(stk, false)
379 stk = b.addEqual(stk, fmt.Sprintf("(%s == false)", conditionExpr)) // stack: [... <condition_result == false>]
383 if len(stmt.body.falseBody) != 0 {
384 label = "else_" + strSequence
386 label = "endif_" + strSequence
388 stk = b.addJumpIf(stk, label)
389 b.addJumpTarget(stk, "if_"+strSequence)
391 // temporary store stack and counts for falseBody
393 elseCounts := make(map[string]int)
394 for k, v := range counts {
398 // compile trueBody statements
399 if len(stmt.body.trueBody) != 0 {
400 for _, st := range stmt.body.trueBody {
401 st.countVarRefs(counts)
404 // modify value amount because of using only once
405 if counts[contract.Value.Amount] > 1 {
406 counts[contract.Value.Amount] = 1
409 // modify value asset because of using only once
410 if counts[contract.Value.Asset] > 1 {
411 counts[contract.Value.Asset] = 1
414 for _, st := range stmt.body.trueBody {
415 if stk, err = compileStatement(b, stk, contract, env, clause, counts, st, sequence); err != nil {
421 // compile falseBody statements
422 if len(stmt.body.falseBody) != 0 {
423 counts := make(map[string]int)
424 for k, v := range elseCounts {
428 for _, st := range stmt.body.falseBody {
429 st.countVarRefs(counts)
432 // modify value amount because of using only once
433 if counts[contract.Value.Amount] > 1 {
434 counts[contract.Value.Amount] = 1
437 // modify value asset because of using only once
438 if counts[contract.Value.Asset] > 1 {
439 counts[contract.Value.Asset] = 1
443 b.addJump(stk, "endif_"+strSequence)
444 b.addJumpTarget(stk, "else_"+strSequence)
446 for _, st := range stmt.body.falseBody {
447 if stk, err = compileStatement(b, stk, contract, env, clause, counts, st, sequence); err != nil {
452 b.addJumpTarget(stk, "endif_"+strSequence)
454 case *defineStatement:
455 // add environ for define variable
456 if err = env.add(stmt.variable.Name, stmt.variable.Type, roleClauseVariable); err != nil {
460 // check whether the variable is used or not
461 if counts[stmt.variable.Name] == 0 {
462 return stk, fmt.Errorf("the defined variable \"%s\" is unused in clause \"%s\"", stmt.variable.Name, clause.Name)
465 if stmt.expr != nil {
467 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.expr)
469 return stk, errors.Wrapf(err, "in define statement in clause \"%s\"", clause.Name)
473 stk.str = stmt.variable.Name
476 case *assignStatement:
477 // find variable from environ with roleClauseVariable
478 if entry := env.lookup(string(stmt.variable.Name)); entry != nil {
479 if entry.r != roleClauseVariable {
480 return stk, fmt.Errorf("the type of variable is not roleClauseVariable in assign statement in clause \"%s\"", clause.Name)
482 stmt.variable.Type = entry.t
484 return stk, fmt.Errorf("the variable \"%s\" is not defined before the assign statement in clause \"%s\"", stmt.variable.Name, clause.Name)
487 // temporary store the counts of defined variable
488 varCount := counts[stmt.variable.Name]
490 // calculate the counts of variable for assign statement
491 tmpCounts := make(map[string]int)
492 stmt.countVarRefs(tmpCounts)
494 // modify the map counts of defined variable to 1 and minus the number of defined variable
495 // when the assign expression contains the defined variable
496 if tmpCounts[stmt.variable.Name] > 0 {
497 counts[stmt.variable.Name] = 1
498 varCount -= tmpCounts[stmt.variable.Name]
500 depth := stk.find(stmt.variable.Name)
507 stk = b.addRoll(stk, depth)
513 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.expr)
515 return stk, errors.Wrapf(err, "in define statement in clause \"%s\"", clause.Name)
518 // restore the defined variable counts
519 counts[stmt.variable.Name] = varCount
522 stk.str = stmt.variable.Name
524 case *verifyStatement:
525 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.expr)
527 return stk, errors.Wrapf(err, "in verify statement in clause \"%s\"", clause.Name)
529 stk = b.addVerify(stk)
531 // special-case reporting of certain function calls
532 if c, ok := stmt.expr.(*callExpr); ok && len(c.args) == 1 {
533 if b := referencedBuiltin(c.fn); b != nil {
536 clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
538 clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
545 stk = b.addInt64(stk, stmt.index)
547 // TODO: permit more complex expressions for locked,
548 // like "lock x+y with foo" (?)
550 if stmt.lockedAmount.String() == contract.Value.Amount && stmt.lockedAsset.String() == contract.Value.Asset {
551 stk = b.addAmount(stk, contract.Value.Amount)
552 stk = b.addAsset(stk, contract.Value.Asset)
554 // calculate the counts of variable for lockStatement
555 lockCounts := make(map[string]int)
556 stmt.countVarRefs(lockCounts)
560 case stmt.lockedAmount.String() == contract.Value.Amount:
561 stk = b.addAmount(stk, contract.Value.Amount)
562 case stmt.lockedAmount.String() != contract.Value.Amount && lockCounts[contract.Value.Amount] > 0:
563 stk = b.addAmount(stk, contract.Value.Amount)
564 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAmount)
566 return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
569 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAmount)
571 return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
577 case stmt.lockedAsset.String() == contract.Value.Asset:
578 stk = b.addAsset(stk, contract.Value.Asset)
579 case stmt.lockedAsset.String() != contract.Value.Asset && lockCounts[contract.Value.Asset] > 0:
580 stk = b.addAsset(stk, contract.Value.Asset)
581 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAsset)
583 return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
586 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.lockedAsset)
588 return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
594 stk = b.addInt64(stk, 1)
597 stk, err = compileExpr(b, stk, contract, clause, env, counts, stmt.program)
599 return stk, errors.Wrapf(err, "in lock statement in clause \"%s\"", clause.Name)
602 stk = b.addCheckOutput(stk, fmt.Sprintf("checkOutput(%s, %s, %s)",
603 stmt.lockedAmount.String(), stmt.lockedAsset.String(), stmt.program))
604 stk = b.addVerify(stk)
606 case *unlockStatement:
607 if len(clause.statements) == 1 {
608 // This is the only statement in the clause, make sure TRUE is
610 stk = b.addBoolean(stk, true)
617 func compileExpr(b *builder, stk stack, contract *Contract, clause *Clause, env *environ, counts map[string]int, expr expression) (stack, error) {
620 switch e := expr.(type) {
622 // Do typechecking after compiling subexpressions (because other
623 // compilation errors are more interesting than type mismatch
626 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.left)
628 return stk, errors.Wrapf(err, "in left operand of \"%s\" expression", e.op.op)
630 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.right)
632 return stk, errors.Wrapf(err, "in right operand of \"%s\" expression", e.op.op)
635 lType := e.left.typ(env)
636 if e.op.left != "" && !(lType == e.op.left || lType == amountType) {
637 return stk, fmt.Errorf("in \"%s\", left operand has type \"%s\", must be \"%s\"", e, lType, e.op.left)
640 rType := e.right.typ(env)
641 if e.op.right != "" && !(rType == e.op.right || rType == amountType) {
642 return stk, fmt.Errorf("in \"%s\", right operand has type \"%s\", must be \"%s\"", e, rType, e.op.right)
648 // Maybe one is Hash and the other is (more-specific-Hash subtype).
649 // TODO(bobg): generalize this mechanism
650 if lType == hashType && isHashSubtype(rType) {
651 propagateType(contract, clause, env, rType, e.left)
652 } else if rType == hashType && isHashSubtype(lType) {
653 propagateType(contract, clause, env, lType, e.right)
655 return stk, fmt.Errorf("type mismatch in \"%s\": left operand has type \"%s\", right operand has type \"%s\"", e, lType, rType)
658 if lType == "Boolean" {
659 return stk, fmt.Errorf("in \"%s\": using \"%s\" on Boolean values not allowed", e, e.op.op)
663 stk = b.addOps(stk.dropN(2), e.op.opcodes, e.String())
666 // Do typechecking after compiling subexpression (because other
667 // compilation errors are more interesting than type mismatch
671 stk, err = compileExpr(b, stk, contract, clause, env, counts, e.expr)
673 return stk, errors.Wrapf(err, "in \"%s\" expression", e.op.op)
676 if e.op.operand != "" && e.expr.typ(env) != e.op.operand {
677 return stk, fmt.Errorf("in \"%s\", operand has type \"%s\", must be \"%s\"", e, e.expr.typ(env), e.op.operand)
679 b.addOps(stk.drop(), e.op.opcodes, e.String())
682 bi := referencedBuiltin(e.fn)
684 if v, ok := e.fn.(varRef); ok {
685 if entry := env.lookup(string(v)); entry != nil && entry.t == contractType {
686 clause.Contracts = append(clause.Contracts, entry.c.Name)
688 partialName := fmt.Sprintf("%s(...)", v)
689 stk = b.addData(stk, nil)
691 if len(e.args) != len(entry.c.Params) {
692 return stk, fmt.Errorf("contract \"%s\" expects %d argument(s), got %d", entry.c.Name, len(entry.c.Params), len(e.args))
695 for i := len(e.args) - 1; i >= 0; i-- {
697 if entry.c.Params[i].Type != "" && arg.typ(env) != entry.c.Params[i].Type {
698 return stk, fmt.Errorf("argument %d to contract \"%s\" has type \"%s\", must be \"%s\"", i, entry.c.Name, arg.typ(env), entry.c.Params[i].Type)
700 stk, err = compileExpr(b, stk, contract, clause, env, counts, arg)
704 stk = b.addCatPushdata(stk, partialName)
708 case entry.c == contract:
709 // Recursive call - cannot use entry.c.Body
710 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
711 stk, err = compileRef(b, stk, counts, varRef(contract.Name))
713 return stk, errors.Wrap(err, "compiling contract call")
715 stk = b.addCatPushdata(stk, partialName)
716 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH), byte(vm.OP_OVER)})
717 stk = b.addCat(stk, partialName)
719 case entry.c.Recursive:
720 // Non-recursive call to a (different) recursive contract
721 // <argN> <argN-1> ... <arg1> <body> DEPTH OVER 0 CHECKPREDICATE
722 if len(entry.c.Body) == 0 {
723 // TODO(bobg): sort input contracts topologically to permit forward calling
724 return stk, fmt.Errorf("contract \"%s\" not defined", entry.c.Name)
726 stk = b.addData(stk, entry.c.Body)
727 stk = b.addCatPushdata(stk, partialName)
728 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH), byte(vm.OP_OVER)})
729 stk = b.addCat(stk, partialName)
732 // Non-recursive call to non-recursive contract
733 // <argN> <argN-1> ... <arg1> DEPTH <body> 0 CHECKPREDICATE
734 stk = b.addData(stk, []byte{byte(vm.OP_DEPTH)})
735 stk = b.addCat(stk, partialName)
736 if len(entry.c.Body) == 0 {
737 // TODO(bobg): sort input contracts topologically to permit forward calling
738 return stk, fmt.Errorf("contract \"%s\" not defined", entry.c.Name)
740 stk = b.addData(stk, entry.c.Body)
741 stk = b.addCatPushdata(stk, partialName)
743 stk = b.addData(stk, vm.Int64Bytes(0))
744 stk = b.addCatPushdata(stk, partialName)
745 stk = b.addData(stk, []byte{byte(vm.OP_CHECKPREDICATE)})
746 stk = b.addCat(stk, e.String())
751 return stk, fmt.Errorf("unknown function \"%s\"", e.fn)
754 if len(e.args) != len(bi.args) {
755 return stk, fmt.Errorf("wrong number of args for \"%s\": have %d, want %d", bi.name, len(e.args), len(bi.args))
758 // WARNING WARNING WOOP WOOP
760 // WARNING WARNING WOOP WOOP
761 if bi.name == "checkTxMultiSig" {
762 if _, ok := e.args[0].(listExpr); !ok {
763 return stk, fmt.Errorf("checkTxMultiSig expects list literals, got %T for argument 0", e.args[0])
765 if _, ok := e.args[1].(listExpr); !ok {
766 return stk, fmt.Errorf("checkTxMultiSig expects list literals, got %T for argument 1", e.args[1])
771 stk, k1, err = compileArg(b, stk, contract, clause, env, counts, e.args[1])
776 // stack: [... sigM ... sig1 M]
779 stk, altEntry = b.addToAltStack(stk) // stack: [... sigM ... sig1]
780 stk = b.addTxSigHash(stk) // stack: [... sigM ... sig1 txsighash]
782 stk, k2, err = compileArg(b, stk, contract, clause, env, counts, e.args[0])
787 // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 N]
789 stk = b.addFromAltStack(stk, altEntry) // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 N M]
790 stk = b.addSwap(stk) // stack: [... sigM ... sig1 txsighash pubkeyN ... pubkey1 M N]
791 stk = b.addCheckMultisig(stk, k1+k2, e.String())
798 for i := len(e.args) - 1; i >= 0; i-- {
802 stk, k2, err = compileArg(b, stk, contract, clause, env, counts, a)
804 return stk, errors.Wrapf(err, "compiling argument %d in call expression", i)
809 // Do typechecking after compiling subexpressions (because other
810 // compilation errors are more interesting than type mismatch
812 for i, actual := range e.args {
813 if bi.args[i] != "" && actual.typ(env) != bi.args[i] {
814 return stk, fmt.Errorf("argument %d to \"%s\" has type \"%s\", must be \"%s\"", i, bi.name, actual.typ(env), bi.args[i])
818 stk = b.addOps(stk.dropN(k), bi.opcodes, e.String())
820 // special-case reporting
822 case "sha3", "sha256":
823 clause.HashCalls = append(clause.HashCalls, HashCall{bi.name, e.args[0].String(), string(e.args[0].typ(env))})
827 return compileRef(b, stk, counts, e)
830 stk = b.addInt64(stk, int64(e))
833 stk = b.addData(stk, []byte(e))
836 stk = b.addBoolean(stk, bool(e))
839 // Lists are excluded here because they disobey the invariant of
840 // this function: namely, that it increases the stack size by
841 // exactly one. (A list pushes its items and its length on the
842 // stack.) But they're OK as function-call arguments because the
843 // function (presumably) consumes all the stack items added.
844 return stk, fmt.Errorf("encountered list outside of function-call context")
849 func compileArg(b *builder, stk stack, contract *Contract, clause *Clause, env *environ, counts map[string]int, expr expression) (stack, int, error) {
851 if list, ok := expr.(listExpr); ok {
852 for i := 0; i < len(list); i++ {
853 elt := list[len(list)-i-1]
855 stk, err = compileExpr(b, stk, contract, clause, env, counts, elt)
861 stk = b.addInt64(stk, int64(len(list)))
866 stk, err = compileExpr(b, stk, contract, clause, env, counts, expr)
870 func compileRef(b *builder, stk stack, counts map[string]int, ref varRef) (stack, error) {
871 depth := stk.find(string(ref))
873 return stk, fmt.Errorf("undefined reference: \"%s\"", ref)
877 if count, ok := counts[string(ref)]; ok && count > 0 {
879 counts[string(ref)] = count
896 stk = b.addRoll(stk, depth)
898 stk = b.addPick(stk, depth)
904 func (a *ContractArg) UnmarshalJSON(b []byte) error {
905 var m map[string]json.RawMessage
906 err := json.Unmarshal(b, &m)
910 if r, ok := m["boolean"]; ok {
912 err = json.Unmarshal(r, &bval)
919 if r, ok := m["integer"]; ok {
921 err = json.Unmarshal(r, &ival)
930 return fmt.Errorf("contract arg must define one of boolean, integer, string")
932 var sval chainjson.HexBytes
933 err = json.Unmarshal(r, &sval)