Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #239 from nervosnetwork/refactor-config
Browse files Browse the repository at this point in the history
refactor: get config from godwoken rpc instead of env
  • Loading branch information
RetricSu authored May 7, 2022
2 parents 2b1dc8e + f784fbb commit b97b1a5
Show file tree
Hide file tree
Showing 20 changed files with 1,109 additions and 148 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ jobs:
- run: yarn install
- run: yarn run build
- run: yarn workspace @godwoken-web3/api-server run test tests/db/helpers.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/utils/erc20.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/base/types/uint32.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/base/types/uint64.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/base/types/uint128.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/base/types/uint256.test.ts
- run: yarn workspace @godwoken-web3/api-server run test tests/utils
- run: yarn workspace @godwoken-web3/api-server run test tests/base/types
- run: yarn run fmt
- run: yarn run lint
- run: git diff --exit-code
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,18 @@ A Web3 RPC compatible layer build upon Godwoken/Polyjuice.
```bash
$ cat > ./packages/api-server/.env <<EOF
DATABASE_URL=postgres://username:password@localhost:5432/your_db
REDIS_URL=redis://user:password@localhost:6379 <redis url, optional, default to localhost on port 6379>
GODWOKEN_JSON_RPC=<godwoken rpc>
GODWOKEN_READONLY_JSON_RPC=<optional, default equals to GODWOKEN_JSON_RPC>
ETH_ACCOUNT_LOCK_HASH=<eth account lock script hash>
ROLLUP_TYPE_HASH=<godwoken rollup type hash>
ROLLUP_CONFIG_HASH=<godwoken rollup config hash>
CHAIN_ID=<godwoken chain id in integer>
CREATOR_ACCOUNT_ID=<your creator account id in integer>
DEFAULT_FROM_ID=<default from eth address's godwoken account id>
POLYJUICE_VALIDATOR_TYPE_HASH=<godwoken polyjuice validator type hash>
L2_SUDT_VALIDATOR_SCRIPT_TYPE_HASH=<l2 sudt validator script type hash>
ETH_ADDRESS_REGISTRY_ACCOUNT_ID=<required, eth address registry account id>
SENTRY_DNS=<sentry dns, optional>
SENTRY_ENVIRONMENT=<sentry environment, optional, default to `development`>,
NEW_RELIC_LICENSE_KEY=<new relic license key, optional>
NEW_RELIC_APP_NAME=<new relic app name, optional, default to 'Godwoken Web3'>
CLUSTER_COUNT=<cluster count, optional, default to num of cpus>
REDIS_URL=redis://user:password@localhost:6379 <redis url, optional, default to localhost on port 6379>
PG_POOL_MAX=<pg pool max count, optional, default to 20>
CLUSTER_COUNT=<cluster count, optional, default to num of cpus>
GAS_PRICE_CACHE_SECONDS=<seconds, optional, default to 0, and 0 means no cache>
EXTRA_ESTIMATE_GAS=<eth_estimateGas will add this number to result, optional, default to 0>
ENABLE_CACHE_ETH_CALL=<optional, enable eth_call cache, default to false>
Expand Down Expand Up @@ -76,11 +69,11 @@ chain_id=<godwoken chain_id in integer>
EOF
```

Or just run script, copy configs from `packages/api-server/.env` file.
Or just run script, generate configs from `packages/api-server/.env` file and godwoken rpc `gw_get_node_info`.

