mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-25 23:42:36 +00:00

* [NOD-1579] Rename AcceptedTxIDs to AcceptedTransactionIDs. * [NOD-1579] Add InsertBlockResult to ValidateAndInsertBlock results. * [NOD-1593] Rename InsertBlockResult to BlockInsertionResult. * [NOD-1593] Add SelectedParentChainChanges to AddBlockToVirtual's result. * [NOD-1593] Implement findSelectedParentChainChanges. * [NOD-1593] Implement TestFindSelectedParentChainChanges. * [NOD-1593] Fix a string. * [NOD-1593] Finish implementing TestFindSelectedParentChainChanges. * [NOD-1593] Fix merge errors. * [NOD-1597] Begin implementing UTXOIndex. * [NOD-1597] Connect UTXOIndex to RPC. * [NOD-1597] Connect Consensus to UTXOIndex. * [NOD-1597] Add AcceptanceData to BlockInfo. * [NOD-1597] Implement UTXOIndex.Update(). * [NOD-1597] Implement add(), remove(), and discard() in utxoIndexStore. * [NOD-1597] Add error cases to add() and remove(). * [NOD-1597] Add special cases to add() and remove(). * [NOD-1597] Implement commit. * [NOD-1597] Add a mutex around UTXOIndex.Update(). * [NOD-1597] Return changes to the UTXO from Update(). * [NOD-1597] Add NotifyUTXOsChangedRequestMessage and related structs. * [NOD-1597] Implement HandleNotifyUTXOsChanged. * [NOD-1597] Begin implementing TestUTXOIndex. * [NOD-1597] Implement RegisterForUTXOsChangedNotifications. * [NOD-1597] Fix bad transaction.ID usage. * [NOD-1597] Implement convertUTXOChangesToUTXOsChangedNotification. * [NOD-1597] Make UTXOsChangedNotificationMessage.Removed UTXOsByAddressesEntry instead of just RPCOutpoint so that the client can discern which address was the UTXO removed for. * [NOD-1597] Collect outpoints in TestUTXOIndex. * [NOD-1597] Rename RPC stuff. * [NOD-1597] Add messages for GetUTXOsByAddresses. * [NOD-1597] Implement HandleGetUTXOsByAddresses. * [NOD-1597] Implement GetUTXOsByAddresses. * [NOD-1597] Implement UTXOs(). * [NOD-1597] Implement getUTXOOutpointEntryPairs(). * [NOD-1597] Expand TestUTXOIndex. * [NOD-1597] Convert SubmitTransaction to use RPCTransaction instead of MsgTx. * [NOD-1597] Finish implementing TestUTXOIndex. * [NOD-1597] Add messages for GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement HandleGetVirtualSelectedParentBlueScore and GetVirtualSelectedParentBlueScore. * [NOD-1597] Implement TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement NotifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Expand TestVirtualSelectedParentBlueScore. * [NOD-1597] Implement notifyVirtualSelectedParentBlueScoreChanged. * [NOD-1597] Make go lint happy. * [NOD-1593] Fix merge errors. * [NOD-1593] Rename findSelectedParentChainChanges to calculateSelectedParentChainChanges. * [NOD-1593] Expand TestCalculateSelectedParentChainChanges. * [NOD-1597] Add logs to utxoindex.go. * [NOD-1597] Add logs to utxoindex/store.go. * [NOD-1597] Add logs to RPCManager.NotifyXXX functions. * [NOD-1597] Ignore transactions that aren't accepted. * [NOD-1597] Use GetBlockAcceptanceData instead of GetBlockInfo. * [NOD-1597] Convert scriptPublicKey to string directly, instead of using hex. * [NOD-1597] Add a comment. * [NOD-1597] Guard against calling utxoindex methods when utxoindex is turned off. * [NOD-1597] Add lock to UTXOs. * [NOD-1597] Guard against calls to getUTXOOutpointEntryPairs when staging isn't empty.
126 lines
4.1 KiB
Go
126 lines
4.1 KiB
Go
package integration
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/kaspanet/go-secp256k1"
|
|
"github.com/kaspanet/kaspad/app/appmessage"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionhelper"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/txscript"
|
|
"github.com/kaspanet/kaspad/util"
|
|
)
|
|
|
|
func TestTxRelay(t *testing.T) {
|
|
payer, mediator, payee, teardown := standardSetup(t)
|
|
defer teardown()
|
|
|
|
// Connect nodes in chain: payer <--> mediator <--> payee
|
|
// So that payee doesn't directly get transactions from payer
|
|
connect(t, payer, mediator)
|
|
connect(t, mediator, payee)
|
|
|
|
payeeBlockAddedChan := make(chan *appmessage.MsgBlockHeader)
|
|
setOnBlockAddedHandler(t, payee, func(notification *appmessage.BlockAddedNotificationMessage) {
|
|
payeeBlockAddedChan <- ¬ification.Block.Header
|
|
})
|
|
// skip the first block because it's paying to genesis script
|
|
mineNextBlock(t, payer)
|
|
waitForPayeeToReceiveBlock(t, payeeBlockAddedChan)
|
|
// use the second block to get money to pay with
|
|
secondBlock := mineNextBlock(t, payer)
|
|
waitForPayeeToReceiveBlock(t, payeeBlockAddedChan)
|
|
|
|
// Mine BlockCoinbaseMaturity more blocks for our money to mature
|
|
for i := uint64(0); i < payer.config.ActiveNetParams.BlockCoinbaseMaturity; i++ {
|
|
mineNextBlock(t, payer)
|
|
waitForPayeeToReceiveBlock(t, payeeBlockAddedChan)
|
|
}
|
|
|
|
msgTx := generateTx(t, secondBlock.Transactions[transactionhelper.CoinbaseTransactionIndex], payer, payee)
|
|
domainTransaction := appmessage.MsgTxToDomainTransaction(msgTx)
|
|
rpcTransaction := appmessage.DomainTransactionToRPCTransaction(domainTransaction)
|
|
response, err := payer.rpcClient.SubmitTransaction(rpcTransaction)
|
|
if err != nil {
|
|
t.Fatalf("Error submitting transaction: %+v", err)
|
|
}
|
|
txID := response.TransactionID
|
|
|
|
txAddedToMempoolChan := make(chan struct{})
|
|
|
|
spawn("TestTxRelay-WaitForTransactionPropagation", func() {
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
_, err := payee.rpcClient.GetMempoolEntry(txID)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "transaction is not in the pool") {
|
|
continue
|
|
}
|
|
|
|
t.Fatalf("Error getting mempool entry: %+v", err)
|
|
}
|
|
close(txAddedToMempoolChan)
|
|
return
|
|
}
|
|
})
|
|
|
|
select {
|
|
case <-txAddedToMempoolChan:
|
|
case <-time.After(defaultTimeout):
|
|
t.Fatalf("Timeout waiting for transaction to be accepted into mempool")
|
|
}
|
|
}
|
|
|
|
func waitForPayeeToReceiveBlock(t *testing.T, payeeBlockAddedChan chan *appmessage.MsgBlockHeader) {
|
|
select {
|
|
case <-payeeBlockAddedChan:
|
|
case <-time.After(defaultTimeout):
|
|
t.Fatalf("Timeout waiting for block added")
|
|
}
|
|
}
|
|
|
|
func generateTx(t *testing.T, firstBlockCoinbase *externalapi.DomainTransaction, payer, payee *appHarness) *appmessage.MsgTx {
|
|
txIns := make([]*appmessage.TxIn, 1)
|
|
txIns[0] = appmessage.NewTxIn(appmessage.NewOutpoint(consensushashing.TransactionID(firstBlockCoinbase), 0), []byte{})
|
|
|
|
payeeAddress, err := util.DecodeAddress(payee.miningAddress, util.Bech32PrefixKaspaSim)
|
|
if err != nil {
|
|
t.Fatalf("Error decoding payeeAddress: %+v", err)
|
|
}
|
|
toScript, err := txscript.PayToAddrScript(payeeAddress)
|
|
if err != nil {
|
|
t.Fatalf("Error generating script: %+v", err)
|
|
}
|
|
|
|
txOuts := []*appmessage.TxOut{appmessage.NewTxOut(firstBlockCoinbase.Outputs[0].Value-1000, toScript)}
|
|
|
|
fromScript := firstBlockCoinbase.Outputs[0].ScriptPublicKey
|
|
|
|
tx := appmessage.NewNativeMsgTx(constants.TransactionVersion, txIns, txOuts)
|
|
|
|
privateKeyBytes, err := hex.DecodeString(payer.miningAddressPrivateKey)
|
|
if err != nil {
|
|
t.Fatalf("Error decoding private key: %+v", err)
|
|
}
|
|
privateKey, err := secp256k1.DeserializePrivateKeyFromSlice(privateKeyBytes)
|
|
if err != nil {
|
|
t.Fatalf("Error deserializing private key: %+v", err)
|
|
}
|
|
|
|
signatureScript, err := txscript.SignatureScript(appmessage.MsgTxToDomainTransaction(tx), 0,
|
|
fromScript, txscript.SigHashAll, privateKey)
|
|
if err != nil {
|
|
t.Fatalf("Error signing transaction: %+v", err)
|
|
}
|
|
tx.TxIn[0].SignatureScript = signatureScript
|
|
|
|
return tx
|
|
}
|