Skip to content

Commit

Permalink
🐛 fix rpc tests and docs (keep-starknet-strange#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt authored Sep 25, 2023
1 parent 2244ec1 commit e6dc108
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 88 deletions.
3 changes: 2 additions & 1 deletion .markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"MD045": false,
"MD003": false,
"MD013": {
"code_blocks": false
"code_blocks": false,
"line_length": 135
}
}
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
target
typescript-api
cairo-contracts/build
madara-app
madara-dev-explorer
madara-docs
madara-infra
madara-infra
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{
"eslint.workingDirectories": ["tests"]
"eslint.workingDirectories": ["tests"],
"workbench.colorCustomizations": {
"activityBar.background": "#561529",
"titleBar.activeBackground": "#781E3A",
"titleBar.activeForeground": "#FEFBFC"
}
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
- ci: increase threshold for codecov to 1%
- test: add `starknet-rpc-test` crate to the workspace
- test: add test to check tx signed by OZ account can be signed with Argent pk
- buid: add rust-analyzer to toolchain components
- ci: increase threshold for codecov to 1%
- replace all calls to `transmute` by calls `from_raw_parts`
- big transaction type refactoring
- impl tx execution and verification as traits
- reduce the amount of data stored in the runtime and use the Substrate block to
as source of data in the client
- perf: use perfect hash function in calculate_l1_gas_by_vm_usage
- chore: add tests for tx hashing
- split `primitives` crates into multiple smaller crates
- fix: std feature import in transactions crate
- chore: improve logging about transaction when nonce is too high
- fix: rpc tests and background node run
- test: add tests for simulate tx offset
- test: add tests for tx hashing

Expand Down
38 changes: 19 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 43 additions & 45 deletions docs/rpc-contribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,60 +187,58 @@ everything is working as expected.

## Integration tests

Integration tests are located in the `tests` folder, and are written in
typescript. They are executed using `mocha` and `chai`. We use `starknet.js` to
interact with the blockchain and test compatibility with Starknet's tooling.
Integration tests are located in the `starknet-rpc-test` folder, and are written
in rust using `rstest`. We use `starknet-rs` to interact with the blockchain and
test compatibility with Starknet's tooling.

You can find the documentation on this
[link](https://www.starknetjs.com/docs/api/provider/rpcprovider/).

```typescript
// tests/tests/test-rpc/test-starknet-rpc.ts
import "@keep-starknet-strange/madara-api-augment";

import { expect } from "chai";

import { describeDevMadara } from "../../util/setup-dev-tests";
import { RpcProvider, validateAndParseAddress } from "starknet";

// `describeDevMadara` will run the node in the background on a random available port and provide you with some context objects.
describeDevMadara("Starknet RPC", (context) => {
let providerRPC: RpcProvider;

// We initialize the RPC provider to use the local spawned node before all tests.
before(async function () {
providerRPC = new RpcProvider({
nodeUrl: `http://127.0.0.1:${context.rpcPort}/`,
retries: 3,
});
});

/// ... other tests

it("my_endpoint", async function () {
// You can fetch the current block hash and number
let block = await providerRPC.getBlockHashAndNumber();
let block_hash = `0x${block.block_hash.slice(2).padStart(64, "0")}`;

// Call the new endpoint
let result = await providerRPC.myEndpoint({
some_str: "Madara",
some_u64: 1234,
});

// Make some assertions to ensure the right behavior
expect(result).to.equal("Let's build the future!");
});
});
[link](https://github.com/xJonathanLEI/starknet-rs).

```rust
#[rstest]
#[tokio::test]
async fn fail_non_existing_block(#[future] madara: MadaraClient) -> Result<(), anyhow::Error> {
// We retrieve the madara client
let madara = madara.await;

// We get the RPC Provider to interact with the madara node
let rpc = madara.get_starknet_client();

// Expected values
let test_contract_class_hash =
FieldElement::from_hex_be(TEST_CONTRACT_CLASS_HASH).expect("Invalid Contract Address");

// Assertions
assert_matches!(
rpc
.get_class(
BlockId::Number(100),
test_contract_class_hash,
)
.await,
Err(StarknetProviderError(StarknetErrorWithMessage { code: MaybeUnknownErrorCode::Known(code), .. })) if code == StarknetError::BlockNotFound
);

Ok(())
}
```

Recompile madara (with method 1 or 2 depending on your needs), and you should be
able to target your new endpoint.

### Run your integration tests

To run the tests, simply run `npm run test-seq` in the `tests/` folder. Make
sure you've ran `npm install` in the `tests/` folder before running the tests.
To run the tests, simply run
`cargo test -p starknet-rpc-test -- test <test_file> -- <test_name> --exact --nocapture --test-threads=1`.

For easier debugging make sure to enable the background node's logs with
`MADARA_LOG=true`.

e.g

```bash
MADARA_LOG=true cargo test --package starknet-rpc-test -- --exact --nocapture --test-threads=1
```

### Test locally

Expand Down
2 changes: 0 additions & 2 deletions starknet-rpc-test/get_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ async fn work_ok_retrieving_class_for_contract_version_0(#[future] madara: Madar
Ok(())
}

// TODO: remove "ignore" when https://github.com/keep-starknet-strange/madara/pull/992 is merged
#[ignore]
#[rstest]
#[tokio::test]
async fn work_ok_retrieving_class_for_contract_version_1(#[future] madara: MadaraClient) -> Result<(), anyhow::Error> {
Expand Down
4 changes: 1 addition & 3 deletions starknet-rpc-test/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ pub const FEE_TOKEN_ADDRESS: &str = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96
pub const TOKEN_CLASS_HASH: &str = "0x0000000000000000000000000000000000000000000000000000000000010000";
pub const ARGENT_CONTRACT_ADDRESS: &str = "0x0000000000000000000000000000000000000000000000000000000000000002";

// Taken from https://github.com/0xSpaceShard/starknet-devnet-rs/blob/main/crates/starknet-server/tests/common/mod.rs#L5
pub const MIN_PORT: u16 = 1025;
pub const MAX_PORT: u16 = 65_535;
pub const ENDING_PORT: u16 = 65535;

pub const MAX_U256: &str = "0xffffffffffffffffffffffffffffffff";
pub const MAX_FEE_OVERRIDE: &str = "0x100000";
Expand Down
Loading

0 comments on commit e6dc108

Please sign in to comment.