Skip to content

Commit

Permalink
Merge pull request #7258 from Agoric/mhofman/state-sync-refactor
Browse files Browse the repository at this point in the history
refactor: move around some helpers before state-sync
  • Loading branch information
mergify[bot] authored Mar 30, 2023
2 parents 2e05748 + 519d707 commit 037fad8
Show file tree
Hide file tree
Showing 31 changed files with 105 additions and 87 deletions.
2 changes: 1 addition & 1 deletion golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func NewKeeper(
// intermediate transaction state.
//
// The actionQueue's format is documented by `makeChainQueue` in
// `packages/cosmic-swingset/src/make-queue.js`.
// `packages/cosmic-swingset/src/helpers/make-queue.js`.
func (k Keeper) PushAction(ctx sdk.Context, action vm.Jsonable) error {
txHash, txHashOk := ctx.Context().Value(baseapp.TxHashContextKey).(string)
if !txHashOk {
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/scripts/clean-core-eval.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env node
import '@endo/init/debug.js';
import * as farExports from '@endo/far';
import { isEntrypoint } from '../src/is-entrypoint.js';
import { isEntrypoint } from '../src/helpers/is-entrypoint.js';

export const compartmentEvaluate = code => {
// const permit = true;
Expand Down
73 changes: 10 additions & 63 deletions packages/cosmic-swingset/src/chain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import { BridgeId as BRIDGE_ID } from '@agoric/internal';
import {
makeBufferedStorage,
makeReadCachingStorage,
} from './bufferedStorage.js';
import stringify from './json-stable-stringify.js';
} from './helpers/bufferedStorage.js';
import stringify from './helpers/json-stable-stringify.js';
import { launch } from './launch-chain.js';
import { getTelemetryProviders } from './kernel-stats.js';
import { makeProcessValue } from './helpers/process-value.js';

// eslint-disable-next-line no-unused-vars
let whenHellFreezesOver = null;
Expand All @@ -52,7 +53,7 @@ const toNumber = specimen => {
* @param {"set" | "legacySet" | "setWithoutNotify"} setterMethod
* @param {(value: string) => T} fromBridgeStringValue
* @param {(value: T) => string} toBridgeStringValue
* @returns {import("./bufferedStorage.js").KVStore<T>}
* @returns {import("./helpers/bufferedStorage.js").KVStore<T>}
*/
const makePrefixedBridgeStorage = (
call,
Expand Down Expand Up @@ -112,68 +113,14 @@ export default async function main(progname, args, { env, homedir, agcc }) {

// TODO: use the 'basedir' pattern

// Try to determine the cosmos chain home.
function getFlagValue(flagName, deflt) {
let flagValue = deflt;
const envValue =
env[`AG_CHAIN_COSMOS_${flagName.toUpperCase().replace(/-/g, '_')}`];
if (envValue !== undefined) {
flagValue = envValue;
}
const flag = `--${flagName}`;
const flagEquals = `--${flagName}=`;
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === flag) {
i += 1;
flagValue = args[i];
} else if (arg.startsWith(flagEquals)) {
flagValue = arg.substr(flagEquals.length);
}
}
return flagValue;
}

/**
* @param {object} options
* @param {string} [options.envName]
* @param {string} [options.flagName]
* @param {string} options.trueValue
* @returns {string | undefined}
*/
function getPathFromEnv({ envName, flagName, trueValue }) {
let option;
if (envName) {
option = env[envName];
} else if (flagName) {
option = getFlagValue(flagName);
} else {
return undefined;
}

switch (option) {
case '0':
case 'false':
case false:
return undefined;
case '1':
case 'true':
case true:
return trueValue;
default:
if (option) {
return pathResolve(option);
} else if (envName && flagName) {
return getPathFromEnv({ flagName, trueValue });
} else {
return undefined;
}
}
}
const processValue = makeProcessValue({ env, args });

// We try to find the actual cosmos state directory (default=~/.ag-chain-cosmos), which
// is better than scribbling into the current directory.
const cosmosHome = getFlagValue('home', `${homedir}/.ag-chain-cosmos`);
const cosmosHome = processValue.getFlag(
'home',
`${homedir}/.ag-chain-cosmos`,
);
const stateDBDir = `${cosmosHome}/data/ag-cosmos-chain-state`;
fs.mkdirSync(stateDBDir, { recursive: true });

Expand Down Expand Up @@ -360,7 +307,7 @@ export default async function main(progname, args, { env, homedir, agcc }) {
serviceName: TELEMETRY_SERVICE_NAME,
});

const swingStoreTraceFile = getPathFromEnv({
const swingStoreTraceFile = processValue.getPath({
envName: 'SWING_STORE_TRACE',
flagName: 'trace-store',
trueValue: pathResolve(stateDBDir, 'store-trace.log'),
Expand Down
File renamed without changes.
68 changes: 68 additions & 0 deletions packages/cosmic-swingset/src/helpers/process-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { resolve as pathResolve } from 'path';

export const makeProcessValue = ({ env, args }) => {
/**
* @param {string} flagName
* @param {string | undefined} [deflt]
* @returns {string | undefined}
*/
const getFlag = (flagName, deflt) => {
let flagValue = deflt;
const envValue =
env[`AG_CHAIN_COSMOS_${flagName.toUpperCase().replace(/-/g, '_')}`];
if (envValue !== undefined) {
flagValue = envValue;
}
const flag = `--${flagName}`;
const flagEquals = `--${flagName}=`;
for (let i = 0; i < args.length; i += 1) {
const arg = args[i];
if (arg === flag) {
i += 1;
flagValue = args[i];
} else if (arg.startsWith(flagEquals)) {
flagValue = arg.substr(flagEquals.length);
}
}
return flagValue;
};

/**
* @param {object} options
* @param {string} [options.envName]
* @param {string} [options.flagName]
* @param {string} options.trueValue
* @returns {string | undefined}
*/
const getPath = ({ envName, flagName, trueValue }) => {
let option;
if (envName) {
option = env[envName];
} else if (flagName) {
option = getFlag(flagName);
} else {
return undefined;
}

switch (option) {
case '0':
case 'false':
case false:
return undefined;
case '1':
case 'true':
case true:
return trueValue;
default:
if (option) {
return pathResolve(option);
} else if (envName && flagName) {
return getPath({ flagName, trueValue });
} else {
return undefined;
}
}
};

return harden({ getFlag, getPath });
};
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/src/launch-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
BeansPerXsnapComputron,
} from './sim-params.js';
import { parseParams } from './params.js';
import { makeQueue } from './make-queue.js';
import { makeQueue } from './helpers/make-queue.js';

const console = anylogger('launch-chain');
const blockManagerConsole = anylogger('block-manager');
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/src/sim-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import { resolve as importMetaResolve } from 'import-meta-resolve';
import { Fail } from '@agoric/assert';
import { makeWithQueue } from '@agoric/internal/src/queue.js';
import { makeBatchedDeliver } from '@agoric/internal/src/batched-deliver.js';
import stringify from './json-stable-stringify.js';
import stringify from './helpers/json-stable-stringify.js';
import { launch } from './launch-chain.js';
import { getTelemetryProviders } from './kernel-stats.js';
import { DEFAULT_SIM_SWINGSET_PARAMS, QueueInbound } from './sim-params.js';
import { parseQueueSizes } from './params.js';
import { makeQueue, makeQueueStorageMock } from './make-queue.js';
import { makeQueue, makeQueueStorageMock } from './helpers/make-queue.js';

const console = anylogger('fake-chain');

Expand Down
2 changes: 1 addition & 1 deletion packages/deploy-script-support/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { assertOfferResult } from './assertOfferResult.js';
import { installInPieces } from './installInPieces.js';
import { makeWriteCoreProposal } from './writeCoreProposal.js';

export * from '@agoric/internal/src/createBundles.js';
export * from '@agoric/internal/src/node/createBundles.js';

// These are also hard-coded in lib-wallet.js.
// TODO: Add methods to the wallet to access these without hard-coding
Expand Down
2 changes: 1 addition & 1 deletion packages/deploy-script-support/src/writeCoreProposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import fs from 'fs';
import { E } from '@endo/far';

import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import {
deeplyFulfilled,
defangAndTrim,
Expand Down
2 changes: 1 addition & 1 deletion packages/governance/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/scripts/build-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '@endo/init';
import {
createBundles,
extractProposalBundles,
} from '@agoric/internal/src/createBundles.js';
} from '@agoric/internal/src/node/createBundles.js';
import url from 'url';
import process from 'process';

Expand Down
1 change: 1 addition & 0 deletions packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@endo/far": "^0.2.14",
"@endo/marshal": "^0.8.1",
"@endo/promise-kit": "^0.2.52",
"anylogger": "^0.21.0",
"jessie.js": "^0.3.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Use modules not prefixed with `node:` since some deploy scripts may
// still be running in esm emulation
import path from 'path';
import { spawnSync } from 'child_process';
import { createRequire } from 'module';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createWriteStream } from 'node:fs';
import { open } from 'node:fs/promises';
import { makeAggregateError } from './utils.js';
import { makeAggregateError } from '../utils.js';

/**
* @param {import("fs").ReadStream | import("fs").WriteStream} stream
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global process */
import process from 'node:process';
import anylogger from 'anylogger';

const console = anylogger('shutdown');
Expand Down
2 changes: 1 addition & 1 deletion packages/smart-wallet/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion packages/solo/src/pipe-entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { makePromiseKit } from '@endo/promise-kit';

import '@agoric/cosmic-swingset/src/anylogger-agoric.js';
import { connectToFakeChain } from '@agoric/cosmic-swingset/src/sim-chain.js';
import { makeShutdown } from '@agoric/telemetry/src/shutdown.js';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';

const { registerShutdown } = makeShutdown(false);
registerShutdown(() => process.exit());
Expand Down
2 changes: 1 addition & 1 deletion packages/solo/src/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
import { makePromiseKit } from '@endo/promise-kit';
import { parse, stringify } from '@endo/marshal';

import { makeShutdown } from '@agoric/telemetry/src/shutdown.js';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);
Expand Down
2 changes: 1 addition & 1 deletion packages/solo/src/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from '@agoric/swingset-vat';
import { openSwingStore } from '@agoric/swing-store';
import { makeWithQueue } from '@agoric/internal/src/queue.js';
import { makeShutdown } from '@agoric/telemetry/src/shutdown.js';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';
import {
makeDefaultMeterProvider,
getTelemetryProviders,
Expand Down
2 changes: 1 addition & 1 deletion packages/spawner/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion packages/swing-store/src/snapStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { promisify } from 'util';
import { createGzip, createGunzip } from 'zlib';
import { Fail, q } from '@agoric/assert';
import { aggregateTryFinally, PromiseAllOrErrors } from '@agoric/internal';
import { fsStreamReady } from '@agoric/internal/src/fs-stream.js';
import { fsStreamReady } from '@agoric/internal/src/node/fs-stream.js';
import { buffer } from './util.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/src/otel-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {

import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { Resource } from '@opentelemetry/resources';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';

import { getResourceAttributes } from './index.js';
import { makeSlogToOtelKit } from './slog-to-otel.js';
import { makeShutdown } from './shutdown.js';

// These numbers are chosen to attempt to export all spans.
export const SPAN_MAX_QUEUE_SIZE = 100_000;
Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/src/slog-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeFsStreamWriter } from '@agoric/internal/src/fs-stream.js';
import { makeFsStreamWriter } from '@agoric/internal/src/node/fs-stream.js';
import { serializeSlogObj } from './serialize-slog-obj.js';

/** @param {import('./index.js').MakeSlogSenderOptions} opts */
Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/src/slog-sender-pipe-entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import '@endo/init';

import { makeAggregateError } from '@agoric/internal';
import anylogger from 'anylogger';
import { makeShutdown } from './shutdown.js';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';

import { makeSlogSender } from './make-slog-sender.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/telemetry/src/slog-sender-pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import anylogger from 'anylogger';

import { makeQueue } from '@endo/stream';

import { makeShutdown } from './shutdown.js';
import { makeShutdown } from '@agoric/internal/src/node/shutdown.js';

const filename = new URL(import.meta.url).pathname;
const dirname = path.dirname(filename);
Expand Down
2 changes: 1 addition & 1 deletion packages/vats/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet/api/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/scripts/build-bundles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /usr/bin/env node
import '@endo/init';
import { createBundles } from '@agoric/internal/src/createBundles.js';
import { createBundles } from '@agoric/internal/src/node/createBundles.js';
import url from 'url';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
Expand Down

0 comments on commit 037fad8

Please sign in to comment.