Skip to content

Commit

Permalink
Fix nil pointer panics for decoding WorkObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Nov 21, 2024
1 parent 9e13bf7 commit f875e3b
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 13 deletions.
10 changes: 6 additions & 4 deletions core/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 9 additions & 7 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
217 changes: 215 additions & 2 deletions core/types/wo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
0xfd, 0x40, 0xd4, 0x93, 0x47, 0x1a, 0x22, 0x0a, 0x20, 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55,
0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0x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
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, 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, 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21, 0x32, 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, 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, 0x12,
0x00, 0x1a, 0x00, 0x22, 0x00, 0x2a, 0x00, 0x32, 0x00,
}
)

func TestWoHash(t *testing.T) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f875e3b

Please sign in to comment.