|
1 | 1 | package encoding |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/rand" |
4 | 5 | "encoding/hex" |
5 | 6 | "math" |
6 | 7 | "strings" |
7 | 8 | "testing" |
8 | 9 |
|
| 10 | + "github.com/agiledragon/gomonkey/v2" |
9 | 11 | "github.com/scroll-tech/go-ethereum/common" |
10 | 12 | "github.com/scroll-tech/go-ethereum/core/types" |
11 | 13 | "github.com/scroll-tech/go-ethereum/crypto" |
@@ -1038,3 +1040,110 @@ func TestDACodecV2SimpleMethods(t *testing.T) { |
1038 | 1040 | assert.Equal(t, CodecV2, version) |
1039 | 1041 | }) |
1040 | 1042 | } |
| 1043 | + |
| 1044 | +func TestCodecV2ChunkCompressedDataCompatibility(t *testing.T) { |
| 1045 | + codecv2, err := CodecFromVersion(CodecV2) |
| 1046 | + require.NoError(t, err) |
| 1047 | + |
| 1048 | + // chunk with a single empty block |
| 1049 | + emptyBlock := &Block{} |
| 1050 | + emptyChunk := &Chunk{Blocks: []*Block{emptyBlock}} |
| 1051 | + |
| 1052 | + compatible, err := codecv2.CheckChunkCompressedDataCompatibility(emptyChunk) |
| 1053 | + assert.NoError(t, err) |
| 1054 | + assert.True(t, compatible) |
| 1055 | + |
| 1056 | + txChunk := &Chunk{ |
| 1057 | + Blocks: []*Block{ |
| 1058 | + { |
| 1059 | + Transactions: []*types.TransactionData{ |
| 1060 | + {Type: types.L1MessageTxType}, |
| 1061 | + }, |
| 1062 | + }, |
| 1063 | + }, |
| 1064 | + } |
| 1065 | + compatible, err = codecv2.CheckChunkCompressedDataCompatibility(txChunk) |
| 1066 | + assert.NoError(t, err) |
| 1067 | + assert.True(t, compatible) |
| 1068 | + |
| 1069 | + testCases := []struct { |
| 1070 | + name string |
| 1071 | + jsonFile string |
| 1072 | + }{ |
| 1073 | + {"Block 02", "testdata/blockTrace_02.json"}, |
| 1074 | + {"Block 03", "testdata/blockTrace_03.json"}, |
| 1075 | + {"Block 04", "testdata/blockTrace_04.json"}, |
| 1076 | + {"Block 05", "testdata/blockTrace_05.json"}, |
| 1077 | + {"Block 06", "testdata/blockTrace_06.json"}, |
| 1078 | + {"Block 07", "testdata/blockTrace_07.json"}, |
| 1079 | + } |
| 1080 | + |
| 1081 | + for _, tc := range testCases { |
| 1082 | + t.Run(tc.name, func(t *testing.T) { |
| 1083 | + block := readBlockFromJSON(t, tc.jsonFile) |
| 1084 | + chunk := &Chunk{Blocks: []*Block{block}} |
| 1085 | + compatible, err := codecv2.CheckChunkCompressedDataCompatibility(chunk) |
| 1086 | + assert.NoError(t, err) |
| 1087 | + assert.True(t, compatible) |
| 1088 | + }) |
| 1089 | + } |
| 1090 | +} |
| 1091 | + |
| 1092 | +func TestCodecV2BatchCompressedDataCompatibility(t *testing.T) { |
| 1093 | + codecv2, err := CodecFromVersion(CodecV2) |
| 1094 | + require.NoError(t, err) |
| 1095 | + |
| 1096 | + // empty batch |
| 1097 | + emptyBatch := &Batch{} |
| 1098 | + compatible, err := codecv2.CheckBatchCompressedDataCompatibility(emptyBatch) |
| 1099 | + assert.NoError(t, err) |
| 1100 | + assert.True(t, compatible) |
| 1101 | + |
| 1102 | + testCases := []struct { |
| 1103 | + name string |
| 1104 | + jsonFiles []string |
| 1105 | + }{ |
| 1106 | + {"Single Block 02", []string{"testdata/blockTrace_02.json"}}, |
| 1107 | + {"Single Block 03", []string{"testdata/blockTrace_03.json"}}, |
| 1108 | + {"Single Block 04", []string{"testdata/blockTrace_04.json"}}, |
| 1109 | + {"Single Block 05", []string{"testdata/blockTrace_05.json"}}, |
| 1110 | + {"Single Block 06", []string{"testdata/blockTrace_06.json"}}, |
| 1111 | + {"Single Block 07", []string{"testdata/blockTrace_07.json"}}, |
| 1112 | + {"Multiple Blocks", []string{"testdata/blockTrace_02.json", "testdata/blockTrace_03.json", "testdata/blockTrace_04.json", "testdata/blockTrace_05.json", "testdata/blockTrace_06.json", "testdata/blockTrace_07.json"}}, |
| 1113 | + } |
| 1114 | + |
| 1115 | + for _, tc := range testCases { |
| 1116 | + t.Run(tc.name, func(t *testing.T) { |
| 1117 | + var chunks []*Chunk |
| 1118 | + for _, jsonFile := range tc.jsonFiles { |
| 1119 | + block := readBlockFromJSON(t, jsonFile) |
| 1120 | + chunks = append(chunks, &Chunk{Blocks: []*Block{block}}) |
| 1121 | + } |
| 1122 | + batch := &Batch{Chunks: chunks} |
| 1123 | + compatible, err := codecv2.CheckBatchCompressedDataCompatibility(batch) |
| 1124 | + assert.NoError(t, err) |
| 1125 | + assert.True(t, compatible) |
| 1126 | + }) |
| 1127 | + } |
| 1128 | +} |
| 1129 | + |
| 1130 | +func TestCodecV2CompressedDataFailedCompatibilityCheck(t *testing.T) { |
| 1131 | + codecv2, err := CodecFromVersion(CodecV2) |
| 1132 | + require.NoError(t, err) |
| 1133 | + |
| 1134 | + patches := gomonkey.ApplyFunc(constructBatchPayloadInBlob, func(_ []*Chunk, _ Codec) ([]byte, error) { |
| 1135 | + randomBytes := make([]byte, minCompressedDataCheckSize+1) |
| 1136 | + _, err := rand.Read(randomBytes) |
| 1137 | + require.NoError(t, err) |
| 1138 | + return []byte(hex.EncodeToString(randomBytes)), nil |
| 1139 | + }) |
| 1140 | + defer patches.Reset() |
| 1141 | + |
| 1142 | + compatible, err := codecv2.CheckChunkCompressedDataCompatibility(nil) |
| 1143 | + assert.NoError(t, err) |
| 1144 | + assert.False(t, compatible) |
| 1145 | + |
| 1146 | + compatible, err = codecv2.CheckBatchCompressedDataCompatibility(&Batch{}) |
| 1147 | + assert.NoError(t, err) |
| 1148 | + assert.False(t, compatible) |
| 1149 | +} |
0 commit comments