mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-08-23 02:53:14 +00:00

* Added model and stubs for all main methods * Add constructors to all main objects * Implement BlockCandidateTransactions * implement expireOldTransactions and expireOrphanTransactions * Rename isHighPriority to neverExpires * Add stub for checkDoubleSpends * Revert "Rename isHighPriority to neverExpires" This reverts commit b2da9a4a00c02fb380d2518cf54fa16257bd8423. * Imeplement transactionsOrderedByFeeRate * Orphan maps should be idToOrphan * Add error.go to mempool * Invert the condition for banning when mempool rejects a transaction * Move all model objects to model package * Implement getParentsInPool * Implemented mempoolUTXOSet.addTransaction * Implement removeTransaction, remove sanity checks * Implemented mempoolUTXOSet.checkDoubleSpends * Implemented removeOrphan * Implement removeOrphan * Implement maybeAddOrphan and AddOrphan * Implemented processOrphansAfterAcceptedTransaction * Implement transactionsPool.addTransaction * Implement RemoveTransaction * If a transaction was removed from the mempool - update it's redeemers in orphan pool as well * Use maximumOrphanTransactionCount * Add allowOrphans to ValidateAndInsertTransaction stub * Implement validateTransaction functions * Implement fillInputs * Implement ValidateAndInsertTransaction * Implement HandleNewBlockTransactions * Implement missing mempool interface methods * Add comments to exported functions * Call ValidateTransactionInIsolation where needed * Implement RevalidateHighPriorityTransactions * Rewire kaspad to use new mempool, and fix compilation errors * Update rebroadcast logic to use new structure * Handle non-standard transaction errors properly * Add mutex to mempool * bugfix: GetTransaction panics when ok is false * properly calculate targetBlocksPerSecond in config.go * Fix various lint errors and tests * Fix expected text in test for duplicate transactions * Skip the coinbase transaction in HandleNewBlockTransactions * Unorphan the correct transactions * Call ValidateTransactionAndPopulateWithConsensusData on unorphanTransaction * Re-apply policy_test as check_transactions_standard_test * removeTransaction: Remove redeemers in orphan pool as well * Remove redundant check for uint64 < 0 * Export and rename isDust -> IsTransactionOutputDust to allow usage by rothschild * Add allowOrphan to SubmitTransaction RPC request * Remove all implementation from mempool.go * tidy go mod * Don't pass acceptedOrphans to handleNewBlockTransactions * Use t.Errorf in stead of t.Fatalf * Remove minimum relay fee from TestDust, as it's no longer configurable * Add separate VirtualDAASCore method for faster retrieval where it's repeated multiple times * Broadcast all transactions that were accepted * Don't re-use GetVirtualDAAScore in GetVirtualInfo - this causes a deadlock * Use real transaction count, and not Orphan * Get mempool config from outside, incorporating values received from cli * Use MinRelayFee and MaxOrphanTxs from global kaspad config * Add explanation for the seemingly redundant check for transaction version in checkTransactionStandard * Update some comment * Convert creation of acceptedTransactions to a single line * Move mempoolUTXOSet out of checkDoubleSpends * Add test for attempt to insert double spend into mempool * fillInputs: Skip check for coinbase - it's always false in mempool * Clarify comment about removeRedeemers when removing random orphan * Don't remove high-priority transactions in limitTransactionCount * Use mempool.removeTransaction in limitTransactionCount * Add mutex comment to handleNewBlockTransactions * Return error from limitTransactionCount * Pluralize the map types * mempoolUTXOSet.removeTransaction: Don't restore utxo if it was not created in mempool * Don't evacuate from orphanPool high-priority transactions * Disallow double-spends in orphan pool * Don't use exported (and locking) methods from inside mempool * Check for double spends in mempool during revalidateTransaction * Add checkOrphanDuplicate * Add orphan to acceptedOrphans, not current * Add TestHighPriorityTransactions * Fix off-by-one error in limitTransactionCount * Add TestRevalidateHighPriorityTransactions * Remove checkDoubleSpends from revalidateTransaction * Fix TestRevalidateHighPriorityTransactions * Move check for MaximumOrphanCount to beggining of maybeAddOrphan * Rename all map type to singulateToSingularMap * limitOrphanPool only after the orphan was added * TestDoubleSpendInMempool: use createChildTxWhenParentTxWasAddedByConsensus instead of createTransactionWithUTXOEntry * Fix some comments * Have separate min/max transaction versions for mempool * Add comment on defaultMaximumOrphanTransactionCount to keep it small as long as we have recursion * Fix comment * Rename: createChildTxWhenParentTxWasAddedByConsensus -> createChildTxWhereParentTxWasAddedByConsensus * Handle error from createChildTxWhereParentTxWasAddedByConsensus * Rename createChildTxWhereParentTxWasAddedByConsensus -> createChildAndParentTxsAndAddParentToConsensus * Convert all MaximumXXX constants to uint64 * Add comment * remove mutex comments
183 lines
4.9 KiB
Go
183 lines
4.9 KiB
Go
package consensushashing
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/serialization"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
"github.com/kaspanet/kaspad/util/binaryserializer"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// txEncoding is a bitmask defining which transaction fields we
|
|
// want to encode and which to ignore.
|
|
type txEncoding uint8
|
|
|
|
const (
|
|
txEncodingFull txEncoding = 0
|
|
|
|
txEncodingExcludeSignatureScript = 1 << iota
|
|
)
|
|
|
|
// TransactionHash returns the transaction hash.
|
|
func TransactionHash(tx *externalapi.DomainTransaction) *externalapi.DomainHash {
|
|
// Encode the header and hash everything prior to the number of
|
|
// transactions.
|
|
writer := hashes.NewTransactionHashWriter()
|
|
err := serializeTransaction(writer, tx, txEncodingFull)
|
|
if err != nil {
|
|
// It seems like this could only happen if the writer returned an error.
|
|
// and this writer should never return an error (no allocations or possible failures)
|
|
// the only non-writer error path here is unknown types in `WriteElement`
|
|
panic(errors.Wrap(err, "TransactionHash() failed. this should never fail for structurally-valid transactions"))
|
|
}
|
|
|
|
return writer.Finalize()
|
|
}
|
|
|
|
// TransactionID generates the Hash for the transaction without the signature script and payload field.
|
|
func TransactionID(tx *externalapi.DomainTransaction) *externalapi.DomainTransactionID {
|
|
// If transaction ID is already cached, return it
|
|
if tx.ID != nil {
|
|
return tx.ID
|
|
}
|
|
|
|
// Encode the transaction, replace signature script with zeroes, cut off
|
|
// payload and hash the result.
|
|
var encodingFlags txEncoding
|
|
if !transactionhelper.IsCoinBase(tx) {
|
|
encodingFlags = txEncodingExcludeSignatureScript
|
|
}
|
|
writer := hashes.NewTransactionIDWriter()
|
|
err := serializeTransaction(writer, tx, encodingFlags)
|
|
if err != nil {
|
|
// this writer never return errors (no allocations or possible failures) so errors can only come from validity checks,
|
|
// and we assume we never construct malformed transactions.
|
|
panic(errors.Wrap(err, "TransactionID() failed. this should never fail for structurally-valid transactions"))
|
|
}
|
|
transactionID := externalapi.DomainTransactionID(*writer.Finalize())
|
|
|
|
tx.ID = &transactionID
|
|
|
|
return tx.ID
|
|
}
|
|
|
|
// TransactionIDs converts the provided slice of DomainTransactions to a corresponding slice of TransactionIDs
|
|
func TransactionIDs(txs []*externalapi.DomainTransaction) []*externalapi.DomainTransactionID {
|
|
txIDs := make([]*externalapi.DomainTransactionID, len(txs))
|
|
for i, tx := range txs {
|
|
txIDs[i] = TransactionID(tx)
|
|
}
|
|
return txIDs
|
|
}
|
|
|
|
func serializeTransaction(w io.Writer, tx *externalapi.DomainTransaction, encodingFlags txEncoding) error {
|
|
err := binaryserializer.PutUint16(w, tx.Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
count := uint64(len(tx.Inputs))
|
|
err = serialization.WriteElement(w, count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, ti := range tx.Inputs {
|
|
err = writeTransactionInput(w, ti, encodingFlags)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
count = uint64(len(tx.Outputs))
|
|
err = serialization.WriteElement(w, count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, output := range tx.Outputs {
|
|
err = writeTxOut(w, output)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
err = binaryserializer.PutUint64(w, tx.LockTime)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write(tx.SubnetworkID[:])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = binaryserializer.PutUint64(w, tx.Gas)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = writeVarBytes(w, tx.Payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// writeTransactionInput encodes ti to the kaspa protocol encoding for a transaction
|
|
// input to w.
|
|
func writeTransactionInput(w io.Writer, ti *externalapi.DomainTransactionInput, encodingFlags txEncoding) error {
|
|
err := writeOutpoint(w, &ti.PreviousOutpoint)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if encodingFlags&txEncodingExcludeSignatureScript != txEncodingExcludeSignatureScript {
|
|
err = writeVarBytes(w, ti.SignatureScript)
|
|
} else {
|
|
err = writeVarBytes(w, []byte{})
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return binaryserializer.PutUint64(w, ti.Sequence)
|
|
}
|
|
|
|
func writeOutpoint(w io.Writer, outpoint *externalapi.DomainOutpoint) error {
|
|
_, err := w.Write(outpoint.TransactionID.ByteSlice())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return binaryserializer.PutUint32(w, outpoint.Index)
|
|
}
|
|
|
|
func writeVarBytes(w io.Writer, data []byte) error {
|
|
dataLength := uint64(len(data))
|
|
err := serialization.WriteElement(w, dataLength)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write(data)
|
|
return err
|
|
}
|
|
|
|
func writeTxOut(w io.Writer, to *externalapi.DomainTransactionOutput) error {
|
|
err := binaryserializer.PutUint64(w, to.Value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = binaryserializer.PutUint16(w, to.ScriptPublicKey.Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return writeVarBytes(w, to.ScriptPublicKey.Script)
|
|
}
|