Skip to content

Commit e18a147

Browse files
author
colinlyguo
committed
add codecv2 & codecv3 CompressedDataCompatibilityCheck unit tests
1 parent 4b5b636 commit e18a147

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

encoding/codecv2_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package encoding
22

33
import (
4+
"crypto/rand"
45
"encoding/hex"
56
"math"
67
"strings"
78
"testing"
89

10+
"github.com/agiledragon/gomonkey/v2"
911
"github.com/scroll-tech/go-ethereum/common"
1012
"github.com/scroll-tech/go-ethereum/core/types"
1113
"github.com/scroll-tech/go-ethereum/crypto"
@@ -1038,3 +1040,110 @@ func TestDACodecV2SimpleMethods(t *testing.T) {
10381040
assert.Equal(t, CodecV2, version)
10391041
})
10401042
}
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+
}

encoding/codecv3_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package encoding
22

33
import (
4+
"crypto/rand"
45
"encoding/hex"
56
"encoding/json"
67
"math"
78
"strings"
89
"testing"
910

11+
"github.com/agiledragon/gomonkey/v2"
1012
"github.com/scroll-tech/go-ethereum/common"
1113
"github.com/scroll-tech/go-ethereum/core/types"
1214
"github.com/scroll-tech/go-ethereum/crypto"
@@ -1182,3 +1184,110 @@ func TestDACodecV3SimpleMethods(t *testing.T) {
11821184
assert.Equal(t, CodecV3, version)
11831185
})
11841186
}
1187+
1188+
func TestCodecV3ChunkCompressedDataCompatibility(t *testing.T) {
1189+
codecv3, err := CodecFromVersion(CodecV3)
1190+
require.NoError(t, err)
1191+
1192+
// chunk with a single empty block
1193+
emptyBlock := &Block{}
1194+
emptyChunk := &Chunk{Blocks: []*Block{emptyBlock}}
1195+
1196+
compatible, err := codecv3.CheckChunkCompressedDataCompatibility(emptyChunk)
1197+
assert.NoError(t, err)
1198+
assert.True(t, compatible)
1199+
1200+
txChunk := &Chunk{
1201+
Blocks: []*Block{
1202+
{
1203+
Transactions: []*types.TransactionData{
1204+
{Type: types.L1MessageTxType},
1205+
},
1206+
},
1207+
},
1208+
}
1209+
compatible, err = codecv3.CheckChunkCompressedDataCompatibility(txChunk)
1210+
assert.NoError(t, err)
1211+
assert.True(t, compatible)
1212+
1213+
testCases := []struct {
1214+
name string
1215+
jsonFile string
1216+
}{
1217+
{"Block 02", "testdata/blockTrace_02.json"},
1218+
{"Block 03", "testdata/blockTrace_03.json"},
1219+
{"Block 04", "testdata/blockTrace_04.json"},
1220+
{"Block 05", "testdata/blockTrace_05.json"},
1221+
{"Block 06", "testdata/blockTrace_06.json"},
1222+
{"Block 07", "testdata/blockTrace_07.json"},
1223+
}
1224+
1225+
for _, tc := range testCases {
1226+
t.Run(tc.name, func(t *testing.T) {
1227+
block := readBlockFromJSON(t, tc.jsonFile)
1228+
chunk := &Chunk{Blocks: []*Block{block}}
1229+
compatible, err := codecv3.CheckChunkCompressedDataCompatibility(chunk)
1230+
assert.NoError(t, err)
1231+
assert.True(t, compatible)
1232+
})
1233+
}
1234+
}
1235+
1236+
func TestCodecV3BatchCompressedDataCompatibility(t *testing.T) {
1237+
codecv3, err := CodecFromVersion(CodecV3)
1238+
require.NoError(t, err)
1239+
1240+
// empty batch
1241+
emptyBatch := &Batch{}
1242+
compatible, err := codecv3.CheckBatchCompressedDataCompatibility(emptyBatch)
1243+
assert.NoError(t, err)
1244+
assert.True(t, compatible)
1245+
1246+
testCases := []struct {
1247+
name string
1248+
jsonFiles []string
1249+
}{
1250+
{"Single Block 02", []string{"testdata/blockTrace_02.json"}},
1251+
{"Single Block 03", []string{"testdata/blockTrace_03.json"}},
1252+
{"Single Block 04", []string{"testdata/blockTrace_04.json"}},
1253+
{"Single Block 05", []string{"testdata/blockTrace_05.json"}},
1254+
{"Single Block 06", []string{"testdata/blockTrace_06.json"}},
1255+
{"Single Block 07", []string{"testdata/blockTrace_07.json"}},
1256+
{"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"}},
1257+
}
1258+
1259+
for _, tc := range testCases {
1260+
t.Run(tc.name, func(t *testing.T) {
1261+
var chunks []*Chunk
1262+
for _, jsonFile := range tc.jsonFiles {
1263+
block := readBlockFromJSON(t, jsonFile)
1264+
chunks = append(chunks, &Chunk{Blocks: []*Block{block}})
1265+
}
1266+
batch := &Batch{Chunks: chunks}
1267+
compatible, err := codecv3.CheckBatchCompressedDataCompatibility(batch)
1268+
assert.NoError(t, err)
1269+
assert.True(t, compatible)
1270+
})
1271+
}
1272+
}
1273+
1274+
func TestCodecV3CompressedDataFailedCompatibilityCheck(t *testing.T) {
1275+
codecv3, err := CodecFromVersion(CodecV3)
1276+
require.NoError(t, err)
1277+
1278+
patches := gomonkey.ApplyFunc(constructBatchPayloadInBlob, func(_ []*Chunk, _ Codec) ([]byte, error) {
1279+
randomBytes := make([]byte, minCompressedDataCheckSize+1)
1280+
_, err := rand.Read(randomBytes)
1281+
require.NoError(t, err)
1282+
return []byte(hex.EncodeToString(randomBytes)), nil
1283+
})
1284+
defer patches.Reset()
1285+
1286+
compatible, err := codecv3.CheckChunkCompressedDataCompatibility(nil)
1287+
assert.NoError(t, err)
1288+
assert.False(t, compatible)
1289+
1290+
compatible, err = codecv3.CheckBatchCompressedDataCompatibility(&Batch{})
1291+
assert.NoError(t, err)
1292+
assert.False(t, compatible)
1293+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/scroll-tech/da-codec
33
go 1.21
44

55
require (
6+
github.com/agiledragon/gomonkey/v2 v2.12.0
67
github.com/scroll-tech/go-ethereum v1.10.14-0.20241010064814-3d88e870ae22
78
github.com/stretchr/testify v1.9.0
89
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t
33
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
44
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
55
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
6+
github.com/agiledragon/gomonkey/v2 v2.12.0 h1:ek0dYu9K1rSV+TgkW5LvNNPRWyDZVIxGMCFI6Pz9o38=
7+
github.com/agiledragon/gomonkey/v2 v2.12.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
68
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
79
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
810
github.com/bits-and-blooms/bitset v1.12.0 h1:U/q1fAF7xXRhFCrhROzIfffYnu+dlS38vCZtmFVPHmA=
@@ -63,13 +65,15 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
6365
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
6466
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
6567
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
68+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
6669
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
6770
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
6871
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
6972
github.com/iden3/go-iden3-crypto v0.0.15 h1:4MJYlrot1l31Fzlo2sF56u7EVFeHHJkxGXXZCtESgK4=
7073
github.com/iden3/go-iden3-crypto v0.0.15/go.mod h1:dLpM4vEPJ3nDHzhWFXDjzkn1qHoBeOT/3UEhXsEsP3E=
7174
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
7275
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
76+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
7377
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
7478
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
7579
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
@@ -115,6 +119,8 @@ github.com/scroll-tech/zktrie v0.8.4 h1:UagmnZ4Z3ITCk+aUq9NQZJNAwnWl4gSxsLb2Nl7I
115119
github.com/scroll-tech/zktrie v0.8.4/go.mod h1:XvNo7vAk8yxNyTjBDj5WIiFzYW4bx/gJ78+NK6Zn6Uk=
116120
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
117121
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
122+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
123+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
118124
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
119125
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
120126
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
@@ -128,15 +134,18 @@ github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9f
128134
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
129135
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
130136
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
137+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
131138
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
132139
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
133140
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
134141
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
135142
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
143+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
136144
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
137145
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
138146
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
139147
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
148+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
140149
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
141150
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
142151
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -146,6 +155,7 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
146155
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
147156
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
148157
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
158+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
149159
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
150160
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
151161
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)