7 cfg "github.com/vapor/config"
8 "github.com/vapor/consensus"
9 "github.com/vapor/errors"
10 "github.com/vapor/protocol"
11 "github.com/vapor/protocol/bc/types"
12 "github.com/vapor/protocol/vm"
16 // ErrRejected means the network rejected a tx (as a double-spend)
17 ErrRejected = errors.New("transaction rejected")
18 // ErrMissingRawTx means missing transaction
19 ErrMissingRawTx = errors.New("missing raw tx")
20 // ErrBadInstructionCount means too many signing instructions compare with inputs
21 ErrBadInstructionCount = errors.New("too many signing instructions in template")
22 // ErrOrphanTx means submit transaction is orphan
23 ErrOrphanTx = errors.New("finalize can't find transaction input utxo")
24 // ErrExtTxFee means transaction fee exceed max limit
25 ErrExtTxFee = errors.New("transaction fee exceed max limit")
26 // ErrNoGasInput means transaction has no gas input
27 ErrNoGasInput = errors.New("transaction has no gas input")
30 // FinalizeTx validates a transaction signature template,
31 // assembles a fully signed tx, and stores the effects of
32 // its changes on the UTXO set.
33 func FinalizeTx(ctx context.Context, c *protocol.Chain, tx *types.Tx) error {
34 if fee := CalculateTxFee(tx); fee > cfg.CommonConfig.Wallet.MaxTxFee {
38 if err := checkTxSighashCommitment(tx); err != nil {
42 if err := checkGasInputIDs(tx); err != nil {
46 // This part is use for prevent tx size is 0
47 data, err := tx.TxData.MarshalText()
51 tx.TxData.SerializedSize = uint64(len(data) / 2)
52 tx.Tx.SerializedSize = uint64(len(data) / 2)
54 isOrphan, err := c.ValidateTx(tx)
55 if errors.Root(err) == protocol.ErrBadTx {
56 return errors.Sub(ErrRejected, err)
59 return errors.WithDetail(err, "tx rejected: "+err.Error())
68 // ErrNoTxSighashCommitment is returned when no input commits to the
69 // complete transaction.
70 // To permit idempotence of transaction submission, we require at
71 // least one input to commit to the complete transaction (what you get
72 // when you build a transaction with allow_additional_actions=false).
73 ErrNoTxSighashCommitment = errors.New("no commitment to tx sighash")
75 // ErrNoTxSighashAttempt is returned when there was no attempt made to sign
77 ErrNoTxSighashAttempt = errors.New("no tx sighash attempted")
79 // ErrTxSignatureFailure is returned when there was an attempt to sign this
80 // transaction, but it failed.
81 ErrTxSignatureFailure = errors.New("tx signature was attempted but failed")
84 func checkTxSighashCommitment(tx *types.Tx) error {
85 // TODO: this is the local sender check rules, we might don't need it due to the rule is difference
89 for i, inp := range tx.Inputs {
91 switch t := inp.TypedInput.(type) {
92 case *types.SpendInput:
95 // Note: These numbers will need to change if more args are added such that the minimum length changes
97 // A conforming arguments list contains
98 // [... arg1 arg2 ... argN N sig1 sig2 ... sigM prog]
99 // The args are the opaque arguments to prog. In the case where
100 // N is 0 (prog takes no args), and assuming there must be at
101 // least one signature, args has a minimum length of 3.
103 lastError = ErrNoTxSighashAttempt
106 lastError = ErrTxSignatureFailure
109 lastError = ErrNoTxSighashCommitment
110 prog := args[len(args)-1]
114 if prog[0] != byte(vm.OP_DATA_32) {
117 if !bytes.Equal(prog[33:], []byte{byte(vm.OP_TXSIGHASH), byte(vm.OP_EQUAL)}) {
120 h := tx.SigHash(uint32(i))
121 if !bytes.Equal(h.Bytes(), prog[1:33]) {
124 // At least one input passes commitment checks
131 // CalculateTxFee calculate transaction fee
132 func CalculateTxFee(tx *types.Tx) (fee uint64) {
133 totalInputBTM := uint64(0)
134 totalOutputBTM := uint64(0)
136 for _, input := range tx.Inputs {
137 if input.InputType() == types.CoinbaseInputType {
140 if input.AssetID() == *consensus.BTMAssetID {
141 totalInputBTM += input.Amount()
145 for _, output := range tx.Outputs {
146 if *output.AssetAmount().AssetId == *consensus.BTMAssetID {
147 totalOutputBTM += output.AssetAmount().Amount
151 fee = totalInputBTM - totalOutputBTM
155 func checkGasInputIDs(tx *types.Tx) error {
156 for _, inp := range tx.Inputs {
157 switch inp.InputType() {
158 case types.CrossChainInputType:
163 if len(tx.GasInputIDs) == 0 {