Repository navigation
feat(harness): keep account keys between deploys #312
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
4 commits
Select commit
Hold shift + click to select a range
2bb6e5b
feat(harness): keep account keys between deploys
hpmaxi 8943368
refactor(harness): keep the keys file at the repository root
hpmaxi 67cecb8
Merge branch 'main' into feat/309-keep-harness-keys
hpmaxi a1898e1
Merge branch 'main' into feat/309-keep-harness-keys
luchobonatti 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { relative } from "node:path" | ||
|
|
||
| import { ACCOUNT_NAMES } from "./lib/accounts.js" | ||
| import { REPO_ROOT } from "./lib/deployment.js" | ||
| import { ensureKeys } from "./lib/keys.js" | ||
| import { network } from "./lib/network.js" | ||
|
|
||
| function main(): void { | ||
| const { file, accounts, created } = ensureKeys(network().name, REPO_ROOT) | ||
| console.log( | ||
| `account keys in ${relative(REPO_ROOT, file)} (${created.length} new)`, | ||
| ) | ||
| for (const name of ACCOUNT_NAMES) { | ||
| const mark = created.includes(name) ? " new" : "" | ||
| console.log(`${name.padEnd(10)} ${accounts[name].publicKey()}${mark}`) | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| main() | ||
| } catch (error: unknown) { | ||
| console.error(error instanceof Error ? error.message : error) | ||
| process.exitCode = 1 | ||
| } |
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,107 @@ | ||
| import { | ||
| Account, | ||
| Keypair, | ||
| Networks, | ||
| rpc, | ||
| type Operation, | ||
| type Transaction, | ||
| } from "@stellar/stellar-sdk" | ||
| import { afterEach, describe, expect, it, vi } from "vitest" | ||
|
|
||
| import { fund } from "./accounts.js" | ||
| import { type Funding, type Network } from "./network.js" | ||
|
|
||
| const networkWith = (funding: Funding): Network => ({ | ||
| name: "local", | ||
| rpcUrl: "http://localhost:8000/rpc", | ||
| passphrase: Networks.STANDALONE, | ||
| funding, | ||
| oracle: { | ||
| freshness_duration: 300, | ||
| cooldown_secs: 5, | ||
| max_up_bps: 1000, | ||
| max_down_bps: null, | ||
| min_answer: "1", | ||
| max_answer: "2", | ||
| }, | ||
| }) | ||
|
|
||
| // Knows which accounts exist, and creates the ones a transaction creates. | ||
| function fakeServer(existing: Set<string>) { | ||
| const created: string[][] = [] | ||
| const server = { | ||
| getAccount(publicKey: string): Promise<Account> { | ||
| if (!existing.has(publicKey)) | ||
| return Promise.reject(new Error(`Account not found: ${publicKey}`)) | ||
| return Promise.resolve(new Account(publicKey, "1")) | ||
| }, | ||
| sendTransaction(tx: Transaction) { | ||
| const destinations = tx.operations.map( | ||
| (op) => (op as Operation.CreateAccount).destination, | ||
| ) | ||
| created.push(destinations) | ||
| for (const destination of destinations) existing.add(destination) | ||
| return Promise.resolve({ status: "PENDING", hash: "hash" }) | ||
| }, | ||
| pollTransaction() { | ||
| return Promise.resolve({ | ||
| status: rpc.Api.GetTransactionStatus.SUCCESS, | ||
| }) | ||
| }, | ||
| } | ||
| return { server: server as unknown as rpc.Server, created } | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals() | ||
| }) | ||
|
|
||
| describe("fund from the root account", () => { | ||
| const root = Keypair.random() | ||
| const net = networkWith({ kind: "root", secret: root.secret() }) | ||
|
|
||
| it("creates only the accounts that do not exist yet", async () => { | ||
| const [known, fresh1, fresh2] = [0, 1, 2].map(() => Keypair.random()) | ||
| const { server, created } = fakeServer( | ||
| new Set([root.publicKey(), known!.publicKey()]), | ||
| ) | ||
|
|
||
| const funded = await fund(net, server, [known!, fresh1!, fresh2!]) | ||
|
|
||
| expect(funded).toBe(2) | ||
| expect(created).toEqual([[fresh1!.publicKey(), fresh2!.publicKey()]]) | ||
| }) | ||
|
|
||
| it("sends nothing when every account exists", async () => { | ||
| const keys = [Keypair.random(), Keypair.random()] | ||
| const { server, created } = fakeServer( | ||
| new Set([root.publicKey(), ...keys.map((key) => key.publicKey())]), | ||
| ) | ||
|
|
||
| expect(await fund(net, server, keys)).toBe(0) | ||
| expect(created).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe("fund from friendbot", () => { | ||
| const url = "https://friendbot.example" | ||
| const net = networkWith({ kind: "friendbot", url }) | ||
|
|
||
| it("asks only for the accounts that do not exist yet", async () => { | ||
| const [known, fresh] = [Keypair.random(), Keypair.random()] | ||
| const existing = new Set([known.publicKey()]) | ||
| const { server } = fakeServer(existing) | ||
| const asked: string[] = [] | ||
| vi.stubGlobal("fetch", (request: string) => { | ||
| const address = new URL(request).searchParams.get("addr") ?? "" | ||
| asked.push(address) | ||
| existing.add(address) | ||
| return Promise.resolve(new Response("{}")) | ||
| }) | ||
|
|
||
| const funded = await fund(net, server, [known, fresh]) | ||
|
|
||
| expect(funded).toBe(1) | ||
| expect(asked).toEqual([fresh.publicKey()]) | ||
| }) | ||
| }) |
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 |
|---|---|---|
|
|
@@ -31,12 +31,6 @@ export const ACCOUNT_NAMES = [ | |
| export type AccountName = (typeof ACCOUNT_NAMES)[number] | ||
| export type Accounts = Record<AccountName, Keypair> | ||
|
|
||
| export function freshAccounts(): Accounts { | ||
| return Object.fromEntries( | ||
| ACCOUNT_NAMES.map((name) => [name, Keypair.random()]), | ||
| ) as Accounts | ||
| } | ||
|
|
||
| export async function submit( | ||
| server: rpc.Server, | ||
| tx: Transaction, | ||
|
|
@@ -119,17 +113,32 @@ async function awaitVisible( | |
| } | ||
| } | ||
|
|
||
| async function exists(server: rpc.Server, publicKey: string): Promise<boolean> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try { | ||
| await server.getAccount(publicKey) | ||
| return true | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| export async function fund( | ||
| net: Network, | ||
| server: rpc.Server, | ||
| keys: readonly Keypair[], | ||
| ): Promise<void> { | ||
| ): Promise<number> { | ||
| const missing: Keypair[] = [] | ||
| for (const key of keys) { | ||
| if (!(await exists(server, key.publicKey()))) missing.push(key) | ||
| } | ||
| if (missing.length === 0) return 0 | ||
| if (net.funding.kind === "root") { | ||
| await fundFromRoot(net, server, net.funding.secret, keys) | ||
| await fundFromRoot(net, server, net.funding.secret, missing) | ||
| } else { | ||
| await fundFromFriendbot(net.funding.url, keys) | ||
| await fundFromFriendbot(net.funding.url, missing) | ||
| } | ||
| for (const key of keys) await awaitVisible(server, key.publicKey()) | ||
| for (const key of missing) await awaitVisible(server, key.publicKey()) | ||
| return missing.length | ||
| } | ||
|
|
||
| async function operate( | ||
|
|
||
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,34 @@ | ||
| import { | ||
| Address, | ||
| Asset, | ||
| Keypair, | ||
| Networks, | ||
| type rpc, | ||
| type xdr, | ||
| } from "@stellar/stellar-sdk" | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import { ensureAssetContract } from "./cli.js" | ||
| import { type Network } from "./network.js" | ||
|
|
||
| describe("ensureAssetContract", () => { | ||
| it("returns the asset contract already on the network", async () => { | ||
| const net = { passphrase: Networks.STANDALONE } as Network | ||
| const issuer = Keypair.random() | ||
| const asset = new Asset("USDC", issuer.publicKey()) | ||
| const id = asset.contractId(net.passphrase) | ||
| const server = { | ||
| getLedgerEntries(key: xdr.LedgerKey) { | ||
| const contract = Address.fromScAddress( | ||
| key.contractData().contract(), | ||
| ).toString() | ||
| return Promise.resolve({ | ||
| entries: contract === id ? [{}] : [], | ||
| latestLedger: 1, | ||
| }) | ||
| }, | ||
| } as unknown as rpc.Server | ||
|
|
||
| expect(await ensureAssetContract(net, server, asset, issuer)).toBe(id) | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The record still carries every secret (line 243) and each deploy now keeps another 0644 copy. With the keys file as the source of truth, maybe store public keys only, or write the records 0600?