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

refactor: get config from godwoken rpc instead of env #239

Merged
merged 23 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ecc3183
refactor: get config from godwoken rpc instead of env
RetricSu Mar 28, 2022
32d5464
chore: merge compatibility branch
RetricSu Apr 22, 2022
0918072
refactor: replace compatible id with chain id
RetricSu Apr 22, 2022
d633917
refactor: don't init gw-config for unit test
RetricSu Apr 22, 2022
30cceb9
refator: use gwConfig to pass test
RetricSu Apr 25, 2022
f89ae78
fix: don't export gwConfig instance from class
RetricSu Apr 25, 2022
840aaac
chore: update indexer-config.toml generate script
RetricSu Apr 25, 2022
730ec9f
refactor: rename RollupCell script to typeScript
RetricSu Apr 28, 2022
01b2b2e
chore: update unit test
RetricSu Apr 28, 2022
d3a2224
fix: rm repeated fmt in ci
RetricSu Apr 29, 2022
10c176f
refactor: use + instead of parseInt
RetricSu Apr 29, 2022
b478e15
chore: make sure gwConfig init successful before start server
RetricSu Apr 29, 2022
4bb3bf2
refactor: gwConfig class via review suggestion
RetricSu Apr 29, 2022
1f8eb11
refactor: hash type in node-info
RetricSu Apr 29, 2022
7cd0c47
chore: rm un-need import path index in./base/
RetricSu Apr 29, 2022
b75ecba
fix: update net.listening method
RetricSu Apr 29, 2022
83a35b1
chore: rm ||
RetricSu Apr 29, 2022
ad248f7
chore: clean code
RetricSu Apr 29, 2022
2fa988f
chore: apply review suggestion for builtin reg id
RetricSu May 5, 2022
446a5e5
chore: use computeScriptHash instead of custom serialize func
RetricSu May 5, 2022
2270bd7
fix: omit unused type in gw-config
RetricSu May 5, 2022
2d40b71
Merge branch 'compatibility-breaking-changes' into refactor-config
RetricSu May 7, 2022
f784fbb
fix: poly_getDefaultFromId return type
RetricSu May 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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/utils
- run: yarn run fmt
RetricSu marked this conversation as resolved.
Show resolved Hide resolved
- 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 @@ -75,11 +68,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