OSDN Git Service

modify equity to available (#1127)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Mon, 9 Jul 2018 08:36:16 +0000 (16:36 +0800)
committerPaladz <yzhu101@uottawa.ca>
Mon, 9 Jul 2018 08:36:16 +0000 (16:36 +0800)
* modify function name from greater/less to below/above

* modify CHECKOUTPUT instruction

* modify path

* modify ivy to equity

* add func IsPushdata

equity/compiler/ast.go
equity/compiler/builder.go
equity/compiler/builtins.go
equity/compiler/cmd/equitycmd/equitycmd.go [moved from equity/compiler/cmd/ivyc/ivyc.go with 98% similarity]
equity/compiler/compile.go
equity/compiler/compile_test.go
equity/compiler/doc.go
equity/compiler/equitytest/equitytest.go [moved from equity/compiler/ivytest/ivytest.go with 89% similarity]
protocol/vm/ops.go
protocol/vm/ops_test.go

index 5a03ce6..8eea5a0 100644 (file)
@@ -9,7 +9,7 @@ import (
        chainjson "github.com/bytom/encoding/json"
 )
 
-// Contract is a compiled Ivy contract.
+// Contract is a compiled Equity contract.
 type Contract struct {
        // Name is the contract name.
        Name string `json:"name"`
index 180896f..757f9b6 100644 (file)
@@ -94,7 +94,7 @@ func (b *builder) addAsset(stk stack) stack {
 }
 
 func (b *builder) addCheckOutput(stk stack, desc string) stack {
-       return b.add("CHECKOUTPUT", stk.dropN(6).add(desc))
+       return b.add("CHECKOUTPUT", stk.dropN(5).add(desc))
 }
 
 func (b *builder) addBoolean(stk stack, val bool) stack {
index 809854d..cdf4530 100644 (file)
@@ -17,8 +17,8 @@ var builtins = []builtin{
        {"checkTxSig", "TXSIGHASH SWAP CHECKSIG", []typeDesc{pubkeyType, sigType}, boolType},
        {"concat", "CAT", []typeDesc{nilType, nilType}, strType},
        {"concatpush", "CATPUSHDATA", []typeDesc{nilType, nilType}, strType},
-       {"greater", "BLOCKHEIGHT GREATERTHAN", []typeDesc{intType}, boolType},
-       {"less", "BLOCKHEIGHT LESSTHAN", []typeDesc{intType}, boolType},
+       {"below", "BLOCKHEIGHT GREATERTHAN", []typeDesc{intType}, boolType},
+       {"above", "BLOCKHEIGHT LESSTHAN", []typeDesc{intType}, boolType},
        {"checkTxMultiSig", "", []typeDesc{listType, listType}, boolType}, // WARNING WARNING WOOP WOOP special case
 }
 
similarity index 98%
rename from equity/compiler/cmd/ivyc/ivyc.go
rename to equity/compiler/cmd/equitycmd/equitycmd.go
index e7eab22..6f44c14 100644 (file)
@@ -26,8 +26,8 @@ func main() {
                "bytes":        true,
                "encoding/hex": true,
                "fmt":          true,
-               "github.com/bytom/exp/ivy/compiler": true,
-               "github.com/bytom/protocol/vm":      true,
+               "github.com/bytom/equity/compiler": true,
+               "github.com/bytom/protocol/vm":     true,
        }
 
        buf := new(bytes.Buffer)
index 4b0d2cf..99fe605 100644 (file)
@@ -42,7 +42,7 @@ type ContractArg struct {
        S *chainjson.HexBytes `json:"string,omitempty"`
 }
 
-// Compile parses a sequence of Ivy contracts from the supplied reader
+// Compile parses a sequence of Equity contracts from the supplied reader
 // and produces Contract objects containing the compiled bytecode and
 // other analysis. If argMap is non-nil, it maps contract names to
 // lists of arguments with which to instantiate them as programs, with
@@ -319,7 +319,7 @@ func compileClause(b *builder, contractStk stack, contract *Contract, env *envir
        var stk stack
        for _, p := range clause.Params {
                // NOTE: the order of clause params is not reversed, unlike
-               // contract params (and also unlike the arguments to Ivy
+               // contract params (and also unlike the arguments to Equity
                // function-calls).
                stk = stk.add(p.Name)
        }
@@ -348,9 +348,9 @@ func compileClause(b *builder, contractStk stack, contract *Contract, env *envir
                        if c, ok := stmt.expr.(*callExpr); ok && len(c.args) == 1 {
                                if b := referencedBuiltin(c.fn); b != nil {
                                        switch b.name {
-                                       case "greater":
+                                       case "below":
                                                clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
-                                       case "less":
+                                       case "above":
                                                clause.BlockHeight = append(clause.BlockHeight, c.args[0].String())
                                        }
                                }
@@ -360,9 +360,6 @@ func compileClause(b *builder, contractStk stack, contract *Contract, env *envir
                        // index
                        stk = b.addInt64(stk, stmt.index)
 
-                       // refdatahash
-                       stk = b.addData(stk, nil)
-
                        // TODO: permit more complex expressions for locked,
                        // like "lock x+y with foo" (?)
 
index 2f6c102..ea3902b 100644 (file)
@@ -6,7 +6,7 @@ import (
        "strings"
        "testing"
 
-       "github.com/bytom/equity/compiler/ivytest"
+       "github.com/bytom/equity/compiler/equitytest"
 )
 
 func TestCompile(t *testing.T) {
@@ -17,63 +17,63 @@ func TestCompile(t *testing.T) {
        }{
                {
                        "TrivialLock",
-                       ivytest.TrivialLock,
+                       equitytest.TrivialLock,
                        `[{"name":"TrivialLock","clauses":[{"name":"trivialUnlock","values":[{"name":"locked"}]}],"value":"locked","body_bytecode":"51","body_opcodes":"TRUE","recursive":false}]`,
                },
                {
                        "LockWithPublicKey",
-                       ivytest.LockWithPublicKey,
+                       equitytest.LockWithPublicKey,
                        `[{"name":"LockWithPublicKey","params":[{"name":"publicKey","type":"PublicKey"}],"clauses":[{"name":"unlockWithSig","params":[{"name":"sig","type":"Signature"}],"values":[{"name":"locked"}]}],"value":"locked","body_bytecode":"ae7cac","body_opcodes":"TXSIGHASH SWAP CHECKSIG","recursive":false}]`,
                },
                {
                        "LockWithPublicKeyHash",
-                       ivytest.LockWithPKHash,
+                       equitytest.LockWithPKHash,
                        `[{"name":"LockWithPublicKeyHash","params":[{"name":"pubKeyHash","type":"Hash","inferred_type":"Sha3(PublicKey)"}],"clauses":[{"name":"spend","params":[{"name":"pubKey","type":"PublicKey"},{"name":"sig","type":"Signature"}],"hash_calls":[{"hash_type":"sha3","arg":"pubKey","arg_type":"PublicKey"}],"values":[{"name":"value"}]}],"value":"value","body_bytecode":"5279aa887cae7cac","body_opcodes":"2 PICK SHA3 EQUALVERIFY SWAP TXSIGHASH SWAP CHECKSIG","recursive":false}]`,
                },
                {
                        "LockWith2of3Keys",
-                       ivytest.LockWith2of3Keys,
+                       equitytest.LockWith2of3Keys,
                        `[{"name":"LockWith3Keys","params":[{"name":"pubkey1","type":"PublicKey"},{"name":"pubkey2","type":"PublicKey"},{"name":"pubkey3","type":"PublicKey"}],"clauses":[{"name":"unlockWith2Sigs","params":[{"name":"sig1","type":"Signature"},{"name":"sig2","type":"Signature"}],"values":[{"name":"locked"}]}],"value":"locked","body_bytecode":"537a547a526bae71557a536c7cad","body_opcodes":"3 ROLL 4 ROLL 2 TOALTSTACK TXSIGHASH 2ROT 5 ROLL 3 FROMALTSTACK SWAP CHECKMULTISIG","recursive":false}]`,
                },
                {
                        "LockToOutput",
-                       ivytest.LockToOutput,
-                       `[{"name":"LockToOutput","params":[{"name":"address","type":"Program"}],"clauses":[{"name":"relock","values":[{"name":"locked","program":"address"}]}],"value":"locked","body_bytecode":"0000c3c251557ac1","body_opcodes":"0 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT","recursive":false}]`,
+                       equitytest.LockToOutput,
+                       `[{"name":"LockToOutput","params":[{"name":"address","type":"Program"}],"clauses":[{"name":"relock","values":[{"name":"locked","program":"address"}]}],"value":"locked","body_bytecode":"00c3c251547ac1","body_opcodes":"0 AMOUNT ASSET 1 4 ROLL CHECKOUTPUT","recursive":false}]`,
                },
                {
                        "TradeOffer",
-                       ivytest.TradeOffer,
-                       `[{"name":"TradeOffer","params":[{"name":"requestedAsset","type":"Asset"},{"name":"requestedAmount","type":"Amount"},{"name":"sellerProgram","type":"Program"},{"name":"sellerKey","type":"PublicKey"}],"clauses":[{"name":"trade","reqs":[{"name":"payment","asset":"requestedAsset","amount":"requestedAmount"}],"values":[{"name":"payment","program":"sellerProgram","asset":"requestedAsset","amount":"requestedAmount"},{"name":"offered"}]},{"name":"cancel","params":[{"name":"sellerSig","type":"Signature"}],"values":[{"name":"offered","program":"sellerProgram"}]}],"value":"offered","body_bytecode":"547a641300000000007251557ac16323000000547a547aae7cac690000c3c251577ac1","body_opcodes":"4 ROLL JUMPIF:$cancel $trade 0 0 2SWAP 1 5 ROLL CHECKOUTPUT JUMP:$_end $cancel 4 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG VERIFY 0 0 AMOUNT ASSET 1 7 ROLL CHECKOUTPUT $_end","recursive":false}]`,
+                       equitytest.TradeOffer,
+                       `[{"name":"TradeOffer","params":[{"name":"requestedAsset","type":"Asset"},{"name":"requestedAmount","type":"Amount"},{"name":"sellerProgram","type":"Program"},{"name":"sellerKey","type":"PublicKey"}],"clauses":[{"name":"trade","reqs":[{"name":"payment","asset":"requestedAsset","amount":"requestedAmount"}],"values":[{"name":"payment","program":"sellerProgram","asset":"requestedAsset","amount":"requestedAmount"},{"name":"offered"}]},{"name":"cancel","params":[{"name":"sellerSig","type":"Signature"}],"values":[{"name":"offered","program":"sellerProgram"}]}],"value":"offered","body_bytecode":"547a6413000000007b7b51547ac16322000000547a547aae7cac6900c3c251567ac1","body_opcodes":"4 ROLL JUMPIF:$cancel $trade 0 ROT ROT 1 4 ROLL CHECKOUTPUT JUMP:$_end $cancel 4 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG VERIFY 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT $_end","recursive":false}]`,
                },
                {
                        "EscrowedTransfer",
-                       ivytest.EscrowedTransfer,
-                       `[{"name":"EscrowedTransfer","params":[{"name":"agent","type":"PublicKey"},{"name":"sender","type":"Program"},{"name":"recipient","type":"Program"}],"clauses":[{"name":"approve","params":[{"name":"sig","type":"Signature"}],"values":[{"name":"value","program":"recipient"}]},{"name":"reject","params":[{"name":"sig","type":"Signature"}],"values":[{"name":"value","program":"sender"}]}],"value":"value","body_bytecode":"537a641b000000537a7cae7cac690000c3c251567ac1632a000000537a7cae7cac690000c3c251557ac1","body_opcodes":"3 ROLL JUMPIF:$reject $approve 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY 0 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT JUMP:$_end $reject 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY 0 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT $_end","recursive":false}]`,
+                       equitytest.EscrowedTransfer,
+                       `[{"name":"EscrowedTransfer","params":[{"name":"agent","type":"PublicKey"},{"name":"sender","type":"Program"},{"name":"recipient","type":"Program"}],"clauses":[{"name":"approve","params":[{"name":"sig","type":"Signature"}],"values":[{"name":"value","program":"recipient"}]},{"name":"reject","params":[{"name":"sig","type":"Signature"}],"values":[{"name":"value","program":"sender"}]}],"value":"value","body_bytecode":"537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac1","body_opcodes":"3 ROLL JUMPIF:$reject $approve 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $reject 3 ROLL SWAP TXSIGHASH SWAP CHECKSIG VERIFY 0 AMOUNT ASSET 1 4 ROLL CHECKOUTPUT $_end","recursive":false}]`,
                },
                {
                        "CollateralizedLoan",
-                       ivytest.CollateralizedLoan,
-                       `[{"name":"CollateralizedLoan","params":[{"name":"balanceAsset","type":"Asset"},{"name":"balanceAmount","type":"Amount"},{"name":"blockHeight","type":"Integer"},{"name":"lender","type":"Program"},{"name":"borrower","type":"Program"}],"clauses":[{"name":"repay","reqs":[{"name":"payment","asset":"balanceAsset","amount":"balanceAmount"}],"values":[{"name":"payment","program":"lender","asset":"balanceAsset","amount":"balanceAmount"},{"name":"collateral","program":"borrower"}]},{"name":"default","blockheight":["blockHeight"],"values":[{"name":"collateral","program":"lender"}]}],"value":"collateral","body_bytecode":"557a641c00000000007251567ac1695100c3c251567ac163280000007bcda0690000c3c251577ac1","body_opcodes":"5 ROLL JUMPIF:$default $repay 0 0 2SWAP 1 6 ROLL CHECKOUTPUT VERIFY 1 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT JUMP:$_end $default ROT BLOCKHEIGHT GREATERTHAN VERIFY 0 0 AMOUNT ASSET 1 7 ROLL CHECKOUTPUT $_end","recursive":false}]`,
+                       equitytest.CollateralizedLoan,
+                       `[{"name":"CollateralizedLoan","params":[{"name":"balanceAsset","type":"Asset"},{"name":"balanceAmount","type":"Amount"},{"name":"finalHeight","type":"Integer"},{"name":"lender","type":"Program"},{"name":"borrower","type":"Program"}],"clauses":[{"name":"repay","reqs":[{"name":"payment","asset":"balanceAsset","amount":"balanceAmount"}],"values":[{"name":"payment","program":"lender","asset":"balanceAsset","amount":"balanceAmount"},{"name":"collateral","program":"borrower"}]},{"name":"default","blockheight":["finalHeight"],"values":[{"name":"collateral","program":"lender"}]}],"value":"collateral","body_bytecode":"557a641b000000007b7b51557ac16951c3c251557ac163260000007bcd9f6900c3c251567ac1","body_opcodes":"5 ROLL JUMPIF:$default $repay 0 ROT ROT 1 5 ROLL CHECKOUTPUT VERIFY 1 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $default ROT BLOCKHEIGHT LESSTHAN VERIFY 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT $_end","recursive":false}]`,
                },
                {
                        "RevealPreimage",
-                       ivytest.RevealPreimage,
+                       equitytest.RevealPreimage,
                        `[{"name":"RevealPreimage","params":[{"name":"hash","type":"Hash","inferred_type":"Sha3(String)"}],"clauses":[{"name":"reveal","params":[{"name":"string","type":"String"}],"hash_calls":[{"hash_type":"sha3","arg":"string","arg_type":"String"}],"values":[{"name":"value"}]}],"value":"value","body_bytecode":"7caa87","body_opcodes":"SWAP SHA3 EQUAL","recursive":false}]`,
                },
                {
                        "CallOptionWithSettlement",
-                       ivytest.CallOptionWithSettlement,
-                       `[{"name":"CallOptionWithSettlement","params":[{"name":"strikePrice","type":"Amount"},{"name":"strikeCurrency","type":"Asset"},{"name":"sellerProgram","type":"Program"},{"name":"sellerKey","type":"PublicKey"},{"name":"buyerKey","type":"PublicKey"},{"name":"blockHeight","type":"Integer"}],"clauses":[{"name":"exercise","params":[{"name":"buyerSig","type":"Signature"}],"reqs":[{"name":"payment","asset":"strikeCurrency","amount":"strikePrice"}],"blockheight":["blockHeight"],"values":[{"name":"payment","program":"sellerProgram","asset":"strikeCurrency","amount":"strikePrice"},{"name":"underlying"}]},{"name":"expire","blockheight":["blockHeight"],"values":[{"name":"underlying","program":"sellerProgram"}]},{"name":"settle","params":[{"name":"sellerSig","type":"Signature"},{"name":"buyerSig","type":"Signature"}],"values":[{"name":"underlying"}]}],"value":"underlying","body_bytecode":"567a76529c64390000006427000000557acd9f6971ae7cac6900007b537a51557ac16349000000557acda0690000c3c251577ac1634900000075577a547aae7cac69557a547aae7cac","body_opcodes":"6 ROLL DUP 2 NUMEQUAL JUMPIF:$settle JUMPIF:$expire $exercise 5 ROLL BLOCKHEIGHT LESSTHAN VERIFY 2ROT TXSIGHASH SWAP CHECKSIG VERIFY 0 0 ROT 3 ROLL 1 5 ROLL CHECKOUTPUT JUMP:$_end $expire 5 ROLL BLOCKHEIGHT GREATERTHAN VERIFY 0 0 AMOUNT ASSET 1 7 ROLL CHECKOUTPUT JUMP:$_end $settle DROP 7 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG VERIFY 5 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG $_end","recursive":false}]`,
+                       equitytest.CallOptionWithSettlement,
+                       `[{"name":"CallOptionWithSettlement","params":[{"name":"strikePrice","type":"Amount"},{"name":"strikeCurrency","type":"Asset"},{"name":"sellerProgram","type":"Program"},{"name":"sellerKey","type":"PublicKey"},{"name":"buyerKey","type":"PublicKey"},{"name":"finalHeight","type":"Integer"}],"clauses":[{"name":"exercise","params":[{"name":"buyerSig","type":"Signature"}],"reqs":[{"name":"payment","asset":"strikeCurrency","amount":"strikePrice"}],"blockheight":["finalHeight"],"values":[{"name":"payment","program":"sellerProgram","asset":"strikeCurrency","amount":"strikePrice"},{"name":"underlying"}]},{"name":"expire","blockheight":["finalHeight"],"values":[{"name":"underlying","program":"sellerProgram"}]},{"name":"settle","params":[{"name":"sellerSig","type":"Signature"},{"name":"buyerSig","type":"Signature"}],"values":[{"name":"underlying"}]}],"value":"underlying","body_bytecode":"567a76529c64360000006425000000557acda06971ae7cac69007c7b51547ac16346000000557acd9f6900c3c251567ac1634600000075577a547aae7cac69557a547aae7cac","body_opcodes":"6 ROLL DUP 2 NUMEQUAL JUMPIF:$settle JUMPIF:$expire $exercise 5 ROLL BLOCKHEIGHT GREATERTHAN VERIFY 2ROT TXSIGHASH SWAP CHECKSIG VERIFY 0 SWAP ROT 1 4 ROLL CHECKOUTPUT JUMP:$_end $expire 5 ROLL BLOCKHEIGHT LESSTHAN VERIFY 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT JUMP:$_end $settle DROP 7 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG VERIFY 5 ROLL 4 ROLL TXSIGHASH SWAP CHECKSIG $_end","recursive":false}]`,
                },
                {
                        "PriceChanger",
-                       ivytest.PriceChanger,
-                       `[{"name":"PriceChanger","params":[{"name":"askAmount","type":"Amount"},{"name":"askAsset","type":"Asset"},{"name":"sellerKey","type":"PublicKey"},{"name":"sellerProg","type":"Program"}],"clauses":[{"name":"changePrice","params":[{"name":"newAmount","type":"Amount"},{"name":"newAsset","type":"Asset"},{"name":"sig","type":"Signature"}],"values":[{"name":"offered","program":"PriceChanger(newAmount, newAsset, sellerKey, sellerProg)"}],"contracts":["PriceChanger"]},{"name":"redeem","reqs":[{"name":"payment","asset":"askAsset","amount":"askAmount"}],"values":[{"name":"payment","program":"sellerProg","asset":"askAsset","amount":"askAmount"},{"name":"offered"}]}],"value":"offered","body_bytecode":"557a6433000000557a5479ae7cac690000c3c251005a7a89597a89597a89597a89567a890274787e008901c07ec1633d0000000000537a547a51577ac1","body_opcodes":"5 ROLL JUMPIF:$redeem $changePrice 5 ROLL 4 PICK TXSIGHASH SWAP CHECKSIG VERIFY 0 0 AMOUNT ASSET 1 0 10 ROLL CATPUSHDATA 9 ROLL CATPUSHDATA 9 ROLL CATPUSHDATA 9 ROLL CATPUSHDATA 6 ROLL CATPUSHDATA 0x7478 CAT 0 CATPUSHDATA 192 CAT CHECKOUTPUT JUMP:$_end $redeem 0 0 3 ROLL 4 ROLL 1 7 ROLL CHECKOUTPUT $_end","recursive":true}]`,
+                       equitytest.PriceChanger,
+                       `[{"name":"PriceChanger","params":[{"name":"askAmount","type":"Amount"},{"name":"askAsset","type":"Asset"},{"name":"sellerKey","type":"PublicKey"},{"name":"sellerProg","type":"Program"}],"clauses":[{"name":"changePrice","params":[{"name":"newAmount","type":"Amount"},{"name":"newAsset","type":"Asset"},{"name":"sig","type":"Signature"}],"values":[{"name":"offered","program":"PriceChanger(newAmount, newAsset, sellerKey, sellerProg)"}],"contracts":["PriceChanger"]},{"name":"redeem","reqs":[{"name":"payment","asset":"askAsset","amount":"askAmount"}],"values":[{"name":"payment","program":"sellerProg","asset":"askAsset","amount":"askAmount"},{"name":"offered"}]}],"value":"offered","body_bytecode":"557a6432000000557a5479ae7cac6900c3c25100597a89587a89587a89587a89557a890274787e008901c07ec1633a000000007b537a51567ac1","body_opcodes":"5 ROLL JUMPIF:$redeem $changePrice 5 ROLL 4 PICK TXSIGHASH SWAP CHECKSIG VERIFY 0 AMOUNT ASSET 1 0 9 ROLL CATPUSHDATA 8 ROLL CATPUSHDATA 8 ROLL CATPUSHDATA 8 ROLL CATPUSHDATA 5 ROLL CATPUSHDATA 0x7478 CAT 0 CATPUSHDATA 192 CAT CHECKOUTPUT JUMP:$_end $redeem 0 ROT 3 ROLL 1 6 ROLL CHECKOUTPUT $_end","recursive":true}]`,
                },
                {
                        "OneTwo",
-                       ivytest.OneTwo,
-                       `[{"name":"Two","params":[{"name":"b","type":"Program"},{"name":"c","type":"Program"},{"name":"blockHeight","type":"Integer"}],"clauses":[{"name":"redeem","blockheight":["blockHeight"],"values":[{"name":"value","program":"b"}]},{"name":"default","blockheight":["blockHeight"],"values":[{"name":"value","program":"c"}]}],"value":"value","body_bytecode":"537a64180000007bcd9f690000c3c251557ac163240000007bcda0690000c3c251567ac1","body_opcodes":"3 ROLL JUMPIF:$default $redeem ROT BLOCKHEIGHT LESSTHAN VERIFY 0 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $default ROT BLOCKHEIGHT GREATERTHAN VERIFY 0 0 AMOUNT ASSET 1 6 ROLL CHECKOUTPUT $_end","recursive":false},{"name":"One","params":[{"name":"a","type":"Program"},{"name":"b","type":"Program"},{"name":"c","type":"Program"},{"name":"switchHeight","type":"Integer"},{"name":"blockHeight","type":"Integer"}],"clauses":[{"name":"redeem","blockheight":["switchHeight"],"values":[{"name":"value","program":"a"}]},{"name":"switch","blockheight":["switchHeight"],"values":[{"name":"value","program":"Two(b, c, blockHeight)"}],"contracts":["Two"]}],"value":"value","body_bytecode":"557a6419000000537acd9f690000c3c251557ac1635c000000537acda0690000c3c25100597a89587a89577a8901747e24537a64180000007bcd9f690000c3c251557ac163240000007bcda0690000c3c251567ac189008901c07ec1","body_opcodes":"5 ROLL JUMPIF:$switch $redeem 3 ROLL BLOCKHEIGHT LESSTHAN VERIFY 0 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT JUMP:$_end $switch 3 ROLL BLOCKHEIGHT GREATERTHAN VERIFY 0 0 AMOUNT ASSET 1 0 9 ROLL CATPUSHDATA 8 ROLL CATPUSHDATA 7 ROLL CATPUSHDATA 116 CAT 0x537a64180000007bcd9f690000c3c251557ac163240000007bcda0690000c3c251567ac1 CATPUSHDATA 0 CATPUSHDATA 192 CAT CHECKOUTPUT $_end","recursive":false}]`,
+                       equitytest.OneTwo,
+                       `[{"name":"Two","params":[{"name":"b","type":"Program"},{"name":"c","type":"Program"},{"name":"expirationHeight","type":"Integer"}],"clauses":[{"name":"redeem","blockheight":["expirationHeight"],"values":[{"name":"value","program":"b"}]},{"name":"default","blockheight":["expirationHeight"],"values":[{"name":"value","program":"c"}]}],"value":"value","body_bytecode":"537a64170000007bcda06900c3c251547ac163220000007bcd9f6900c3c251557ac1","body_opcodes":"3 ROLL JUMPIF:$default $redeem ROT BLOCKHEIGHT GREATERTHAN VERIFY 0 AMOUNT ASSET 1 4 ROLL CHECKOUTPUT JUMP:$_end $default ROT BLOCKHEIGHT LESSTHAN VERIFY 0 AMOUNT ASSET 1 5 ROLL CHECKOUTPUT $_end","recursive":false},{"name":"One","params":[{"name":"a","type":"Program"},{"name":"b","type":"Program"},{"name":"c","type":"Program"},{"name":"switchHeight","type":"Integer"},{"name":"blockHeight","type":"Integer"}],"clauses":[{"name":"redeem","blockheight":["switchHeight"],"values":[{"name":"value","program":"a"}]},{"name":"switch","blockheight":["switchHeight"],"values":[{"name":"value","program":"Two(b, c, blockHeight)"}],"contracts":["Two"]}],"value":"value","body_bytecode":"557a6418000000537acda06900c3c251547ac16358000000537acd9f6900c3c25100587a89577a89567a8901747e22537a64170000007bcda06900c3c251547ac163220000007bcd9f6900c3c251557ac189008901c07ec1","body_opcodes":"5 ROLL JUMPIF:$switch $redeem 3 ROLL BLOCKHEIGHT GREATERTHAN VERIFY 0 AMOUNT ASSET 1 4 ROLL CHECKOUTPUT JUMP:$_end $switch 3 ROLL BLOCKHEIGHT LESSTHAN VERIFY 0 AMOUNT ASSET 1 0 8 ROLL CATPUSHDATA 7 ROLL CATPUSHDATA 6 ROLL CATPUSHDATA 116 CAT 0x537a64170000007bcda06900c3c251547ac163220000007bcd9f6900c3c251557ac1 CATPUSHDATA 0 CATPUSHDATA 192 CAT CHECKOUTPUT $_end","recursive":false}]`,
                },
        }
        for _, c := range cases {
index 5a1736a..84de0f3 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Package ivy provides a compiler for Chain's Ivy contract language.
+Package equity provides a compiler for Chain's Equity contract language.
 
 A contract is a means to lock some payment in the output of a
 transaction. It contains a number of clauses, each describing a way to
@@ -73,7 +73,7 @@ of late May 2017.
 
   call_expr = expr "(" [args] ")"
 
-    If expr is the name of an Ivy contract, then calling it (with
+    If expr is the name of an Equity contract, then calling it (with
     the appropriate arguments) produces a program suitable for use
     in "lock" statements.
 
similarity index 89%
rename from equity/compiler/ivytest/ivytest.go
rename to equity/compiler/equitytest/equitytest.go
index 00fd1ad..da6a7a0 100644 (file)
@@ -1,4 +1,4 @@
-package ivytest
+package equitytest
 
 const TrivialLock = `
 contract TrivialLock() locks locked {
@@ -71,13 +71,13 @@ contract EscrowedTransfer(agent: PublicKey, sender: Program, recipient: Program)
 `
 
 const CollateralizedLoan = `
-contract CollateralizedLoan(balanceAsset: Asset, balanceAmount: Amount, blockHeight: Integer, lender: Program, borrower: Program) locks collateral {
+contract CollateralizedLoan(balanceAsset: Asset, balanceAmount: Amount, finalHeight: Integer, lender: Program, borrower: Program) locks collateral {
   clause repay() requires payment: balanceAmount of balanceAsset {
     lock payment with lender
     lock collateral with borrower
   }
   clause default() {
-    verify greater(blockHeight)
+    verify above(finalHeight)
     lock collateral with lender
   }
 }
@@ -111,16 +111,16 @@ contract CallOptionWithSettlement(strikePrice: Amount,
                     sellerProgram: Program,
                     sellerKey: PublicKey,
                     buyerKey: PublicKey,
-                    blockHeight: Integer) locks underlying {
+                    finalHeight: Integer) locks underlying {
   clause exercise(buyerSig: Signature)
                  requires payment: strikePrice of strikeCurrency {
-    verify less(blockHeight)
+    verify below(finalHeight)
     verify checkTxSig(buyerKey, buyerSig)
     lock payment with sellerProgram
     unlock underlying
   }
   clause expire() {
-    verify greater(blockHeight)
+    verify above(finalHeight)
     lock underlying with sellerProgram
   }
   clause settle(sellerSig: Signature, buyerSig: Signature) {
@@ -132,23 +132,23 @@ contract CallOptionWithSettlement(strikePrice: Amount,
 `
 
 const OneTwo = `
-contract Two(b, c: Program, blockHeight: Integer) locks value {
+contract Two(b, c: Program, expirationHeight: Integer) locks value {
   clause redeem() {
-    verify less(blockHeight)
+    verify below(expirationHeight)
     lock value with b
   }
   clause default() {
-    verify greater(blockHeight)
+    verify above(expirationHeight)
     lock value with c
   }
 }
 contract One(a, b, c: Program, switchHeight, blockHeight: Integer) locks value {
   clause redeem() {
-    verify less(switchHeight)
+    verify below(switchHeight)
     lock value with a
   }
   clause switch() {
-    verify greater(switchHeight)
+    verify above(switchHeight)
     lock value with Two(b, c, blockHeight)
   }
 }
index ab32f07..af0225e 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/binary"
        "fmt"
        "math"
+       "reflect"
 
        "github.com/bytom/errors"
        "github.com/bytom/math/checked"
@@ -482,3 +483,13 @@ func init() {
                }
        }
 }
+
+// IsPushdata judge instruction whether is a pushdata operation(include opFalse operation)
+func (inst *Instruction) IsPushdata() bool {
+       if reflect.ValueOf(ops[inst.Op].fn) == reflect.ValueOf(ops[OP_1].fn) ||
+               reflect.ValueOf(ops[inst.Op].fn) == reflect.ValueOf(ops[OP_0].fn) {
+               return true
+       }
+
+       return false
+}
index 70c17e3..bac92bc 100644 (file)
@@ -138,3 +138,41 @@ func TestParseProgram(t *testing.T) {
                }
        }
 }
+
+func TestIsPushData(t *testing.T) {
+       cases := []struct {
+               want    Instruction
+               wantErr error
+       }{
+               {
+                       want: Instruction{Op: OP_16, Data: []byte{16}, Len: 1},
+               },
+               {
+                       want: Instruction{Op: OP_DATA_32, Data: []byte{16}, Len: 1},
+               },
+               {
+                       want: Instruction{Op: OP_FALSE, Data: []byte{}, Len: 1},
+               },
+               {
+                       want: Instruction{Op: OP_TRUE, Data: []byte{1}, Len: 1},
+               },
+               {
+                       want:    Instruction{Op: OP_JUMP, Data: []byte{0x00000000}, Len: 1},
+                       wantErr: ErrShortProgram,
+               },
+               {
+                       want:    Instruction{Op: OP_ADD, Data: []byte{0x12, 0x56}, Len: 2},
+                       wantErr: ErrShortProgram,
+               },
+       }
+
+       for _, c := range cases {
+               if c.want.IsPushdata() {
+                       t.Logf("check success")
+               } else if c.wantErr != nil {
+                       t.Logf("check err success")
+               } else {
+                       t.Errorf("check false: %v -- %v", ops[OP_1].fn, ops[c.want.Op].fn)
+               }
+       }
+}