-
Notifications
You must be signed in to change notification settings - Fork 3
chore: make bridge configurable in tests #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cb7e421
feat(tests): add ERC-20 bridge testing utilities and integration tests
outerlook d6114a6
feat(tests): enhance ERC-20 bridge tests with mock listener and path …
outerlook a27d905
feat(migrations): enhance seed script handling and add test-only SQL …
outerlook 2f20768
feat(tests): add comprehensive ERC-20 bridge tests for epoch flow and…
outerlook 788a71f
refactor(tests): streamline ERC-20 bridge tests and remove mock liste…
outerlook aa73919
feat(tests): add comprehensive ERC-20 bridge tests for various functi…
outerlook d59bef3
feat(tests): enhance ERC-20 bridge tests with new consecutive test ca…
outerlook 434ea77
refactor(tests): streamline ERC-20 bridge tests by removing seed scri…
outerlook a65927b
fix(ci): update Go test command to include 'kwiltest' build tag for i…
outerlook a7e4bb4
refactor(tests): update CallLockAdmin function signature and improve …
outerlook 354986c
Merge branch 'main' into chore/bridge-config
outerlook c07094c
chore(deps): update kwil-db and go-ethereum dependencies in go.mod an…
outerlook f9950ae
docs(migrations): add comment to bootstrap_erc20.sql for test clarity
outerlook d6e74dd
refactor(tests): unify ERC-20 bridge test instance activation with ex…
outerlook cede833
fix(tests): improve error handling in ERC-20 test setup and admin loc…
outerlook d148bea
refactor(tests): replace kwilTesting.RunSchemaTest with testutils.Run…
outerlook 42babb5
refactor(tests): update test files to use testutils.RunSchemaTest for…
outerlook 3a2c9be
refactor(tests): enhance RunSchemaTest to include SeedStatements, Tes…
outerlook 4e26a93
fix(tests): make test case handling a fatal error in RunSchemaTest
outerlook File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| -- This file exists to manually enable the ERC20 bootstrap process for our tests | ||
|
|
||
| USE kwil_erc20_meta as kwil_erc20_meta; | ||
|
|
||
| -- ordered-sync is provisioned by its genesis hook in node runtime. The test harness | ||
| -- does not run genesis, so we create the namespace and schema explicitly for tests. | ||
| CREATE NAMESPACE IF NOT EXISTS kwil_ordered_sync; | ||
| SET CURRENT NAMESPACE TO kwil_ordered_sync; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS topics ( | ||
| id UUID PRIMARY KEY, | ||
| name TEXT UNIQUE NOT NULL, | ||
| resolve_func TEXT NOT NULL, | ||
| last_processed_point int8 | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS pending_data ( | ||
| point int8, | ||
| topic_id UUID REFERENCES topics(id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
| previous_point int8, | ||
| data bytea not null, | ||
| PRIMARY KEY (point, topic_id) | ||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS meta( | ||
| version int8 PRIMARY KEY | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| Notice: to run tests in this directory, you need to build the node with the `kwiltest` tag. | ||
| To make it easier on your IDE, you might want to include in the configurations, for example, for VSCode, you can add the following to your `settings.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "go.testTags": "kwiltest" | ||
| } | ||
| ``` | ||
|
|
||
| ### Objective | ||
|
|
||
| This directory demonstrates a minimal, production-faithful structure for wiring and invoking the ERC‑20 bridge extension in tests. It: | ||
|
|
||
| - Shows how to bootstrap an instance, call actions, and cleanly tear down using the `ForTesting*` helpers. | ||
| - Demonstrates how tests should evoke the extension (e.g., alias usage, numeric arguments, engine context, authz boundaries) in isolation. | ||
| - Serves as a reference pattern to later integrate these flows into our standard streams tests appropriately. | ||
|
|
||
| ### Quick start | ||
|
|
||
| - Enable the `kwiltest` tag (above) so the test helpers are available. | ||
| - Seed and activate an instance for a test: | ||
|
|
||
| ```go | ||
| err := erc20shim.ForTestingSeedAndActivateInstance(ctx, platform, chain, escrow, erc20, 18, 60, alias) | ||
| ``` | ||
|
|
||
| - Invoke actions using the alias (e.g., `balance`, `bridge`, `lock_admin`). | ||
| - For epoch flows, use `ForTestingFinalizeAndConfirmCurrentEpoch(...)` to deterministically advance. | ||
| - Cleanup explicitly with `ForTestingDisableInstance(...)` or rely on the test wrapper’s registered cleanup. | ||
|
|
||
| ### Why helpers over plain SQL/`USE ...` for ERC-20 tests | ||
|
|
||
| - **The ERC-20 bridge extension has runtime wiring beyond SQL.** In production it creates and manages background components and in‑memory state: ordered‑sync topics/listeners for `Transfer` events, a state poller, a singleton cache rehydrated on start, epoch lifecycle rows, and synced ERC‑20 metadata. A bare `USE erc20 { ... } AS alias` only creates the alias; it does not guarantee these side‑effects are present or initialized in the current process. | ||
| - **Plain seeds miss critical initialization.** Relying only on SQL seeds/`USE` won’t ensure the `kwil_erc20_meta` schema exists, the `reward_instances` row and first epoch are created, the instance is marked synced with the ERC‑20 address/decimals, the ordered‑sync topic is registered, or that the extension’s singleton has been rehydrated. That leads to flaky tests and "already active"/missing‑wiring failures. | ||
|
|
||
| ### What `ForTestingSeedAndActivateInstance` does | ||
|
|
||
| `ForTestingSeedAndActivateInstance(ctx, platform, chain, escrow, erc20, decimals, periodSeconds, alias)` performs, idempotently: | ||
|
|
||
| - Creates the extension alias via `USE erc20 { chain: '<chain>', escrow: '<escrow>' } AS <alias>`. | ||
| - Ensures the `kwil_erc20_meta` namespace/schema exist and computes the deterministic instance ID. | ||
| - Registers the ordered‑sync transfer topic/state‑poller names for the instance. | ||
| - Creates the `reward_instances` row if missing and guarantees a first pending epoch. | ||
| - Marks the instance as synced with the provided ERC‑20 address and `decimals`. | ||
| - Sets the distribution period to `periodSeconds` for predictable epochs. | ||
| - Re‑initializes the extension (OnStart‑like) so the in‑memory singleton reflects DB state. | ||
|
|
||
| This yields a production‑like, deterministic setup inside a test transaction, without depending on external chains. | ||
|
|
||
| ### Cleanup: disabling and teardown | ||
|
|
||
| `ForTestingDisableInstance(ctx, platform, chain, escrow, alias)` performs deterministic teardown: | ||
|
|
||
| - Executes `UNUSE <alias>` to drop the alias in the test DB. | ||
| - Calls `kwil_erc20_meta.disable(id)` to deactivate the instance in storage. | ||
| - Unregisters the state poller and transfer listener for the instance. | ||
| - Resets and re‑initializes the singleton to a clean state. | ||
|
|
||
| Additionally, our test wrapper registers cleanup that resets the singleton and clears runtimes to prevent cross‑test contamination. | ||
|
|
||
| ### TL;DR | ||
|
|
||
| Use the provided `ForTesting*` helpers instead of plain SQL/`USE` to mirror production wiring that SQL alone cannot trigger, ensuring deterministic, isolated ERC‑20 extension tests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| //go:build kwiltest | ||
|
|
||
| package tests | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/trufnetwork/kwil-db/common" | ||
| kwilTesting "github.com/trufnetwork/kwil-db/testing" | ||
| "github.com/trufnetwork/node/internal/migrations" | ||
| testutils "github.com/trufnetwork/node/tests/streams/utils" | ||
|
|
||
| erc20shim "github.com/trufnetwork/kwil-db/node/exts/erc20-bridge/erc20" | ||
| ) | ||
|
|
||
| // Common deterministic values used across ERC20 bridge tests | ||
| const ( | ||
| TestChain = "sepolia" | ||
| TestExtensionAlias = "erc20_bridge_test" | ||
| TestEscrowA = "0x1111111111111111111111111111111111111111" | ||
| TestEscrowB = "0x2222222222222222222222222222222222222222" | ||
| TestERC20 = "0x2222222222222222222222222222222222222222" | ||
| TestUserA = "0xabc0000000000000000000000000000000000001" | ||
| TestUserB = "0xabc0000000000000000000000000000000000002" | ||
| TestAmount1 = "1000000000000000000" // 1.0 tokens | ||
| TestAmount2 = "2000000000000000000" // 2.0 tokens | ||
| ) | ||
|
|
||
| // seedAndRun is a helper that handles test execution with proper isolation | ||
| func seedAndRun(t TestingT, name string, fn kwilTesting.TestFunc) { | ||
| seedScripts := migrations.GetSeedScriptPaths() | ||
|
|
||
| // Wrap the test function to add singleton reset and cleanup | ||
| wrappedFn := func(ctx context.Context, platform *kwilTesting.Platform) error { | ||
| // STEP 1: Register cleanup (runs after transaction rollback) | ||
| t.Cleanup(func() { | ||
| erc20shim.ForTestingClearAllInstances(ctx, platform) | ||
| erc20shim.ForTestingResetSingleton() | ||
| }) | ||
|
|
||
| // STEP 2: Run the actual test inside a transaction for rollback isolation | ||
| tx, err := platform.DB.BeginTx(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("begin tx: %w", err) | ||
| } | ||
| defer tx.Rollback(ctx) | ||
|
|
||
| txPlatform := &kwilTesting.Platform{ | ||
| Engine: platform.Engine, | ||
| DB: tx, | ||
| Deployer: platform.Deployer, | ||
| Logger: platform.Logger, | ||
| } | ||
|
|
||
| return fn(ctx, txPlatform) | ||
| } | ||
|
|
||
| testutils.RunSchemaTest(t.(*testing.T), kwilTesting.SchemaTest{ | ||
| Name: name, | ||
| SeedScripts: seedScripts, | ||
| FunctionTests: []kwilTesting.TestFunc{wrappedFn}, | ||
| }, &testutils.Options{Options: testutils.GetTestOptions()}) | ||
| } | ||
|
|
||
| // engCtx creates a standard EngineContext for testing | ||
| func engCtx(ctx context.Context, platform *kwilTesting.Platform, caller string, height int64, overrideAuthz bool) *common.EngineContext { | ||
| return &common.EngineContext{ | ||
| TxContext: &common.TxContext{ | ||
| Ctx: ctx, | ||
| BlockContext: &common.BlockContext{Height: height}, | ||
| Signer: platform.Deployer, | ||
| Caller: caller, | ||
| TxID: platform.Txid(), | ||
| }, | ||
| OverrideAuthz: overrideAuthz, | ||
| } | ||
| } | ||
|
|
||
| // TestingT interface for test functions (matches testutils) | ||
| type TestingT interface { | ||
| Fatalf(format string, args ...interface{}) | ||
| Errorf(format string, args ...interface{}) | ||
| Cleanup(func()) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.