OSDN Git Service

Revert "update master (#487)" (#518)
[bytom/bytom.git] / blockchain / txbuilder / constraint.go
index f99b8fb..62ddf4c 100644 (file)
@@ -1,6 +1,7 @@
 package txbuilder
 
 import (
+       "github.com/bytom/crypto/sha3pool"
        "github.com/bytom/protocol/bc"
        "github.com/bytom/protocol/vm"
        "github.com/bytom/protocol/vm/vmutil"
@@ -29,6 +30,28 @@ func (o outputIDConstraint) code() []byte {
        return prog
 }
 
+// refdataConstraint requires the refdatahash of the transaction (if
+// tx is true) or the input (if tx is false) to match that of the
+// given data.
+type refdataConstraint struct {
+       data []byte
+       tx   bool
+}
+
+func (r refdataConstraint) code() []byte {
+       var h [32]byte
+       sha3pool.Sum256(h[:], r.data)
+       builder := vmutil.NewBuilder()
+       builder.AddData(h[:])
+       if r.tx {
+               builder.AddOp(vm.OP_TXDATA)
+       } else {
+               builder.AddOp(vm.OP_ENTRYDATA)
+       }
+       builder.AddOp(vm.OP_EQUAL)
+       prog, _ := builder.Build() // error is impossible
+       return prog
+}
 
 // PayConstraint requires the transaction to include a given output
 // at the given index, optionally with the given refdatahash.
@@ -36,11 +59,17 @@ type payConstraint struct {
        Index int
        bc.AssetAmount
        Program     []byte
+       RefDataHash *bc.Hash
 }
 
 func (p payConstraint) code() []byte {
        builder := vmutil.NewBuilder()
        builder.AddInt64(int64(p.Index))
+       if p.RefDataHash == nil {
+               builder.AddData([]byte{})
+       } else {
+               builder.AddData(p.RefDataHash.Bytes())
+       }
        builder.AddInt64(int64(p.Amount)).AddData(p.AssetId.Bytes()).AddInt64(1).AddData(p.Program)
        builder.AddOp(vm.OP_CHECKOUTPUT)
        prog, _ := builder.Build() // error is impossible