# RPC API
The **RPC (Remote Procedure Call) API** is the gateway for the outside world to interact with the pETHit node. It allows external clients (like wallets or block explorers) to submit transactions, query account balances, and inspect the blockchain state.
## Current Architecture (v0.3.0)
In this iteration, the RPC layer evolved to support **Binary Data Transport**. Since transactions are now complex cryptographic objects signed by wallets, sending them as simple JSON objects (`{"to": "..."}`) is no longer sufficient.
Instead, we follow the industry standard: **Encode locally, broadcast raw.**
### 1. Hex & RLP Handling
The RPC now acts as a translation layer:
* **Input:** It receives a Hexadecimal string representing the raw bytes of a transaction (RLP encoded).
* **Processing:** It decodes the Hex into bytes, then uses the `Execution` crate to decode those bytes into a `SignedTransaction` struct.
* **Output:** It pushes the valid Rust struct into the `TxPool`.
### 2. State Querying
To support the new Wallet (which needs to know the current nonce to sign transactions correctly), the RPC now exposes direct access to the `Storage` via specific read-only endpoints.
## API / Usage
The server listens on `http://127.0.0.1:8000` by default.
### Endpoints
#### 1. `POST /send_tx`
Broadcasts a signed transaction to the network.
* **Input:** A JSON object containing the raw RLP-encoded hex string of the signed transaction.
```json
{
"raw_tx": "0xf86d808502540be40082520894000000000000000000000000000000000000000164801ba0..."
}
```
* **Process:**
1. Strips the `0x` prefix.
2. Decodes Hex -> Bytes.
3. Decodes Bytes (RLP) -> `SignedTransaction`.
4. Calculates the Transaction Hash (`Keccak256`).
5. Adds to the Mempool.
* **Response:**
* `200 OK`: "Transaction received and added to pool!"
* `400 Bad Request`: If RLP decoding fails.
#### 2. `POST /get_account`
Retrieves the state of a specific address. Essential for wallets to sync their `nonce`.
* **Input:**
```json
{
"raw_acc": "0x71C7656EC7ab88b098defB751B7401B5f6d8976F"
}
```
* **Process:** Queries the `SharedStorage`. If the account doesn't exist, it returns default values (Nonce 0, Balance 0).
* **Response:**
```json
{
"address": "0x71C7656EC7ab88b098defB751B7401B5f6d8976F",
"nonce": 5,
"balance": "100"
}
```
#### 3. `POST /get_block` (Legacy)
Retrieves a block by its hash.
* **Input:** `{"hash": "0x..."}`.
* **Response:** The block details or an error if not found.
## Evolution Log
### v0.1.0: The Simple Server
* **Protocol:** Pure REST (`GET /tx`, `POST /tx`).
* **Payload:** Simple JSON objects (`key`, `value`).
* **Logic:** Passed data directly to the pool without decoding or validation.
### v0.2.0: Block Querying
* **Additions:** Added the `/get_block` endpoint.
* **Logic:** Integrated with `SharedChain` to read history.
### v0.3.0: The Hex Update
* **Protocol Change:** Switched input format to **Raw Hex Strings** to support the new `SignedTransaction` format.
* **New Endpoint:** Added `/get_account` to allow external wallets to fetch nonces.
* **Dependency:** Added `alloy-rlp` and `hex` handling to bridge the gap between JSON (Network) and RLP (Internal).