mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-19 22:58:43 +00:00
Red inclusion (#1275)
* Accept red blocks transactions * Add comments to TestTransactionAcceptance * Fix tests * Remove fetchUTXOSetIfMissing * Remove redundant dependency * Fix comments
This commit is contained in:
@@ -59,8 +59,8 @@ func (flow *handleRelayInvsFlow) runIBDIfNotRunning(highHash *externalapi.Domain
|
||||
}
|
||||
|
||||
if !isValid {
|
||||
log.Infof("The suggested pruning point is incompatible to this node DAG, so stopping IBD with this" +
|
||||
" peer")
|
||||
log.Infof("The suggested pruning point %s is incompatible to this node DAG, so stopping IBD with this"+
|
||||
" peer", msgIBDRootHash.Hash)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -91,40 +91,6 @@ func (flow *handleRelayInvsFlow) runIBDIfNotRunning(highHash *externalapi.Domain
|
||||
return nil
|
||||
}
|
||||
|
||||
func (flow *handleRelayInvsFlow) fetchUTXOSetIfMissing() (bool, error) {
|
||||
err := flow.outgoingRoute.Enqueue(appmessage.NewMsgRequestIBDRootHash())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
message, err := flow.dequeueIncomingMessageAndSkipInvs(common.DefaultTimeout)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
msgIBDRootHash, ok := message.(*appmessage.MsgIBDRootHash)
|
||||
if !ok {
|
||||
return false, protocolerrors.Errorf(true, "received unexpected message type. "+
|
||||
"expected: %s, got: %s", appmessage.CmdIBDRootHash, message.Command())
|
||||
}
|
||||
|
||||
isValid, err := flow.Domain().Consensus().IsValidPruningPoint(msgIBDRootHash.Hash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if !isValid {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
found, err := flow.fetchMissingUTXOSet(msgIBDRootHash.Hash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (flow *handleRelayInvsFlow) syncHeaders(highHash *externalapi.DomainHash) error {
|
||||
log.Debugf("Trying to find highest shared chain block with peer %s with high hash %s", flow.peer, highHash)
|
||||
highestSharedBlockHash, err := flow.findHighestSharedBlockHash(highHash)
|
||||
|
||||
@@ -117,12 +117,12 @@ func (tx *DomainTransaction) Equal(other *DomainTransaction) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if tx.Fee != other.Fee {
|
||||
return false
|
||||
if tx.Fee != 0 && other.Fee != 0 && tx.Fee != other.Fee {
|
||||
panic(errors.New("identical transactions should always have the same fee"))
|
||||
}
|
||||
|
||||
if tx.Mass != other.Mass {
|
||||
return false
|
||||
if tx.Mass != 0 && other.Mass != 0 && tx.Mass != other.Mass {
|
||||
panic(errors.New("identical transactions should always have the same mass"))
|
||||
}
|
||||
|
||||
if tx.ID != nil && other.ID != nil && !tx.ID.Equal(other.ID) {
|
||||
@@ -163,8 +163,8 @@ func (input *DomainTransactionInput) Equal(other *DomainTransactionInput) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
if !input.UTXOEntry.Equal(other.UTXOEntry) {
|
||||
return false
|
||||
if input.UTXOEntry != nil && other.UTXOEntry != nil && !input.UTXOEntry.Equal(other.UTXOEntry) {
|
||||
panic(errors.New("identical inputs should always have the same UTXO entry"))
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
@@ -25,6 +25,7 @@ type testDomainTransactionStruct struct {
|
||||
type transactionInputToCompare struct {
|
||||
tx *externalapi.DomainTransactionInput
|
||||
expectedResult bool
|
||||
expectsPanic bool
|
||||
}
|
||||
|
||||
type testDomainTransactionInputStruct struct {
|
||||
@@ -291,7 +292,7 @@ func initTestTransactionToCompare() []*transactionToCompare {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}),
|
||||
},
|
||||
expectedResult: false,
|
||||
expectedResult: true,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransaction{
|
||||
2, //Changed
|
||||
@@ -317,7 +318,7 @@ func initTestTransactionToCompare() []*transactionToCompare {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}),
|
||||
}, //6
|
||||
},
|
||||
expectedResult: false,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransaction{
|
||||
@@ -344,8 +345,8 @@ func initTestTransactionToCompare() []*transactionToCompare {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}),
|
||||
}, //7
|
||||
expectedResult: false,
|
||||
},
|
||||
expectsPanic: true,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransaction{
|
||||
1,
|
||||
@@ -463,8 +464,8 @@ func initTestTransactionToCompare() []*transactionToCompare {
|
||||
[]*externalapi.DomainTransactionInput{{externalapi.DomainOutpoint{
|
||||
*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01}), 0xFFFF},
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(1, []byte{0, 1, 2, 3, 4}, true, 2)}},
|
||||
uint64(0xFFFFFFF0), // Changed sequence
|
||||
utxo.NewUTXOEntry(1, []byte{0, 1, 2, 3}, true, 2)}},
|
||||
[]*externalapi.DomainTransactionOutput{{uint64(0xFFFF),
|
||||
[]byte{1, 2}}, {uint64(0xFFFF),
|
||||
[]byte{1, 3}}},
|
||||
@@ -485,6 +486,35 @@ func initTestTransactionToCompare() []*transactionToCompare {
|
||||
},
|
||||
expectedResult: false,
|
||||
},
|
||||
{
|
||||
tx: &externalapi.DomainTransaction{
|
||||
1,
|
||||
[]*externalapi.DomainTransactionInput{{externalapi.DomainOutpoint{
|
||||
*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01}), 0xFFFF},
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(1, []byte{0, 1, 2, 3}, true, 2)}},
|
||||
[]*externalapi.DomainTransactionOutput{{uint64(0xFFFF),
|
||||
[]byte{1, 2}},
|
||||
{uint64(0xFFFF),
|
||||
[]byte{1, 3}}},
|
||||
1,
|
||||
externalapi.DomainSubnetworkID{0x01},
|
||||
2, // Changed
|
||||
*externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
|
||||
[]byte{0x01},
|
||||
0,
|
||||
1,
|
||||
externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}),
|
||||
},
|
||||
expectedResult: false,
|
||||
},
|
||||
}
|
||||
return testTx
|
||||
}
|
||||
@@ -583,7 +613,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
|
||||
[]byte{0x01},
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
@@ -606,7 +636,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
|
||||
[]byte{0x01},
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
@@ -624,11 +654,29 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
|
||||
[]byte{0x01},
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
expectedResult: true,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransaction{
|
||||
1,
|
||||
[]*externalapi.DomainTransactionInput{},
|
||||
[]*externalapi.DomainTransactionOutput{},
|
||||
1,
|
||||
externalapi.DomainSubnetworkID{0x01},
|
||||
1,
|
||||
*externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}),
|
||||
[]byte{0x01},
|
||||
2, // Changed fee
|
||||
1,
|
||||
nil,
|
||||
},
|
||||
expectsPanic: true,
|
||||
}},
|
||||
},
|
||||
}
|
||||
@@ -661,7 +709,15 @@ func initTestDomainTxInputToCompare() []*transactionInputToCompare {
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(1, []byte{0, 1, 2, 3}, false, 2), // Changed
|
||||
},
|
||||
expectedResult: false,
|
||||
expectsPanic: true,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransactionInput{
|
||||
externalapi.DomainOutpoint{*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01}), 0xFFFF},
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFFF),
|
||||
nil, // Changed
|
||||
},
|
||||
expectedResult: true,
|
||||
}, {
|
||||
tx: &externalapi.DomainTransactionInput{
|
||||
externalapi.DomainOutpoint{*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01}), 0xFFFF},
|
||||
@@ -682,7 +738,7 @@ func initTestDomainTxInputToCompare() []*transactionInputToCompare {
|
||||
tx: &externalapi.DomainTransactionInput{
|
||||
externalapi.DomainOutpoint{*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01, 0x02}), 0xFFFF}, // Changed
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFF0), // Changed
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(1, []byte{0, 1, 2, 3}, true, 2),
|
||||
},
|
||||
expectedResult: false,
|
||||
@@ -690,7 +746,7 @@ func initTestDomainTxInputToCompare() []*transactionInputToCompare {
|
||||
tx: &externalapi.DomainTransactionInput{
|
||||
externalapi.DomainOutpoint{*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01, 0x02}), 0xFFFF}, // Changed
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFF0), // Changed
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(2 /* Changed */, []byte{0, 1, 2, 3}, true, 2), // Changed
|
||||
},
|
||||
expectedResult: false,
|
||||
@@ -698,7 +754,7 @@ func initTestDomainTxInputToCompare() []*transactionInputToCompare {
|
||||
tx: &externalapi.DomainTransactionInput{
|
||||
externalapi.DomainOutpoint{*externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01, 0x02}), 0xFFFF}, // Changed
|
||||
[]byte{1, 2, 3},
|
||||
uint64(0xFFFFFFF0), // Changed
|
||||
uint64(0xFFFFFFFF),
|
||||
utxo.NewUTXOEntry(3 /* Changed */, []byte{0, 1, 2, 3}, true, 3), // Changed
|
||||
},
|
||||
expectedResult: false,
|
||||
@@ -852,7 +908,7 @@ func TestDomainTransaction_Equal(t *testing.T) {
|
||||
r := recover()
|
||||
panicked := r != nil
|
||||
if panicked != subTest.expectsPanic {
|
||||
t.Fatalf("panicked expected to be %t but got %t", subTest.expectsPanic, panicked)
|
||||
t.Fatalf("Test #%d:%d: panicked expected to be %t but got %t: %s", i, j, subTest.expectsPanic, panicked, r)
|
||||
}
|
||||
}()
|
||||
result1 := test.baseTx.Equal(subTest.tx)
|
||||
@@ -865,7 +921,7 @@ func TestDomainTransaction_Equal(t *testing.T) {
|
||||
r := recover()
|
||||
panicked := r != nil
|
||||
if panicked != subTest.expectsPanic {
|
||||
t.Fatalf("panicked expected to be %t but got %t", subTest.expectsPanic, panicked)
|
||||
t.Fatalf("Test #%d:%d: panicked expected to be %t but got %t: %s", i, j, subTest.expectsPanic, panicked, r)
|
||||
}
|
||||
}()
|
||||
result2 := subTest.tx.Equal(test.baseTx)
|
||||
@@ -896,14 +952,32 @@ func TestDomainTransactionInput_Equal(t *testing.T) {
|
||||
txTests := initTestDomainTransactionInputForEqual()
|
||||
for i, test := range txTests {
|
||||
for j, subTest := range test.transactionInputToCompareTo {
|
||||
result1 := test.baseTx.Equal(subTest.tx)
|
||||
if result1 != subTest.expectedResult {
|
||||
t.Fatalf("Test #%d:%d: Expected %t but got %t", i, j, subTest.expectedResult, result1)
|
||||
}
|
||||
result2 := subTest.tx.Equal(test.baseTx)
|
||||
if result2 != subTest.expectedResult {
|
||||
t.Fatalf("Test #%d:%d: Expected %t but got %t", i, j, subTest.expectedResult, result2)
|
||||
}
|
||||
func() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
panicked := r != nil
|
||||
if panicked != subTest.expectsPanic {
|
||||
t.Fatalf("Test #%d:%d: panicked expected to be %t but got %t: %s", i, j, subTest.expectsPanic, panicked, r)
|
||||
}
|
||||
}()
|
||||
result1 := test.baseTx.Equal(subTest.tx)
|
||||
if result1 != subTest.expectedResult {
|
||||
t.Fatalf("Test #%d:%d: Expected %t but got %t", i, j, subTest.expectedResult, result1)
|
||||
}
|
||||
}()
|
||||
func() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
panicked := r != nil
|
||||
if panicked != subTest.expectsPanic {
|
||||
t.Fatalf("Test #%d:%d: panicked expected to be %t but got %t: %s", i, j, subTest.expectsPanic, panicked, r)
|
||||
}
|
||||
}()
|
||||
result2 := subTest.tx.Equal(test.baseTx)
|
||||
if result2 != subTest.expectedResult {
|
||||
t.Fatalf("Test #%d:%d: Expected %t but got %t", i, j, subTest.expectedResult, result2)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ type BlockGHOSTDAGData interface {
|
||||
SelectedParent() *externalapi.DomainHash
|
||||
MergeSetBlues() []*externalapi.DomainHash
|
||||
MergeSetReds() []*externalapi.DomainHash
|
||||
MergeSet() []*externalapi.DomainHash
|
||||
BluesAnticoneSizes() map[externalapi.DomainHash]KType
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ func (csm *consensusStateManager) CalculatePastUTXOAndAcceptanceData(blockHash *
|
||||
}
|
||||
|
||||
log.Tracef("Applying blue blocks to the selected parent past UTXO of block %s", blockHash)
|
||||
acceptanceData, utxoDiff, err := csm.applyBlueBlocks(blockHash, selectedParentPastUTXO, blockGHOSTDAGData)
|
||||
acceptanceData, utxoDiff, err := csm.applyMergeSetBlocks(blockHash, selectedParentPastUTXO, blockGHOSTDAGData)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
@@ -107,14 +107,14 @@ func (csm *consensusStateManager) restorePastUTXO(blockHash *externalapi.DomainH
|
||||
return accumulatedDiff, nil
|
||||
}
|
||||
|
||||
func (csm *consensusStateManager) applyBlueBlocks(blockHash *externalapi.DomainHash,
|
||||
func (csm *consensusStateManager) applyMergeSetBlocks(blockHash *externalapi.DomainHash,
|
||||
selectedParentPastUTXODiff model.MutableUTXODiff, ghostdagData model.BlockGHOSTDAGData) (
|
||||
externalapi.AcceptanceData, model.MutableUTXODiff, error) {
|
||||
|
||||
log.Tracef("applyBlueBlocks start for block %s", blockHash)
|
||||
defer log.Tracef("applyBlueBlocks end for block %s", blockHash)
|
||||
log.Tracef("applyMergeSetBlocks start for block %s", blockHash)
|
||||
defer log.Tracef("applyMergeSetBlocks end for block %s", blockHash)
|
||||
|
||||
blueBlocks, err := csm.blockStore.Blocks(csm.databaseContext, ghostdagData.MergeSetBlues())
|
||||
mergeSetBlocks, err := csm.blockStore.Blocks(csm.databaseContext, ghostdagData.MergeSet())
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -125,26 +125,26 @@ func (csm *consensusStateManager) applyBlueBlocks(blockHash *externalapi.DomainH
|
||||
}
|
||||
log.Tracef("The past median time for block %s is: %d", blockHash, selectedParentMedianTime)
|
||||
|
||||
multiblockAcceptanceData := make(externalapi.AcceptanceData, len(blueBlocks))
|
||||
multiblockAcceptanceData := make(externalapi.AcceptanceData, len(mergeSetBlocks))
|
||||
accumulatedUTXODiff := selectedParentPastUTXODiff
|
||||
accumulatedMass := uint64(0)
|
||||
|
||||
for i, blueBlock := range blueBlocks {
|
||||
blueBlockHash := consensushashing.BlockHash(blueBlock)
|
||||
log.Tracef("Applying blue block %s", blueBlockHash)
|
||||
for i, mergeSetBlock := range mergeSetBlocks {
|
||||
mergeSetBlockHash := consensushashing.BlockHash(mergeSetBlock)
|
||||
log.Tracef("Applying merge set block %s", mergeSetBlockHash)
|
||||
blockAcceptanceData := &externalapi.BlockAcceptanceData{
|
||||
BlockHash: blueBlockHash,
|
||||
TransactionAcceptanceData: make([]*externalapi.TransactionAcceptanceData, len(blueBlock.Transactions)),
|
||||
BlockHash: mergeSetBlockHash,
|
||||
TransactionAcceptanceData: make([]*externalapi.TransactionAcceptanceData, len(mergeSetBlock.Transactions)),
|
||||
}
|
||||
isSelectedParent := i == 0
|
||||
log.Tracef("Is blue block %s the selected parent: %t", blueBlockHash, isSelectedParent)
|
||||
log.Tracef("Is merge set block %s the selected parent: %t", mergeSetBlockHash, isSelectedParent)
|
||||
|
||||
for j, transaction := range blueBlock.Transactions {
|
||||
for j, transaction := range mergeSetBlock.Transactions {
|
||||
var isAccepted bool
|
||||
|
||||
transactionID := consensushashing.TransactionID(transaction)
|
||||
log.Tracef("Attempting to accept transaction %s in block %s",
|
||||
transactionID, blueBlockHash)
|
||||
transactionID, mergeSetBlockHash)
|
||||
|
||||
isAccepted, accumulatedMass, err = csm.maybeAcceptTransaction(transaction, blockHash, isSelectedParent,
|
||||
accumulatedUTXODiff, accumulatedMass, selectedParentMedianTime, ghostdagData.BlueScore())
|
||||
@@ -152,7 +152,7 @@ func (csm *consensusStateManager) applyBlueBlocks(blockHash *externalapi.DomainH
|
||||
return nil, nil, err
|
||||
}
|
||||
log.Tracef("Transaction %s in block %s isAccepted: %t, fee: %d",
|
||||
transactionID, blueBlockHash, isAccepted, transaction.Fee)
|
||||
transactionID, mergeSetBlockHash, isAccepted, transaction.Fee)
|
||||
|
||||
blockAcceptanceData.TransactionAcceptanceData[j] = &externalapi.TransactionAcceptanceData{
|
||||
Transaction: transaction,
|
||||
|
||||
@@ -2,6 +2,9 @@ package consensusstatemanager_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||
"testing"
|
||||
|
||||
"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
|
||||
@@ -151,6 +154,215 @@ func TestDoubleSpends(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestTransactionAcceptance checks that blue blocks transactions are favoured above
|
||||
// red blocks transactions, and that the block reward is paid only for blue blocks.
|
||||
func TestTransactionAcceptance(t *testing.T) {
|
||||
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
||||
params.BlockCoinbaseMaturity = 0
|
||||
|
||||
factory := consensus.NewFactory()
|
||||
testConsensus, teardown, err := factory.NewTestConsensus(params, "TestTransactionAcceptance")
|
||||
if err != nil {
|
||||
t.Fatalf("Error setting up testConsensus: %+v", err)
|
||||
}
|
||||
defer teardown(false)
|
||||
|
||||
fundingBlock1Hash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fundingBlock1: %+v", err)
|
||||
}
|
||||
|
||||
fundingBlock2Hash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{fundingBlock1Hash}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fundingBlock2: %+v", err)
|
||||
}
|
||||
|
||||
// Generate fundingBlock3 to pay for fundingBlock2
|
||||
fundingBlock3Hash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{fundingBlock2Hash}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fundingBlock3: %+v", err)
|
||||
}
|
||||
|
||||
// Add a chain of K blocks above fundingBlock3 so we'll
|
||||
// be able to mine a red block on top of it.
|
||||
tipHash := fundingBlock3Hash
|
||||
for i := model.KType(0); i < params.K; i++ {
|
||||
var err error
|
||||
tipHash, _, err = testConsensus.AddBlock([]*externalapi.DomainHash{tipHash}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating fundingBlock1: %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
fundingBlock2, err := testConsensus.GetBlock(fundingBlock2Hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting fundingBlock: %+v", err)
|
||||
}
|
||||
|
||||
fundingTransaction1 := fundingBlock2.Transactions[transactionhelper.CoinbaseTransactionIndex]
|
||||
|
||||
fundingBlock3, err := testConsensus.GetBlock(fundingBlock3Hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting fundingBlock: %+v", err)
|
||||
}
|
||||
|
||||
fundingTransaction2 := fundingBlock3.Transactions[transactionhelper.CoinbaseTransactionIndex]
|
||||
|
||||
spendingTransaction1, err := testutils.CreateTransaction(fundingTransaction1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating spendingTransaction1: %+v", err)
|
||||
}
|
||||
|
||||
spendingTransaction2, err := testutils.CreateTransaction(fundingTransaction2)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating spendingTransaction1: %+v", err)
|
||||
}
|
||||
|
||||
redHash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{fundingBlock3Hash}, nil,
|
||||
[]*externalapi.DomainTransaction{spendingTransaction1, spendingTransaction2})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating redBlock: %+v", err)
|
||||
}
|
||||
|
||||
blueScriptPublicKey := []byte{1}
|
||||
blueHash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{tipHash}, &externalapi.DomainCoinbaseData{
|
||||
ScriptPublicKey: blueScriptPublicKey,
|
||||
ExtraData: nil,
|
||||
},
|
||||
[]*externalapi.DomainTransaction{spendingTransaction1})
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating blue: %+v", err)
|
||||
}
|
||||
|
||||
// Mining two blocks so tipHash will definitely be the selected tip.
|
||||
tipHash, _, err = testConsensus.AddBlock([]*externalapi.DomainHash{tipHash}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating tip: %+v", err)
|
||||
}
|
||||
|
||||
finalTipSelectedParentScriptPublicKey := []byte{3}
|
||||
finalTipSelectedParentHash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{tipHash},
|
||||
&externalapi.DomainCoinbaseData{
|
||||
ScriptPublicKey: finalTipSelectedParentScriptPublicKey,
|
||||
ExtraData: nil,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating tip: %+v", err)
|
||||
}
|
||||
|
||||
finalTipHash, _, err := testConsensus.AddBlock([]*externalapi.DomainHash{finalTipSelectedParentHash, redHash, blueHash}, nil,
|
||||
nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating finalTip: %+v", err)
|
||||
}
|
||||
|
||||
acceptanceData, err := testConsensus.AcceptanceDataStore().Get(testConsensus.DatabaseContext(), finalTipHash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting acceptance data: %+v", err)
|
||||
}
|
||||
|
||||
finalTipSelectedParent, err := testConsensus.GetBlock(finalTipSelectedParentHash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting finalTipSelectedParent: %+v", err)
|
||||
}
|
||||
|
||||
blue, err := testConsensus.GetBlock(blueHash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting blue: %+v", err)
|
||||
}
|
||||
|
||||
red, err := testConsensus.GetBlock(redHash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting red: %+v", err)
|
||||
}
|
||||
|
||||
// We expect spendingTransaction1 to be accepted by the blue block and not by the red one, because
|
||||
// blue blocks in the merge set should always be ordered before red blocks in the merge set.
|
||||
// We also expect spendingTransaction2 to be accepted by the red because nothing conflicts it.
|
||||
expectedAcceptanceData := externalapi.AcceptanceData{
|
||||
{
|
||||
BlockHash: finalTipSelectedParentHash,
|
||||
TransactionAcceptanceData: []*externalapi.TransactionAcceptanceData{
|
||||
{
|
||||
Transaction: finalTipSelectedParent.Transactions[0],
|
||||
Fee: 0,
|
||||
IsAccepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
BlockHash: blueHash,
|
||||
TransactionAcceptanceData: []*externalapi.TransactionAcceptanceData{
|
||||
{
|
||||
Transaction: blue.Transactions[0],
|
||||
Fee: 0,
|
||||
IsAccepted: false,
|
||||
},
|
||||
{
|
||||
Transaction: spendingTransaction1,
|
||||
Fee: 1,
|
||||
IsAccepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
BlockHash: redHash,
|
||||
TransactionAcceptanceData: []*externalapi.TransactionAcceptanceData{
|
||||
{
|
||||
Transaction: red.Transactions[0],
|
||||
Fee: 0,
|
||||
IsAccepted: false,
|
||||
},
|
||||
{
|
||||
Transaction: spendingTransaction1,
|
||||
Fee: 0,
|
||||
IsAccepted: false,
|
||||
},
|
||||
{
|
||||
Transaction: spendingTransaction2,
|
||||
Fee: 1,
|
||||
IsAccepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if !acceptanceData.Equal(expectedAcceptanceData) {
|
||||
t.Fatalf("The acceptance data is not the expected acceptance data")
|
||||
}
|
||||
|
||||
finalTip, err := testConsensus.GetBlock(finalTipHash)
|
||||
if err != nil {
|
||||
t.Fatalf("Error getting finalTip: %+v", err)
|
||||
}
|
||||
|
||||
// We expect the coinbase transaction to pay reward for the selected parent, the
|
||||
// blue block, and not for the red block.
|
||||
expectedCoinbase := &externalapi.DomainTransaction{
|
||||
Version: constants.TransactionVersion,
|
||||
Inputs: nil,
|
||||
Outputs: []*externalapi.DomainTransactionOutput{
|
||||
{
|
||||
Value: 50 * constants.SompiPerKaspa,
|
||||
ScriptPublicKey: finalTipSelectedParentScriptPublicKey,
|
||||
},
|
||||
{
|
||||
Value: 50*constants.SompiPerKaspa + 1, // testutils.CreateTransaction pays a fee of 1 sompi
|
||||
ScriptPublicKey: blueScriptPublicKey,
|
||||
},
|
||||
},
|
||||
LockTime: 0,
|
||||
SubnetworkID: subnetworks.SubnetworkIDCoinbase,
|
||||
Gas: 0,
|
||||
PayloadHash: finalTip.Transactions[0].PayloadHash,
|
||||
Payload: finalTip.Transactions[0].Payload,
|
||||
}
|
||||
if !finalTip.Transactions[transactionhelper.CoinbaseTransactionIndex].Equal(expectedCoinbase) {
|
||||
t.Fatalf("Unexpected coinbase transaction")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestResolveBlockStatusSanity(t *testing.T) {
|
||||
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
|
||||
consensus, teardown, err := consensus.NewFactory().NewTestConsensus(params, "TestResolveBlockStatusSanity")
|
||||
|
||||
@@ -57,3 +57,13 @@ func (bgd *blockGHOSTDAGData) MergeSetReds() []*externalapi.DomainHash {
|
||||
func (bgd *blockGHOSTDAGData) BluesAnticoneSizes() map[externalapi.DomainHash]model.KType {
|
||||
return bgd.bluesAnticoneSizes
|
||||
}
|
||||
|
||||
func (bgd *blockGHOSTDAGData) MergeSet() []*externalapi.DomainHash {
|
||||
mergeSet := make([]*externalapi.DomainHash, len(bgd.mergeSetBlues)+len(bgd.mergeSetReds))
|
||||
copy(mergeSet, bgd.mergeSetBlues)
|
||||
if len(bgd.mergeSetReds) > 0 {
|
||||
copy(mergeSet[len(bgd.mergeSetBlues):], bgd.mergeSetReds)
|
||||
}
|
||||
|
||||
return mergeSet
|
||||
}
|
||||
|
||||
@@ -135,7 +135,8 @@ func DecodeAddress(addr string, expectedPrefix Bech32Prefix) (Address, error) {
|
||||
return nil, errors.Errorf("decoded address's prefix could not be parsed: %s", err)
|
||||
}
|
||||
if expectedPrefix != Bech32PrefixUnknown && expectedPrefix != prefix {
|
||||
return nil, errors.Errorf("decoded address is of wrong network: %s", err)
|
||||
return nil, errors.Errorf("decoded address is of wrong network. Expected %s but got %s", expectedPrefix,
|
||||
prefix)
|
||||
}
|
||||
|
||||
// Switch on decoded length to determine the type.
|
||||
|
||||
Reference in New Issue
Block a user