Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: the all-in-one file of core-run #1349

Merged
merged 8 commits into from
Sep 1, 2023
Merged

Conversation

yangby-cryptape
Copy link
Collaborator

@yangby-cryptape yangby-cryptape commented Aug 23, 2023

What this PR does / why we need it?

This PR refactors the all-in-one file of core-run:

  • Split the all-in-one file of core-run into several files.

    The original file is the 2nd largest file in the whole repository.

    It has 1000+ lines and 70+ lines for importing items.

  • Split the "huge" struct Axon.

    Examples:

    • 👉 Disallow "one-time" field, i.e., don't save intermediate data in global.

      Should we have to refresh the fields that only used once?
      We don't have to concern that if there is no "one-time" fields.

      • In Axon, the spec (only has the accounts field), genesis and state_root are "one-time" fields.

        They are only used when boot the services.

        axon/core/run/src/lib.rs

        Lines 84 to 90 in 0ed2ce6

        pub struct Axon {
        version: String,
        config: Config,
        spec: ChainSpec,
        genesis: RichBlock,
        state_root: MerkleRoot,
        }

        Especially state_root, it's just an intermediate data which only used once.
        And, it can be replaced with self.genesis.block.header.state_root.

        self.state_root = resp.state_root;

        axon/core/run/src/lib.rs

        Lines 575 to 579 in 0ed2ce6

        last_state_root: if header.number == 0 {
        self.state_root
        } else {
        header.state_root
        },

    • 👉 If a method only use self.aaa.bbb.ccc, then should not use self as the parameter.

      This will result in that's hard to detect minimal dependencies for class methods, so that:

      • The method is hard to reusable.
      • If writes unit tests, it has to construct a huge Self with many dummy fields.
  • Remove all default unspecified addresses.

    Examples:

    • 👉 The jaeger service listens 0.0.0.0:6831 as default.

      axon/core/run/src/lib.rs

      Lines 764 to 767 in 0ed2ce6

      let tracing_address = match jaeger_config.tracing_address {
      Some(address) => address,
      None => std::net::SocketAddr::from(([0, 0, 0, 0], 6831)),
      };

  • Remove lots of unnecessary unrwap().

    Examples:

    • 👉 The caller function already handle the errors.

      axon/core/run/src/lib.rs

      Lines 739 to 755 in 0ed2ce6

      fn register_rpc<K: KeyProvider>(&self, network_service: &mut NetworkService<K>) {
      network_service
      .register_rpc_response(RPC_RESP_PULL_TXS)
      .unwrap();
      network_service
      .register_rpc_response(RPC_RESP_PULL_TXS_SYNC)
      .unwrap();
      network_service
      .register_rpc_response(RPC_RESP_SYNC_PULL_BLOCK)
      .unwrap();
      network_service
      .register_rpc_response(RPC_RESP_SYNC_PULL_PROOF)
      .unwrap();
      network_service
      .register_rpc_response(RPC_RESP_SYNC_PULL_TXS)
      .unwrap();
      }

      axon/core/run/src/lib.rs

      Lines 254 to 259 in 0ed2ce6

      pub async fn start<K: KeyProvider>(
      self,
      key_provider: Option<K>,
      storage: Arc<ImplStorage<RocksAdapter>>,
      inner_db: Arc<RocksDB>,
      ) -> ProtocolResult<()> {

  • Use latest block if possible.

    • 👉 When initialize MetaData.

      The following code is executed when axon starts:

      axon/core/run/src/lib.rs

      Lines 316 to 323 in 2877191

      // Get the validator list from current metadata for consensus initialization
      let metadata_root = AxonExecutorReadOnlyAdapter::from_root(
      current_state_root,
      Arc::clone(&trie_db),
      Arc::clone(&storage),
      Proposal::new_without_state_root(&self.genesis.block.header).into(),
      )?
      .get_metadata_root();

      Modified as the following code:

        // Get the validator list from current metadata for consensus initialization 
        let metadata_root = AxonExecutorReadOnlyAdapter::from_root( 
            current_state_root, 
            Arc::clone(&trie_db), 
            Arc::clone(&storage), 
      -     Proposal::new_without_state_root(&self.genesis.block.header).into(), 
      +     Proposal::new_without_state_root(&current_block.header).into(), 
        )? 
    • 👉 When initialize MemPool.

      The following code is executed when axon starts:

      axon/core/run/src/lib.rs

      Lines 436 to 445 in 2877191

      let mempool_adapter = DefaultMemPoolAdapter::<Secp256k1, _, _, _, InteroperationImpl>::new(
      network_service.clone(),
      Arc::clone(storage),
      Arc::clone(trie_db),
      self.genesis.block.header.chain_id,
      self.genesis.block.header.gas_limit.as_u64(),
      self.config.mempool.pool_size as usize,
      self.config.mempool.broadcast_txs_size,
      self.config.mempool.broadcast_txs_interval,
      );

      Modified as the following code:

        let mempool_adapter = DefaultMemPoolAdapter::<Secp256k1, _, _, _, InteroperationImpl>::new(
            network_service.clone(),
            Arc::clone(storage),
            Arc::clone(trie_db),
      -     self.genesis.block.header.chain_id,
      -     self.genesis.block.header.gas_limit.as_u64(),
      +     current_block.header.chain_id,
      +     current_block.header.gas_limit.as_u64(),
            self.config.mempool.pool_size as usize,
            self.config.mempool.broadcast_txs_size,
            self.config.mempool.broadcast_txs_interval,
        );

Major Changes

  • The jaeger service won't be started if no tracing_address is set in the configuration file.

    No default address anymore.

What is the impact of this PR?

✅ No Breaking Change

PR relation:

CI Settings

CI Usage

Tip: Check the CI you want to run below, and then comment /run-ci.

CI Switch

  • Cargo Clippy
  • E2E Tests
  • Code Format
  • Unit Tests
  • Web3 Compatible Tests
  • OCT 1-5 And 12-15
  • OCT 6-10
  • OCT 11
  • OCT 16-19
  • v3 Core Tests

CI Description

CI Name Description
Chaos CI Test the liveness and robustness of Axon under terrible network condition
Cargo Clippy Run cargo clippy --all --all-targets --all-features
Coverage Test Get the unit test coverage report
E2E Test Run end-to-end test to check interfaces
Code Format Run cargo +nightly fmt --all -- --check and cargo sort -gwc
Web3 Compatible Test Test the Web3 compatibility of Axon
v3 Core Test Run the compatibility tests provided by Uniswap V3
OCT 1-5 | 6-10 | 11 | 12-15 | 16-19 Run the compatibility tests provided by OpenZeppelin

@Flouse
Copy link
Contributor

Flouse commented Aug 23, 2023

  • The jaeger service won't be started if no tracing_address is set in the configuration file.
    No default address anymore.

cc @Simon-Tl


The prometheus service already applied the same change in #1348. -- update by @yangby-cryptape

@Flouse Flouse requested a review from Simon-Tl August 23, 2023 15:46
@yangby-cryptape yangby-cryptape force-pushed the yangby/refactor-core-run branch from d1154c5 to 7aad3a2 Compare August 29, 2023 15:46
@yangby-cryptape yangby-cryptape changed the title WIP refactor: the all-in-one file of core-run refactor: the all-in-one file of core-run Aug 29, 2023
@yangby-cryptape yangby-cryptape marked this pull request as ready for review August 29, 2023 15:49
@yangby-cryptape yangby-cryptape requested a review from a team as a code owner August 29, 2023 15:49
@yangby-cryptape yangby-cryptape force-pushed the yangby/refactor-core-run branch from 7aad3a2 to 62d01e3 Compare August 29, 2023 16:01
@yangby-cryptape yangby-cryptape requested review from Flouse, driftluo and KaoImin and removed request for felicityin August 29, 2023 16:05
@KaoImin

This comment was marked as off-topic.

@github-actions

This comment was marked as outdated.

@yangby-cryptape yangby-cryptape force-pushed the yangby/refactor-core-run branch from 62d01e3 to 68e2b0e Compare August 31, 2023 08:30
@yangby-cryptape

This comment was marked as off-topic.

@github-actions
Copy link

CI tests run on commit:

CI test list:

  • E2E Tests
  • OCT 1-5 And 12-15
  • OCT 6-10
  • OCT 11
  • OCT 16-19
  • v3 Core Tests
  • Web3 Compatible Tests

Please check ci test results later.

@KaoImin KaoImin enabled auto-merge August 31, 2023 08:45
@KaoImin KaoImin added this pull request to the merge queue Sep 1, 2023
Merged via the queue into main with commit c86f065 Sep 1, 2023
26 of 27 checks passed
@yangby-cryptape yangby-cryptape deleted the yangby/refactor-core-run branch September 1, 2023 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants