Skip to content

Commit

Permalink
Support sepolia network tests (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
DelevoXDG authored Jan 10, 2024
1 parent 66cf155 commit fa0cf07
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 202 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,16 @@ To select the network, please set the `NETWORK_TEST_NETWORK_NAME` environment va
- `GOERLI_INTEGRATION`

Please note that `GOERLI` networks are deprecated, and won't be supported in the future. The number of tests working on `SEPOLIA` is, however, temporarily limited.
You will also need to provide an **RPC node URL** and an **account address** (along with its **private key**).
To properly configure your network, ensure the following variables are set with the `NETWORK_NAME_` prefix:
- `RPC_URL` - url of your RPC node
- `ACCOUNT_ADDRESS` and `PRIVATE_KEY` - address and private key of your account
Additionally, you can also set:
- `CONST_NONCE_ACCOUNT_ADDRESS` and `CONST_NONCE_PRIVATE_KEY` - address and private key exclusively for non-gas network tests, preventing potential inconsistencies (sometimes, `getNonce` may report higher nonce than expected).
Recommended for reliable non-gas testing.
These default to `ACCOUNT_ADDRESS` and `PRIVATE_KEY` if not set.
- `ACCOUNT_CAIRO_VERSION` - Cairo version of the `ACCOUNT_ADDRESS` and `CONST_NONCE_ACCOUNT_ADDRESS` accounts. Defaults to `0`.
Network tests are disabled by default. To enable them, you can set the environment variable:
```env
NETWORK_TEST_MODE=non_gas
Expand All @@ -205,12 +214,7 @@ Alternatively, you can use flag to specify whether to run network and gas tests:
./gradlew :lib:test -PnetworkTestMode=non_gas
./gradlew :lib:test -PnetworkTestMode=all
```
Flag takes precendece over the env variable if both are set.
⚠️ WARNING ⚠️ Some network tests may fail due to `getNonce` receiving higher nonce than expected by other methods.
It is adviced to additionaly provide an account (along with its **private key**) with a constant **nonce** to ensure non-gas tests pass.
Such account shouldn't be used for any other purpose than running non-gas network tests.
If not set, the main account provided in the config will be used for this purpose.
Flag takes precendece over the environment variable if both are set.
### Ensuring idiomatic Java code
We want this library to be used by both kotlin & java users. In order to ensure a nice API for java always follow those rules:
Expand Down
146 changes: 86 additions & 60 deletions lib/src/test/kotlin/network/account/AccountTest.kt

Large diffs are not rendered by default.

229 changes: 95 additions & 134 deletions lib/src/test/kotlin/network/provider/ProviderTest.kt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/src/test/kotlin/network/utils/NetworkConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NetworkConfig {
val rpcUrl: String,
val accountAddress: Felt,
val privateKey: Felt,
val cairoVersion: Int = 0,
val constNonceAccountAddress: Felt? = null,
val constNoncePrivateKey: Felt? = null,
)
Expand Down Expand Up @@ -66,6 +67,7 @@ class NetworkConfig {
rpcUrl = "${network.value}_RPC_URL".let { env.getOrElse(it) { throw RuntimeException("$it not found in environment variables") } },
accountAddress = "${network.value}_ACCOUNT_ADDRESS".let { env.getOrElse(it) { throw RuntimeException("$it not found in environment variables") } }.let { Felt.fromHex(it) },
privateKey = "${network.value}_PRIVATE_KEY".let { env.getOrElse(it) { throw RuntimeException("$it not found in environment variables") } }.let { Felt.fromHex(it) },
cairoVersion = env["${network.value}_ACCOUNT_CAIRO_VERSION"]?.toInt() ?: 0,
constNonceAccountAddress = env["${network.value}_CONST_NONCE_ACCOUNT_ADDRESS"]?.let { Felt.fromHex(it) },
constNoncePrivateKey = env["${network.value}_CONST_NONCE_PRIVATE_KEY"]?.let { Felt.fromHex(it) },
)
Expand Down
1 change: 1 addition & 0 deletions lib/src/test/resources/contracts/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod balance;
mod contract_with_constructor;
mod events;
mod map;
28 changes: 28 additions & 0 deletions lib/src/test/resources/contracts/src/map.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[starknet::interface]
trait IMap<T> {
// Returns the value associated with the given key.
fn get(self: @T, key: felt252) -> felt252;
// Sets the value associated with the given key.
fn put(ref self: T, key: felt252, value: felt252);
}

#[starknet::contract]
mod Map {
use traits::Into;

#[storage]
struct Storage {
map: LegacyMap::<felt252, felt252>,
}

#[external(v0)]
impl Map of super::IMap<ContractState> {
fn get(self: @ContractState, key: felt252) -> felt252 {
self.map.read(key)
}
fn put(ref self: ContractState, key: felt252, value: felt252) {
self.map.write(key, value);
}
}
}

25 changes: 25 additions & 0 deletions lib/src/test/resources/contracts_v0/src/map.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Declare this file as a StarkNet contract and set the required
// builtins.
%lang starknet
%builtins pedersen range_check

from starkware.cairo.common.cairo_builtins import HashBuiltin

// Define a storage variable.
@storage_var
func storage(key: felt) -> (value: felt) {
}

@external
func put{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(key: felt, value: felt) {
storage.write(key, value);
return ();
}

@view
func get{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(key: felt) -> (
res: felt
) {
let (value) = storage.read(key);
return (value,);
}
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

4 changes: 4 additions & 0 deletions test_variables.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,28 @@ GOERLI_INTEGRATION_PRIVATE_KEY=0x123456789
GOERLI_INTEGRATION_ACCOUNT_ADDRESS=0x123456789123456789123456789123456789123456789123456789123456789
GOERLI_INTEGRATION_CONST_NONCE_PRIVATE_KEY=0x1a1a1a1a1a
GOERLI_INTEGRATION_CONST_NONCE_ACCOUNT_ADDRESS=0x2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
GOERLI_INTEGRATION_ACCOUNT_CAIRO_VERSION=0

# Goerli Testnet config
GOERLI_TESTNET_RPC_URL=http://example-node-url.com/rpc
GOERLI_TESTNET_PRIVATE_KEY=0x123456789
GOERLI_TESTNET_ACCOUNT_ADDRESS=0x123456789123456789123456789123456789123456789123456789123456789
GOERLI_TESTNET_CONST_NONCE_PRIVATE_KEY=0x1a1a1a1a1a
GOERLI_TESTNET_CONST_NONCE_ACCOUNT_ADDRESS=0x2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
GOERLI_TESTNET_ACCOUNT_CAIRO_VERSION=0

# Sepolia Integration tests
SEPOLIA_INTEGRATION_RPC_URL=http://example-node-url.com/rpc
SEPOLIA_INTEGRATION_PRIVATE_KEY=0x123456789
SEPOLIA_INTEGRATION_ACCOUNT_ADDRESS=0x123456789123456789123456789123456789123456789123456789123456789
SEPOLIA_INTEGRATION_CONST_NONCE_PRIVATE_KEY=0x1a1a1a1a1a
SEPOLIA_INTEGRAION_CONST_NONCE_ACCOUNT_ADDRESS=0x2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
SEPOLIA_INTEGRATION_ACCOUNT_CAIRO_VERSION=1

# Sepolia Testnet config
SEPOLIA_TESTNET_RPC_URL=http://example-node-url.com/rpc
SEPOLIA_TESTNET_PRIVATE_KEY=0x123456789
SEPOLIA_TESTNET_ACCOUNT_ADDRESS=0x123456789123456789123456789123456789123456789123456789123456789
SEPOLIA_TESTNET_CONST_NONCE_PRIVATE_KEY=0x1a1a1a1a1a
SEPOLIA_TESTNET_CONST_NONCE_ACCOUNT_ADDRESS=0x2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
SEPOLIA_TESTNET_ACCOUNT_CAIRO_VERSION=1

0 comments on commit fa0cf07

Please sign in to comment.