X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=blockchain%2Ftxbuilder%2Fconstraint.go;h=62ddf4c0d63f7b63c6618f4ef342bd659124642f;hb=eed5312542ad10b3f62275eadeab226b119a22fd;hp=f99b8fb63c695580935fd569535489ba3feac174;hpb=01180396e6e36d20c8a666286c985637e6186bfd;p=bytom%2Fbytom.git diff --git a/blockchain/txbuilder/constraint.go b/blockchain/txbuilder/constraint.go index f99b8fb6..62ddf4c0 100644 --- a/blockchain/txbuilder/constraint.go +++ b/blockchain/txbuilder/constraint.go @@ -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