Skip to content

Commit 9fdc2f5

Browse files
zander-xyzjhunsaker
authored andcommitted
unit tests for validate
1 parent b4b4ac4 commit 9fdc2f5

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

category/statesync/test/test_statesync.cpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,3 +1202,141 @@ TEST(Deletions, exceed_max_deletions)
12021202
bool const success = deletions->for_each(12, [](auto const &) {});
12031203
EXPECT_TRUE(success);
12041204
}
1205+
1206+
// Unit tests for request validation logic
1207+
TEST_F(StateSyncFixture, validation_prefix_bytes_too_large)
1208+
{
1209+
// Set up a valid database state
1210+
bytes32_t parent_hash{NULL_HASH};
1211+
for (size_t i = 0; i < 100; ++i) {
1212+
if (i > 0) {
1213+
stdb.set_block_and_prefix(i - 1);
1214+
}
1215+
commit_sequential(
1216+
stdb, {}, {}, BlockHeader{.parent_hash = parent_hash, .number = i});
1217+
parent_hash = to_bytes(
1218+
keccak256(rlp::encode_block_header(stdb.read_eth_header())));
1219+
}
1220+
init();
1221+
1222+
monad_sync_request rq{
1223+
.prefix = 0,
1224+
.prefix_bytes = 9, // exceeds maximum of 8 - INVALID
1225+
.target = 99,
1226+
.from = 0,
1227+
.until = 99,
1228+
.old_target = INVALID_BLOCK_NUM};
1229+
1230+
client.rqs.push_back(rq);
1231+
monad_statesync_server_run_once(server);
1232+
// Should fail due to validation, not due to missing data
1233+
EXPECT_FALSE(client.success);
1234+
}
1235+
1236+
TEST_F(StateSyncFixture, validation_target_invalid)
1237+
{
1238+
bytes32_t parent_hash{NULL_HASH};
1239+
for (size_t i = 0; i < 100; ++i) {
1240+
if (i > 0) {
1241+
stdb.set_block_and_prefix(i - 1);
1242+
}
1243+
commit_sequential(
1244+
stdb, {}, {}, BlockHeader{.parent_hash = parent_hash, .number = i});
1245+
parent_hash = to_bytes(
1246+
keccak256(rlp::encode_block_header(stdb.read_eth_header())));
1247+
}
1248+
init();
1249+
1250+
monad_sync_request rq{
1251+
.prefix = 0,
1252+
.prefix_bytes = 8,
1253+
.target = INVALID_BLOCK_NUM, // invalid target
1254+
.from = 0,
1255+
.until = 99,
1256+
.old_target = INVALID_BLOCK_NUM};
1257+
1258+
client.rqs.push_back(rq);
1259+
monad_statesync_server_run_once(server);
1260+
EXPECT_FALSE(client.success);
1261+
}
1262+
1263+
TEST_F(StateSyncFixture, validation_from_greater_than_until)
1264+
{
1265+
bytes32_t parent_hash{NULL_HASH};
1266+
for (size_t i = 0; i < 100; ++i) {
1267+
if (i > 0) {
1268+
stdb.set_block_and_prefix(i - 1);
1269+
}
1270+
commit_sequential(
1271+
stdb, {}, {}, BlockHeader{.parent_hash = parent_hash, .number = i});
1272+
parent_hash = to_bytes(
1273+
keccak256(rlp::encode_block_header(stdb.read_eth_header())));
1274+
}
1275+
init();
1276+
1277+
monad_sync_request rq{
1278+
.prefix = 0,
1279+
.prefix_bytes = 8,
1280+
.target = 99,
1281+
.from = 50,
1282+
.until = 40, // from > until - INVALID
1283+
.old_target = INVALID_BLOCK_NUM};
1284+
1285+
client.rqs.push_back(rq);
1286+
monad_statesync_server_run_once(server);
1287+
EXPECT_FALSE(client.success);
1288+
}
1289+
1290+
TEST_F(StateSyncFixture, validation_until_greater_than_target)
1291+
{
1292+
bytes32_t parent_hash{NULL_HASH};
1293+
for (size_t i = 0; i < 100; ++i) {
1294+
if (i > 0) {
1295+
stdb.set_block_and_prefix(i - 1);
1296+
}
1297+
commit_sequential(
1298+
stdb, {}, {}, BlockHeader{.parent_hash = parent_hash, .number = i});
1299+
parent_hash = to_bytes(
1300+
keccak256(rlp::encode_block_header(stdb.read_eth_header())));
1301+
}
1302+
init();
1303+
1304+
monad_sync_request rq{
1305+
.prefix = 0,
1306+
.prefix_bytes = 8,
1307+
.target = 99,
1308+
.from = 0,
1309+
.until = 100, // until > target - INVALID
1310+
.old_target = INVALID_BLOCK_NUM};
1311+
1312+
client.rqs.push_back(rq);
1313+
monad_statesync_server_run_once(server);
1314+
EXPECT_FALSE(client.success);
1315+
}
1316+
1317+
TEST_F(StateSyncFixture, validation_old_target_greater_than_target)
1318+
{
1319+
bytes32_t parent_hash{NULL_HASH};
1320+
for (size_t i = 0; i < 100; ++i) {
1321+
if (i > 0) {
1322+
stdb.set_block_and_prefix(i - 1);
1323+
}
1324+
commit_sequential(
1325+
stdb, {}, {}, BlockHeader{.parent_hash = parent_hash, .number = i});
1326+
parent_hash = to_bytes(
1327+
keccak256(rlp::encode_block_header(stdb.read_eth_header())));
1328+
}
1329+
init();
1330+
1331+
monad_sync_request rq{
1332+
.prefix = 0,
1333+
.prefix_bytes = 8,
1334+
.target = 50,
1335+
.from = 0,
1336+
.until = 50,
1337+
.old_target = 51}; // old_target > target - INVALID
1338+
1339+
client.rqs.push_back(rq);
1340+
monad_statesync_server_run_once(server);
1341+
EXPECT_FALSE(client.success);
1342+
}

0 commit comments

Comments
 (0)