OSDN Git Service

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