OSDN Git Service

add free gas (#295)
[bytom/vapor.git] / blockchain / txbuilder / finalize.go
1 package txbuilder
2
3 import (
4         "bytes"
5         "context"
6
7         "github.com/vapor/common/arithmetic"
8         cfg "github.com/vapor/config"
9         "github.com/vapor/errors"
10         "github.com/vapor/math/checked"
11         "github.com/vapor/protocol"
12         "github.com/vapor/protocol/bc/types"
13         "github.com/vapor/protocol/vm"
14 )
15
16 var (
17         // ErrRejected means the network rejected a tx (as a double-spend)
18         ErrRejected = errors.New("transaction rejected")
19         // ErrMissingRawTx means missing transaction
20         ErrMissingRawTx = errors.New("missing raw tx")
21         // ErrBadInstructionCount means too many signing instructions compare with inputs
22         ErrBadInstructionCount = errors.New("too many signing instructions in template")
23         // ErrOrphanTx means submit transaction is orphan
24         ErrOrphanTx = errors.New("finalize can't find transaction input utxo")
25         // ErrExtTxFee means transaction fee exceed max limit
26         ErrExtTxFee = errors.New("transaction fee exceed max limit")
27 )
28
29 // FinalizeTx validates a transaction signature template,
30 // assembles a fully signed tx, and stores the effects of
31 // its changes on the UTXO set.
32 func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
33         if fee, err := arithmetic.CalculateTxFee(tx); err != nil {
34                 return checked.ErrOverflow
35         } else if fee > cfg.CommonConfig.Wallet.MaxTxFee {
36                 return ErrExtTxFee
37         }
38
39         if err := checkTxSighashCommitment(tx); err != nil {
40                 return err
41         }
42
43         // This part is use for prevent tx size  is 0
44         data, err := tx.TxData.MarshalText()
45         if err != nil {
46                 return err
47         }
48         tx.TxData.SerializedSize = uint64(len(data) / 2)
49         tx.Tx.SerializedSize = uint64(len(data) / 2)
50
51         isOrphan, err := c.ValidateTx(tx)
52         if err != nil {
53                 if errors.Root(err) == err {
54                         return errors.Sub(ErrRejected, err)
55                 }
56                 return err
57         }
58
59         if isOrphan {
60                 return ErrOrphanTx
61         }
62         return nil
63 }
64
65 var (
66         // ErrNoTxSighashCommitment is returned when no input commits to the
67         // complete transaction.
68         // To permit idempotence of transaction submission, we require at
69         // least one input to commit to the complete transaction (what you get
70         // when you build a transaction with allow_additional_actions=false).
71         ErrNoTxSighashCommitment = errors.New("no commitment to tx sighash")
72
73         // ErrNoTxSighashAttempt is returned when there was no attempt made to sign
74         // this transaction.
75         ErrNoTxSighashAttempt = errors.New("no tx sighash attempted")
76
77         // ErrTxSignatureFailure is returned when there was an attempt to sign this
78         // transaction, but it failed.
79         ErrTxSignatureFailure = errors.New("tx signature was attempted but failed")
80 )
81
82 func checkTxSighashCommitment(tx *types.Tx) error {
83         // TODO: this is the local sender check rules, we might don't need it due to the rule is difference
84         return nil
85         var lastError error
86
87         for i, inp := range tx.Inputs {
88                 var args [][]byte
89                 switch t := inp.TypedInput.(type) {
90                 case *types.SpendInput:
91                         args = t.Arguments
92                 }
93                 // Note: These numbers will need to change if more args are added such that the minimum length changes
94                 switch {
95                 // A conforming arguments list contains
96                 // [... arg1 arg2 ... argN N sig1 sig2 ... sigM prog]
97                 // The args are the opaque arguments to prog. In the case where
98                 // N is 0 (prog takes no args), and assuming there must be at
99                 // least one signature, args has a minimum length of 3.
100                 case len(args) == 0:
101                         lastError = ErrNoTxSighashAttempt
102                         continue
103                 case len(args) < 3:
104                         lastError = ErrTxSignatureFailure
105                         continue
106                 }
107                 lastError = ErrNoTxSighashCommitment
108                 prog := args[len(args)-1]
109                 if len(prog) != 35 {
110                         continue
111                 }
112                 if prog[0] != byte(vm.OP_DATA_32) {
113                         continue
114                 }
115                 if !bytes.Equal(prog[33:], []byte{byte(vm.OP_TXSIGHASH), byte(vm.OP_EQUAL)}) {
116                         continue
117                 }
118                 h := tx.SigHash(uint32(i))
119                 if !bytes.Equal(h.Bytes(), prog[1:33]) {
120                         continue
121                 }
122                 // At least one input passes commitment checks
123                 return nil
124         }
125
126         return lastError
127 }