Skip to content

Commit

Permalink
add chunk hash unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Oct 16, 2024
1 parent 558d029 commit 24da905
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 0 deletions.
65 changes: 65 additions & 0 deletions encoding/codecv0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,68 @@ func TestCodecV0ChunkEncode(t *testing.T) {
encoded = hex.EncodeToString(encodedBytes)
assert.Equal(t, "01000000000000001100000000646b6ed0000000000000000000000000000000000000000000000000000000000000000000000000007a120001010101", encoded)
}

func TestCodecV0ChunkHash(t *testing.T) {
codecv0, err := CodecFromVersion(CodecV0)
assert.NoError(t, err)

// chunk with a single empty block
daBlock := &daBlockV0{}
chunk := &daChunkV0{blocks: []DABlock{daBlock}, transactions: [][]*types.TransactionData{nil}}
hash, err := chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x7cdb9d7f02ea58dfeb797ed6b4f7ea68846e4f2b0e30ed1535fc98b60c4ec809", hash.Hex())

// invalid hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0xg"})
_, err = chunk.Hash()
assert.Error(t, err)

block := readBlockFromJSON(t, "testdata/blockTrace_02.json")
originalChunk := &Chunk{Blocks: []*Block{block}}
daChunk, err := codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xde642c68122634b33fa1e6e4243b17be3bfd0dc6f996f204ef6d7522516bd840", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_03.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xde29f4371cc396b2e7c536cdc7a7c20ac5c728cbb8af3247074c746ff452632b", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_04.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x9e643c8a9203df542e39d9bfdcb07c99575b3c3d557791329fef9d83cc4147d0", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_05.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x854fc3136f47ce482ec85ee3325adfa16a1a1d60126e1c119eaaf0c3a9e90f8e", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_06.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x2aa220ca7bd1368e59e8053eb3831e30854aa2ec8bd3af65cee350c1c0718ba6", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_07.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv0.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xb65521bea7daff75838de07951c3c055966750fb5a270fead5e0e727c32455c3", hash.Hex())
}
86 changes: 86 additions & 0 deletions encoding/codecv1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,89 @@ func TestCodecV1ChunkEncode(t *testing.T) {
encoded = hex.EncodeToString(encodedBytes)
assert.Equal(t, "01000000000000001100000000646b6ed0000000000000000000000000000000000000000000000000000000000000000000000000007a120001010101", encoded)
}

func TestCodecV1ChunkHash(t *testing.T) {
codecv1, err := CodecFromVersion(CodecV1)
assert.NoError(t, err)

// chunk with a single empty block
daBlock := &daBlockV0{}
chunk := &daChunkV1{blocks: []DABlock{daBlock}, transactions: [][]*types.TransactionData{nil}}
hash, err := chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x7cdb9d7f02ea58dfeb797ed6b4f7ea68846e4f2b0e30ed1535fc98b60c4ec809", hash.Hex())

// L1 transactions are part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// L2 transactions are not part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.DynamicFeeTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// numL1Messages are not part of the hash
daBlock = chunk.blocks[0].(*daBlockV0)
daBlock.numL1Messages = 1
chunk.blocks[0] = daBlock

hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// invalid hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0xg"})
_, err = chunk.Hash()
assert.Error(t, err)

block := readBlockFromJSON(t, "testdata/blockTrace_02.json")
originalChunk := &Chunk{Blocks: []*Block{block}}
daChunk, err := codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x820f25d806ddea0ccdbfa463ee480da5b6ea3906e8a658417fb5417d0f837f5c", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_03.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x4620b3900e8454133448b677cbb2054c5dd61d467d7ebf752bfb12cffff90f40", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_04.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x059c6451e83012b405c7e1a38818369012a4a1c87d7d699366eac946d0410d73", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_05.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x854fc3136f47ce482ec85ee3325adfa16a1a1d60126e1c119eaaf0c3a9e90f8e", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_06.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x2aa220ca7bd1368e59e8053eb3831e30854aa2ec8bd3af65cee350c1c0718ba6", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_07.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv1.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xb65521bea7daff75838de07951c3c055966750fb5a270fead5e0e727c32455c3", hash.Hex())
}
86 changes: 86 additions & 0 deletions encoding/codecv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,89 @@ func TestCodecV2ChunkEncode(t *testing.T) {
encoded = hex.EncodeToString(encodedBytes)
assert.Equal(t, "01000000000000001100000000646b6ed0000000000000000000000000000000000000000000000000000000000000000000000000007a120001010101", encoded)
}

