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()