7 "github.com/vapor/errors"
8 "github.com/vapor/protocol/bc/types"
11 // NewBuilder return new TemplateBuilder instance
12 func NewBuilder(maxTime time.Time) *TemplateBuilder {
13 return &TemplateBuilder{maxTime: maxTime}
16 // TemplateBuilder is struct of building transactions
17 type TemplateBuilder struct {
19 inputs []*types.TxInput
20 outputs []*types.TxOutput
21 signingInstructions []*SigningInstruction
26 callbacks []func() error
29 // AddInput add inputs of transactions
30 func (b *TemplateBuilder) AddInput(in *types.TxInput, sigInstruction *SigningInstruction) error {
31 if in.InputType() != types.CoinbaseInputType && in.Amount() > math.MaxInt64 {
32 return errors.WithDetailf(ErrBadAmount, "amount %d exceeds maximum value 2^63", in.Amount())
34 b.inputs = append(b.inputs, in)
35 b.signingInstructions = append(b.signingInstructions, sigInstruction)
39 // AddOutput add outputs of transactions
40 func (b *TemplateBuilder) AddOutput(o *types.TxOutput) error {
41 if o.Amount > math.MaxInt64 {
42 return errors.WithDetailf(ErrBadAmount, "amount %d exceeds maximum value 2^63", o.Amount)
44 b.outputs = append(b.outputs, o)
48 // InputCount return number of input in the template builder
49 func (b *TemplateBuilder) InputCount() int {
53 // RestrictMinTime set minTime
54 func (b *TemplateBuilder) RestrictMinTime(t time.Time) {
55 if t.After(b.minTime) {
60 // RestrictMaxTime set maxTime
61 func (b *TemplateBuilder) RestrictMaxTime(t time.Time) {
62 if t.Before(b.maxTime) {
67 // MaxTime return maxTime
68 func (b *TemplateBuilder) MaxTime() time.Time {
72 // OnRollback registers a function that can be
73 // used to attempt to undo any side effects of building
74 // actions. For example, it might cancel any reservations
75 // reservations that were made on UTXOs in a spend action.
76 // Rollback is a "best-effort" operation and not guaranteed
77 // to succeed. Each action's side effects, if any, must be
78 // designed with this in mind.
79 func (b *TemplateBuilder) OnRollback(rollbackFn func()) {
80 b.rollbacks = append(b.rollbacks, rollbackFn)
83 // OnBuild registers a function that will be run after all
84 // actions have been successfully built.
85 func (b *TemplateBuilder) OnBuild(buildFn func() error) {
86 b.callbacks = append(b.callbacks, buildFn)
89 // Rollback action for handle fail build
90 func (b *TemplateBuilder) Rollback() {
91 for _, f := range b.rollbacks {
96 // Build build transactions with template
97 func (b *TemplateBuilder) Build() (*Template, *types.TxData, error) {
98 // Run any building callbacks.
99 for _, cb := range b.callbacks {
114 if b.timeRange != 0 {
115 tx.TimeRange = b.timeRange
118 // Add all the built outputs.
119 tx.Outputs = append(tx.Outputs, b.outputs...)
121 // Add all the built inputs and their corresponding signing instructions.
122 for i, in := range b.inputs {
123 instruction := b.signingInstructions[i]
124 instruction.Position = uint32(len(tx.Inputs))
126 // Empty signature arrays should be serialized as empty arrays, not null.
127 if instruction.WitnessComponents == nil {
128 instruction.WitnessComponents = []witnessComponent{}
130 tpl.SigningInstructions = append(tpl.SigningInstructions, instruction)
131 tx.Inputs = append(tx.Inputs, in)
134 tpl.Transaction = types.NewTx(*tx)
135 tpl.Fee = CalculateTxFee(tpl.Transaction)