From 4e4325913868d583eb91efe50a0a536bfc3af079 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 19 Jan 2025 09:53:06 +0200 Subject: [PATCH] Fix DomainTransaction clone, equal and tests --- .../model/acceptancedata_equal_clone_test.go | 11 +++++ .../model/externalapi/transaction.go | 27 ++++++----- .../transaction_equal_clone_test.go | 47 +++++++++++++++++++ 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/domain/consensus/model/acceptancedata_equal_clone_test.go b/domain/consensus/model/acceptancedata_equal_clone_test.go index c7143172d..2878249f8 100644 --- a/domain/consensus/model/acceptancedata_equal_clone_test.go +++ b/domain/consensus/model/acceptancedata_equal_clone_test.go @@ -329,6 +329,7 @@ func initTestBlockAcceptanceDataForClone() []*externalapi.BlockAcceptanceData { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -377,6 +378,7 @@ func iniBlockAcceptanceDataForEqual() []testBlockAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -410,6 +412,7 @@ func iniBlockAcceptanceDataForEqual() []testBlockAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -443,6 +446,7 @@ func iniBlockAcceptanceDataForEqual() []testBlockAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -476,6 +480,7 @@ func iniBlockAcceptanceDataForEqual() []testBlockAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -510,6 +515,7 @@ func iniBlockAcceptanceDataForEqual() []testBlockAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -614,6 +620,7 @@ func initTestAcceptanceDataForClone() []externalapi.AcceptanceData { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -664,6 +671,7 @@ func initAcceptanceDataForEqual() []testAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -697,6 +705,7 @@ func initAcceptanceDataForEqual() []testAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -730,6 +739,7 @@ func initAcceptanceDataForEqual() []testAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -763,6 +773,7 @@ func initAcceptanceDataForEqual() []testAcceptanceDataStruct { []byte{0x01}, 0, 1, + 0, externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/domain/consensus/model/externalapi/transaction.go b/domain/consensus/model/externalapi/transaction.go index f61553f3b..a61400ede 100644 --- a/domain/consensus/model/externalapi/transaction.go +++ b/domain/consensus/model/externalapi/transaction.go @@ -48,23 +48,24 @@ func (tx *DomainTransaction) Clone() *DomainTransaction { } return &DomainTransaction{ - Version: tx.Version, - Inputs: inputsClone, - Outputs: outputsClone, - LockTime: tx.LockTime, - SubnetworkID: *tx.SubnetworkID.Clone(), - Gas: tx.Gas, - Payload: payloadClone, - Fee: tx.Fee, - Mass: tx.Mass, - ID: idClone, + Version: tx.Version, + Inputs: inputsClone, + Outputs: outputsClone, + LockTime: tx.LockTime, + SubnetworkID: *tx.SubnetworkID.Clone(), + Gas: tx.Gas, + Payload: payloadClone, + Fee: tx.Fee, + Mass: tx.Mass, + MassCommitment: tx.MassCommitment, + ID: idClone, } } // If this doesn't compile, it means the type definition has been changed, so it's // an indication to update Equal and Clone accordingly. var _ = DomainTransaction{0, []*DomainTransactionInput{}, []*DomainTransactionOutput{}, 0, - DomainSubnetworkID{}, 0, []byte{}, 0, 0, + DomainSubnetworkID{}, 0, []byte{}, 0, 0, 0, &DomainTransactionID{}} // Equal returns whether tx equals to other @@ -113,6 +114,10 @@ func (tx *DomainTransaction) Equal(other *DomainTransaction) bool { return false } + if tx.MassCommitment != other.MassCommitment { + return false + } + if tx.Fee != 0 && other.Fee != 0 && tx.Fee != other.Fee { panic(errors.New("identical transactions should always have the same fee")) } diff --git a/domain/consensus/model/externalapi/transaction_equal_clone_test.go b/domain/consensus/model/externalapi/transaction_equal_clone_test.go index ec23162e8..512c26090 100644 --- a/domain/consensus/model/externalapi/transaction_equal_clone_test.go +++ b/domain/consensus/model/externalapi/transaction_equal_clone_test.go @@ -83,6 +83,7 @@ func initTestBaseTransaction() *externalapi.DomainTransaction { []byte{0x01}, 0, 1, + 0, 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, @@ -112,6 +113,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -137,6 +139,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -162,6 +165,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01, 0x02}, //Changed 0, 1, + 0, 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, @@ -186,6 +190,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -213,6 +218,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -239,6 +245,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 1000000000, //Changed 1, + 0, 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, @@ -263,6 +270,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -287,6 +295,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 2, //Changed + 0, 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, @@ -311,6 +320,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -341,6 +351,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -366,6 +377,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -390,6 +402,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, nil, //changed }, expectedResult: true, @@ -411,6 +424,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -435,6 +449,7 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, 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, @@ -461,6 +476,34 @@ func initTestTransactionToCompare() []*transactionToCompare { []byte{0x01}, 0, 1, + 0, + 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, + }, + { + tx: &externalapi.DomainTransaction{ + 1, + []*externalapi.DomainTransactionInput{{externalapi.DomainOutpoint{ + *externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{0x01}), 0xFFFF}, + []byte{1, 2, 3}, + uint64(0xFFFFFFFF), + 1, + utxo.NewUTXOEntry(1, &externalapi.ScriptPublicKey{Script: []byte{0, 1, 2, 3}, Version: 0}, true, 2)}}, + []*externalapi.DomainTransactionOutput{{uint64(0xFFFF), + &externalapi.ScriptPublicKey{Script: []byte{1, 2}, Version: 0}}, + {uint64(0xFFFF), + &externalapi.ScriptPublicKey{Script: []byte{1, 3}, Version: 0}}}, + 1, + externalapi.DomainSubnetworkID{0x01}, + 1, + []byte{0x01}, + 0, + 1, + 1, // Changed 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, @@ -536,6 +579,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct { []byte{0x01}, 1, 1, + 0, 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, @@ -555,6 +599,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct { []byte{0x01}, 1, 1, + 0, nil, }, expectedResult: false, @@ -569,6 +614,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct { []byte{0x01}, 1, 1, + 0, nil, }, expectedResult: true, @@ -583,6 +629,7 @@ func initTestDomainTransactionForEqual() []testDomainTransactionStruct { []byte{0x01}, 2, // Changed fee 1, + 0, nil, }, expectsPanic: true,