From 58817969f4774aea29e4858c6021413e7ff5fe22 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 8 Mar 2022 16:12:52 +0800 Subject: [PATCH 1/3] fix(indexer): support compatible_chain_id for calculating eth_tx_hash --- crates/indexer/src/config.rs | 2 ++ crates/indexer/src/indexer.rs | 5 ++++- crates/indexer/src/runner.rs | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/indexer/src/config.rs b/crates/indexer/src/config.rs index 392be7a0..fe4796a7 100644 --- a/crates/indexer/src/config.rs +++ b/crates/indexer/src/config.rs @@ -14,6 +14,7 @@ pub struct IndexerConfig { pub godwoken_rpc_url: String, pub ws_rpc_url: String, pub pg_url: String, + pub compatible_chain_id: u64, pub sentry_dsn: Option, pub sentry_environment: Option, } @@ -45,6 +46,7 @@ impl Display for IndexerConfig { write!(f, "godwoken_rpc_url: {}, ", self.godwoken_rpc_url)?; write!(f, "ws_rpc_url: {}, ", self.ws_rpc_url)?; write!(f, "pg_url: {}", self.pg_url)?; + write!(f, "compatible_chain_id: {}", self.compatible_chain_id)?; if let Some(t) = &self.sentry_dsn { write!(f, "sentry_dsn: {}, ", t)?; } else { diff --git a/crates/indexer/src/indexer.rs b/crates/indexer/src/indexer.rs index 5bdaa3e3..e7f69058 100644 --- a/crates/indexer/src/indexer.rs +++ b/crates/indexer/src/indexer.rs @@ -32,6 +32,7 @@ pub struct Web3Indexer { rollup_type_hash: H256, allowed_eoa_hashes: HashSet, godwoken_rpc_client: GodwokenRpcClient, + compatible_chain_id: u64, } impl Web3Indexer { @@ -42,6 +43,7 @@ impl Web3Indexer { eth_account_lock_hash: H256, tron_account_lock_hash: Option, gw_rpc_url: &str, + compatible_chain_id: u64, ) -> Self { let mut allowed_eoa_hashes = HashSet::default(); allowed_eoa_hashes.insert(eth_account_lock_hash); @@ -56,6 +58,7 @@ impl Web3Indexer { rollup_type_hash, allowed_eoa_hashes, godwoken_rpc_client, + compatible_chain_id, } } @@ -260,7 +263,7 @@ impl Web3Indexer { (Some(address), polyjuice_chain_id) }; // calculate chain_id - let chain_id: u64 = polyjuice_chain_id as u64; + let chain_id: u64 = (self.compatible_chain_id << 32) | (polyjuice_chain_id as u64); let nonce: u32 = l2_transaction.raw().nonce().unpack(); let input = polyjuice_args.input.clone().unwrap_or_default(); diff --git a/crates/indexer/src/runner.rs b/crates/indexer/src/runner.rs index 9f3851ac..b035a450 100644 --- a/crates/indexer/src/runner.rs +++ b/crates/indexer/src/runner.rs @@ -22,6 +22,7 @@ impl Runner { config.eth_account_lock_hash, config.tron_account_lock_hash, config.godwoken_rpc_url.as_str(), + config.compatible_chain_id, ); let godwoken_rpc_client = GodwokenRpcClient::new(config.godwoken_rpc_url.as_str()); let runner = Runner { From 63e5fd233e5d3368b5b5fc8cb1ed1ab586dfacd3 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Tue, 8 Mar 2022 22:33:23 +0800 Subject: [PATCH 2/3] refactor: replace chainId with compatible_chain_id in env-config --- README.md | 3 ++- packages/api-server/src/base/env-config.ts | 17 ++++++++++++++++- packages/api-server/src/methods/modules/poly.ts | 4 ++-- scripts/generate-indexer-config.js | 7 ++++++- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 78991ad5..007cdfc8 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ GODWOKEN_READONLY_JSON_RPC= ETH_ACCOUNT_LOCK_HASH= ROLLUP_TYPE_HASH= ROLLUP_CONFIG_HASH= -CHAIN_ID= +COMPATIBLE_CHAIN_ID= CREATOR_ACCOUNT_ID= DEFAULT_FROM_ADDRESS= DEFAULT_FROM_ID= @@ -89,6 +89,7 @@ tron_account_lock_hash= godwoken_rpc_url= ws_rpc_url= pg_url="postgres://username:password@localhost:5432/your_db" +compatible_chain_id= EOF ``` diff --git a/packages/api-server/src/base/env-config.ts b/packages/api-server/src/base/env-config.ts index dc8153c7..d1b1e357 100644 --- a/packages/api-server/src/base/env-config.ts +++ b/packages/api-server/src/base/env-config.ts @@ -9,7 +9,11 @@ export const envConfig = { rollupTypeHash: getRequired("ROLLUP_TYPE_HASH"), godwokenJsonRpc: getRequired("GODWOKEN_JSON_RPC"), creatorAccountId: getRequired("CREATOR_ACCOUNT_ID"), - chainId: getRequired("CHAIN_ID"), + compatibleChainId: getRequired("COMPATIBLE_CHAIN_ID"), + chainId: calculateChainId( + +getRequired("CREATOR_ACCOUNT_ID"), + +getRequired("COMPATIBLE_CHAIN_ID") + ), defaultFromAddress: getRequired("DEFAULT_FROM_ADDRESS"), defaultFromId: getRequired("DEFAULT_FROM_ID"), l2SudtValidatorScriptTypeHash: getRequired( @@ -41,3 +45,14 @@ function getRequired(name: string): string { function getOptional(name: string): string | undefined { return env[name]; } + +export function calculateChainId( + creator_id: number, + compatible_chain_id: number +): string { + const chain_id_num = compatible_chain_id * Math.pow(2, 32) + creator_id; + console.log( + `web3 chain_id: ${chain_id_num}, calculating from compatible_chain_id: ${compatible_chain_id}, creator_id: ${creator_id}` + ); + return BigInt(chain_id_num).toString(10); +} diff --git a/packages/api-server/src/methods/modules/poly.ts b/packages/api-server/src/methods/modules/poly.ts index cd31a766..332f3244 100644 --- a/packages/api-server/src/methods/modules/poly.ts +++ b/packages/api-server/src/methods/modules/poly.ts @@ -10,7 +10,7 @@ export class Poly { try { const creatorIdHex = toHexNumber(BigInt(envConfig.creatorAccountId)); return creatorIdHex; - } catch (err) { + } catch (err: any) { throw new Web3Error(err.message); } } @@ -59,7 +59,7 @@ export class Poly { chainId: envConfig.chainId || null, }; return chainInfo; - } catch (error) { + } catch (error: any) { throw new Web3Error(error.message); } } diff --git a/scripts/generate-indexer-config.js b/scripts/generate-indexer-config.js index 517689ed..ccf2c0be 100644 --- a/scripts/generate-indexer-config.js +++ b/scripts/generate-indexer-config.js @@ -16,6 +16,7 @@ let config = { tron_account_lock_hash: process.env.TRON_ACCOUNT_LOCK_HASH, godwoken_rpc_url: process.env.GODWOKEN_JSON_RPC, pg_url: process.env.DATABASE_URL, + compatible_chain_id: process.env.COMPATIBLE_CHAIN_ID, sentry_dsn: process.env.SENTRY_DNS, sentry_environment: process.env.SENTRY_ENVIRONMENT, } @@ -28,8 +29,12 @@ let tomlStr = ""; for (const [key, value] of Object.entries(config)) { console.log(`[${key}]: ${value}`) + if(value != null && key === "compatible_chain_id"){ + tomlStr += `${key}=${Number(value)}\n`; + continue; + } if (value != null) { - tomlStr += `${key}="${value}"\n` + tomlStr += `${key}="${value}"\n`; } } From 377bbcea8edbbe5c4cd77de52fd1ef2041ff1cf7 Mon Sep 17 00:00:00 2001 From: RetricSu Date: Wed, 9 Mar 2022 11:21:30 +0800 Subject: [PATCH 3/3] apply review suggestion Co-authored-by: CL --- packages/api-server/src/base/env-config.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/api-server/src/base/env-config.ts b/packages/api-server/src/base/env-config.ts index d1b1e357..6da2a94b 100644 --- a/packages/api-server/src/base/env-config.ts +++ b/packages/api-server/src/base/env-config.ts @@ -47,12 +47,12 @@ function getOptional(name: string): string | undefined { } export function calculateChainId( - creator_id: number, - compatible_chain_id: number + creatorId: number, + compatibleChainId: number ): string { - const chain_id_num = compatible_chain_id * Math.pow(2, 32) + creator_id; + const chainId = (BigInt(compatibleChainId) << 32n) + BigInt(creatorId); console.log( - `web3 chain_id: ${chain_id_num}, calculating from compatible_chain_id: ${compatible_chain_id}, creator_id: ${creator_id}` + `web3 chain_id: ${chainId}, calculating from compatible_chain_id: ${compatibleChainId}, creator_id: ${creatorId}` ); - return BigInt(chain_id_num).toString(10); + return chainId.toString(10); }