Skip to content

Commit

Permalink
Merge pull request bitrocks#38 from nervosnetwork/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
classicalliu authored Aug 6, 2021
2 parents ba7d4f4 + 44b15f4 commit 2940599
Show file tree
Hide file tree
Showing 26 changed files with 1,453 additions and 1,958 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"argsIgnorePattern": "^_|^args$",
"varsIgnorePattern": "^_"
}
]
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn install
- run: yarn workspace @godwoken-web3/godwoken tsc
- run: yarn workspace @godwoken-web3/api-server tsc
- run: yarn workspaces run fmt
- run: yarn workspaces run lint
- run: yarn run build
- run: yarn run fmt
- run: yarn run lint
- run: git diff --exit-code
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ $ yarn workspace @godwoken-web3/api-server reset_database
### Start API server

```
yarn workspace @godwoken-web3/godwoken tsc
yarn workspace @godwoken-web3/api-server start
yarn run build:godwoken
yarn run start
```

## Web3 RPC Modules
Expand Down
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
"license": "MIT",
"private": true,
"workspaces": [
"packages/godwoken",
"packages/api-server",
"packages/*"
],
"scripts": {
"build:godwoken": "yarn workspace @godwoken-web3/godwoken run build",
"build:api-server": "yarn workspace @godwoken-web3/api-server run build",
"build": "yarn workspaces run build",
"fmt": "yarn workspaces run fmt",
"lint": "yarn workspaces run lint",
"start": "yarn workspace @godwoken-web3/api-server run start"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
Expand Down
23 changes: 12 additions & 11 deletions packages/api-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
"test": "ava",
"fmt": "prettier --write \"{migrations,tests}/**/*.js\" app.js src/* package.json",
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
"reset_database": "knex migrate:down && knex migrate:up && knex seed:run"
"reset_database": "knex migrate:down && knex migrate:up && knex seed:run",
"build": "tsc"
},
"dependencies": {
"@ckb-lumos/base": "^0.16.0",
"@godwoken-web3/godwoken": "0.2.0-rc1",
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.1",
"ava": "^3.15.0",
"blake2b": "2.1.3",
"ckb-js-toolkit": "^0.10.2",
"concurrently": "^6.0.0",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
Expand All @@ -27,18 +24,22 @@
"immutable": "^4.0.0-rc.12",
"jayson": "~3.4.4",
"keccak256": "^1.0.2",
"knex": "^0.21.19",
"knex": "^0.95.7",
"leveldown": "^6.0.0",
"levelup": "^5.0.1",
"morgan": "~1.9.1",
"nodemon": "^2.0.7",
"pg": "^8.5.1",
"rlp": "^2.2.6",
"secp256k1": "^4.0.2",
"ts-node": "^9.1.1",
"typescript": "^4.2.2"
"secp256k1": "^4.0.2"
},
"devDependencies": {
"@types/secp256k1": "^4.0.2"
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.1",
"@types/secp256k1": "^4.0.2",
"ava": "^3.15.0",
"concurrently": "^6.0.0",
"nodemon": "^2.0.7",
"ts-node": "^9.1.1",
"typescript": "^4.2.2"
}
}
29 changes: 29 additions & 0 deletions packages/api-server/src/base/env-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { env } from "process";
import dotenv from "dotenv";

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"),
defaultFromAddress: getRequired("DEFAULT_FROM_ADDRESS"),
polyjuiceValidatorTypeHash: getOptional("POLYJUICE_VALIDATOR_TYPE_HASH"),
rollupConfigHash: getOptional("ROLLUP_CONFIG_HASH"),
};

function getRequired(name: string): string {
const value = env[name];
if (value == null) {
throw new Error(`no env ${name} provided`);
}

return value;
}

function getOptional(name: string): string | undefined {
return env[name];
}
10 changes: 10 additions & 0 deletions packages/api-server/src/base/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function defaultLogger(level: string, ...messages: any[]) {
console.log(`[${level}] `, ...messages);
}

export const logger = {
debug: (...args: any[]) => defaultLogger("debug", ...args),
info: (...args: any[]) => defaultLogger("info", ...args),
warn: (...args: any[]) => defaultLogger("warn", ...args),
error: (...args: any[]) => defaultLogger("error", ...args),
};
70 changes: 70 additions & 0 deletions packages/api-server/src/base/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Hash, HexNumber, HexString } from "@ckb-lumos/base";

export interface EthTransaction {
hash: Hash;
// when pending, blockNumber & blockHash = null
blockHash: Hash | null;
blockNumber: HexNumber | null;
transactionIndex: HexNumber;
from: HexString;
to: HexString | null;
gas: HexNumber;
gasPrice: HexNumber;
input: HexString;
nonce: HexNumber;
value: HexNumber;
v: HexNumber;
r: HexString;
s: HexString;
}

export interface EthBlock {
// when pending, number & hash & nonce & logsBloom = pending
number: HexNumber | null;
hash: Hash;
parentHash: Hash;
gasLimit: HexNumber;
gasUsed: HexNumber;
miner: HexString;
size: HexNumber;
logsBloom: HexString;
transactions: (EthTransaction | Hash)[];
timestamp: HexNumber;
mixHash: Hash;
nonce: HexNumber;
stateRoot: Hash;
sha3Uncles: Hash;
receiptsRoot: Hash;
transactionsRoot: Hash;
uncles: [];
totalDifficulty: HexNumber;
extraData: HexString;
}

