Skip to content

Commit

Permalink
New tests for WorkObject creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Nov 21, 2024
1 parent dabcd7a commit 0087009
Showing 1 changed file with 149 additions and 19 deletions.
168 changes: 149 additions & 19 deletions core/types/wo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func woTestData() (*WorkObject, common.Hash) {
}

var (
expectedWoHash = common.HexToHash("0x7a107825db4e54d262248b6d619679a94e8235a2ae01fd06b4e3819179b81d3a")
expectedUncleHash = common.HexToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
expectedProtoBytes = []byte{
expectedWoHash = common.HexToHash("0x7a107825db4e54d262248b6d619679a94e8235a2ae01fd06b4e3819179b81d3a")
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,
Expand Down Expand Up @@ -88,6 +88,62 @@ var (
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, 0x0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00, 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 @@ -214,30 +270,104 @@ func TestCalcUncleHash(t *testing.T) {
}

func TestProtoEncode(t *testing.T) {
// Test data
testWo, _ := woTestData()

protoTestWo, err := testWo.ProtoEncode(PEtxObject)
require.NoError(t, err)
// Define test cases
tests := []struct {
name string
objectType WorkObjectView
expectedBytes []byte
}{
{
name: "PEtxObject",
objectType: PEtxObject,
expectedBytes: expectedPETXProtoBytes,
},
{
name: "BlockObject",
objectType: BlockObject,
expectedBytes: expectedBlockProtoBytes,
},
}

protoTestWoBytes, err := proto.Marshal(protoTestWo)
require.NoError(t, err)
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)

require.Equal(t, expectedProtoBytes, protoTestWoBytes)
// Compare with expected bytes
require.Equal(t, tt.expectedBytes, protoTestWoBytes)
})
}
}

func TestProtoDecode(t *testing.T) {
_, testWoHash := woTestData()
protoWo := &ProtoWorkObject{}
err := proto.Unmarshal(expectedProtoBytes, protoWo)
require.NoError(t, err)

decodedPETX := &WorkObject{}
decodedPETX.ProtoDecode(protoWo, common.Location{0, 0}, PEtxObject)
require.Equal(t, decodedPETX.Hash(), testWoHash)

decodedDefault := &WorkObject{}
decodedDefault.ProtoDecode(protoWo, common.Location{0, 0}, BlockObject)
require.Equal(t, decodedDefault.Hash(), testWoHash)

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) {
Expand Down

0 comments on commit 0087009

Please sign in to comment.