From f875e3b4940b5a5a52a32c75d7c307b208a81d3f Mon Sep 17 00:00:00 2001 From: Hussam Date: Wed, 20 Nov 2024 17:21:39 -0600 Subject: [PATCH] Fix nil pointer panics for decoding WorkObjects --- core/types/block.go | 10 +- core/types/transaction.go | 16 +-- core/types/wo_test.go | 217 +++++++++++++++++++++++++++++++++++++- 3 files changed, 230 insertions(+), 13 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index cb4ffc43c5..70457e654e 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -1234,10 +1234,12 @@ func (m BlockManifest) ProtoEncode() (*ProtoManifest, error) { // ProtoDecode deserializes th ProtoManifest into the BlockManifest format func (m *BlockManifest) ProtoDecode(protoManifest *ProtoManifest) error { - for _, protoHash := range protoManifest.Manifest { - hash := &common.Hash{} - hash.ProtoDecode(protoHash) - *m = append(*m, *hash) + if protoManifest != nil { + for _, protoHash := range protoManifest.Manifest { + hash := &common.Hash{} + hash.ProtoDecode(protoHash) + *m = append(*m, *hash) + } } return nil } diff --git a/core/types/transaction.go b/core/types/transaction.go index a857889871..ed93ae672a 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -799,14 +799,16 @@ func (s Transactions) ProtoEncode() (*ProtoTransactions, error) { // ProtoDecode decodes the ProtoTransactions into the Transactions format func (s *Transactions) ProtoDecode(transactions *ProtoTransactions, location common.Location) error { - *s = make(Transactions, 0, len(transactions.Transactions)) - for _, protoTx := range transactions.Transactions { - tx := &Transaction{} - err := tx.ProtoDecode(protoTx, location) - if err != nil { - return err + if transactions != nil { + *s = make(Transactions, 0, len(transactions.Transactions)) + for _, protoTx := range transactions.Transactions { + tx := &Transaction{} + err := tx.ProtoDecode(protoTx, location) + if err != nil { + return err + } + *s = append(*s, tx) } - *s = append(*s, tx) } return nil } diff --git a/core/types/wo_test.go b/core/types/wo_test.go index 83c77bcb6f..31d8bd6349 100644 --- a/core/types/wo_test.go +++ b/core/types/wo_test.go @@ -7,6 +7,7 @@ import ( "github.com/dominant-strategies/go-quai/common" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" ) func woTestData() (*WorkObject, common.Hash) { @@ -30,8 +31,119 @@ func woTestData() (*WorkObject, common.Hash) { } var ( - expectedWoHash = common.HexToHash("0xd8e3ef0d1804c06495b219308535844169ccfdbd8565770077ea12f928fc8000") - expectedUncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") + expectedWoHash = common.HexToHash("0xd8e3ef0d1804c06495b219308535844169ccfdbd8565770077ea12f928fc8000") + expectedUncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347") + expectedPETXProtoBytes = []byte{ + 0x0a, 0xc0, 0x01, 0x0a, 0x22, 0x0a, 0x20, 0x97, 0xb8, 0xd8, 0x2d, 0x3f, 0x97, 0x82, 0x7d, 0x2f, + 0x95, 0x8f, 0x53, 0xa5, 0x31, 0x4a, 0x3c, 0x36, 0xe5, 0x1c, 0x57, 0xb9, 0xbb, 0x77, 0x08, 0x80, + 0xb2, 0x48, 0x79, 0x5d, 0x40, 0xa0, 0x1e, 0x12, 0x22, 0x0a, 0x20, 0x97, 0xb8, 0xd8, 0x2d, 0x3f, + 0x97, 0x82, 0x7d, 0x2f, 0x95, 0x8f, 0x53, 0xa5, 0x31, 0x4a, 0x3c, 0x36, 0xe5, 0x1c, 0x57, 0xb9, + 0xbb, 0x77, 0x08, 0x80, 0xb2, 0x48, 0x79, 0x5d, 0x40, 0xa0, 0x1e, 0x1a, 0x01, 0x01, 0x22, 0x04, + 0x07, 0x5b, 0xcd, 0x15, 0x2a, 0x22, 0x0a, 0x20, 0x00, 0x04, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf3, 0x30, 0x01, 0x3a, 0x04, 0x0a, 0x02, 0x00, 0x00, + 0x42, 0x22, 0x0a, 0x20, 0x00, 0x00, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, + 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, + 0x9a, 0xbc, 0xde, 0xf4, 0x48, 0x01, 0x52, 0x01, 0x2a, 0x58, 0x00, 0x62, 0x16, 0x0a, 0x14, 0x23, + 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, + 0x45, 0x67, 0x89, 0x12, 0x89, 0x05, 0x0a, 0x86, 0x05, 0x0a, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, + 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, + 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0x0a, 0x22, 0x0a, + 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, + 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, + 0x21, 0x12, 0x22, 0x0a, 0x20, 0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, + 0x67, 0xb6, 0xcc, 0xd4, 0x1a, 0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, + 0x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xb4, 0x21, 0x3a, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, + 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, + 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0x3a, 0x22, 0x0a, + 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, + 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, + 0x21, 0x42, 0x22, 0x0a, 0x20, 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, 0x52, 0x00, 0x52, 0x00, 0x52, 0x00, 0x5a, 0x00, 0x5a, 0x00, 0x5a, + 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x00, 0x6a, 0x00, 0x72, 0x00, 0x72, 0x00, 0x78, 0x00, 0x80, + 0x01, 0x00, 0x8a, 0x01, 0x00, 0x9a, 0x01, 0x00, 0xb2, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, + 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, + 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xba, 0x01, 0x22, + 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, + 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, + 0xb4, 0x21, 0xc0, 0x01, 0x00, 0xc8, 0x01, 0x00, 0xd0, 0x01, 0x00, 0xda, 0x01, 0x22, 0x0a, 0x20, + 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, + 0xe2, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, + 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, + 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xea, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, + 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, + 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xf0, 0x01, 0x00, 0xf8, 0x01, 0x00, + 0x82, 0x02, 0x00, 0x8a, 0x02, 0x00, 0x92, 0x02, 0x00, 0x9a, 0x02, 0x00, 0xa2, 0x02, 0x00, + } + expectedBlockProtoBytes = []byte{ + 0x0a, 0xc0, 0x01, 0x0a, 0x22, 0x0a, 0x20, 0x97, 0xb8, 0xd8, 0x2d, 0x3f, 0x97, 0x82, 0x7d, 0x2f, + 0x95, 0x8f, 0x53, 0xa5, 0x31, 0x4a, 0x3c, 0x36, 0xe5, 0x1c, 0x57, 0xb9, 0xbb, 0x77, 0x08, 0x80, + 0xb2, 0x48, 0x79, 0x5d, 0x40, 0xa0, 0x1e, 0x12, 0x22, 0x0a, 0x20, 0x97, 0xb8, 0xd8, 0x2d, 0x3f, + 0x97, 0x82, 0x7d, 0x2f, 0x95, 0x8f, 0x53, 0xa5, 0x31, 0x4a, 0x3c, 0x36, 0xe5, 0x1c, 0x57, 0xb9, + 0xbb, 0x77, 0x08, 0x80, 0xb2, 0x48, 0x79, 0x5d, 0x40, 0xa0, 0x1e, 0x1a, 0x01, 0x01, 0x22, 0x04, + 0x07, 0x5b, 0xcd, 0x15, 0x2a, 0x22, 0x0a, 0x20, 0x00, 0x04, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, + 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf3, 0x30, 0x01, 0x3a, 0x04, 0x0a, 0x02, 0x00, 0x00, + 0x42, 0x22, 0x0a, 0x20, 0x00, 0x00, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, + 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, + 0x9a, 0xbc, 0xde, 0xf4, 0x48, 0x01, 0x52, 0x01, 0x2a, 0x58, 0x00, 0x62, 0x16, 0x0a, 0x14, 0x23, + 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, + 0x45, 0x67, 0x89, 0x12, 0x93, 0x05, 0x0a, 0x86, 0x05,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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, 0x52, 0x00, 0x52, 0x00, 0x52, 0x00, 0x5a, 0x00, 0x5a, 0x00, 0x5a, + 0x00, 0x62, 0x00, 0x62, 0x00, 0x62, 0x00, 0x6a, 0x00, 0x72, 0x00, 0x72, 0x00, 0x78, 0x00, 0x80, + 0x01, 0x00, 0x8a, 0x01, 0x00, 0x9a, 0x01, 0x00, 0xb2, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, + 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, + 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xba, 0x01, 0x22, + 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, + 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, + 0xb4, 0x21, 0xc0, 0x01, 0x00, 0xc8, 0x01, 0x00, 0xd0, 0x01, 0x00, 0xda, 0x01, 0x22, 0x0a, 0x20, + 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, + 0xe2, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, + 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, + 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xea, 0x01, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, + 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, + 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, 0xf0, 0x01, 0x00, 0xf8, 0x01, 0x00, + 0x82, 0x02, 0x00, 0x8a, 0x02, 0x00, 0x92, 0x02, 0x00, 0x9a, 0x02, 0x00, 0xa2, 0x02, 0x00, 0x12, + 0x00, 0x1a, 0x00, 0x22, 0x00, 0x2a, 0x00, 0x32, 0x00, + } ) func TestWoHash(t *testing.T) { @@ -157,6 +269,107 @@ func TestCalcUncleHash(t *testing.T) { } } +func TestProtoEncode(t *testing.T) { + // Test data + testWo, _ := woTestData() + + // Define test cases + tests := []struct { + name string + objectType WorkObjectView + expectedBytes []byte + }{ + { + name: "PEtxObject", + objectType: PEtxObject, + expectedBytes: expectedPETXProtoBytes, + }, + { + name: "BlockObject", + objectType: BlockObject, + expectedBytes: expectedBlockProtoBytes, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // ProtoEncode the test WorkObject + protoTestWo, err := testWo.ProtoEncode(tt.objectType) + require.NoError(t, err) + + // Marshal to bytes + protoTestWoBytes, err := proto.Marshal(protoTestWo) + require.NoError(t, err) + + // Compare with expected bytes + require.Equal(t, tt.expectedBytes, protoTestWoBytes) + }) + } +} + +func TestProtoDecode(t *testing.T) { + _, testWoHash := woTestData() + + tests := []struct { + name string + objectType WorkObjectView + testBytes []byte + expectedHash common.Hash + }{ + { + "PETX", + PEtxObject, + expectedPETXProtoBytes, + testWoHash, + }, + { + "BlockObject", + BlockObject, + expectedBlockProtoBytes, + testWoHash, + }, + { + "WorkShareObject", + WorkShareObject, + expectedBlockProtoBytes, + testWoHash, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + protoWo := &ProtoWorkObject{} + err := proto.Unmarshal(tt.testBytes, protoWo) + require.NoError(t, err) + + decoded := &WorkObject{} + decoded.ProtoDecode(protoWo, common.Location{0, 0}, tt.objectType) + require.Equal(t, decoded.Hash(), tt.expectedHash) + }) + } +} + +func TestCopyWorkObject(t *testing.T) { + originalWo, expectedHash := woTestData() + + newWo := CopyWorkObject(originalWo) + + require.Equal(t, expectedHash, newWo.Hash(), "Copied work object is different from new work object") + + // Test to make sure the copy doesn't modify original. + newWo.WorkObjectHeader().SetLocation(common.Location{2, 2}) + require.NotEqual(t, expectedHash, newWo.Hash(), "WorkObject hash didn't change with a new location") + require.Equal(t, originalWo.Hash(), expectedHash, "Copied WorkObject changed values from the original") +} + +func TestNewWorkObject(t *testing.T) { + // Verify that copy is same as original. + originalWo, expectedHash := woTestData() + newWo := NewWorkObject(originalWo.WorkObjectHeader(), originalWo.Body(), originalWo.Tx()) + + require.Equal(t, expectedHash, newWo.Hash(), "NewWorkObject created a different WorkObject than the original") +} + func assertUncleHash(t *testing.T, uncleNum int, expectedUncleHash common.Hash, expectedWoHash common.Hash, shouldPass bool) { wo, _ := woTestData() wo.Body().uncles = make([]*WorkObjectHeader, uncleNum)