Skip to content

Commit

Permalink
fix: tx fee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Sep 21, 2020
1 parent c4b42a9 commit a3a6c87
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,21 @@ func SingleSegmentSignTransaction(transaction *types.Transaction, start int, end
}

func CalculateTransactionFee(tx *types.Transaction, feeRate uint64) (uint64, error) {
bytes, err := tx.Serialize()
// raw tx serialize
rawTxBytes, err := tx.Serialize()
if err != nil {
return 0, err
}

// raw tx serialize
txSize := uint64(len(bytes))
// witness serialize
txSize += uint64(len(types.SerializeDynVec(tx.Witnesses)))
// raw tx + witness offset
txSize += 12
// tx offset
var witnessBytes [][]byte
for _, witness := range tx.Witnesses {
witnessBytes = append(witnessBytes, types.SerializeBytes(witness))
}
witnessesBytes := types.SerializeDynVec(witnessBytes)
//tx serialize
txBytes := types.SerializeTable([][]byte{rawTxBytes, witnessesBytes})
txSize := uint64(len(txBytes))
// tx offset cost
txSize += 4
fee := txSize * feeRate / 1000
if fee*1000 < txSize*feeRate {
Expand Down

0 comments on commit a3a6c87

Please sign in to comment.