export interface EthTransactionReceipt {
transactionHash: Hash;
transactionIndex: HexNumber;
blockHash: Hash;
blockNumber: HexNumber;
from: HexString;
to: HexString | null;
gasUsed: HexNumber;
cumulativeGasUsed: HexNumber;
logsBloom: HexString;
logs: EthLog[];
contractAddress: HexString | null;
status: HexNumber;
}

export interface EthLog {
// when pending logIndex, transactionIndex, transactionHash, blockHash, blockNumber = null
address: HexString;
blockHash: Hash | null;
blockNumber: HexNumber | null;
transactionIndex: HexNumber | null;
transactionHash: Hash | null;
data: HexString;
logIndex: HexNumber | null;
topics: HexString[];
removed: boolean;
}
149 changes: 149 additions & 0 deletions packages/api-server/src/base/types/uint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { HexNumber, HexString } from "@ckb-lumos/base";

export function toHexNumber(num: number | bigint): HexNumber {
return "0x" + num.toString(16);
}

export class Uint32 {
private value: number;

public static MIN = 0;
public static MAX = 2 ** 32 - 1;

constructor(value: number) {
if (typeof value !== "number") {
throw new Error("Uint32 value must be a number!");
}
if (value < Uint32.MIN || value > Uint32.MAX) {
throw new Error("value to small or too big");
}
this.value = value;
}

public getValue(): number {
return this.value;
}

public toHex(): HexNumber {
return toHexNumber(this.value);
}

public static fromHex(value: HexNumber): Uint32 {
assertHexNumber("Uint32.fromHex args", value);
return new Uint32(+value);
}

public toLittleEndian(): HexString {
const buf = Buffer.alloc(4);
buf.writeUInt32LE(this.value);
return `0x${buf.toString("hex")}`;
}

public static fromLittleEndian(hex: HexString): Uint32 {
assertHexNumber("Uint32.fromLittleEndian args", hex);
if (hex.length !== 10 || !hex.startsWith("0x")) {
throw new Error(`little endian hex format error`);
}
const buf = Buffer.from(hex.slice(2), "hex");
const num = buf.readUInt32LE();
return new Uint32(num);
}
}

export class Uint64 {
private value: bigint;

public static MIN = 0;
public static MAX = 2n ** 64n - 1n;

constructor(value: bigint) {
if (typeof value !== "bigint") {
throw new Error("Uint64 value must be a bigint!");
}
if (value < Uint64.MIN || value > Uint64.MAX) {
throw new Error("value to small or too big");
}
this.value = value;
}

public getValue(): bigint {
return this.value;
}

public toHex(): HexNumber {
return toHexNumber(this.value);
}

public static fromHex(value: HexNumber): Uint64 {
assertHexNumber("Uint64.fromHex args", value);
return new Uint64(BigInt(value));
}

public toLittleEndian(): HexString {
const buf = Buffer.alloc(8);
buf.writeBigUInt64LE(this.value);
return `0x${buf.toString("hex")}`;
}

public static fromLittleEndian(hex: HexNumber): Uint64 {
assertHexNumber("Uint64.fromLittleEndian args", hex);
if (hex.length !== 18 || !hex.startsWith("0x")) {
throw new Error(`little endian hex format error`);
}
const buf = Buffer.from(hex.slice(2), "hex");
const num = buf.readBigUInt64LE();
return new Uint64(num);
}
}

export class Uint128 {
private value: bigint;

public static MIN: bigint = 0n;
public static MAX: bigint = 2n ** 128n - 1n;

constructor(value: bigint) {
if (typeof value !== "bigint") {
throw new Error("Uint128 value must be a bigint!");
}
if (value < Uint128.MIN || value > Uint128.MAX) {
throw new Error("value to small or too big");
}
this.value = value;
}

public getValue(): bigint {
return this.value;
}

public toHex(): HexNumber {
return toHexNumber(this.value);
}

public static fromHex(value: HexNumber): Uint128 {
assertHexNumber("Uint128.fromHex args", value);
return new Uint128(BigInt(value));
}

public toLittleEndian(): HexString {
const buf = Buffer.alloc(16);
buf.writeBigUInt64LE(this.value & BigInt("0xFFFFFFFFFFFFFFFF"), 0);
buf.writeBigUInt64LE(this.value >> BigInt(64), 8);
return "0x" + buf.toString("hex");
}

public static fromLittleEndian(hex: HexNumber): Uint128 {
if (hex.length !== 34 || !hex.startsWith("0x")) {
throw new Error(`little endian hex format error`);
}
const buf = Buffer.from(hex.slice(2, 34), "hex");
const num = (buf.readBigUInt64LE(8) << BigInt(64)) + buf.readBigUInt64LE(0);
return new Uint128(num);
}
}

function assertHexNumber(debugPath: string, str: string) {
if (!/^0x(0|[0-9a-fA-F]+)$/.test(str)) {
throw new Error(`${debugPath} must be a hex number!`);
}
}
Loading

0 comments on commit 2940599

Please sign in to comment.