mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-09-13 04:50:11 +00:00

* Add GetUTXOsByBalances command to rpc * Fix wrong commands in GetBalanceByAddress * Moved calculation of TransactionMass out of TransactionValidator, so t that it can be used in kaspawallet * Allow CreateUnsignedTransaction to return multiple transactions * Start working on split * Implement maybeSplitTransactionInner * estimateMassIncreaseForSignatures should multiply by the number of inputs * Implement createSplitTransaction * Implement mergeTransactions * Broadcast all transaction, not only 1 * workaround missing UTXOEntry in partially signed transaction * Bugfix in broadcast loop * Add underscores in some constants * Make all nets RelayNonStdTxs: false * Change estimateMassIncreaseForSignatures to estimateMassAfterSignatures * Allow situations where merge transaction doesn't have enough funds to pay fees * Add comments * A few of renames * Handle missed errors * Fix clone of PubKeySignaturePair to properly clone nil signatures * Add sanity check to make sure originalTransaction has exactly two outputs * Re-use change address for splitAddress * Add one more utxo if the total amount is smaller then what we need to send due to fees * Fix off-by-1 error in splitTrasnaction * Add a comment to maybeAutoCompoundTransaction * Add comment on why we are increasing inputCountPerSplit * Add comment explaining while originalTransaction has 1 or 2 outputs * Move oneMoreUTXOForMergeTransaction to split_transaction.go * Allow to add multiple utxos to pay fee for mergeTransactions, if needed * calculate split input counts and sizes properly * Allow multiple transactions inside the create-unsigned-transaction -> sign -> broadcast workflow * Print the number of transaction which was sent, in case there were multiple * Rename broadcastConfig.Transaction(File) to Transactions(File) * Convert alreadySelectedUTXOs to a map * Fix a typo * Add comment explaining that we assume all inputs are the same * Revert over-refactor of rename of config.Transaction -> config.Transactions * Rename: inputPerSplitCount -> inputsPerSplitCount * Add comment for splitAndInputPerSplitCounts * Use createSplitTransaction to calculate the upper bound of mass for split transactions
120 lines
4.0 KiB
Go
120 lines
4.0 KiB
Go
package txmass
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
)
|
|
|
|
// Calculator exposes methods to calculate the mass of a transaction
|
|
type Calculator struct {
|
|
massPerTxByte uint64
|
|
massPerScriptPubKeyByte uint64
|
|
massPerSigOp uint64
|
|
}
|
|
|
|
// NewCalculator creates a new instance of Calculator
|
|
func NewCalculator(massPerTxByte, massPerScriptPubKeyByte, massPerSigOp uint64) *Calculator {
|
|
return &Calculator{
|
|
massPerTxByte: massPerTxByte,
|
|
massPerScriptPubKeyByte: massPerScriptPubKeyByte,
|
|
massPerSigOp: massPerSigOp,
|
|
}
|
|
}
|
|
|
|
// MassPerTxByte returns the mass per transaction byte configured for this Calculator
|
|
func (c *Calculator) MassPerTxByte() uint64 { return c.massPerTxByte }
|
|
|
|
// MassPerScriptPubKeyByte returns the mass per ScriptPublicKey byte configured for this Calculator
|
|
func (c *Calculator) MassPerScriptPubKeyByte() uint64 { return c.massPerScriptPubKeyByte }
|
|
|
|
// MassPerSigOp returns the mass per SigOp byte configured for this Calculator
|
|
func (c *Calculator) MassPerSigOp() uint64 { return c.massPerSigOp }
|
|
|
|
// CalculateTransactionMass calculates the mass of the given transaction
|
|
func (c *Calculator) CalculateTransactionMass(transaction *externalapi.DomainTransaction) uint64 {
|
|
if transactionhelper.IsCoinBase(transaction) {
|
|
return 0
|
|
}
|
|
|
|
// calculate mass for size
|
|
size := transactionEstimatedSerializedSize(transaction)
|
|
massForSize := size * c.massPerTxByte
|
|
|
|
// calculate mass for scriptPubKey
|
|
totalScriptPubKeySize := uint64(0)
|
|
for _, output := range transaction.Outputs {
|
|
totalScriptPubKeySize += 2 //output.ScriptPublicKey.Version (uint16)
|
|
totalScriptPubKeySize += uint64(len(output.ScriptPublicKey.Script))
|
|
}
|
|
massForScriptPubKey := totalScriptPubKeySize * c.massPerScriptPubKeyByte
|
|
|
|
// calculate mass for SigOps
|
|
totalSigOpCount := uint64(0)
|
|
for _, input := range transaction.Inputs {
|
|
totalSigOpCount += uint64(input.SigOpCount)
|
|
}
|
|
massForSigOps := totalSigOpCount * c.massPerSigOp
|
|
|
|
// Sum all components of mass
|
|
return massForSize + massForScriptPubKey + massForSigOps
|
|
}
|
|
|
|
// transactionEstimatedSerializedSize is the estimated size of a transaction in some
|
|
// serialization. This has to be deterministic, but not necessarily accurate, since
|
|
// it's only used as the size component in the transaction and block mass limit
|
|
// calculation.
|
|
func transactionEstimatedSerializedSize(tx *externalapi.DomainTransaction) uint64 {
|
|
if transactionhelper.IsCoinBase(tx) {
|
|
return 0
|
|
}
|
|
size := uint64(0)
|
|
size += 2 // Txn Version
|
|
size += 8 // number of inputs (uint64)
|
|
for _, input := range tx.Inputs {
|
|
size += transactionInputEstimatedSerializedSize(input)
|
|
}
|
|
|
|
size += 8 // number of outputs (uint64)
|
|
for _, output := range tx.Outputs {
|
|
size += TransactionOutputEstimatedSerializedSize(output)
|
|
}
|
|
|
|
size += 8 // lock time (uint64)
|
|
size += externalapi.DomainSubnetworkIDSize
|
|
size += 8 // gas (uint64)
|
|
size += externalapi.DomainHashSize // payload hash
|
|
|
|
size += 8 // length of the payload (uint64)
|
|
size += uint64(len(tx.Payload))
|
|
|
|
return size
|
|
}
|
|
|
|
func transactionInputEstimatedSerializedSize(input *externalapi.DomainTransactionInput) uint64 {
|
|
size := uint64(0)
|
|
size += outpointEstimatedSerializedSize()
|
|
|
|
size += 8 // length of signature script (uint64)
|
|
size += uint64(len(input.SignatureScript))
|
|
|
|
size += 8 // sequence (uint64)
|
|
return size
|
|
}
|
|
|
|
func outpointEstimatedSerializedSize() uint64 {
|
|
size := uint64(0)
|
|
size += externalapi.DomainHashSize // ID
|
|
size += 4 // index (uint32)
|
|
return size
|
|
}
|
|
|
|
// TransactionOutputEstimatedSerializedSize is the same as transactionEstimatedSerializedSize but for outputs only
|
|
func TransactionOutputEstimatedSerializedSize(output *externalapi.DomainTransactionOutput) uint64 {
|
|
size := uint64(0)
|
|
size += 8 // value (uint64)
|
|
size += 2 // output.ScriptPublicKey.Version (uint 16)
|
|
size += 8 // length of script public key (uint64)
|
|
size += uint64(len(output.ScriptPublicKey.Script))
|
|
return size
|
|
}
|