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 len(tx.GasInputIDs) == 0 {
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:
94 case *types.IssuanceInput:
97 // Note: These numbers will need to change if more args are added such that the minimum length changes
99 // A conforming arguments list contains
100 // [... arg1 arg2 ... argN N sig1 sig2 ... sigM prog]
101 // The args are the opaque arguments to prog. In the case where
102 // N is 0 (prog takes no args), and assuming there must be at
103 // least one signature, args has a minimum length of 3.
105 lastError = ErrNoTxSighashAttempt
108 lastError = ErrTxSignatureFailure
111 lastError = ErrNoTxSighashCommitment
112 prog := args[len(args)-1]
116 if prog[0] != byte(vm.OP_DATA_32) {
119 if !bytes.Equal(prog[33:], []byte{byte(vm.OP_TXSIGHASH), byte(vm.OP_EQUAL)}) {
122 h := tx.SigHash(uint32(i))
123 if !bytes.Equal(h.Bytes(), prog[1:33]) {
126 // At least one input passes commitment checks
133 // CalculateTxFee calculate transaction fee
134 func CalculateTxFee(tx *types.Tx) (fee uint64) {
135 totalInputBTM := uint64(0)
136 totalOutputBTM := uint64(0)
138 for _, input := range tx.Inputs {
139 if input.InputType() == types.CoinbaseInputType {
142 if input.AssetID() == *consensus.BTMAssetID {
143 totalInputBTM += input.Amount()
147 for _, output := range tx.Outputs {
148 if *output.AssetId == *consensus.BTMAssetID {
149 totalOutputBTM += output.Amount
153 fee = totalInputBTM - totalOutputBTM