github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/blockchain/txbuilder/constraint.go (about) 1 package txbuilder 2 3 import ( 4 "github.com/bytom/bytom/protocol/bc" 5 "github.com/bytom/bytom/protocol/vm" 6 "github.com/bytom/bytom/protocol/vm/vmutil" 7 ) 8 9 // Constraint types express a constraint on an input of a proposed 10 // transaction, and know how to turn that constraint into part of a 11 // signature program in that input's witness. 12 type constraint interface { 13 // Code produces bytecode expressing the constraint. The code, when 14 // executed, must consume nothing from the stack and leave a new 15 // boolean value on top of it. 16 code() []byte 17 } 18 19 // outpointConstraint requires the outputID (and therefore, the outpoint) being spent to equal the 20 // given value. 21 type outputIDConstraint bc.Hash 22 23 func (o outputIDConstraint) code() []byte { 24 builder := vmutil.NewBuilder() 25 builder.AddData(bc.Hash(o).Bytes()) 26 builder.AddOp(vm.OP_OUTPUTID) 27 builder.AddOp(vm.OP_EQUAL) 28 prog, _ := builder.Build() // error is impossible 29 return prog 30 } 31 32 // PayConstraint requires the transaction to include a given output 33 // at the given index, optionally with the given refdatahash. 34 type payConstraint struct { 35 Index int 36 bc.AssetAmount 37 Program []byte 38 } 39 40 func (p payConstraint) code() []byte { 41 builder := vmutil.NewBuilder() 42 builder.AddInt64(int64(p.Index)) 43 builder.AddInt64(int64(p.Amount)).AddData(p.AssetId.Bytes()).AddInt64(1).AddData(p.Program) 44 builder.AddOp(vm.OP_CHECKOUTPUT) 45 prog, _ := builder.Build() // error is impossible 46 return prog 47 }