Skip to content

Commit

Permalink
fix: language of code blocks in design pattern (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaoImin authored Jul 17, 2023
1 parent dd0a9c3 commit 039ec68
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions docs/getting-started/for-contributor/design_pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Let's use the MemPool module as an example.

### MemPoolAdapter
This is the abstract trait for the MemPool adapter, which declares all the external interfaces required by the MemPool module. The specific implementation is as follows:
```Rust

```rust
pub trait MemPoolAdapter: Send + Sync {
async fn pull_txs(
&self,
Expand All @@ -56,13 +57,13 @@ pub trait MemPoolAdapter: Send + Sync {
) -> ProtocolResult<U256>;

async fn check_transaction(&self, ctx: Context, tx: &SignedTransaction) -> ProtocolResult<()>;

...
}
```
### DefaultMemPoolAdapter
DefaultMemPoolAdapter is the actual implementation of the MempoolAdapter in the Axon code, as shown below:
```Rust

```rust
pub struct DefaultMemPoolAdapter<C, N, S, DB, I> {
network: N,
storage: Arc<S>,
Expand Down Expand Up @@ -168,6 +169,7 @@ where
Ok(tx.transaction.unsigned.nonce() - account.nonce)
}
...
}
```
You can see that some interface implementations are relatively complex, such as `check_authorization`.

Expand All @@ -185,7 +187,8 @@ The class diagram suggests that, except for DefaultMemPoolAdapter, the remaining

### Simplified Testing
MemPoolAdapter also facilitates testing. As shown below, the HashMemPoolAdapter implemented tested is much simpler than the actual implementation of HashMemPoolAdapter, which is DefaultMemPoolAdapter.
```Rust

```rust
pub struct HashMemPoolAdapter {
network_txs: DashMap<Hash, SignedTransaction>,
}
Expand Down Expand Up @@ -232,14 +235,15 @@ impl MemPoolAdapter for HashMemPoolAdapter {
) -> ProtocolResult<U256> {
Ok(U256::zero())
}
...
...
}
```
The adapter pattern effectively encapsulates external modules like Storage and Network. Comparing it with DefaultMemPoolAdapter, we can see that the implementation of HashMemPoolAdapter is straightforward. For instance, in transaction pool module testing, there may be no specific requirements for the `check_authorization` interface. In such cases, we can simply return Ok(U256::zero()), significantly simplifying the testing code.

## Implementation of MemPool Module
As explained earlier, the MemPool module abstracts other modules (such as Network and Storage) as traits. Similarly, MemPool module also provides services to other modules and requires its own abstraction. Specifically, it exposes the MemPool abstraction to interact with others. Here is the code for reference:

```Rust
```rust
pub trait MemPool: Send + Sync {
async fn insert(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

Expand Down Expand Up @@ -274,6 +278,7 @@ pub trait MemPool: Send + Sync {
async fn get_tx_count_by_address(&self, ctx: Context, address: H160) -> ProtocolResult<usize>;

fn get_tx_from_mem(&self, ctx: Context, tx_hash: &Hash) -> Option<SignedTransaction>;

fn set_args(&self, context: Context, state_root: MerkleRoot, gas_limit: u64, max_tx_size: u64);
}
```
Expand Down

1 comment on commit 039ec68

@vercel
Copy link

@vercel vercel bot commented on 039ec68 Jul 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

axon-docs – ./

axon-docs.vercel.app
axon-docs-axonweb3.vercel.app
axon-docs-git-main-axonweb3.vercel.app

Please sign in to comment.