From a3a6c87933a7fae9b7ce7705ca4eab92400076a6 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Mon, 21 Sep 2020 15:03:10 +0800 Subject: [PATCH] fix: tx fee calculation --- transaction/transaction.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/transaction/transaction.go b/transaction/transaction.go index 847f3599..dcd86c08 100644 --- a/transaction/transaction.go +++ b/transaction/transaction.go @@ -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 {