func TestCodecV2ChunkHash(t *testing.T) {
codecv2, err := CodecFromVersion(CodecV2)
assert.NoError(t, err)

// chunk with a single empty block
daBlock := &daBlockV0{}
chunk := &daChunkV1{blocks: []DABlock{daBlock}, transactions: [][]*types.TransactionData{nil}}
hash, err := chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x7cdb9d7f02ea58dfeb797ed6b4f7ea68846e4f2b0e30ed1535fc98b60c4ec809", hash.Hex())

// L1 transactions are part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// L2 transactions are not part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.DynamicFeeTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// numL1Messages are not part of the hash
daBlock = chunk.blocks[0].(*daBlockV0)
daBlock.numL1Messages = 1
chunk.blocks[0] = daBlock

hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// invalid hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0xg"})
_, err = chunk.Hash()
assert.Error(t, err)

block := readBlockFromJSON(t, "testdata/blockTrace_02.json")
originalChunk := &Chunk{Blocks: []*Block{block}}
daChunk, err := codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x820f25d806ddea0ccdbfa463ee480da5b6ea3906e8a658417fb5417d0f837f5c", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_03.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x4620b3900e8454133448b677cbb2054c5dd61d467d7ebf752bfb12cffff90f40", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_04.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x059c6451e83012b405c7e1a38818369012a4a1c87d7d699366eac946d0410d73", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_05.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x854fc3136f47ce482ec85ee3325adfa16a1a1d60126e1c119eaaf0c3a9e90f8e", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_06.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x2aa220ca7bd1368e59e8053eb3831e30854aa2ec8bd3af65cee350c1c0718ba6", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_07.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv2.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xb65521bea7daff75838de07951c3c055966750fb5a270fead5e0e727c32455c3", hash.Hex())
}
86 changes: 86 additions & 0 deletions encoding/codecv3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,89 @@ func TestCodecV3ChunkEncode(t *testing.T) {
encoded = hex.EncodeToString(encodedBytes)
assert.Equal(t, "01000000000000001100000000646b6ed0000000000000000000000000000000000000000000000000000000000000000000000000007a120001010101", encoded)
}

func TestCodecV3ChunkHash(t *testing.T) {
codecv3, err := CodecFromVersion(CodecV3)
assert.NoError(t, err)

// chunk with a single empty block
daBlock := &daBlockV0{}
chunk := &daChunkV1{blocks: []DABlock{daBlock}, transactions: [][]*types.TransactionData{nil}}
hash, err := chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x7cdb9d7f02ea58dfeb797ed6b4f7ea68846e4f2b0e30ed1535fc98b60c4ec809", hash.Hex())

// L1 transactions are part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// L2 transactions are not part of the hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.DynamicFeeTxType, TxHash: "0x0000000000000000000000000000000000000000000000000000000000000000"})
hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// numL1Messages are not part of the hash
daBlock = chunk.blocks[0].(*daBlockV0)
daBlock.numL1Messages = 1
chunk.blocks[0] = daBlock

hash, err = chunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xdcb42a70c54293e75a19dd1303d167822182d78b361dd7504758c35e516871b2", hash.Hex())

// invalid hash
chunk.transactions[0] = append(chunk.transactions[0], &types.TransactionData{Type: types.L1MessageTxType, TxHash: "0xg"})
_, err = chunk.Hash()
assert.Error(t, err)

block := readBlockFromJSON(t, "testdata/blockTrace_02.json")
originalChunk := &Chunk{Blocks: []*Block{block}}
daChunk, err := codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x820f25d806ddea0ccdbfa463ee480da5b6ea3906e8a658417fb5417d0f837f5c", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_03.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x4620b3900e8454133448b677cbb2054c5dd61d467d7ebf752bfb12cffff90f40", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_04.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x059c6451e83012b405c7e1a38818369012a4a1c87d7d699366eac946d0410d73", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_05.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x854fc3136f47ce482ec85ee3325adfa16a1a1d60126e1c119eaaf0c3a9e90f8e", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_06.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0x2aa220ca7bd1368e59e8053eb3831e30854aa2ec8bd3af65cee350c1c0718ba6", hash.Hex())

block = readBlockFromJSON(t, "testdata/blockTrace_07.json")
originalChunk = &Chunk{Blocks: []*Block{block}}
daChunk, err = codecv3.NewDAChunk(originalChunk, 0)
assert.NoError(t, err)
hash, err = daChunk.Hash()
assert.NoError(t, err)
assert.Equal(t, "0xb65521bea7daff75838de07951c3c055966750fb5a270fead5e0e727c32455c3", hash.Hex())
}
Loading

0 comments on commit 24da905

Please sign in to comment.