Skip to content

Commit

Permalink
test(sdk): update for nonce issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dtbuchholz committed Apr 18, 2024
1 parent 7398c9b commit ab90f87
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 70 deletions.
48 changes: 25 additions & 23 deletions packages/sdk/test/aliases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
import { strictEqual, rejects } from "assert";
import { describe, test } from "mocha";
import { getAccounts } from "@tableland/local";
import { NonceManager } from "ethers";
import { type NameMapping, getDefaultProvider } from "../src/helpers/index.js";
import { type NameMapping, createSigner } from "../src/helpers/index.js";
import { Database } from "../src/index.js";
import { TEST_TIMEOUT_FACTOR, TEST_PROVIDER_URL } from "./setup";

describe("aliases", function () {
this.timeout(TEST_TIMEOUT_FACTOR * 10000);
// Note that we're using the second account here
const [, wallet] = getAccounts();
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
// Test out creating a signer via `createSigner`
const signer = createSigner({
privateKey: wallet.privateKey,
providerUrl: TEST_PROVIDER_URL,
});

describe("in memory aliases", function () {
// keeping name mappings in memory during these tests, but in practice
Expand Down Expand Up @@ -48,21 +47,21 @@ describe("aliases", function () {
});

test("insert and select uses aliases table name mappings", async function () {
await db
const { meta: metaCreate } = await db
.prepare(
"CREATE TABLE students (first_name text, last_name text);"
// testing`first` here
)
.first();
.all();
await metaCreate.txn?.wait();

const { meta } = await db
const { meta: metaWrite } = await db
.prepare(
"INSERT INTO students (first_name, last_name) VALUES ('Bobby', 'Tables');"
// testing`run` here
)
.run();

await meta.txn?.wait();
await metaWrite.txn?.wait();

const { results } = await db
.prepare(
Expand All @@ -85,6 +84,7 @@ describe("aliases", function () {
db.prepare(`CREATE TABLE ${prefixes[1]} (counter int, info text);`),
db.prepare(`CREATE TABLE ${prefixes[2]} (counter int, info text);`),
]);
await meta.txn?.wait();

const uuNames = meta.txn?.names ?? [];

Expand All @@ -94,9 +94,12 @@ describe("aliases", function () {
});

test("batch mutate uses aliases table name mappings", async function () {
await db.prepare("CREATE TABLE mutate_test (k text, val text);").first();
const { meta: metaCreate } = await db
.prepare("CREATE TABLE mutate_test (k text, val text);")
.all();
await metaCreate.txn?.wait();

const [{ meta }] = await db.batch([
const [{ meta: metaWrite }] = await db.batch([
db.prepare(
"INSERT INTO mutate_test (k, val) VALUES ('token1', 'asdfgh');"
),
Expand All @@ -107,8 +110,7 @@ describe("aliases", function () {
"INSERT INTO mutate_test (k, val) VALUES ('token3', 'zxcvbn');"
),
]);

await meta.txn?.wait();
await metaWrite.txn?.wait();

const { results } = await db
.prepare(`SELECT * FROM mutate_test;`)
Expand All @@ -126,19 +128,19 @@ describe("aliases", function () {
test("batch select uses aliases table name mappings", async function () {
const prefixes = ["batch_select1", "batch_select2", "batch_select3"];

await db.batch([
const [{ meta: metaCreate }] = await db.batch([
db.prepare(`CREATE TABLE ${prefixes[0]} (counter int);`),
db.prepare(`CREATE TABLE ${prefixes[1]} (counter int);`),
db.prepare(`CREATE TABLE ${prefixes[2]} (counter int);`),
]);
await metaCreate.txn?.wait();

const [{ meta }] = await db.batch([
const [{ meta: metaWrite }] = await db.batch([
db.prepare(`INSERT INTO ${prefixes[0]} (counter) VALUES (1);`),
db.prepare(`INSERT INTO ${prefixes[1]} (counter) VALUES (2);`),
db.prepare(`INSERT INTO ${prefixes[2]} (counter) VALUES (3);`),
]);

await meta.txn?.wait();
await metaWrite.txn?.wait();

const results = await db.batch<{ counter: number }>([
db.prepare(`SELECT * FROM ${prefixes[0]};`),
Expand All @@ -156,15 +158,15 @@ describe("aliases", function () {
});

test("using universal unique table name works with aliases", async function () {
const { meta } = await db
const { meta: metaCreate } = await db
.prepare("CREATE TABLE uu_name (counter int);")
.all();
const uuTableName = meta.txn?.name ?? "";
await metaCreate.txn?.wait();
const uuTableName = metaCreate.txn?.name ?? "";

const { meta: insertMeta } = await db
.prepare(`INSERT INTO ${uuTableName} (counter) VALUES (1);`)
.all();

await insertMeta.txn?.wait();

const { results } = await db
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/test/browser/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"vue": "^3.2.45"
},
"devDependencies": {
"@tableland/local": "^2.0.0",
"@tableland/local": "^3.0.0",
"@types/node": "^18.11.12",
"@vitejs/plugin-vue": "^4.0.0",
"@vue/tsconfig": "^0.1.3",
Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getChainId,
type Eip1193Provider,
} from "../src/helpers/index.js";
import { checkProvider } from "../src/helpers/ethers.js";
import { checkProviderOfSigner } from "../src/helpers/ethers.js";
import { TEST_PROVIDER_URL, TEST_VALIDATOR_URL } from "./setup";

describe("config", function () {
Expand Down Expand Up @@ -73,6 +73,8 @@ describe("config", function () {
return [wallet.address];
case "eth_accounts":
return [wallet.address];
case "eth_chainId":
return "0x7a69"; // 31337
default:
throw new Error(
`method ${request.method} not supported by the mock provider`
Expand All @@ -99,6 +101,8 @@ describe("config", function () {
return [wallet.address];
case "eth_accounts":
return [wallet.address];
case "eth_chainId":
return "0x7a69"; // 31337
default:
throw new Error(
`method ${request.method} not supported by the mock provider`
Expand All @@ -108,7 +112,7 @@ describe("config", function () {
};
(globalThis as any).ethereum = ethereum;
const extracted = await extractSigner(conn);
checkProvider(extracted);
checkProviderOfSigner(extracted);
notEqual(await extracted.getAddress(), null);
notEqual(extracted.provider, null);
delete (globalThis as any).ethereum;
Expand Down
18 changes: 4 additions & 14 deletions packages/sdk/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,25 @@ import assert, {
} from "assert";
import { describe, test } from "mocha";
import { getAccounts } from "@tableland/local";
import { getDefaultProvider, NonceManager } from "ethers";
import { Database } from "../src/database.js";
import { Statement } from "../src/statement.js";
import { createPollingController } from "../src/helpers/await.js";
import { getDefaultProvider } from "../src/helpers/ethers.js";
import { getDelay, getRange } from "../src/helpers/utils.js";
import {
TEST_TIMEOUT_FACTOR,
TEST_PROVIDER_URL,
TEST_VALIDATOR_URL,
} from "./setup";

// TODO: figure out why tests log repeated errors (but still pass) for:
// `JsonRpcProvider failed to detect network and cannot start up; retry in 1s
// (perhaps the URL is wrong or the node is not started)` even though the nodes
// are running and can be accessed without issue via cURL.
describe("database", function () {
this.timeout(TEST_TIMEOUT_FACTOR * 15000);

const accounts = getAccounts();
// Note that we're using the second account here
const wallet = accounts[1];
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
const signer = wallet.connect(provider);
const db = new Database({ signer, autoWait: true });

test("when initialized via constructor", async function () {
Expand Down Expand Up @@ -400,10 +393,7 @@ describe("database", function () {
// note we are using the third account
const wallet = accounts[2];
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
const signer = wallet.connect(provider);
const db2 = new Database({ signer, autoWait: true });

test("when doing grant with batch", async function () {
Expand Down Expand Up @@ -478,7 +468,7 @@ describe("database", function () {
// test after insert is revoked
test("when doing grant with batch", async function () {
// Need to make a lot of changes in this test, increase timeout
this.timeout(TEST_TIMEOUT_FACTOR * 20000);
this.timeout(TEST_TIMEOUT_FACTOR * 25000);

const [batch] = await db.batch([
db.prepare(`CREATE TABLE test_revoke_1 (id INTEGER, name TEXT);`),
Expand Down
17 changes: 7 additions & 10 deletions packages/sdk/test/lowlevel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,8 @@ describe("lowlevel", function () {
tables: ["test_all"],
}
);
await txn.wait();
tableName = txn.name ?? "";
// For testing purposes, we abort the wait before we even start
// This is ok, because we'll await the next transaction
const controller = createPollingController();
controller.abort();
await txn.wait(controller).catch(() => {});
}
{
const txn = await exec(
Expand Down Expand Up @@ -196,7 +192,7 @@ describe("lowlevel", function () {
);
});

test("when select statment has a runtime error", async function () {
test("when select statement has a runtime error", async function () {
await rejects(
queryAll({ baseUrl }, "SELECT * FROM test_all_31337_0;"),
(err: any) => {
Expand Down Expand Up @@ -299,6 +295,7 @@ describe("lowlevel", function () {
tables: ["test_all"],
}
);
await txn.wait();
tableName = txn.name ?? "";
}
{
Expand All @@ -315,7 +312,7 @@ describe("lowlevel", function () {
}
});

test("when select statment has a error parsing statement", async function () {
test("when select statement has a error parsing statement", async function () {
// This rejects on the validator because we don't have the parser to catch the syntax error
await rejects(
queryFirst({ baseUrl }, "SELECT * FROM 3.14;"),
Expand Down Expand Up @@ -343,7 +340,7 @@ describe("lowlevel", function () {
);
});

test("when select statment has a runtime error", async function () {
test("when select statement has a runtime error", async function () {
await rejects(
queryFirst({ baseUrl }, "SELECT * FROM test_first_31337_0;"),
(err: any) => {
Expand Down Expand Up @@ -414,7 +411,7 @@ describe("lowlevel", function () {
}
});

test("when select statment has a error parsing statement", async function () {
test("when select statement has a error parsing statement", async function () {
await rejects(
queryRaw({ baseUrl }, "SELECT * FROM 3.14;"),
(err: any) => {
Expand All @@ -427,7 +424,7 @@ describe("lowlevel", function () {
);
});

test("when select statment has a runtime error", async function () {
test("when select statement has a runtime error", async function () {
await rejects(
queryRaw({ baseUrl }, "SELECT * FROM test_raw_31337_0;"),
(err: any) => {
Expand Down
15 changes: 8 additions & 7 deletions packages/sdk/test/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { match, notStrictEqual, rejects, strictEqual } from "assert";
import { describe, test } from "mocha";
import { getAccounts } from "@tableland/local";
import { NonceManager } from "ethers";
import {
getDefaultProvider,
type MultiEventTransactionReceipt,
Expand All @@ -12,15 +11,12 @@ import { wrapTransaction } from "../src/registry/utils.js";
import { Registry } from "../src/registry/index.js";
import { TEST_TIMEOUT_FACTOR, TEST_PROVIDER_URL } from "./setup";

describe("registry", function () {
describe.only("registry", function () {
this.timeout(TEST_TIMEOUT_FACTOR * 10000);
// Note that we're using the second account here
const [, wallet, controller] = getAccounts();
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
const signer = wallet.connect(provider);
const reg = new Registry({ signer });

test("when initialized via constructor", async function () {
Expand Down Expand Up @@ -235,12 +231,15 @@ describe("registry", function () {
statement:
"create table test_runsql_31337 (id integer primary key, counter integer, info text)",
});
await tx.wait();
console.log("nonce: ", await signer.getNonce());
receipt = await getContractReceipt(tx);
notStrictEqual(receipt.tableIds[0], undefined);
strictEqual(receipt.chainId, 31337);
});

test("when insert statement is valid", async function () {
console.log("nonce: ", await signer.getNonce());
const tx = await reg.mutate({
chainId: receipt.chainId,
tableId: receipt.tableIds[0],
Expand All @@ -251,7 +250,9 @@ describe("registry", function () {
strictEqual(rec?.hash.length, 66);
});

test("when insert statement is valid", async function () {
test("when update statement is valid", async function () {
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log("nonce: ", await signer.getNonce());
const tx = await reg.mutate({
chainId: receipt.chainId,
tableId: receipt.tableIds[0],
Expand Down
12 changes: 3 additions & 9 deletions packages/sdk/test/statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import assert, {
throws,
} from "assert";
import { describe, test } from "mocha";
import { NonceManager } from "ethers";
import { getAccounts } from "@tableland/local";
import { getDelay } from "../src/helpers/utils.js";
import {
Expand All @@ -22,10 +21,7 @@ describe("statement", function () {
// Note that we're using the second account here
const [, wallet, wallet2] = getAccounts();
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
const signer = wallet.connect(provider);
const db = new Database({ signer });

test("when initialized via constructor", async function () {
Expand Down Expand Up @@ -205,10 +201,7 @@ CREATE TABLE test_run (counter blurg);

test("when non-owner tries to alter table", async function () {
const provider = getDefaultProvider(TEST_PROVIDER_URL);
const baseSigner = wallet2.connect(provider);
// TODO: figure out why tests fail when using the base signer directly due to
// nonce too low / nonce has already been used / NONCE_EXPIRED error
const signer = new NonceManager(baseSigner);
const signer = wallet2.connect(provider);
const db2 = new Database({ signer });

const [batchGrant] = await db.batch([
Expand Down Expand Up @@ -548,6 +541,7 @@ SELECT * FROM 3.14;
const { meta } = await db
.prepare("CREATE TABLE test_first (counter integer, info text);")
.run();
await meta.txn?.wait();
tableName = meta.txn?.name ?? "";
}
{
Expand Down
Loading

0 comments on commit ab90f87

Please sign in to comment.