Skip to content

Commit

Permalink
feat: made external node tests use a snapshot of the main node db (#913)
Browse files Browse the repository at this point in the history
## What ❔

Made external node tests use a snapshot of the main node db.
It is implemented by postgres dump & restore in "zk db setup" call.

## Why ❔

Current implementation of the gossip network-based block syncing for
external node needs to know the certificate on the consensus genesis
block (which might not be the first miniblock in the db) to be able to
verify all subsequent blocks.
This certificate cannot be generated by the external node, because it
contains signature of the validator (aka main node). For simplicity, we
assume that this certificate on genesis block is already present in the
external node's db, which has been restored from a snapshot of the main
node db.

Integration tests for external node start with an empty db instead,
which disables fetching blocks over gossip network altogether. This PR
is supposed to fix that.
  • Loading branch information
pompon0 authored Jan 23, 2024
1 parent d379612 commit 60f600b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions etc/env/ext-node-docker.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
database_url = "postgres://postgres@postgres/zksync_local_ext_node"
# Optional variable. If set, "zk db setup" will recreate "database_url" db by
# cloning "template_database_url" db instead of creating an empty db.
# "template_database_url" is not used by EN itself.
template_database_url = "postgres://postgres@postgres/zksync_local"
test_database_url = "postgres://postgres@host:5433/zksync_local_test_ext_node"
database_pool_size = 50
zksync_action="dont_ask"
Expand Down
6 changes: 5 additions & 1 deletion etc/env/ext-node.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
# All the variables must be provided explicitly.
# This is on purpose: if EN will accidentally depend on the main node env, it may cause problems.

test_database_url = "postgres://postgres@localhost:5433/zksync_local_test_ext_node"
database_url = "postgres://postgres@localhost/zksync_local_ext_node"
# Optional variable. If set, "zk db setup" will recreate "database_url" db by
# cloning "template_database_url" db instead of creating an empty db.
# "template_database_url" is not used by EN itself.
template_database_url="postgres://postgres@localhost/zksync_local"
test_database_url = "postgres://postgres@localhost:5433/zksync_local_test_ext_node"
database_pool_size = 50
zksync_action="dont_ask"

Expand Down
19 changes: 15 additions & 4 deletions infrastructure/zk/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ export async function setup() {
// Remote database, we can't show the contents.
console.log(`WARNING! Using prod db!`);
}
await utils.spawn('cargo sqlx database create');
await utils.spawn('cargo sqlx migrate run');
if (process.env.DATABASE_URL!.startsWith(localDbUrl)) {
await utils.spawn('cargo sqlx prepare --check -- --tests || cargo sqlx prepare -- --tests');
if (process.env.TEMPLATE_DATABASE_URL !== undefined) {
// Dump and restore from template database (simulate backup)
console.log(`Template DB URL provided. Creating a DB via dump from ${process.env.TEMPLATE_DATABASE_URL}`);
await utils.spawn('cargo sqlx database drop -y');
await utils.spawn('cargo sqlx database create');
await utils.spawn(
`pg_dump ${process.env.TEMPLATE_DATABASE_URL} -F c | pg_restore -d ${process.env.DATABASE_URL}`
);
} else {
// Create an empty database.
await utils.spawn('cargo sqlx database create');
await utils.spawn('cargo sqlx migrate run');
if (process.env.DATABASE_URL!.startsWith(localDbUrl)) {
await utils.spawn('cargo sqlx prepare --check -- --tests || cargo sqlx prepare -- --tests');
}
}

process.chdir(process.env.ZKSYNC_HOME as string);
Expand Down

0 comments on commit 60f600b

Please sign in to comment.