From ea2f14ec22cb154f563aef79bdb507802e6cbfb4 Mon Sep 17 00:00:00 2001 From: Hussam Date: Fri, 13 Dec 2024 13:42:16 -0600 Subject: [PATCH 1/3] Fixed broken test, increased coverage on Tx parameters --- core/types/block.go | 3 +- core/types/transaction.go | 87 ++++++++++----- core/types/transaction_test.go | 133 ++++++++--------------- p2p/node/pubsubManager/gossipsub_test.go | 2 +- 4 files changed, 108 insertions(+), 117 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 2945f2c8b3..05e255465d 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -194,7 +194,8 @@ func EmptyWorkObject(nodeCtx int) *WorkObject { wo.woBody.SetTransactions([]*Transaction{}) wo.woBody.SetOutboundEtxs([]*Transaction{}) wo.woBody.SetManifest(BlockManifest{}) - return NewWorkObjectWithHeader(wo, NewEmptyTx(), nodeCtx, BlockObject) + tx := QuaiTxData() + return NewWorkObjectWithHeader(wo, tx, nodeCtx, BlockObject) } func EmptyZoneWorkObject() *WorkObject { diff --git a/core/types/transaction.go b/core/types/transaction.go index ed93ae672a..bc908819c5 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -82,25 +82,58 @@ func NewTx(inner TxData) *Transaction { return tx } -func NewEmptyTx() *Transaction { - to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0}) +func QuaiTxData() *Transaction { + to := common.HexToAddress("0x00bcdef0123456789abcdef0123456789abcdef2", common.Location{0, 0}) + address := common.HexToAddress("0x0056789abcdef0123456789abcdef0123456789a", common.Location{0, 0}) + parentHash := common.HexToHash("0x456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef3") + mixHash := common.HexToHash("0x56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef4") + workNonce := EncodeNonce(1) inner := &QuaiTx{ - ChainID: new(big.Int).SetUint64(1), - Nonce: 1, - MinerTip: new(big.Int).SetUint64(0), - GasPrice: new(big.Int).SetUint64(0), - Gas: uint64(0), - To: &to, - Value: new(big.Int).SetUint64(0), - Data: []byte{}, - AccessList: AccessList{}, - V: new(big.Int).SetUint64(0), - R: new(big.Int).SetUint64(0), - S: new(big.Int).SetUint64(0), + ChainID: big.NewInt(1), + Nonce: 1, + MinerTip: big.NewInt(1), + GasPrice: big.NewInt(1), + Gas: 1, + To: &to, + Value: big.NewInt(1), + Data: []byte{0x04}, + AccessList: AccessList{AccessTuple{ + Address: address, + StorageKeys: []common.Hash{common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1")}, + }, + }, + V: new(big.Int).SetUint64(1), + R: new(big.Int).SetUint64(1), + S: new(big.Int).SetUint64(1), + ParentHash: &parentHash, + MixHash: &mixHash, + WorkNonce: &workNonce, } return NewTx(inner) } +func NewEmptyQiTx() *Transaction { + to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0}) + in := TxIn{ + PreviousOutPoint: *NewOutPoint(&common.Hash{}, + MaxOutputIndex), + PubKey: []byte{0x04, 0x50, 0x49, 0x5c, 0xb2, 0xf9, 0x53, 0x5c, 0x68, 0x4e, 0xbe, 0x46, 0x87, 0xb5, 0x01, 0xc0, 0xd4, 0x1a, 0x62, 0x3d, 0x68, 0xc1, 0x18, 0xb8, 0xdc, 0xec, 0xd3, 0x93, 0x37, 0x0f, 0x1d, 0x90, 0xe6, 0x5c, 0x4c, 0x6c, 0x44, 0xcd, 0x3f, 0xe8, 0x09, 0xb4, 0x1d, 0xfa, 0xc9, 0x06, 0x0a, 0xd8, 0x4c, 0xb5, 0x7e, 0x2d, 0x57, 0x5f, 0xad, 0x24, 0xd2, 0x5a, 0x7e, 0xfa, 0x33, 0x96, 0xe7, 0x3c, 0x10}, + } + + newOut := TxOut{ + Denomination: uint8(1), + Address: to.Bytes(), + } + + utxo := &QiTx{ + ChainID: big.NewInt(1337), + TxIn: TxIns{in}, + TxOut: TxOuts{newOut}, + } + + return NewTx(utxo) +} + func (tx *Transaction) SetInner(inner TxData) { tx.setDecoded(inner, 0) } @@ -151,27 +184,27 @@ func (tx *Transaction) ProtoEncode() (*ProtoTransaction, error) { // Other fields are set conditionally depending on tx type. switch tx.Type() { case QuaiTxType: + if tx.To() != nil { + protoTx.To = tx.To().Bytes() + } nonce := tx.Nonce() - gas := tx.Gas() protoTx.Nonce = &nonce - protoTx.Gas = &gas - protoTx.AccessList = tx.AccessList().ProtoEncode() protoTx.Value = tx.Value().Bytes() + gas := tx.Gas() + protoTx.Gas = &gas if tx.Data() == nil { protoTx.Data = []byte{} } else { protoTx.Data = tx.Data() } - if tx.To() != nil { - protoTx.To = tx.To().Bytes() - } + protoTx.ChainId = tx.ChainId().Bytes() protoTx.MinerTip = tx.MinerTip().Bytes() protoTx.GasPrice = tx.GasPrice().Bytes() + protoTx.AccessList = tx.AccessList().ProtoEncode() V, R, S := tx.GetEcdsaSignatureValues() protoTx.V = V.Bytes() protoTx.R = R.Bytes() protoTx.S = S.Bytes() - protoTx.ChainId = tx.ChainId().Bytes() if tx.ParentHash() != nil { protoTx.ParentHash = tx.ParentHash().ProtoEncode() } @@ -235,7 +268,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo txType := protoTx.GetType() switch txType { - case 0: + case QuaiTxType: if protoTx.Nonce == nil { return errors.New("missing required field 'Nonce' in ProtoTransaction") } @@ -318,7 +351,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo } tx.SetInner(&quaiTx) - case 1: + case ExternalTxType: if protoTx.Gas == nil { return errors.New("missing required field 'Gas' in ProtoTransaction") } @@ -359,7 +392,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo tx.SetInner(&etx) - case 2: + case QiTxType: if protoTx.TxIns == nil { return errors.New("missing required field 'TxIns' in ProtoTransaction") } @@ -425,7 +458,7 @@ func (tx *Transaction) ProtoEncodeTxSigningData() *ProtoTransaction { return protoTxSigningData } switch tx.Type() { - case 0: + case QuaiTxType: txType := uint64(tx.Type()) protoTxSigningData.Type = &txType protoTxSigningData.ChainId = tx.ChainId().Bytes() @@ -445,9 +478,9 @@ func (tx *Transaction) ProtoEncodeTxSigningData() *ProtoTransaction { } protoTxSigningData.MinerTip = tx.MinerTip().Bytes() protoTxSigningData.GasPrice = tx.GasPrice().Bytes() - case 1: + case ExternalTxType: return protoTxSigningData - case 2: + case QiTxType: txType := uint64(tx.Type()) protoTxSigningData.Type = &txType protoTxSigningData.ChainId = tx.ChainId().Bytes() diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 546d106af0..51cad01a82 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -14,24 +14,9 @@ import ( ) func TestTransactionProtoEncodeDecode(t *testing.T) { - t.Skip("Todo: Fix failing test") // Create a new transaction - to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0}) - inner := &QuaiTx{ - ChainID: new(big.Int).SetUint64(1), - Nonce: uint64(0), - MinerTip: new(big.Int).SetUint64(0), - GasPrice: new(big.Int).SetUint64(0), - Gas: uint64(0), - To: &to, - Value: new(big.Int).SetUint64(0), - Data: []byte{0x04}, - AccessList: AccessList{}, - V: new(big.Int).SetUint64(0), - R: new(big.Int).SetUint64(0), - S: new(big.Int).SetUint64(0), - } - tx := NewTx(inner) + tx := QuaiTxData() + originalTxHash := tx.Hash() // Encode the transaction to ProtoTransaction format protoTx, err := tx.ProtoEncode() @@ -56,33 +41,17 @@ func TestTransactionProtoEncodeDecode(t *testing.T) { t.Log("secondProtoTx", secondProtoTx) // Compare the original transaction and the decoded transaction - if !reflect.DeepEqual(tx, decodedTx) { - t.Errorf("Decoded transaction does not match the original transaction") - } require.Equal(t, protoTx, secondProtoTx) + + // Compare the original transaction hash and the decoded transaction hash + decodedTxHash := decodedTx.Hash() + + require.Equal(t, originalTxHash, decodedTxHash) } func TestUTXOTransactionEncode(t *testing.T) { // Create a new transaction - to := common.BytesToAddress([]byte{0x01}, common.Location{0, 0}) - in := TxIn{ - PreviousOutPoint: *NewOutPoint(&common.Hash{}, - MaxOutputIndex), - PubKey: []byte{0x04, 0x50, 0x49, 0x5c, 0xb2, 0xf9, 0x53, 0x5c, 0x68, 0x4e, 0xbe, 0x46, 0x87, 0xb5, 0x01, 0xc0, 0xd4, 0x1a, 0x62, 0x3d, 0x68, 0xc1, 0x18, 0xb8, 0xdc, 0xec, 0xd3, 0x93, 0x37, 0x0f, 0x1d, 0x90, 0xe6, 0x5c, 0x4c, 0x6c, 0x44, 0xcd, 0x3f, 0xe8, 0x09, 0xb4, 0x1d, 0xfa, 0xc9, 0x06, 0x0a, 0xd8, 0x4c, 0xb5, 0x7e, 0x2d, 0x57, 0x5f, 0xad, 0x24, 0xd2, 0x5a, 0x7e, 0xfa, 0x33, 0x96, 0xe7, 0x3c, 0x10}, - } - - newOut := TxOut{ - Denomination: uint8(1), - Address: to.Bytes(), - } - - utxo := &QiTx{ - ChainID: big.NewInt(1337), - TxIn: TxIns{in}, - TxOut: TxOuts{newOut}, - } - - tx := NewTx(utxo) + tx := NewEmptyQiTx() // Encode the transaction to ProtoTransaction format protoTx, err := tx.ProtoEncode() @@ -110,46 +79,17 @@ func TestUTXOTransactionEncode(t *testing.T) { } // Quai Transaction tests -func quaiTxData() (*Transaction, common.Hash) { - to := common.HexToAddress("0x00bcdef0123456789abcdef0123456789abcdef2", common.Location{0, 0}) - address := common.HexToAddress("0x3456789abcdef0123456789abcdef0123456789a", common.Location{0, 0}) - parentHash := common.HexToHash("0x456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef3") - mixHash := common.HexToHash("0x56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef4") - workNonce := EncodeNonce(1) - inner := &QuaiTx{ - ChainID: new(big.Int).SetUint64(1), - Nonce: uint64(1), - MinerTip: new(big.Int).SetUint64(1), - GasPrice: new(big.Int).SetUint64(1), - Gas: uint64(1), - To: &to, - Value: new(big.Int).SetUint64(1), - Data: []byte{0x04}, - AccessList: AccessList{AccessTuple{ - Address: address, - StorageKeys: []common.Hash{common.HexToHash("0x23456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef1")}, - }, - }, - V: new(big.Int).SetUint64(1), - R: new(big.Int).SetUint64(1), - S: new(big.Int).SetUint64(1), - ParentHash: &parentHash, - MixHash: &mixHash, - WorkNonce: &workNonce, - } - tx := NewTx(inner) - return tx, tx.Hash() -} - func TestQuaiTxHash(t *testing.T) { - _, hash := quaiTxData() - correctHash := common.HexToHash("0x3a203a4f1589fe3a57a68482c048fb28c571b761a42c4cde81767e20a3d0416d") - require.Equal(t, hash, correctHash, "Hash not equal to expected hash") + tx := QuaiTxData() + hash := tx.Hash() + correctHash := common.HexToHash("0x6d626d68a816062adfaa789a16fd061d93ea7cda7e8b4db2acf9ca088e963ed5") + require.Equal(t, correctHash, hash, "Hash not equal to expected hash") } func fuzzQuaiTxHashingField(f *testing.F, getField func(TxData) *common.Hash, setField func(*QuaiTx, *common.Hash)) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testByte) // Verify the hash of the transaction if hash == (common.Hash{}) { @@ -173,7 +113,8 @@ func fuzzQuaiTxHashingField(f *testing.F, getField func(TxData) *common.Hash, se func FuzzQuaiTxHashingChainID(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.chainID().Uint64()) // Verify the hash of the transaction @@ -196,7 +137,8 @@ func FuzzQuaiTxHashingChainID(f *testing.F) { } func FuzzQuaiTxHashingV(f *testing.F) { - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) v, _, _ := tx.inner.getEcdsaSignatureValues() f.Add(v.Uint64()) @@ -219,7 +161,8 @@ func FuzzQuaiTxHashingV(f *testing.F) { } func FuzzQuaiTxHashingR(f *testing.F) { - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) _, r, _ := tx.inner.getEcdsaSignatureValues() f.Add(r.Uint64()) @@ -242,7 +185,8 @@ func FuzzQuaiTxHashingR(f *testing.F) { } func FuzzQuaiTxHashingS(f *testing.F) { - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) _, _, s := tx.inner.getEcdsaSignatureValues() f.Add(s.Uint64()) @@ -266,7 +210,8 @@ func FuzzQuaiTxHashingS(f *testing.F) { func FuzzQuaiTxHashingNonce(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.nonce()) // Verify the hash of the transaction @@ -289,7 +234,8 @@ func FuzzQuaiTxHashingNonce(f *testing.F) { func FuzzQuaiTxHashingMinerTip(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.minerTip().Uint64()) // Verify the hash of the transaction @@ -313,7 +259,8 @@ func FuzzQuaiTxHashingMinerTip(f *testing.F) { func FuzzQuaiTxHashingGasPrice(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.gasPrice().Uint64()) // Verify the hash of the transaction @@ -337,7 +284,8 @@ func FuzzQuaiTxHashingGasPrice(f *testing.F) { func FuzzQuaiTxHashingGas(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.gas()) // Verify the hash of the transaction @@ -360,7 +308,8 @@ func FuzzQuaiTxHashingGas(f *testing.F) { func FuzzQuaiTxHashingTo(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.to().Bytes()) @@ -392,7 +341,8 @@ func FuzzQuaiTxHashingTo(f *testing.F) { func FuzzQuaiTxHashingValue(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.value().Uint64()) // Verify the hash of the transaction @@ -415,7 +365,8 @@ func FuzzQuaiTxHashingValue(f *testing.F) { func FuzzQuaiTxHashingData(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testByte) // Verify the hash of the transaction if hash == (common.Hash{}) { @@ -437,7 +388,8 @@ func FuzzQuaiTxHashingData(f *testing.F) { } func FuzzQuaiTxHashingAccessList(f *testing.F) { - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testByte) if hash == (common.Hash{}) { f.Errorf("Transaction hash is empty") @@ -478,7 +430,8 @@ func FuzzQuaiTxMixHash(f *testing.F) { func FuzzQuaiTxHashingWorkNonce(f *testing.F) { // Create a new transaction - tx, hash := quaiTxData() + tx := QuaiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.workNonce().Uint64()) // Verify the hash of the transaction @@ -1082,7 +1035,7 @@ func TestTxNilDecode(t *testing.T) { func TestNilChainIDDecode(t *testing.T) { // get an empty quai tx - quaiTx, _ := quaiTxData() + quaiTx := QuaiTxData() protoQuaiTx, err := quaiTx.ProtoEncode() require.Equal(t, err, nil) @@ -1098,3 +1051,7 @@ func TestNilChainIDDecode(t *testing.T) { require.NotEqual(t, err, nil) } + +func TestFromChain(t *testing.T) { + +} diff --git a/p2p/node/pubsubManager/gossipsub_test.go b/p2p/node/pubsubManager/gossipsub_test.go index e72e4403c2..39f772afd0 100644 --- a/p2p/node/pubsubManager/gossipsub_test.go +++ b/p2p/node/pubsubManager/gossipsub_test.go @@ -158,7 +158,7 @@ func TestMultipleRequests(t *testing.T) { wo := types.EmptyZoneWorkObject() - tx := types.NewEmptyTx() + tx := types.QuaiTxData() txs := types.Transactions{tx} headerView := wo.ConvertToHeaderView() From 8f2f12a11e157ce4ae692d5a27155a03ee3fb207 Mon Sep 17 00:00:00 2001 From: Hussam Date: Fri, 13 Dec 2024 14:44:47 -0600 Subject: [PATCH 2/3] Fix failing Cobra/Viper tests --- cmd/utils/utils.go | 2 ++ cmd/utils/utils_test.go | 15 +++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cmd/utils/utils.go b/cmd/utils/utils.go index d3e95bb9e8..f6622e9fac 100644 --- a/cmd/utils/utils.go +++ b/cmd/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "errors" "io/fs" + "strings" "github.com/spf13/viper" @@ -40,4 +41,5 @@ func InitConfig() { log.Global.Infof("Loading config from environment variables with prefix: '%s_'", constants.ENV_PREFIX) viper.SetEnvPrefix(constants.ENV_PREFIX) viper.AutomaticEnv() + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) // Replace hyphens with underscores for env variables } diff --git a/cmd/utils/utils_test.go b/cmd/utils/utils_test.go index d2bfda9e7d..29c2d84bc3 100644 --- a/cmd/utils/utils_test.go +++ b/cmd/utils/utils_test.go @@ -2,6 +2,7 @@ package utils import ( "os" + "strings" "testing" "github.com/dominant-strategies/go-quai/common/constants" @@ -21,8 +22,8 @@ func testXDGConfigLoading(t *testing.T) { defer tempFile.Close() defer os.RemoveAll(mockConfigPath) - // write 'LOG_LEVEL=debug' config to mock config.yaml file - _, err := tempFile.WriteString(LogLevelFlag.Name + " : " + "debug\n") + // write 'log-level = debug' config to mock config.yaml file + _, err := tempFile.WriteString(LogLevelFlag.Name + " = " + "\"debug\"\n") require.NoError(t, err) // Set config path to the temporary config directory @@ -38,19 +39,21 @@ func testXDGConfigLoading(t *testing.T) { // the loading of the environment variable and the loading of the cobra flag. // It verifies the expected order of precedence of config loading. func TestCobraFlagConfigLoading(t *testing.T) { - t.Skip("Todo: fix failing test") // Clear viper instance to simulate a fresh start viper.Reset() + viper.AutomaticEnv() + viper.SetEnvPrefix(constants.ENV_PREFIX) + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) // Replace hyphens with underscores // Test loading config from XDG config home testXDGConfigLoading(t) assert.Equal(t, "debug", viper.GetString(LogLevelFlag.Name)) // Test loading config from environment variable - err := os.Setenv(constants.ENV_PREFIX+"_"+"LOG-LEVEL", "error") - defer os.Unsetenv(constants.ENV_PREFIX + "_" + "LOG-LEVEL") + err := os.Setenv(constants.ENV_PREFIX+"_"+"LOG_LEVEL", "error") + defer os.Unsetenv(constants.ENV_PREFIX + "_" + "LOG_LEVEL") require.NoError(t, err) - assert.Equal(t, "error", viper.GetString(LogLevelFlag.Name)) + assert.Equal(t, "error", viper.GetString("LOG_LEVEL")) // Test loading config from cobra flag rootCmd := &cobra.Command{} From b53b0bdf802f6f61b5953b897d97554909f86fb7 Mon Sep 17 00:00:00 2001 From: Hussam Date: Fri, 13 Dec 2024 19:08:01 -0600 Subject: [PATCH 3/3] Added isLocal and location decoded tests --- core/types/transaction_test.go | 141 +++++++++++++++++++++++++++------ core/types/wo_test.go | 39 ++++----- 2 files changed, 135 insertions(+), 45 deletions(-) diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 51cad01a82..aea0b3c964 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -460,7 +460,7 @@ func TestQiAddressScope(t *testing.T) { } // ETX hash tests -func etxData() (*Transaction, common.Hash) { +func etxData() *Transaction { to := common.HexToAddress("0x00bcdef0123456789abcdef0123456789abcdef2", common.Location{0, 0}) address := common.HexToAddress("0x3456789abcdef0123456789abcdef0123456789a", common.Location{0, 0}) sender := common.HexToAddress("0x89abcdef0123456789abcdef0123456789abcdef0123456789abcdef7", common.Location{0, 0}) @@ -482,18 +482,20 @@ func etxData() (*Transaction, common.Hash) { EtxType: 0, } tx := NewTx(inner) - return tx, tx.Hash() + return tx } func TestEtxHash(t *testing.T) { - _, hash := etxData() + tx := etxData() + hash := tx.Hash() correctHash := common.HexToHash("0x569200efce076a61575a3661dcb6f59e77e0407279c8db136ef9b2fa23d361ce") require.Equal(t, hash, correctHash, "Hash not equal to expected hash") } func FuzzEtxOriginatingTxHash(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testByte) // Verify the hash of the transaction if hash == (common.Hash{}) { @@ -518,7 +520,8 @@ func FuzzEtxOriginatingTxHash(f *testing.F) { func FuzzEtxType(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testUInt16) f.Add(tx.inner.etxIndex()) // Verify the hash of the transaction @@ -541,7 +544,8 @@ func FuzzEtxType(f *testing.F) { func FuzzEtxIndex(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testUInt16) f.Add(tx.inner.etxIndex()) // Verify the hash of the transaction @@ -564,7 +568,8 @@ func FuzzEtxIndex(f *testing.F) { func FuzzEtxGas(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.gas()) // Verify the hash of the transaction @@ -587,7 +592,8 @@ func FuzzEtxGas(f *testing.F) { func FuzzEtxTo(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.to().Bytes()) @@ -622,7 +628,8 @@ func FuzzEtxTo(f *testing.F) { func FuzzEtxValue(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.value().Uint64()) // Verify the hash of the transaction @@ -646,7 +653,8 @@ func FuzzEtxValue(f *testing.F) { func FuzzEtxData(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.data()) // Verify the hash of the transaction @@ -669,7 +677,8 @@ func FuzzEtxData(f *testing.F) { } func FuzzEtxAccessList(f *testing.F) { - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.(*ExternalTx).accessList()[0].Address.Bytes()) if hash == (common.Hash{}) { @@ -699,7 +708,8 @@ func FuzzEtxAccessList(f *testing.F) { func FuzzEtxSender(f *testing.F) { // Create a new transaction - tx, hash := etxData() + tx := etxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.etxSender().Bytes()) @@ -722,7 +732,7 @@ func FuzzEtxSender(f *testing.F) { } // QiTx hash tests -func qiTxData() (*Transaction, common.Hash) { +func qiTxData() *Transaction { parentHash := common.HexToHash("0x456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef3") mixHash := common.HexToHash("0x56789abcdef0123456789abcdef0123456789abcdef0123456789abcdef4") txHash := common.HexToHash("0x3a203a4f1589fe3a57a68482c048fb28c571b761a42c4cde81767e20a3d0416d") @@ -738,18 +748,20 @@ func qiTxData() (*Transaction, common.Hash) { WorkNonce: &workNonce, } tx := NewTx(inner) - return tx, tx.Hash() + return tx } func TestQiTxHash(t *testing.T) { - _, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() correctHash := common.HexToHash("0x3ab73ae4860b3db006d7a19fed6be3efe5619f53f499ef561f42c46bc12b555d") require.Equal(t, correctHash, hash, "Hash not equal to expected hash") } func FuzzQiTxHashingChainID(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.chainID().Uint64()) // Verify the hash of the transaction @@ -773,7 +785,8 @@ func FuzzQiTxHashingChainID(f *testing.F) { func FuzzQiTxHashingTxInOutPointTxHash(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.TxIn()[0].PreviousOutPoint.TxHash.Bytes()) // Verify the hash of the transaction @@ -798,7 +811,8 @@ func FuzzQiTxHashingTxInOutPointTxHash(f *testing.F) { func FuzzQiTxHashingTxInOutPointIndex(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testUInt16) f.Add(tx.TxIn()[0].PreviousOutPoint.Index) // Verify the hash of the transaction @@ -822,7 +836,8 @@ func FuzzQiTxHashingTxInOutPointIndex(f *testing.F) { func FuzzQiTxHashingTxInOutPointPubKey(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.TxIn()[0].PubKey) // Verify the hash of the transaction @@ -844,7 +859,8 @@ func FuzzQiTxHashingTxInOutPointPubKey(f *testing.F) { func FuzzQiTxHashingTxOutDenomination(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testUInt8) f.Add(tx.TxOut()[0].Denomination) // Verify the hash of the transaction @@ -867,7 +883,8 @@ func FuzzQiTxHashingTxOutDenomination(f *testing.F) { func FuzzQiTxHashingTxOutAddress(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.inner.txOut()[0].Address) @@ -891,7 +908,8 @@ func FuzzQiTxHashingTxOutAddress(f *testing.F) { func FuzzQiTxHashingTxOutLock(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.txOut()[0].Lock.Uint64()) // Verify the hash of the transaction @@ -918,7 +936,8 @@ func FuzzQiTxHashingTxOutLock(f *testing.F) { func FuzzQiTxHashingSignature(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testByte) f.Add(tx.GetSchnorrSignature().Serialize()) @@ -944,7 +963,8 @@ func FuzzQiTxHashingSignature(f *testing.F) { func fuzzQitxHashingField(f *testing.F, getField func(TxData) *common.Hash, setField func(*QiTx, *common.Hash)) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testByte) // Verify the hash of the transaction if hash == (common.Hash{}) { @@ -980,7 +1000,8 @@ func FuzzQitxMixHash(f *testing.F) { func FuzzQiTxHashingWorkNonce(f *testing.F) { // Create a new transaction - tx, hash := qiTxData() + tx := qiTxData() + hash := tx.Hash() f.Add(testUInt64) f.Add(tx.inner.workNonce().Uint64()) // Verify the hash of the transaction @@ -1052,6 +1073,74 @@ func TestNilChainIDDecode(t *testing.T) { require.NotEqual(t, err, nil) } -func TestFromChain(t *testing.T) { +func TestLocal(t *testing.T) { + tests := []struct { + name string + tx *Transaction + }{ + { + "QuaiTx", + QuaiTxData(), + }, + { + "QiTx", + qiTxData(), + }, + { + "ExternalTx", + etxData(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.tx.SetLocal(true) + require.True(t, tt.tx.IsLocal()) + + tt.tx.SetLocal(false) + require.False(t, tt.tx.IsLocal()) + }) + } +} + +func TestTransactionHash(t *testing.T) { + tests := []struct { + name string + tx func() *Transaction + shouldEqual bool + }{ + { + "QuaiTx", + QuaiTxData, + // Should not equal because our VRS are made up and don't map to any zone + // This means that we are actually using the location variable + false, + }, + { + "QiTx", + qiTxData, + true, + }, + { + "ExternalTx", + etxData, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + for _, loc := range locations { + // Generate two different transactions to avoid caching + originalHash := tt.tx().Hash() + locProvidedHash := tt.tx().Hash(loc...) + + if tt.shouldEqual { + require.Equal(t, originalHash, locProvidedHash) + } else { + require.NotEqual(t, originalHash, locProvidedHash) + } + } + }) + } } diff --git a/core/types/wo_test.go b/core/types/wo_test.go index 31d8bd6349..181c3a0bd4 100644 --- a/core/types/wo_test.go +++ b/core/types/wo_test.go @@ -10,6 +10,25 @@ import ( "google.golang.org/protobuf/proto" ) +var locations = []common.Location{ + {0, 0}, + {0, 1}, + {0, 2}, + {0, 3}, + {1, 0}, + {1, 1}, + {1, 2}, + {1, 3}, + {2, 0}, + {2, 1}, + {2, 2}, + {2, 3}, + {3, 0}, + {3, 1}, + {3, 2}, + {3, 3}, +} + func woTestData() (*WorkObject, common.Hash) { wo := &WorkObject{} wo.SetWorkObjectHeader(&WorkObjectHeader{}) @@ -197,25 +216,7 @@ func FuzzMixHash(f *testing.F) { func TestLocationHash(t *testing.T) { wo, hash := woTestData() - locations := []common.Location{ - {0, 1}, - {0, 2}, - {0, 3}, - {1, 0}, - {1, 1}, - {1, 2}, - {1, 3}, - {2, 0}, - {2, 1}, - {2, 2}, - {2, 3}, - {3, 0}, - {3, 1}, - {3, 2}, - {3, 3}, - } - - for _, loc := range locations { + for _, loc := range locations[1:] { woCopy := *wo woCopy.woHeader.location = loc require.NotEqual(t, woCopy.Hash(), hash, "Hash equal for location \noriginal: %v, modified: %v", wo.woHeader.location, loc)