```bash
node scripts/generate-indexer-config.js <websocket rpc url>
```
```

### Start Indexer

Expand Down
29 changes: 28 additions & 1 deletion packages/api-server/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Sentry from "@sentry/node";
import { applyRateLimitByIp } from "../rate-limit";
import { initSentry } from "../sentry";
import { envConfig } from "../base/env-config";
import { gwConfig } from "../base/index";
import { expressLogger, logger } from "../base/logger";
import { Server } from "http";

let newrelic: any | undefined = undefined;
if (envConfig.newRelicLicenseKey) {
Expand Down Expand Up @@ -132,4 +134,29 @@ app.use(function (
res.render("error");
});

export { app };
let server: Server | undefined;

async function startServer(port: number): Promise<void> {
try {
await gwConfig.init();
logger.info("godwoken config initialized!");
} catch (err) {
logger.error("godwoken config initialize failed:", err);
process.exit(1);
}
server = app.listen(port, () => {
const addr = (server as Server).address();
const bind =
typeof addr === "string" ? "pipe " + addr : "port " + addr!.port;
logger.info("godwoken-web3-api:server Listening on " + bind);
});
}

function isListening() {
if (server == null) {
return false;
}
return server.listening;
}

export { startServer, isListening };
13 changes: 2 additions & 11 deletions packages/api-server/src/app/www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
* Module dependencies.
*/

import { logger } from "../base/logger";
import { app } from "./app";
import { startServer } from "./app";

/**
* Get port from environment and store in Express.
*/
const port: number = +(process.env.PORT || "3000");
const server = app.listen(port, () => {
const addr = server.address();
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr!.port;
logger.info("godwoken-web3-api:server Listening on " + bind);
});

export const isListening = function () {
return server.listening;
};
startServer(port);
9 changes: 5 additions & 4 deletions packages/api-server/src/base/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { GodwokenClient } from "@godwoken-web3/godwoken";
import { Store } from "../cache/store";
import { COMPATIBLE_DOCS_URL } from "../methods/constant";
import { envConfig } from "./env-config";
import { gwConfig } from "./index";
import { logger } from "./logger";
import { Uint32 } from "./types/uint";

Expand All @@ -14,7 +15,7 @@ scriptHashCache.init();

// Only support eth address now!
export class EthRegistryAddress {
private registryId: number = +envConfig.ethAddressRegistryAccountId;
private registryId: number = +gwConfig.accounts.ethAddrReg.id;
private addressByteSize: number = 20;
public readonly address: HexString;

Expand Down Expand Up @@ -86,7 +87,7 @@ export async function ethAddressToAccountId(
godwokenClient: GodwokenClient
): Promise<number | undefined> {
if (ethAddress === "0x") {
return +envConfig.creatorAccountId;
return +gwConfig.accounts.polyjuiceCreator.id;
}

if (ethAddress === ZERO_ETH_ADDRESS) {
Expand All @@ -111,9 +112,9 @@ export async function ethAddressToAccountId(

export function ethEoaAddressToScriptHash(address: string) {
const script: Script = {
code_hash: envConfig.ethAccountLockHash,
code_hash: gwConfig.eoaScripts.eth.typeHash,
hash_type: "type",
args: envConfig.rollupTypeHash + address.slice(2),
args: gwConfig.rollupCell.typeHash + address.slice(2),
};
const scriptHash = utils.computeScriptHash(script);
return scriptHash;
Expand Down
11 changes: 0 additions & 11 deletions packages/api-server/src/base/env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,7 @@ dotenv.config({ path: "./.env" });

export const envConfig = {
databaseUrl: getRequired("DATABASE_URL"),
ethAccountLockHash: getRequired("ETH_ACCOUNT_LOCK_HASH"),
rollupTypeHash: getRequired("ROLLUP_TYPE_HASH"),
godwokenJsonRpc: getRequired("GODWOKEN_JSON_RPC"),
creatorAccountId: getRequired("CREATOR_ACCOUNT_ID"),
chainId: getRequired("CHAIN_ID"),
defaultFromId: getRequired("DEFAULT_FROM_ID"),
l2SudtValidatorScriptTypeHash: getRequired(
"L2_SUDT_VALIDATOR_SCRIPT_TYPE_HASH"
),
ethAddressRegistryAccountId: getRequired("ETH_ADDRESS_REGISTRY_ACCOUNT_ID"),
polyjuiceValidatorTypeHash: getOptional("POLYJUICE_VALIDATOR_TYPE_HASH"),
rollupConfigHash: getOptional("ROLLUP_CONFIG_HASH"),
newRelicLicenseKey: getOptional("NEW_RELIC_LICENSE_KEY"),
clusterCount: getOptional("CLUSTER_COUNT"),
redisUrl: getOptional("REDIS_URL"),
Expand Down
Loading

0 comments on commit b97b1a5

Please sign in to comment.